Commits

Anonymous committed 012d5fd

WW-1464
- Provides callback that gets informed when WebWork started up and shuts down

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2920573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits c03eafe

Comments (0)

Files changed (5)

File src/java/com/opensymphony/webwork/WebWorkConstants.java

     
     public static final String WEBWORK_FREEMARKER_SITEMESH_APPLY_DECORATOR_TRANSFORM = "webwork.freemarker.sitemesh.applyDecoratorTransform";
 
+    public static final String WEBWORK_DISPATCHER_START_UP_LISTENER = "webwork.dispatcher.startUpListener";
+    public static final String WEBWORK_DISPATCHER_SHUTDOWN_LISTENER = "webwork.dispatcher.shutDownListener";
 }

File src/java/com/opensymphony/webwork/default.properties

 #
 webwork.freemarker.sitemesh.applyDecoratorTransform=true
 
+### A start up listener class name (must implements com.opensymphony.webwork.dispatcher.StartUpListener 
+### interface) that will get invoked only once when WebWork started up. The class names could be 
+### comma separated and will be executed in order.
+#
+#webwork.dispatcher.startUpListener=foo.bar.StartUpListener1, foo.bar.StartUpListener2
+
+### A shut down listener class name (must implements com.opensymphony.webwork.dispatcher.ShutDownListener
+### interface) that will get invoked only once when WebWork shuts down.  The class names could be
+### comma separated and will be executed in order.
+#
+#webwork.dispatcher.shutDownListener=foo.bar.ShutDownListener1, foo.bar.ShutDownListener2
+
 ### END SNIPPET: complete_file

