KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ReloadMonitor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * @(#) ReloadMonitor.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.server;
38
39 import java.io.File JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45 import com.sun.logging.LogDomains;
46 import com.sun.enterprise.admin.server.core.channel.ReconfigHelper;
47
48 /**
49  * Monitors dynamic reload files for applications and stand alone modules
50  * from the server runtime.
51  *
52  * @atuthor Nazrul Islam
53  * @since JDK 1.4
54  */

55 class ReloadMonitor extends AbstractMonitor {
56
57     /** name of the file that user will update time stamp to trigger a reload */
58     static final String JavaDoc RELOAD_FILE = ".reload";
59     
60     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
61
62     /** singleton instance */
63     private static ReloadMonitor _instance = null;
64
65     /**
66      * Constructor - prohibits anyone from constructing this object.
67      *
68      * @param pollInterval polling interval
69      */

70     private ReloadMonitor(long pollInterval) {
71         super(pollInterval);
72     }
73
74     /**
75      * Returns the singleton instance. If the object was not created already,
76      * it uses the given polling interval.
77      *
78      * @param pollInterval polling interval
79      */

80     static ReloadMonitor getInstance(long pollInterval) {
81         if (_instance == null) {
82             _instance = new ReloadMonitor(pollInterval);
83         }
84         return _instance;
85     }
86
87     /**
88      * Removes the given application or stand alone module from the
89      * monitored list.
90      *
91      * @param id registration name of application
92      *
93      * @return true if removed successfully
94      */

95     boolean removeMonitoredEntry(String JavaDoc id) {
96
97         boolean removed = false;
98
99         // returns false if application name is null
100
if (id == null) {
101             return removed;
102         }
103
104         synchronized (this._monitoredEntries) {
105             Iterator JavaDoc iter = this._monitoredEntries.iterator();
106             while (iter.hasNext()) {
107                 MonitorableEntry entry = (MonitorableEntry) iter.next();
108                 if ( id.equals(entry.getId()) ) {
109                     this._monitoredEntries.remove(entry);
110                     removed = true;
111                     break;
112                 }
113             }
114         }
115
116         return removed;
117     }
118
119     /**
120      * This method gets called from the monitor thread. This goes through
121      * all the monitored entried and checks the time stamps. If any of the
122      * time stamps is modified, it makes a callback to its listener. The
123      * callbacks are single threaded, i.e., waits for one to finish before
124      * makes the second call.
125      *
126      * <p> The time stamp of the monitored entry is set to the current
127      * time stamp before the callback is made.
128      */

129     public void run() {
130
131         try {
132             ArrayList JavaDoc reloadList = new ArrayList JavaDoc();
133
134             synchronized (_monitoredEntries) {
135                 Iterator JavaDoc iter = _monitoredEntries.iterator();
136                 MonitorableEntry entry = null;
137
138                 while (iter.hasNext()) {
139                     entry = (MonitorableEntry) iter.next();
140                     File JavaDoc file = entry.getMonitoredFile();
141                     long lastModified = file.lastModified();
142                     long lastReloadedAt = entry.getLastReloadedTimeStamp();
143
144                     // time stamp is updated
145
if (lastModified > lastReloadedAt) {
146                         // sets the time stamp so that it gets called once
147
entry.setLastReloadedTimeStamp(lastModified);
148
149                         reloadList.add(entry);
150                     }
151                 }
152             }
153
154             // found some entries with modified time stamp
155
if (reloadList.size() > 0) {
156
157                 _logger.log(Level.FINEST,
158                     "[ReloadMonitor] Monitor detected reloadable entry!");
159
160                 int size = reloadList.size();
161                 MonitorableEntry entry = null;
162
163                 for (int i=0; i<size; i++) {
164                     entry = (MonitorableEntry) reloadList.get(i);
165
166                     MonitorListener l = entry.getListener();
167
168                     // calls back the listener
169
boolean success = l.reload(entry);
170
171                     // log status
172
if (success) {
173                         _logger.log(Level.INFO,
174                             "core.application_reload_successful",
175                             entry.getDisplayName());
176                     } else {
177                         _logger.log(Level.INFO,
178                             "core.application_reload_failed",
179                             entry.getDisplayName());
180                     }
181                 }
182
183                 // Reload the web modules
184
/*
185                  * Remove compile time dependence on J2EERunner. Replace it
186                  * temporarily with call to ReconfigHelper.
187                 J2EERunner.requestReconfiguration();
188                 */

189                 ReconfigHelper.sendReconfigMessage("");
190
191                 // removes all the entries after the call back
192
reloadList.clear();
193             }
194
195         } catch (Throwable JavaDoc t) {
196             // catches any uncaught exceptions thrown from the handlers
197
_logger.log(Level.WARNING, "core.exception", t);
198         }
199     }
200 }
201
Popular Tags