KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseContextListener.java,v 1.8 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.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.ServletContextEvent JavaDoc;
29 import javax.servlet.ServletContextListener JavaDoc;
30
31 import org.opensubsystems.core.error.OSSException;
32 import org.opensubsystems.core.persist.db.Database;
33 import org.opensubsystems.core.persist.db.DatabaseImpl;
34 import org.opensubsystems.core.persist.db.DatabaseSchema;
35 import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
36 import org.opensubsystems.core.util.Log;
37
38 /**
39  * DatabaseContextListener is responsible for initialization of database access
40  * layer for the web application.
41  * It determines what database schemas are used by this web application, loads
42  * them and initialize them and then initialize and starts the database access.
43  *
44  * The database schemas to must be specified in web.xml using context parameters
45  * for example:
46  *
47  * <context-param>
48  * <param-name>database.schema.0</param-name>
49  * <param-value>org.opensubsystems.user.data.UserDatabaseSchema</param-value>
50  * </context-param>
51  * <context-param>
52  * <param-name>database.schema.1</param-name>
53  * <param-value>org.opensubsystems.session.data.SessionDatabaseSchema</param-value>
54  * </context-param>
55  * <context-param>
56  * <param-name>database.schema.2</param-name>
57  * <param-value>org.opensubsystems.security.data.RoleDatabaseSchema</param-value>
58  * </context-param>
59  *
60  * @version $Id: DatabaseContextListener.java,v 1.8 2007/01/07 06:14:09 bastafidli Exp $
61  * @author Miro Halas
62  * @code.reviewer Miro Halas
63  * @code.reviewed 1.5 2006/04/20 13:59:45 bastafidli
64  */

65 public class DatabaseContextListener implements ServletContextListener JavaDoc
66 {
67    // Configuration parameters /////////////////////////////////////////////////
68

69    /**
70     * This configuration setting is used to name parameters such as
71     * oss.database.schema.0, oss.database.schema.1 which specify what database
72     * schemas the application consists of. The database schemas are initialized
73     * in the numeric order specified in the configuration setting name after
74     * all dependencies exposed by the individual database schemas are taken into
75     * account.
76     */

77    public static final String JavaDoc DATABASE_SCHEMA_PREFIX = "oss.database.schema.";
78
79    // Cached variables /////////////////////////////////////////////////////////
80

81    /**
82     * Logger for this class
83     */

84    private static Logger JavaDoc s_logger = Log.getInstance(DatabaseContextListener.class);
85
86    // Constructors ////////////////////////////////////////////////////////////
87

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

91    public void contextInitialized(
92       ServletContextEvent JavaDoc servletContextEvent
93    )
94    {
95       s_logger.entering(this.getClass().getName(), "contextInitialized");
96       try
97       {
98          // Do this when context is created to that the database is aware of
99
// required schemas as soon as possible
100
Database dbDatabase;
101          ServletContext JavaDoc scContext;
102          String JavaDoc strSchemaClassName;
103          int iIndex = 0;
104          DatabaseSchema schema;
105          
106          scContext = servletContextEvent.getServletContext();
107    
108          try
109          {
110             dbDatabase = DatabaseImpl.getInstance();
111           
112             do
113             {
114                strSchemaClassName = WebUtils.readProperty(scContext,
115                                        DATABASE_SCHEMA_PREFIX + iIndex, null, true);
116                if ((strSchemaClassName != null) && (strSchemaClassName.length() > 0))
117                {
118                   s_logger.fine("Read schema name " + strSchemaClassName);
119                   schema = DatabaseSchemaManager.getInstance(strSchemaClassName);
120                   s_logger.fine("Instantiated schema " + strSchemaClassName);
121                   dbDatabase.add(schema);
122                   iIndex++;
123                }
124             }
125             while (strSchemaClassName != null);
126                                                  
127             // Try to initialize the default database for this web application
128
// now when all the schemas are added
129
s_logger.fine("Initializing default database.");
130             dbDatabase.start();
131             s_logger.fine("Database default initialized.");
132          }
133          catch (OSSException ossExc)
134          {
135             // No way to throw checked exception so convert it to unchecked
136
s_logger.log(Level.SEVERE, "Unexpected exception.", ossExc);
137             throw new RuntimeException JavaDoc("Unexpected exception.", ossExc);
138          }
139          catch (SecurityException JavaDoc eSec)
140          {
141             // No way to throw checked exception so convert it to unchecked
142
s_logger.log(Level.SEVERE, "Unexpected exception.", eSec);
143             throw new RuntimeException JavaDoc("Unexpected exception.", eSec);
144          }
145          catch (IllegalArgumentException JavaDoc eIllArg)
146          {
147             // No way to throw checked exception so convert it to unchecked
148
s_logger.log(Level.SEVERE, "Unexpected exception.", eIllArg);
149             throw new RuntimeException JavaDoc("Unexpected exception.", eIllArg);
150          }
151          // This is here just so we get a log about the exception since the
152
// web server may not print it out
153
catch (Throwable JavaDoc thr)
154          {
155             // No way to throw checked exception so convert it to unchecked
156
s_logger.log(Level.SEVERE, "Unexpected exception.", thr);
157             throw new RuntimeException JavaDoc("Unexpected exception.", thr);
158          }
159       }
160       finally
161       {
162          s_logger.exiting(this.getClass().getName(), "contextInitialized");
163       }
164    }
165    
166    /**
167     * {@inheritDoc}
168     */

169    public void contextDestroyed(
170       ServletContextEvent JavaDoc servletContextEvent
171    )
172    {
173       s_logger.entering(this.getClass().getName(), "contextDestroyed");
174       try
175       {
176          // Stop the default database
177
Database dbDatabase;
178       
179          try
180          {
181             dbDatabase = DatabaseImpl.getInstance();
182             if (dbDatabase != null)
183             {
184                dbDatabase.stop();
185             }
186          }
187          catch (OSSException bfeExc)
188          {
189             throw new RuntimeException JavaDoc("Unexpected exception.", bfeExc);
190          }
191       }
192       finally
193       {
194          s_logger.exiting(this.getClass().getName(), "contextDestroyed");
195       }
196    }
197 }
198
Popular Tags