001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose.pool.datasource;
021:
022: import uk.org.primrose.GeneralException;
023: import uk.org.primrose.Util;
024: import uk.org.primrose.pool.core.*;
025:
026: import java.util.*;
027: import java.io.*;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NoInitialContextException;
032:
033: public class GenericDataSourceFactory {
034:
035: /**
036: * Generate a PrimroseDataSource object - which provides access to pooled connections,
037: * from config file containing one or more defined pools
038: */
039: public PrimroseDataSource loadPool(String poolName,
040: String configFile) throws GeneralException, IOException {
041:
042: if (poolName == null || configFile == null) {
043: throw new GeneralException(
044: "You must specify a non-null 'poolName' and a 'configFile' as parameters");
045: }
046:
047: Properties poolProps = Util.generatePropertiesForPoolName(
048: configFile, poolName);
049:
050: return loadPool(poolName, poolProps);
051:
052: }
053:
054: /**
055: * Generate a PrimroseDataSource object - which provides access to pooled connections,
056: * from a Properties object containing pool config data
057: */
058: public PrimroseDataSource loadPool(String poolName,
059: Properties poolProps) throws GeneralException, IOException {
060:
061: if (poolName == null || poolProps == null) {
062: throw new GeneralException(
063: "You must specify a non-null 'poolName' pool Properties object as parameters");
064: }
065:
066: if (poolProps != null) {
067: PoolLoader.loadPool(poolProps, true /* create new */);
068: }
069:
070: PrimroseDataSource pds = new PrimroseDataSource();
071: pds.setPoolName(poolName);
072:
073: return pds;
074: }
075:
076: public Context findOrBindJNDIEnvContext() throws GeneralException {
077: Context envCtx = null;
078: Context initCtx = null;
079: try {
080: initCtx = new InitialContext();
081:
082: try {
083: // Try to find the env subcontext
084: envCtx = (Context) initCtx.lookup("java:comp/env");
085: } catch (NoInitialContextException e) {
086:
087: // If they've loaded Jetty 5 with no JNDI support, use our own JNDI context
088: // and create the relevant sub contexts
089: System.setProperty("java.naming.factory.url.pkgs",
090: "uk.org.primrose.jndi");
091: System
092: .setProperty("java.naming.factory.initial",
093: "uk.org.primrose.jndi.PrimroseInitialContextFactory");
094:
095: initCtx = new InitialContext();
096: Context ctx = (Context) initCtx.lookup("java:comp");
097: try {
098: envCtx = (Context) ctx.lookup("env");
099: } catch (Exception e2) {
100: // no env context - forget it and create it below in a while
101: }
102: } catch (Exception e) {
103: // We have a JNDI context (primrose's or jetty's)
104: // but no env subcontext - forget it and create it below in a while
105: }
106:
107: } catch (Exception e) {
108: throw new GeneralException(
109: "Error looking up JNDI context java:comp or java:comp/env",
110: e);
111: }
112:
113: if (envCtx == null) {
114: try {
115: Context ctx = (Context) initCtx.lookup("java:comp");
116: envCtx = ctx.createSubcontext("env");
117: } catch (Exception e2) {
118: throw new GeneralException(
119: "Error creating JNDI subcontext 'env' under java:comp",
120: e2);
121: }
122: }
123:
124: return envCtx;
125: }
126:
127: /**
128: * Load a set of pools from a config file, and bind them to a JNDI context under java:comp/env
129: * If there is no JNDI context, then Primrose's own JNDI implementation is used.
130: *
131: * @param primroseConfigFile
132: * @throws GeneralException
133: */
134:
135: public void loadPools(String primroseConfigFile)
136: throws GeneralException {
137: String currentPoolName = null;
138: try {
139: Context envCtx = findOrBindJNDIEnvContext();
140: List<String> loadedPoolNames = PoolLoader.loadPool(
141: primroseConfigFile, true);
142: for (String poolName : loadedPoolNames) {
143: currentPoolName = poolName;
144: PrimroseDataSource pds = new PrimroseDataSource();
145: pds.setPoolName(poolName);
146: envCtx.rebind(poolName, pds);
147: }
148:
149: } catch (Exception e) {
150: throw new GeneralException("Error loading pool '"
151: + currentPoolName + "'", e);
152: }
153: }
154:
155: }
|