File src/java/com/opensymphony/webwork/dispatcher/DispatcherUtils.java

     }
 
     public void cleanup() {
-    	// clean up ObjectFactory
         ObjectFactory objectFactory = ObjectFactory.getObjectFactory();
+        
+    	// inform ShutDownListener, we are shuting down
+        if (Configuration.isSet(WebWorkConstants.WEBWORK_DISPATCHER_SHUTDOWN_LISTENER)) {
+        	String[] shutdownListenerClassNames = Configuration.getString(WebWorkConstants.WEBWORK_DISPATCHER_SHUTDOWN_LISTENER).split(",");
+        	for (int a=0; a<shutdownListenerClassNames.length; a++) {
+        		String shutdownListenerClassName = shutdownListenerClassNames[a].trim();
+        		try {
+        			ShutDownListener shutDownListener = (ShutDownListener) objectFactory.buildBean(shutdownListenerClassName, Collections.EMPTY_MAP);
+        			if (LOG.isDebugEnabled()) {
+        				LOG.debug("notifying shutdown listener ["+shutDownListener+"]");
+        			}
+        			shutDownListener.shutdown();
+        		}
+        		catch(Exception e) { // we might also get ClassCastException
+        			LOG.warn("shutdown listener ["+shutdownListenerClassName+"] failed to be initialized, it will be ignored", e);
+        		}
+        	}
+        }
+    	
+    	
+    	
+    	// clean up ObjectFactory
         if (objectFactory == null) {
             LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed");
         }
         boolean reloadi18n = Boolean.valueOf((String) Configuration.get(WebWorkConstants.WEBWORK_I18N_RELOAD)).booleanValue();
         LocalizedTextUtil.setReloadBundles(reloadi18n);
 
+        // initialize ObjectFactory
+        ObjectFactory objectFactory = null;
         if (Configuration.isSet(WebWorkConstants.WEBWORK_OBJECTFACTORY)) {
             String className = (String) Configuration.get(WebWorkConstants.WEBWORK_OBJECTFACTORY);
             if (className.equals("spring")) {
 
             try {
                 Class clazz = ClassLoaderUtil.loadClass(className, DispatcherUtils.class);
-                ObjectFactory objectFactory = (ObjectFactory) clazz.newInstance();
+                objectFactory = (ObjectFactory) clazz.newInstance();
                 if (objectFactory instanceof ObjectFactoryInitializable) {
                     ((ObjectFactoryInitializable) objectFactory).init(servletContext);
                 }
             }
         }
 
+        
+        // Intialize ObjecTypeDeterminer
         if (Configuration.isSet(WebWorkConstants.WEBWORK_OBJECTTYPEDETERMINER)) {
             String className = (String) Configuration.get(WebWorkConstants.WEBWORK_OBJECTTYPEDETERMINER);
             if (className.equals("tiger")) {
         } else {
             LOG.debug("Parameter access work-around disabled.");
         }
+        
+        // inform startup listeners
+        if (Configuration.isSet(WebWorkConstants.WEBWORK_DISPATCHER_START_UP_LISTENER)) {
+        	String[] startupListenerClassNames = Configuration.getString(WebWorkConstants.WEBWORK_DISPATCHER_START_UP_LISTENER).split(",");
+        	for (int a=0; a<startupListenerClassNames.length; a++) {
+        		String startupListenerClassName = startupListenerClassNames[a].trim();
+        		try {
+        			StartUpListener startUpListener = (StartUpListener) objectFactory.buildBean(startupListenerClassName, Collections.EMPTY_MAP);
+        			if (LOG.isDebugEnabled()) {
+        				LOG.debug("notifying start up listener ["+startUpListener+"]");
+        			}
+        			startUpListener.startup();
+        		}
+        		catch(Exception e) { // we might also get ClassCastException
+        			LOG.warn("shutdown listener ["+startupListenerClassName+"] failed to be initialized, it will be ignored", e);
+        		}
+        	}
+        }
 
     }
 

File src/java/com/opensymphony/webwork/dispatcher/ShutDownListener.java

+/*
+ * Copyright (c) 2002-2007 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.webwork.dispatcher;
+
+/**
+ * An interface to be implemented if require to get informed when WebWork
+ * shuts down, It's being hooked up through {@link FilterDispatcher#destroy()} 
+ * -> {@link DispatcherUtils#cleanup()} methods calls.
+ * <p/>
+ * It can be configured through <code>webwork.properties</code> that 
+ * resides in the classpath, typically in <code>/WEB-INF/classes</code> directory
+ * through the following entry
+ * <p/>
+ * 
+ * <pre>
+ * webwork.dispatcher.shutDownListener=foo.bar.ShutdownListener1, foo.bar.ShutdownListener2
+ * </pre>
+ * 
+ * The value of the properties is the FQN (Fully Quantified class name of the shut down listeners)
+ *  It must implements this interface, else a ClassCastException will be registered in the log.
+ *  Multiple class names could be specified, but they must be comma separated.
+ * 
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public interface ShutDownListener {
+	void shutdown();
+}

File src/java/com/opensymphony/webwork/dispatcher/StartUpListener.java

+/*
+ * Copyright (c) 2002-2007 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.webwork.dispatcher;
+
+/**
+ * An interface to be implemented if require to get informed when WebWork
+ * starts up, It's being hooked up through {@link FilterDispatcher#init(javax.servlet.FilterConfig)} 
+ * -> {@link DispatcherUtils#init(javax.servlet.ServletContext)} methods calls.
+ * <p/>
+ * It can be configured through <code>webwork.properties</code> that 
+ * resides in the classpath, typically in <code>/WEB-INF/classes</code> directory
+ * through the following entry
+ * <p/>
+ * 
+ * <pre>
+ * webwork.dispatcher.startUpListener=foo.bar.StartupListener1, foo.bar.StartupListener2
+ * </pre>
+ * 
+ * The value of the properties is the FQN (Fully Quantified class name of the start up listeners)
+ *  It must implements this interface, else a ClassCastException will be registered in the log.
+ *  Multiple class names could be specified, but they must be comma separated.
+ * 
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public interface StartUpListener {
+	void startup();
+}