1 26 30 package org.jresearch.gossip.listeners; 31 32 import java.io.IOException ; 33 import java.util.Properties ; 34 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 import javax.naming.Reference ; 38 import javax.naming.StringRefAddr ; 39 import javax.rmi.PortableRemoteObject ; 40 import javax.servlet.ServletContextEvent ; 41 import javax.servlet.ServletContextListener ; 42 import javax.sql.DataSource ; 43 44 import org.jresearch.gossip.dao.file.berkeleydb.FileDbEnv; 45 import org.jresearch.gossip.exception.SystemException; 46 47 51 public class ContextListener implements ServletContextListener { 52 53 58 public void contextInitialized(ServletContextEvent evt) { 59 boolean useDatasource = Boolean.valueOf( 60 evt.getServletContext().getInitParameter("useDatasource")) 61 .booleanValue(); 62 String datasourceName = evt.getServletContext().getInitParameter( 63 "datasourceName"); 64 65 InitialContext ic; 66 try { 67 ic = new InitialContext (); 68 if (useDatasource) { 69 if (datasourceName == null) 70 throw new RuntimeException ( 71 "Using datasource is enabled but datasourceName parameter is not specified."); 72 73 DataSource dataSource = (DataSource ) PortableRemoteObject 74 .narrow(ic.lookup(datasourceName), 75 javax.sql.DataSource .class); 76 ic.rebind("jgossip_db", dataSource); 77 78 } else { 79 Properties dbconf = new Properties (); 80 dbconf 81 .load(evt 82 .getServletContext() 83 .getResourceAsStream( 84 "/WEB-INF/classes/org/jresearch/gossip/resources/db.properties")); 85 Reference ref = new Reference ("javax.sql.DataSource", 87 "org.apache.commons.dbcp.BasicDataSourceFactory", null); 88 ref.add(new StringRefAddr ("driverClassName", dbconf 89 .getProperty("driverClassName"))); 90 ref.add(new StringRefAddr ("url", dbconf.getProperty("url"))); 91 ref.add(new StringRefAddr ("password", dbconf 92 .getProperty("password"))); 93 ref.add(new StringRefAddr ("username", dbconf 94 .getProperty("username"))); 95 ref.add(new StringRefAddr ("maxActive", dbconf 96 .getProperty("maxActive"))); 97 ref.add(new StringRefAddr ("maxWait", dbconf 98 .getProperty("maxWait"))); 99 ref.add(new StringRefAddr ("initialSize", dbconf 100 .getProperty("initialSize"))); 101 ref.add(new StringRefAddr ("defaultAutoCommit", dbconf 102 .getProperty("defaultAutoCommit"))); 103 ref.add(new StringRefAddr ("defaultReadOnly", dbconf 104 .getProperty("defaultReadOnly"))); 105 ref.add(new StringRefAddr ("poolPreparedStatements", dbconf 106 .getProperty("poolPreparedStatements"))); 107 ref.add(new StringRefAddr ("maxOpenPreparedStatements", dbconf 108 .getProperty("maxOpenPreparedStatements"))); 109 110 ic.rebind("jgossip_db", ref); 111 } 112 113 } catch (NamingException e) { 114 throw new RuntimeException (e); 115 } catch (IOException e) { 116 throw new RuntimeException (e); 117 } 118 } 119 120 125 public void contextDestroyed(ServletContextEvent arg0) { 126 try { 127 FileDbEnv.close(); 128 } catch (SystemException e) { 129 e.printStackTrace(); 130 } 131 } 132 133 } | Popular Tags |