KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > PageFlowContextListener


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
31 import javax.servlet.ServletContextEvent JavaDoc;
32 import javax.servlet.ServletContextListener JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36
37
38 /**
39  * Performs various initialization at ServletContext-init time.
40  */

41 public class PageFlowContextListener
42         implements ServletContextListener JavaDoc
43 {
44     private static final String JavaDoc 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 JavaDoc event )
50     {
51         performInitializations( event.getServletContext() );
52     }
53
54     public void contextDestroyed( ServletContextEvent JavaDoc event )
55     {
56     }
57     
58     static boolean isInit( ServletContext JavaDoc servletContext )
59     {
60         return servletContext.getAttribute( ALREADY_INIT_ATTR ) != null;
61     }
62     
63     static void performInitializations( ServletContext JavaDoc servletContext )
64     {
65         servletContext.setAttribute( ALREADY_INIT_ATTR, Boolean.TRUE );
66         
67         //
68
// Initialize the config file, unless it's already initialized. This can happen because the scope for the
69
// config (static) isn't the same as the scope for PageFlowActionServlet, which may get created and destroyed
70
// within a classloader (which is the case during StrutsTestCase tests).
71
//
72
if ( ! ConfigUtil.isInit() )
73         {
74             try
75             {
76                 InputStream JavaDoc 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 JavaDoc 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 JavaDoc( "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     /**
112      * This method will initialize all of the PrefixHandlers registered in the netui config.
113      * The prefix handlers are registered with ProcessPopulate and are typically implemented as
114      * public inner classes in the tags that require prefix handlers.
115      */

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 JavaDoc prefixClass = Class.forName( prefixHandlers[i].getHandlerClass() );
131                     String JavaDoc 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 JavaDoc 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 JavaDoc e )
148                 {
149                     _log.warn( "Class '" + prefixHandlers[i].getHandlerClass() + "' not found", e );
150                 }
151                 catch ( IllegalAccessException JavaDoc e )
152                 {
153                     _log.warn( "Illegal access on Class '" + prefixHandlers[i].getHandlerClass() + "'", e );
154
155                 }
156                 catch ( InstantiationException JavaDoc e )
157                 {
158                     _log.warn( "InstantiationException on Class '" + prefixHandlers[i].getHandlerClass() + "'",
159                                e.getCause() );
160                 }
161             }
162         }
163     }
164 }
165
Popular Tags