2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.dialog.scxml.SCXMLLifecycleListener
 
Classes in this File Line Coverage Branch Coverage Complexity
SCXMLLifecycleListener
0%
0/75
0%
0/16
15
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.dialog.scxml;
 19  
 
 20  
 import java.net.URL;
 21  
 import java.util.Enumeration;
 22  
 import java.util.HashMap;
 23  
 import java.util.Map;
 24  
 
 25  
 import javax.faces.FacesException;
 26  
 import javax.servlet.ServletContextEvent;
 27  
 import javax.servlet.ServletContextListener;
 28  
 
 29  
 import org.apache.commons.beanutils.PropertyUtils;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 import org.apache.shale.dialog.scxml.config.ConfigurationParser;
 33  
 
 34  
 /**
 35  
  * <p>ServletContextListener that loads dialog configuration resources
 36  
  * at application startup, and cleans up the libraries we depend on at
 37  
  * application shutdown.</p>
 38  
  *
 39  
  * $Id: SCXMLLifecycleListener.java 476713 2006-11-19 05:10:57Z rahul $
 40  
  *
 41  
  * @since 1.0.4
 42  
  */
 43  0
 public class SCXMLLifecycleListener implements ServletContextListener {
 44  
 
 45  
 
 46  
     // -------------------------------------------------------- Static Variables
 47  
 
 48  
 
 49  
     /**
 50  
      * <p>The default configuration resource we will process (if present),
 51  
      * even if it is not explicitly configured.</p>
 52  
      */
 53  
     private static final String DEFAULT_CONFIGURATION_RESOURCE =
 54  
             "/WEB-INF/dialog-config.xml";
 55  
 
 56  
 
 57  
     /**
 58  
      * <p>Resource name for dialog configuration resource(s) embedded in
 59  
      * JAR files inside the application.</p>
 60  
      */
 61  
     private static final String EMBEDDED_CONFIGURATION_RESOURCE =
 62  
             "META-INF/dialog-config.xml";
 63  
 
 64  
 
 65  
     // ------------------------------------------------------ Instance Variables
 66  
 
 67  
 
 68  
     /**
 69  
      * <p>The <code>Log</code> instance we will use for this listener.</p>
 70  
      */
 71  0
     private Log log = LogFactory.getLog(SCXMLLifecycleListener.class);
 72  
 
 73  
 
 74  
     // ------------------------------------------ ServletContextListener Methods
 75  
 
 76  
 
 77  
     /**
 78  
      * <p>Process the application shutdown event, cleanup resources.</p>
 79  
      *
 80  
      * @param event Shutdown event to be processed
 81  
      */
 82  
     public void contextDestroyed(ServletContextEvent event) {
 83  
 
 84  0
         if (log.isInfoEnabled()) {
 85  0
             log.info("Finalizing Dialog SCXML Implementation");
 86  
         }
 87  
 
 88  
         // Clean up our cache of dialog configuration information
 89  0
         event.getServletContext().removeAttribute(Globals.DIALOGS);
 90  
 
 91  
         // Clean up dependency libraries we have used
 92  0
         PropertyUtils.clearDescriptors();
 93  0
         LogFactory.release(Thread.currentThread().getContextClassLoader());
 94  0
         log = null;
 95  
 
 96  0
     }
 97  
 
 98  
 
 99  
     /**
 100  
      * <p>Process the application startup event, parse dialog
 101  
      * configurations.</p>
 102  
      *
 103  
      * @param event Startup event to be processed
 104  
      */
 105  
     public void contextInitialized(ServletContextEvent event) {
 106  
 
 107  0
         if (log.isInfoEnabled()) {
 108  0
             log.info("Initializing Dialog SCXML Implementation");
 109  
         }
 110  
 
 111  
         // Set up to parse our specified configuration resources and cache the results
 112  0
         boolean didDefault = false;
 113  0
         ConfigurationParser parser = new ConfigurationParser();
 114  0
         parser.setDialogs(new HashMap());
 115  
 
 116  
         // Parse implicitly specified resources embedded in JAR files
 117  0
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 118  0
         if (loader == null) {
 119  0
             loader = this.getClass().getClassLoader();
 120  
         }
 121  
         try {
 122  0
             Enumeration resources = loader.getResources(EMBEDDED_CONFIGURATION_RESOURCE);
 123  0
             while (resources.hasMoreElements()) {
 124  0
                 URL resource = (URL) resources.nextElement();
 125  0
                 if (log.isDebugEnabled()) {
 126  0
                     log.debug("Parsing configuration resource '"
 127  
                             + resource + "'");
 128  
                 }
 129  0
                 parser.setResource(resource);
 130  0
                 parser.parse();
 131  0
             }
 132  0
         } catch (RuntimeException e) {
 133  0
             throw e;
 134  0
         } catch (Exception e) {
 135  0
             throw new FacesException(e);
 136  0
         }
 137  
 
 138  
         // Parse explicitly specified resources
 139  0
         String resources =
 140  
           event.getServletContext().getInitParameter(Globals.CONFIGURATION);
 141  0
         if (resources == null) {
 142  0
             resources = "";
 143  
         }
 144  0
         int comma = 0;
 145  0
         String path = null;
 146  
         try {
 147  
             while (true) {
 148  0
                 comma = resources.indexOf(",");
 149  0
                 if (comma < 0) {
 150  0
                     path = resources.trim();
 151  0
                     resources = "";
 152  0
                 } else {
 153  0
                     path = resources.substring(0, comma).trim();
 154  0
                     resources = resources.substring(comma + 1);
 155  
                 }
 156  0
                 if (path.length() < 1) {
 157  0
                     break;
 158  
                 }
 159  0
                 URL resource = event.getServletContext().getResource(path);
 160  0
                 if (log.isDebugEnabled()) {
 161  0
                     log.debug("Parsing configuration resource '"
 162  
                             + resource + "'");
 163  
                 }
 164  0
                 parser.setResource(resource);
 165  0
                 parser.parse();
 166  0
                 if (DEFAULT_CONFIGURATION_RESOURCE.equals(path)) {
 167  0
                     didDefault = true;
 168  
                 }
 169  0
             }
 170  0
         } catch (RuntimeException e) {
 171  0
             throw e;
 172  0
         } catch (Exception e) {
 173  0
             throw new FacesException(e);
 174  0
         }
 175  
 
 176  
         // Parse the default configuration resource if it exists and has not
 177  
         // already been parsed
 178  0
         if (!didDefault) {
 179  
             try {
 180  0
                 URL resource =
 181  
                   event.getServletContext().getResource(DEFAULT_CONFIGURATION_RESOURCE);
 182  0
                 if (resource != null) {
 183  0
                     if (log.isDebugEnabled()) {
 184  0
                         log.debug("Parsing configuration resource '"
 185  
                                 + resource + "'");
 186  
                     }
 187  0
                     parser.setResource(resource);
 188  0
                     parser.parse();
 189  
                 }
 190  0
             } catch (RuntimeException e) {
 191  0
                 throw e;
 192  0
             } catch (Exception e) {
 193  0
                 throw new FacesException(e);
 194  0
             }
 195  
         }
 196  
 
 197  
         // Cache the results in application scope
 198  0
         Map dialogs = parser.getDialogs();
 199  
 
 200  0
         if (dialogs.size() == 0) {
 201  0
             if (log.isWarnEnabled()) {
 202  0
                 log.warn("No dialog configuration information present.  No default configuration found at: "
 203  
                     + DEFAULT_CONFIGURATION_RESOURCE + ".  No embedded configuration found at: "
 204  
                     + EMBEDDED_CONFIGURATION_RESOURCE);
 205  
             }
 206  
         }
 207  0
         event.getServletContext().setAttribute(Globals.DIALOGS, dialogs);
 208  
 
 209  0
     }
 210  
 
 211  
 
 212  
 }