1 13 18 package org.jahia.registries; 19 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Properties ; 27 import java.util.Vector ; 28 29 import javax.servlet.ServletConfig ; 30 31 import org.jahia.data.events.JahiaEvent; 32 import org.jahia.data.events.JahiaEventListenerInterface; 33 import org.jahia.exceptions.JahiaException; 34 35 public class JahiaListenersRegistry { 36 37 private static org.apache.log4j.Logger logger = 38 org.apache.log4j.Logger.getLogger(JahiaListenersRegistry.class); 39 40 private static JahiaListenersRegistry theObject = null; 41 42 private Hashtable classNameToInstance = new Hashtable (); 43 private Hashtable listenerNameToClassName = new Hashtable (); 44 private boolean initialized = false; 45 46 private static final String REGISTRY_FILENAME = "listeners.registry"; 47 private String registryFile = null; 48 private Properties registryProperties = new Properties (); 49 public Vector listeners = new Vector (); 50 51 55 private JahiaListenersRegistry () { 56 logger.debug( "Starting up..."); 57 } 59 63 public static synchronized JahiaListenersRegistry getInstance () { 64 if (theObject == null) { 65 theObject = new JahiaListenersRegistry(); 66 } 67 return theObject; 68 } 70 76 public void init (ServletConfig config) { 77 if (!initialized) { 78 logger.debug("Start Loading"); 79 initialized = loadAllListenersFromFile(config); 80 logger.debug("Ended Loading"); 81 } 82 } 84 90 public Vector getAllListeners () { 91 final int i = classNameToInstance.size(); 92 if (i > 0 && listeners.size()!=i) { 93 listeners.clear(); 94 Enumeration keys = classNameToInstance.keys(); 95 while (keys.hasMoreElements()) { 96 String theKey = (String ) keys.nextElement(); 97 listeners.add( classNameToInstance.get(theKey)); 98 } 99 } 100 return listeners; 101 } 103 110 public JahiaEventListenerInterface getListenerByName (String listenerName) { 111 String listenerClassName = (String ) listenerNameToClassName.get(listenerName); 112 if (listenerClassName == null) { 113 return null; 114 } 115 return this.getListener(listenerClassName); 116 } 118 125 public JahiaEventListenerInterface getListener (String listenerClassName) { 126 return (JahiaEventListenerInterface) classNameToInstance.get(listenerClassName); 127 } 129 137 public synchronized boolean addListener (String lName, String lClassName) { 138 boolean out = false; 139 JahiaEventListenerInterface theListener = instanceListener(lClassName); 140 if (theListener != null) { 141 classNameToInstance.put(lClassName, theListener); 142 listenerNameToClassName.put(lName, lClassName); 143 registryProperties.setProperty(lName, lClassName); 144 if (saveRegistryInFile()) { 145 out = true; 146 } 147 } 148 return out; 149 } 151 160 public synchronized boolean addListener (String lName, 161 JahiaEventListenerInterface listener, 162 boolean storeInRegistryFile) { 163 boolean out = false; 164 if (listener != null) { 165 String className = listener.getClass().getName(); 166 classNameToInstance.put(className, listener); 167 listenerNameToClassName.put(lName, className); 168 registryProperties.setProperty(lName, className); 169 if (!storeInRegistryFile){ 170 out = true; 171 } else if (saveRegistryInFile()) { 172 out = true; 173 } 174 } 175 return out; 176 } 178 185 public synchronized boolean removeListener (String lName) { 186 boolean out = false; 187 String lClassName = (String ) listenerNameToClassName.get(lName); 188 classNameToInstance.remove(lClassName); 189 listenerNameToClassName.remove(lName); 190 registryProperties.remove(lName); 191 if (saveRegistryInFile()) { 192 out = true; 193 } 194 return out; 195 } 197 204 public void wakeupListeners (String methodName, JahiaEvent theEvent) 205 throws JahiaException { 206 try { 207 Vector listeners = getAllListeners(); 208 final int listenersSize = listeners.size(); 209 if (listenersSize > 0) { 212 for (int i = 0; i < listenersSize; i++) { 213 JahiaEventListenerInterface theListener = ( 214 JahiaEventListenerInterface) listeners.elementAt(i); 215 Class theClass = theListener.getClass(); 216 Class eventClass = theEvent.getClass(); 217 Method theMethod = theClass.getMethod(methodName, 218 new Class [] {eventClass}); 219 if (theMethod != null) { 220 theMethod.invoke(theListener, 222 new Object [] { theEvent}); 223 226 } 227 } 228 } 229 232 } catch (NoSuchMethodException nsme) { 233 String errorMsg = 234 "NoSuchMethodException when trying to execute method " + 235 methodName + "(" + nsme.getMessage() + ")"; 236 logger.error( errorMsg, nsme); 237 throw new JahiaException("NoSuchMethodException", 238 errorMsg, JahiaException.LISTENER_ERROR, 239 JahiaException.WARNING_SEVERITY, nsme); 240 } catch (InvocationTargetException ite) { 241 String errorMsg = 242 "InvocationTargetException when trying to execute method " + 243 methodName + "(" + ite.getTargetException().getMessage() + ")"; 244 logger.error( errorMsg, ite.getTargetException()); 245 throw new JahiaException("InvocationTargetException", 246 errorMsg, JahiaException.LISTENER_ERROR, 247 JahiaException.WARNING_SEVERITY, 248 ite.getTargetException()); 249 } catch (IllegalAccessException iae) { 250 String errorMsg = 251 "IllegalAccessException when trying to execute method " + 252 methodName + "(" + iae.getMessage() + ")"; 253 logger.error( errorMsg, iae); 254 throw new JahiaException("IllegalAccessException", 255 errorMsg, JahiaException.LISTENER_ERROR, 256 JahiaException.WARNING_SEVERITY, iae); 257 } 258 } 260 267 private boolean loadAllListenersFromFile (ServletConfig config) { 268 this.registryFile = config.getServletContext().getRealPath( 269 config.getInitParameter("webinf_path") + "etc/config/" + 270 REGISTRY_FILENAME); 271 272 try { 273 FileInputStream in = new FileInputStream (registryFile); 274 registryProperties.load(in); 275 Enumeration keys = registryProperties.propertyNames(); 276 while (keys.hasMoreElements()) { 277 String theKey = (String ) keys.nextElement(); 278 String listenerName = registryProperties.getProperty(theKey); 279 if (listenerName != null) { 280 listenerName = listenerName.trim(); 281 282 listenerNameToClassName.put(theKey, listenerName); 284 285 JahiaEventListenerInterface theListener = instanceListener( 287 listenerName); 288 if (theListener != null) { 289 classNameToInstance.put(listenerName, theListener); 290 logger.debug("Loading " + theKey); 291 } 292 } 293 } 294 return true; 295 } catch (NullPointerException npe) { 296 return createRegistryFile(); 297 } catch (java.io.IOException ioe) { 298 return createRegistryFile(); 299 } 300 } 302 309 private JahiaEventListenerInterface instanceListener (String className) { 310 JahiaEventListenerInterface theListener = null; 311 Class listenerClass = null; 312 try { 313 listenerClass = Class.forName(className); 314 theListener = (JahiaEventListenerInterface) listenerClass. 315 newInstance(); 316 317 } catch (ClassNotFoundException cnfe) { 318 String errorMsg = "ClassNotFound when trying to load " + className + 319 "(" + cnfe.getMessage() + ")"; 320 JahiaException je = new JahiaException("ClassNotFoundException", 321 errorMsg, JahiaException.LISTENER_ERROR, 322 JahiaException.WARNING_SEVERITY, cnfe); 323 324 } catch (InstantiationException cie) { 325 String errorMsg = "InstantiationException when trying to load " + 326 className + "(" + cie.getMessage() + ")"; 327 JahiaException je = new JahiaException("InstanciationException", 328 errorMsg, JahiaException.LISTENER_ERROR, 329 JahiaException.WARNING_SEVERITY, cie); 330 331 } catch (IllegalAccessException iae) { 332 String errorMsg = "IllegalAccessException when trying to load " + 333 className + "(" + iae.getMessage() + ")"; 334 JahiaException je = new JahiaException("IllegalAccessException", 335 errorMsg, JahiaException.LISTENER_ERROR, 336 JahiaException.WARNING_SEVERITY, iae); 337 } 338 return theListener; 339 } 341 347 private synchronized boolean createRegistryFile () { 348 try { 349 logger.debug("Creating registry file"); 350 FileOutputStream out = new FileOutputStream (registryFile); 351 registryProperties.store(out, "// Jahia Listeners Registry"); 352 out.close(); 353 return true; 354 } catch (java.io.IOException e) { 355 String errorMsg = "Cannot create registry file " + registryFile + 356 " !"; 357 JahiaException je = new JahiaException( 358 "Cannot create registry file !", 359 errorMsg, JahiaException.FILE_ERROR, 360 JahiaException.WARNING_SEVERITY, e); 361 return false; 362 } 363 } 365 371 private synchronized boolean saveRegistryInFile () { 372 try { 373 FileOutputStream out = new FileOutputStream (registryFile); 374 registryProperties.store(out, "// Jahia Listeners Registry"); 375 out.close(); 377 return true; 378 } catch (java.io.IOException e) { 379 String errorMsg = "Cannot save registry file " + registryFile + 380 " !"; 381 JahiaException je = new JahiaException( 382 "Cannot save registry file !", 383 errorMsg, JahiaException.FILE_ERROR, 384 JahiaException.WARNING_SEVERITY, e); 385 return false; 386 } 387 } 389 } | Popular Tags |