1 7 package com.inversoft.verge.config; 8 9 10 import java.util.Iterator ; 11 import java.util.Timer ; 12 import java.util.TimerTask ; 13 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.servlet.ServletContext ; 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 50 public class VergeConfigMonitor extends TimerTask { 51 52 55 private static final Logger logger = Logger.getLogger(VergeConfigMonitor.class); 56 57 61 public static final String REBUILD_INTERVAL_PARAM = "verge.config.rebuild.interval"; 62 63 64 67 private static VergeConfigMonitor instance; 68 69 72 private ServletContext context; 73 74 77 private long interval; 78 79 80 84 private VergeConfigMonitor(ServletContext context, long interval) { 85 this.interval = interval; 86 this.context = context; 87 } 88 89 102 public static synchronized void refresh(ServletContext context) { 103 104 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 String 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 initContext = new InitialContext (); 124 rebuildInterval = (String ) initContext.lookup(REBUILD_INTERVAL_PARAM); 125 } catch (NamingException ne) { 126 } 128 } 129 } 130 131 long interval = -1; 132 if (!StringTools.isEmpty(rebuildInterval)) { 133 try { 134 interval = Long.valueOf(rebuildInterval).longValue() * 1000l; 135 } catch (NumberFormatException nfe) { 136 logger.error("Invalid rebuild interval: " + rebuildInterval); 138 } 139 } 140 141 if (interval > 0) { 143 instance = new VergeConfigMonitor(context, interval); 144 145 Timer timer = new Timer (true); 146 timer.schedule(instance, instance.interval, instance.interval); 147 } 148 } 149 150 153 public static void shutdown() { 154 instance.cancel(); 155 } 156 157 162 public static void logErrors(ConfigurationException exception) { 163 164 Iterator 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 178 179 180 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 |