You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2009/08/21 09:06:14 UTC

svn commit: r806432 - in /myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp: StartupListener.java StartupServletContextListener.java

Author: werpu
Date: Fri Aug 21 07:06:14 2009
New Revision: 806432

URL: http://svn.apache.org/viewvc?rev=806432&view=rev
Log:
https://issues.apache.org/jira/browse/MYFACES-2335

adding the callback listeners for the startup

Added:
    myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java   (with props)
Modified:
    myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java

Added: myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java?rev=806432&view=auto
==============================================================================
--- myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java (added)
+++ myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java Fri Aug 21 07:06:14 2009
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.webapp;
+
+import javax.servlet.ServletContextEvent;
+
+
+/**
+ * Startup Listener for the myfaces init process
+ * This interface allows to implement
+ * Plugins which then can be hooked into the various stages
+ * of our initialisation process to add various plugins
+ * which depend on the various phases of the init
+ */
+public interface StartupListener {
+    /**
+     * This method is called before myfaces initializes
+     *
+     * @param evt the corresponding servlet context event keeping all the servlet context data and references
+     */
+    public void preInit(ServletContextEvent evt);
+
+    /**
+     * This method is called after myfaces has initialized
+     *
+     * @param evt the corresponding servlet context event keeping all the servlet context data and references
+     */
+    public void postInit(ServletContextEvent evt);
+
+    /**
+     * This method is called before myfaces is destroyed
+     *
+     * @param evt the corresponding servlet context event keeping all the servlet context data and references
+     */
+    public void preDestroy(ServletContextEvent evt);
+
+    /**
+     * This method is called after myfaces is destroyed
+     *
+     * @param evt the corresponding servlet context event keeping all the servlet context data and references
+     */
+    public void postDestroy(ServletContextEvent evt);
+
+}
+

Propchange: myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupListener.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java?rev=806432&r1=806431&r2=806432&view=diff
==============================================================================
--- myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java (original)
+++ myfaces/core/branches/1.2.x/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java Fri Aug 21 07:06:14 2009
@@ -56,6 +56,10 @@
     /*comma delimited list of plugin classes which can be hooked into myfaces*/
     static final String FACES_INIT_PLUGINS = "org.apache.myfaces.FACES_INIT_PLUGINS";
 
+    private static final byte FACES_INIT_PHASE_PREINIT = 0;
+    private static final byte FACES_INIT_PHASE_POSTINIT = 1;
+    private static final byte FACES_INIT_PHASE_PREDESTROY = 2;
+    private static final byte FACES_INIT_PHASE_POSTDESTROY = 3;
 
     private static final Log log = LogFactory.getLog(StartupServletContextListener.class);
 
@@ -63,47 +67,50 @@
     private ServletContext _servletContext;
 
 
-    private void initializePlugins(ServletContextEvent event) {
+    /**
+     * the central initialisation event dispatcher which calls
+     * our listeners
+     * @param event
+     * @param operation
+     */
+    private void dispatchInitializationEvent(ServletContextEvent event, int operation) {
+        String [] pluginEntries = (String []) _servletContext.getAttribute(FACES_INIT_PLUGINS);
 
-        String plugins = (String) _servletContext.getInitParameter(FACES_INIT_PLUGINS);
+        if(pluginEntries == null) {
+            String plugins = (String) _servletContext.getInitParameter(FACES_INIT_PLUGINS);
+            log.info("Checking for plugins:"+FACES_INIT_PLUGINS);
+            if(plugins == null) return;
+            log.info("Plugins found");
+            pluginEntries = plugins.split(",");
+            _servletContext.setAttribute(FACES_INIT_PLUGINS, pluginEntries);
+        }
 
-        log.info("Checking for plugins:"+FACES_INIT_PLUGINS);
-        if(plugins == null) return;
-        log.info("Plugins found");
-        String [] pluginEntries = plugins.split(",");
         //now we process the plugins
         for(String plugin: pluginEntries) {
             log.info("Processing plugin:"+plugin);
             try {
+                //for now the initializers have to be stateless to
+                //so that we do not have to enforce that the initializer
+                //must be serializable
                 Class pluginClass = ClassUtils.getContextClassLoader().loadClass(plugin);
-                ServletContextListener initializer = (ServletContextListener) pluginClass.newInstance();
-                initializer.contextInitialized(event);
-            } catch (ClassNotFoundException e) {
-                log.error(e);
-            } catch (IllegalAccessException e) {
-                log.error(e);
-            } catch (InstantiationException e) {
-                log.error(e);
-            }
-
-        }
-        log.info("Processing plugins done");
-    }
-
-    private void deinitializePlugins(ServletContextEvent event) {
-        String plugins = (String) _servletContext.getInitParameter(FACES_INIT_PLUGINS);
+                StartupListener initializer = (StartupListener) pluginClass.newInstance();
+                
+                switch(operation) {
+                    case FACES_INIT_PHASE_PREINIT:
+                        initializer.preInit(event);
+                        break;
+                    case FACES_INIT_PHASE_POSTINIT:
+                        initializer.postInit(event);
+                        break;
+                    case FACES_INIT_PHASE_PREDESTROY:
+                        initializer.preDestroy(event);
+                        break;
+                    default:
+                        initializer.postDestroy(event);
+                        break;
+                }
 
-        log.info("Checking for plugins:"+FACES_INIT_PLUGINS);
-        if(plugins == null) return;
-        log.info("Plugins found");
-        String [] pluginEntries = plugins.split(",");
-        //now we process the plugins
-        for(String plugin: pluginEntries) {
-            log.info("Processing plugin:"+plugin);
-            try {
-                Class pluginClass = Thread.currentThread().getContextClassLoader().loadClass(plugin);
-                ServletContextListener initializer = (ServletContextListener) pluginClass.newInstance();
-                initializer.contextDestroyed(event);
+               
             } catch (ClassNotFoundException e) {
                 log.error(e);
             } catch (IllegalAccessException e) {
@@ -128,8 +135,9 @@
 
         if (b == null || b.booleanValue() == false)
         {
-            initializePlugins(event);
+            dispatchInitializationEvent(event, FACES_INIT_PHASE_PREINIT);
             getFacesInitializer().initFaces(_servletContext);
+            dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTINIT);
             _servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE);
         }
         else
@@ -182,6 +190,8 @@
             _facesInitializer.destroyFaces(_servletContext);
         }
         FactoryFinder.releaseFactories();
+        dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTDESTROY);
+
         _servletContext = null;
     }
     
@@ -189,7 +199,7 @@
                 
            ServletContext ctx = event.getServletContext();
 
-           deinitializePlugins(event);
+           dispatchInitializationEvent(event, FACES_INIT_PHASE_PREDESTROY);
            Enumeration<String> attributes = ctx.getAttributeNames();
 
            while(attributes.hasMoreElements())