1 10 package org.mmbase.module; 11 12 import java.util.*; 13 import java.net.*; 14 import org.xml.sax.*; 15 16 import org.mmbase.util.*; 17 import org.mmbase.util.xml.ModuleReader; 18 19 import org.mmbase.util.functions.*; 20 import org.mmbase.util.logging.Logging; 21 import org.mmbase.util.logging.Logger; 22 23 38 public abstract class Module extends FunctionProvider { 39 40 43 static Map modules; 44 45 private static final Logger log = Logging.getLoggerInstance(Module.class); 46 47 53 protected Function getVersionFunction = new AbstractFunction("getVersion", Parameter.EMPTY, ReturnType.INTEGER) { 54 public Object getFunctionValue(Parameters arguments) { 55 return new Integer (getVersion()); 56 } 57 }; 58 59 65 protected Function getMaintainerFunction = new AbstractFunction("getMaintainer", Parameter.EMPTY, ReturnType.STRING) { 66 public Object getFunctionValue(Parameters arguments) { 67 return getMaintainer(); 68 } 69 }; 70 71 String moduleName = null; 72 Hashtable state = new Hashtable(); 73 Hashtable properties; String maintainer; 75 int version; 76 77 private boolean started = false; 79 80 public Module() { 81 addFunction(getVersionFunction); 82 addFunction(getMaintainerFunction); 83 String startedAt = (new Date(System.currentTimeMillis())).toString(); 84 state.put("Start Time", startedAt); 85 } 86 87 public final void setName(String name) { 88 if (moduleName == null) { 89 moduleName = name; 90 } 91 } 92 93 96 public static ResourceLoader getModuleLoader() { 97 return ResourceLoader.getConfigurationRoot().getChildResourceLoader("modules"); 98 } 99 100 103 public static ModuleReader getModuleReader(String moduleName) { 104 try { 105 InputSource is = getModuleLoader().getInputSource(moduleName + ".xml"); 106 if (is == null) return null; 107 return new ModuleReader(is); 108 } catch (Exception e) { 109 log.error(e); 110 return null; 111 } 112 } 113 114 122 public final void startModule() { 123 if (started) return; 124 synchronized(Module.class) { 125 init(); 126 started = true; 127 } 128 } 129 130 134 public final boolean hasStarted() { 135 return started; 136 } 137 138 145 public void init() { 146 } 147 148 156 public void onload() { 157 } 158 159 164 protected void shutdown() { 165 } 167 168 172 public Hashtable state() { 173 return state; 174 } 175 176 179 public void setInitParameter(String key,String value) { 180 if (properties != null) { 181 properties.put(key, value); 182 } 183 } 184 185 188 public String getInitParameter(String key) { 189 if (properties != null) { 190 String value=(String )properties.get(key); 191 if (value == null) { 192 key = key.toLowerCase(); 193 value = (String )properties.get(key); 194 if (value == null) { 198 value = System.getProperty(moduleName+"."+key); 199 } 200 } 201 return value; 202 } else { 203 log.error("getInitParameters(" + key + "): No properties found, called before they where loaded"); 204 } 205 return null; 206 } 207 208 211 protected Hashtable getProperties(String propertytable) { 212 return null; 215 } 217 218 221 protected String getProperty(String name, String var) { 222 return ""; 223 } 224 225 228 public Hashtable getInitParameters() { 229 return properties; 230 } 231 232 237 public void loadInitParameters(String contextPath) { 238 try { 239 Map contextMap = ApplicationContextReader.getProperties(contextPath); 240 if (!contextMap.isEmpty()) { 241 properties.putAll(contextMap); 242 } 243 } catch (javax.naming.NamingException ne) { 244 log.debug("Can't obtain properties from application context: " + ne.getMessage()); 245 } 246 } 247 248 249 255 public static final Iterator getModules() { 256 if (modules == null) { 257 return null; 258 } else { 259 return modules.values().iterator(); 260 } 261 } 262 263 267 public final String getName() { 268 return moduleName; } 270 271 274 public String getModuleInfo() { 275 return "No module info provided"; 276 } 277 278 281 public void maintainance() { 282 } 283 284 285 290 public static synchronized final void shutdownModules() { 291 Iterator i = getModules(); 292 while (i != null && i.hasNext()) { 293 Module m = (Module) i.next(); 294 log.service("Shutting down " + m.getName()); 295 m.shutdown(); 296 log.service("Shut down " + m.getName()); 297 } 298 modules = null; 299 } 300 301 public static synchronized final void startModules() { 302 log.service("Starting modules " + modules.keySet()); 304 for (Iterator i = modules.values().iterator(); i.hasNext();) { 305 if (Thread.currentThread().isInterrupted()) { 306 log.info("Interrupted"); 307 return; 308 } 309 Module mod = (Module)i.next(); 310 if( log.isDebugEnabled() ) { 311 log.debug("startModules(): modules.onload(" + mod + ")"); 312 } 313 try { 314 mod.onload(); 315 } catch (Exception f) { 316 log.warn("startModules(): modules(" + mod + ") not found to 'onload'!", f); 317 } 318 } 319 if (log.isDebugEnabled()) { 321 log.debug("startModules(): init the modules(" + modules + ")"); 322 } 323 for (Iterator i = modules.values().iterator(); i.hasNext();) { 324 if (Thread.currentThread().isInterrupted()) { 325 log.info("Interrupted"); 326 return; 327 } 328 Module mod = (Module) i.next(); 329 log.info("Starting module " + mod.getName()); 330 if ( log.isDebugEnabled()) { 331 log.debug("startModules(): mod.startModule(" + mod + ")"); 332 } 333 try { 334 mod.startModule(); 335 } catch (Exception f) { 336 log.error("startModules(): module(" + mod + ") not found to 'init'!: " + f.getClass() + ": " + f.getMessage()); 337 log.error(Logging.stackTrace(f)); 338 } 339 } 340 } 341 342 public static boolean hasModule(String name) { 343 boolean check = modules.containsKey(name.toLowerCase()); 344 if (!check) { 345 check = modules.containsKey(name); 346 } 347 return check; 348 } 349 350 358 public static Module getModule(String name) { 359 return getModule(name, false); 360 } 361 362 376 public static Module getModule(String name, boolean startOnLoad) { 377 synchronized(Module.class) { 379 if (modules == null) { log.service("Loading MMBase modules..."); 381 modules = loadModulesFromDisk(); 382 if (log.isDebugEnabled()) { 383 log.debug("getModule(" + name + "): Modules not loaded, loading them.."); 384 } 385 startModules(); 386 new ModuleProbe().start(); 388 } 389 } 390 Module obj = (Module) modules.get(name.toLowerCase()); 392 if (obj == null) { obj = (Module) modules.get(name); 394 } 395 if (obj != null) { 396 if (startOnLoad) { 399 obj.startModule(); 400 } 401 return obj; 402 } else { 403 log.warn("The module '" + name + "' could not be found!"); 404 return null; 405 } 406 } 407 408 public String getMaintainer() { 409 return maintainer; 410 } 411 412 public void setMaintainer(String m) { 413 maintainer = m; 414 } 415 416 public void setVersion(int v) { 417 version = v; 418 } 419 420 public int getVersion() { 421 return version; 422 } 423 424 429 private static synchronized Hashtable loadModulesFromDisk() { 430 Hashtable results = new Hashtable(); 431 ResourceLoader moduleLoader = getModuleLoader(); 432 Collection modules = moduleLoader.getResourcePaths(ResourceLoader.XML_PATTERN, false); 433 log.info("In " + moduleLoader + " the following module XML's were found " + modules); 434 Iterator i = modules.iterator(); 435 while (i.hasNext()) { 436 String file = (String ) i.next(); 437 String fileName = ResourceLoader.getName(file); 438 ModuleReader parser = null; 439 try { 440 InputSource is = moduleLoader.getInputSource(file); 441 if (is != null) parser = new ModuleReader(is); 442 } catch (Exception e) { 443 log.error(e); 444 } 445 if (parser != null && parser.getStatus().equals("active")) { 446 String className = parser.getClassName(); 447 try { 449 log.service("Loading module " + fileName + " with class " + className); 450 Module mod; 451 if (parser.getURLString() != null){ 452 log.service("loading module from jar " + parser.getURLString()); 453 URL url = new URL(parser.getURLString()); 454 URLClassLoader c = new URLClassLoader(new URL[]{url}, Module.class.getClassLoader()); 455 Class newClass = c.loadClass(className); 456 mod = (Module) newClass.newInstance(); 457 } else { 458 Class newClass = Class.forName(className); 459 mod = (Module) newClass.newInstance(); 460 } 461 462 results.put(fileName, mod); 463 464 mod.properties = new Hashtable(parser.getProperties()); 465 466 mod.setName(fileName); 469 470 mod.setMaintainer(parser.getMaintainer()); 471 mod.setVersion(parser.getVersion()); 472 } catch (ClassNotFoundException cnfe) { 473 log.error("Could not load class with name '" + className + "', " + 474 "which was specified in the module:'" + file + " '(" + cnfe + ")" ); 475 } catch (Throwable e) { 476 log.error("Error while loading module class" + Logging.stackTrace(e)); 477 } 478 } 479 } 480 return results; 481 } 482 483 } 484
| Popular Tags
|