1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.beehive.netui.core.urltemplates.URLTemplateDescriptor; 21 import org.apache.beehive.netui.pageflow.handler.Handlers; 22 import org.apache.beehive.netui.pageflow.internal.AdapterManager; 23 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 24 import org.apache.beehive.netui.pageflow.internal.LegacySettings; 25 import org.apache.beehive.netui.util.config.ConfigInitializationException; 26 import org.apache.beehive.netui.util.config.ConfigUtil; 27 import org.apache.beehive.netui.util.config.bean.PrefixHandlers; 28 import org.apache.beehive.netui.util.logging.Logger; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletContextEvent ; 32 import javax.servlet.ServletContextListener ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 36 37 38 41 public class PageFlowContextListener 42 implements ServletContextListener 43 { 44 private static final String ALREADY_INIT_ATTR = InternalConstants.ATTR_PREFIX + "contextInit"; 45 46 private static final Logger _log = Logger.getInstance( PageFlowContextListener.class ); 47 48 49 public void contextInitialized( ServletContextEvent event ) 50 { 51 performInitializations( event.getServletContext() ); 52 } 53 54 public void contextDestroyed( ServletContextEvent event ) 55 { 56 } 57 58 static boolean isInit( ServletContext servletContext ) 59 { 60 return servletContext.getAttribute( ALREADY_INIT_ATTR ) != null; 61 } 62 63 static void performInitializations( ServletContext servletContext ) 64 { 65 servletContext.setAttribute( ALREADY_INIT_ATTR, Boolean.TRUE ); 66 67 if ( ! ConfigUtil.isInit() ) 73 { 74 try 75 { 76 InputStream configInput = servletContext.getResourceAsStream( InternalConstants.NETUI_CONFIG_PATH ); 77 78 try 79 { 80 ConfigUtil.init( configInput ); 81 } 82 finally 83 { 84 try 85 { 86 if ( configInput != null ) configInput.close(); 87 } 88 catch ( IOException e ) 89 { 90 if ( _log.isErrorEnabled() ) 91 { 92 _log.error( "Could not close input for " + InternalConstants.NETUI_CONFIG_PATH ); 93 } 94 } 95 } 96 } 97 catch ( ConfigInitializationException e ) 98 { 99 _log.fatal( "Could not initialize from " + InternalConstants.NETUI_CONFIG_PATH, e ); 100 throw new IllegalStateException ( "Could not initialize from " + InternalConstants.NETUI_CONFIG_PATH, e ); 101 } 102 } 103 104 AdapterManager.initServletContext( servletContext ); 105 LegacySettings.init( servletContext ); 106 Handlers.init( servletContext ); 107 URLTemplateDescriptor.getInstance().load( servletContext ); 108 initPrefixHandlers(); 109 } 110 111 116 private static void initPrefixHandlers() 117 { 118 PrefixHandlers ph = ConfigUtil.getConfig().getPrefixHandlers(); 119 if ( ph == null ) 120 { 121 return; 122 } 123 PrefixHandlers.PrefixHandler[] prefixHandlers = ph.getPrefixHandlerArray(); 124 if ( prefixHandlers != null ) 125 { 126 for ( int i = 0; i < prefixHandlers.length; i++ ) 127 { 128 try 129 { 130 Class prefixClass = Class.forName( prefixHandlers[i].getHandlerClass() ); 131 String name = prefixHandlers[i].getName(); 132 if ( name == null || name.equals( "" ) ) 133 { 134 _log.warn( "The name for the prefix handler '" + prefixHandlers[i].getHandlerClass() 135 + "' must not be null" ); 136 continue; 137 } 138 Object o = prefixClass.newInstance(); 139 if ( !( o instanceof RequestParameterHandler ) ) 140 { 141 _log.warn( "The class '" + prefixHandlers[i].getHandlerClass() 142 + "' must be an instance of RequestParameterHandler" ); 143 continue; 144 } 145 ProcessPopulate.registerPrefixHandler( name, ( RequestParameterHandler ) o ); 146 } 147 catch ( ClassNotFoundException e ) 148 { 149 _log.warn( "Class '" + prefixHandlers[i].getHandlerClass() + "' not found", e ); 150 } 151 catch ( IllegalAccessException e ) 152 { 153 _log.warn( "Illegal access on Class '" + prefixHandlers[i].getHandlerClass() + "'", e ); 154 155 } 156 catch ( InstantiationException e ) 157 { 158 _log.warn( "InstantiationException on Class '" + prefixHandlers[i].getHandlerClass() + "'", 159 e.getCause() ); 160 } 161 } 162 } 163 } 164 } 165 | Popular Tags |