KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > WebModuleListener


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: WebModuleListener.java,v 1.6 2007/01/07 06:14:09 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.www;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.ServletContextEvent JavaDoc;
31 import javax.servlet.ServletContextListener JavaDoc;
32
33 import org.opensubsystems.core.error.OSSException;
34 import org.opensubsystems.core.util.Log;
35
36 /**
37  * WebModuleListener is responsible for initialization of
38  * web modules available in the system. It determines what
39  * web modules are used by this web application, loads them
40  * and initialize them.
41  *
42  * The web module definition bundles must be specified in web.xml using context
43  * parameters, for example:
44  *
45  * <context-param>
46  * <param-name>oss.webclient.module.0</param-name>
47  * <param-value>org.opensubsystems.security.www.SecurityWebModule</param-value>
48  * </context-param>
49  * <context-param>
50  * <param-name>oss.webclient.module.1</param-name>
51  * <param-value>org.opensubsystems.portal.www.PortalWebModule</param-value>
52  * </context-param>
53  *
54  * @version $Id: WebModuleListener.java,v 1.6 2007/01/07 06:14:09 bastafidli Exp $
55  * @author Julian Legeny
56  * @code.reviewer Miro Halas
57  * @code.reviewed 1.2 2006/03/25 01:53:36 jlegeny
58  */

59 public class WebModuleListener implements ServletContextListener JavaDoc
60 {
61    // Cached values ////////////////////////////////////////////////////////////
62

63    /**
64     * Logger for this class
65     */

66    private static Logger JavaDoc s_logger = Log.getInstance(WebModuleListener.class);
67
68    // Configuration parameters /////////////////////////////////////////////////
69

70    /**
71     * This is used to name parameters such as oss.webclient.module.0,
72     * oss.webclient.module.1 which specify what web modules the application
73     * consists of. The web modules are initialized in the numeric order
74     * specified in the configuration setting name.
75     */

76    public static final String JavaDoc WEBCLIENT_MODULE_PREFIX = "oss.webclient.module.";
77    
78    /**
79     * This is used to name parameters such as oss.webclient.module.url.0,
80     * oss.webclient.module.url.1 and they are used for defining URL related
81     * to particular web module.
82     */

83    public static final String JavaDoc WEBCLIENT_MODULE_URL_PREFIX = WEBCLIENT_MODULE_PREFIX + "url.";
84
85    // Public methods ///////////////////////////////////////////////////////////
86

87    /**
88     * {@inheritDoc}
89     */

90    public void contextInitialized(
91       ServletContextEvent JavaDoc servletContextEvent
92    )
93    {
94       s_logger.entering(this.getClass().getName(), "contextInitialized");
95       try
96       {
97          // Do this when context is created to that the application is aware
98
// what access rights are available for each module
99
ServletContext JavaDoc scContext;
100          String JavaDoc strModuleClassName;
101          String JavaDoc strModuleURL;
102          int iIndex = 0;
103          WebModule module;
104          Method JavaDoc methodGetInstance;
105          WebModuleDefinitionManager manager;
106          
107          scContext = servletContextEvent.getServletContext();
108    
109          try
110          {
111             manager = WebModuleDefinitionManager.getInstance();
112             do
113             {
114                // read web module name
115
strModuleClassName = WebUtils.readProperty(scContext,
116                                        WEBCLIENT_MODULE_PREFIX + iIndex, null, true);
117                if ((strModuleClassName != null) && (strModuleClassName.length() > 0))
118                {
119                   s_logger.fine("Read web module name " + strModuleClassName);
120                   methodGetInstance = Class.forName(
121                                          strModuleClassName).getMethod("getInstance", null);
122                   module = (WebModule)methodGetInstance.invoke(null, null);
123                   s_logger.fine("Instantiated web module " + strModuleClassName);
124    
125                   // if web module name exists, read particular web module URL
126
strModuleURL = WebUtils.readProperty(scContext,
127                                          WEBCLIENT_MODULE_URL_PREFIX + iIndex, null, true);
128                   if ((strModuleURL != null) && (strModuleURL.length() > 0))
129                   {
130                      s_logger.fine("Read web module URL " + strModuleURL);
131                      // set up web module URL into the module definition
132
module.setURL(strModuleURL);
133                   }
134                   manager.add(module);
135                   iIndex++;
136                }
137             }
138             while (strModuleClassName != null);
139          }
140          catch (OSSException ossExc)
141          {
142             // No way to throw checked exception so convert it to unchecked
143
s_logger.log(Level.SEVERE, "Unexpected exception.", ossExc);
144             throw new RuntimeException JavaDoc("Unexpected exception.", ossExc);
145          }
146          catch (SecurityException JavaDoc eSec)
147          {
148             s_logger.log(Level.SEVERE, "Unexpected exception.", eSec);
149             throw new RuntimeException JavaDoc("Unexpected exception.", eSec);
150          }
151          catch (NoSuchMethodException JavaDoc eNoMeth)
152          {
153             s_logger.log(Level.SEVERE, "Unexpected exception.", eNoMeth);
154             throw new RuntimeException JavaDoc("Unexpected exception.", eNoMeth);
155          }
156          catch (ClassNotFoundException JavaDoc eNoClass)
157          {
158             s_logger.log(Level.SEVERE, "Unexpected exception.", eNoClass);
159             throw new RuntimeException JavaDoc("Unexpected exception.", eNoClass);
160          }
161          catch (IllegalArgumentException JavaDoc eIllArg)
162          {
163             s_logger.log(Level.SEVERE, "Unexpected exception.", eIllArg);
164             throw new RuntimeException JavaDoc("Unexpected exception.", eIllArg);
165          }
166          catch (IllegalAccessException JavaDoc eIllAcc)
167          {
168             s_logger.log(Level.SEVERE, "Unexpected exception.", eIllAcc);
169             throw new RuntimeException JavaDoc("Unexpected exception.", eIllAcc);
170          }
171          catch (InvocationTargetException JavaDoc eInvoTarg)
172          {
173             s_logger.log(Level.SEVERE, "Unexpected exception.", eInvoTarg);
174             throw new RuntimeException JavaDoc("Unexpected exception.", eInvoTarg);
175          }
176          // This is here just so we get a log about the exception since the
177
// web server may not print it out
178
catch (Throwable JavaDoc thr)
179          {
180             // No way to throw checked exception so convert it to unchecked
181
s_logger.log(Level.SEVERE, "Unexpected exception.", thr);
182             throw new RuntimeException JavaDoc("Unexpected exception.", thr);
183          }
184       }
185       finally
186       {
187          s_logger.exiting(this.getClass().getName(), "contextInitialized");
188       }
189    }
190    
191    /**
192     * {@inheritDoc}
193     */

194    public void contextDestroyed(
195       ServletContextEvent JavaDoc servletContextEvent
196    )
197    {
198       s_logger.entering(this.getClass().getName(), "contextDestroyed");
199       try
200       {
201          // Nothing to do for now
202
}
203       finally
204       {
205          s_logger.exiting(this.getClass().getName(), "contextDestroyed");
206       }
207    }
208 }
209
Popular Tags