KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > listeners > ContextListener


1 /*
2  * $Id: ContextListener.java,v 1.6 2005/06/08 08:04:12 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * Tariq Dweik <tariqd@gmail.com>
24  * .
25  * * ***** END LICENSE BLOCK ***** */

26 /*
27  * Created on 08.08.2004
28  *
29  */

30 package org.jresearch.gossip.listeners;
31
32 import java.io.IOException JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.Reference JavaDoc;
38 import javax.naming.StringRefAddr JavaDoc;
39 import javax.rmi.PortableRemoteObject JavaDoc;
40 import javax.servlet.ServletContextEvent JavaDoc;
41 import javax.servlet.ServletContextListener JavaDoc;
42 import javax.sql.DataSource JavaDoc;
43
44 import org.jresearch.gossip.dao.file.berkeleydb.FileDbEnv;
45 import org.jresearch.gossip.exception.SystemException;
46
47 /**
48  * @author Dmitry Belov
49  *
50  */

51 public class ContextListener implements ServletContextListener JavaDoc {
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
57      */

58     public void contextInitialized(ServletContextEvent JavaDoc evt) {
59         boolean useDatasource = Boolean.valueOf(
60                 evt.getServletContext().getInitParameter("useDatasource"))
61                 .booleanValue();
62         String JavaDoc datasourceName = evt.getServletContext().getInitParameter(
63                 "datasourceName");
64
65         InitialContext JavaDoc ic;
66         try {
67             ic = new InitialContext JavaDoc();
68             if (useDatasource) {
69                 if (datasourceName == null)
70                     throw new RuntimeException JavaDoc(
71                             "Using datasource is enabled but datasourceName parameter is not specified.");
72
73                 DataSource JavaDoc dataSource = (DataSource JavaDoc) PortableRemoteObject
74                         .narrow(ic.lookup(datasourceName),
75                                 javax.sql.DataSource JavaDoc.class);
76                 ic.rebind("jgossip_db", dataSource);
77
78             } else {
79                 Properties JavaDoc dbconf = new Properties JavaDoc();
80                 dbconf
81                         .load(evt
82                                 .getServletContext()
83                                 .getResourceAsStream(
84                                         "/WEB-INF/classes/org/jresearch/gossip/resources/db.properties"));
85                 // Construct BasicDataSource reference
86
Reference JavaDoc ref = new Reference JavaDoc("javax.sql.DataSource",
87                         "org.apache.commons.dbcp.BasicDataSourceFactory", null);
88                 ref.add(new StringRefAddr JavaDoc("driverClassName", dbconf
89                         .getProperty("driverClassName")));
90                 ref.add(new StringRefAddr JavaDoc("url", dbconf.getProperty("url")));
91                 ref.add(new StringRefAddr JavaDoc("password", dbconf
92                         .getProperty("password")));
93                 ref.add(new StringRefAddr JavaDoc("username", dbconf
94                         .getProperty("username")));
95                 ref.add(new StringRefAddr JavaDoc("maxActive", dbconf
96                         .getProperty("maxActive")));
97                 ref.add(new StringRefAddr JavaDoc("maxWait", dbconf
98                         .getProperty("maxWait")));
99                 ref.add(new StringRefAddr JavaDoc("initialSize", dbconf
100                         .getProperty("initialSize")));
101                 ref.add(new StringRefAddr JavaDoc("defaultAutoCommit", dbconf
102                         .getProperty("defaultAutoCommit")));
103                 ref.add(new StringRefAddr JavaDoc("defaultReadOnly", dbconf
104                         .getProperty("defaultReadOnly")));
105                 ref.add(new StringRefAddr JavaDoc("poolPreparedStatements", dbconf
106                         .getProperty("poolPreparedStatements")));
107                 ref.add(new StringRefAddr JavaDoc("maxOpenPreparedStatements", dbconf
108                         .getProperty("maxOpenPreparedStatements")));
109
110                 ic.rebind("jgossip_db", ref);
111             }
112
113         } catch (NamingException JavaDoc e) {
114             throw new RuntimeException JavaDoc(e);
115         } catch (IOException JavaDoc e) {
116             throw new RuntimeException JavaDoc(e);
117         }
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
124      */

125     public void contextDestroyed(ServletContextEvent JavaDoc arg0) {
126         try {
127             FileDbEnv.close();
128         } catch (SystemException e) {
129             e.printStackTrace();
130         }
131     }
132
133 }
Popular Tags