KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > config > VergeConfigMonitor


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Timer JavaDoc;
12 import java.util.TimerTask JavaDoc;
13
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.servlet.ServletContext JavaDoc;
17
18 import org.apache.log4j.Logger;
19
20 import com.inversoft.config.ConfigurationException;
21 import com.inversoft.error.BasicError;
22 import com.inversoft.util.StringTools;
23
24
25 /**
26  * <p>
27  * This class is a Monitor that can be used to detemine
28  * the state of the configuration as well as do dynamic
29  * reloading during development. The Inversoft configuration
30  * system is called by this class either on application
31  * start or configuration reload or a manual call to refresh.
32  * </p>
33  *
34  * <p>
35  * The rebuild interval can be changed for this monitor using
36  * either a servlet context parameter, JVM property or JNDI
37  * environment value. The key for all three options is:
38  * </p>
39  *
40  * <code>verge.config.rebuild.interval</code>
41  *
42  * <p>
43  * The search order is JVM property, servlet context
44  * parameter and finally the JNDI tree. If nothing is found,
45  * rebuilding is disabled.
46  * </p>
47  *
48  * @author Brian Pontarelli
49  */

50 public class VergeConfigMonitor extends TimerTask JavaDoc {
51
52     /**
53      * This classes logger
54      */

55     private static final Logger logger = Logger.getLogger(VergeConfigMonitor.class);
56
57     /**
58      * Name of the init parameter, JVM property, or JNDI environment value for
59      * the rebuild interval (in seconds)
60      */

61     public static final String JavaDoc REBUILD_INTERVAL_PARAM = "verge.config.rebuild.interval";
62
63
64     /**
65      * The currently running manager instance, if any
66      */

67     private static VergeConfigMonitor instance;
68
69     /**
70      * The ServletContext for this monitor
71      */

72     private ServletContext JavaDoc context;
73
74     /**
75      * The rebuild interval for a monitor instance is milliseconds
76      */

77     private long interval;
78
79
80     /**
81      * Constructs a new <code>VergeConfigMonitor</code> and is private so that
82      * it is started and managed as a Thread by this class only.
83      */

84     private VergeConfigMonitor(ServletContext JavaDoc context, long interval) {
85         this.interval = interval;
86         this.context = context;
87     }
88
89     /**
90      * This is the static method that should only be called once and this
91      * is enforced and asserted. This method does the initial building of
92      * the configuration as well as spawns off a new Thread to do rebuilds if
93      * specified.
94      * <p>
95      * If the config file init parameter does not exist, this class exits without
96      * doing anything. If the rebuild interval is not a valid integer, this logs
97      * an error and continues initialization but does not spawn any threads.
98      *
99      * @param context The ServletContext to get the init parameters from
100      * @asserts That this method is only called once
101      */

102     public static synchronized void refresh(ServletContext JavaDoc context) {
103
104         // If there was a previous instance, shut it down
105
if (instance != null) {
106             instance.cancel();
107         }
108
109         VergeConfigMediator mediator = new VergeConfigMediator();
110         try {
111             mediator.mediate(context);
112         } catch (ConfigurationException ce) {
113             logErrors(ce);
114         }
115
116         // Try to get the interval, if it exists. If it does, be sure to convert
117
// from seconds to milliseconds when setting the instances interval
118
String JavaDoc rebuildInterval = System.getProperty(REBUILD_INTERVAL_PARAM);
119         if (StringTools.isEmpty(rebuildInterval)) {
120             rebuildInterval = context.getInitParameter(REBUILD_INTERVAL_PARAM);
121             if (StringTools.isEmpty(rebuildInterval)) {
122                 try {
123                     InitialContext JavaDoc initContext = new InitialContext JavaDoc();
124                     rebuildInterval = (String JavaDoc) initContext.lookup(REBUILD_INTERVAL_PARAM);
125                 } catch (NamingException JavaDoc ne) {
126                     // Smother
127
}
128             }
129         }
130
131         long interval = -1;
132         if (!StringTools.isEmpty(rebuildInterval)) {
133             try {
134                 interval = Long.valueOf(rebuildInterval).longValue() * 1000l;
135             } catch (NumberFormatException JavaDoc nfe) {
136                 // Smother
137
logger.error("Invalid rebuild interval: " + rebuildInterval);
138             }
139         }
140
141         // If there is a refresh interval, start the timer
142
if (interval > 0) {
143             instance = new VergeConfigMonitor(context, interval);
144
145             Timer JavaDoc timer = new Timer JavaDoc(true);
146             timer.schedule(instance, instance.interval, instance.interval);
147         }
148     }
149
150     /**
151      * Causes the running manager thread, if any, to be shutdown.
152      */

153     public static void shutdown() {
154         instance.cancel();
155     }
156
157     /**
158      * Outputs the errors from the given ConfigurationException to the logger
159      *
160      * @param exception The ConfigurationException to log the errors for
161      */

162     public static void logErrors(ConfigurationException exception) {
163
164         // If there was a problem during the build, log all the errors
165
Iterator JavaDoc iter = exception.getErrors().iterator();
166         BasicError error;
167         while (iter.hasNext()) {
168             error = (BasicError) iter.next();
169             logger.error(error);
170         }
171     }
172
173
174     //-------------------------------------------------------------------------
175
// Thread Methods
176
//-------------------------------------------------------------------------
177

178
179
180     /**
181      * Runs the TimerTask to rebuild the configuration at the interval setup in this
182      * instance and set on the TImer
183      */

184     public void run() {
185         VergeConfigMediator mediator = new VergeConfigMediator();
186
187         try {
188             logger.debug("Doing build of configuration files");
189             mediator.mediate(context);
190             logger.debug("Build finished");
191         } catch (ConfigurationException ce) {
192             logErrors(ce);
193         }
194     }
195 }
Popular Tags