01: /**
02: * Library name : Primrose - A Java Database Connection Pool.
03: * Published by Ben Keeping, http://primrose.org.uk .
04: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
05: * Email: Use "Contact Us Form" on website
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library 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 GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */package uk.org.primrose.vendor.tomcat;
21:
22: import uk.org.primrose.GeneralException;
23: import uk.org.primrose.pool.datasource.*;
24: import java.util.*;
25: import javax.naming.*;
26: import javax.naming.spi.ObjectFactory;
27:
28: public class PrimroseDataSourceFactory extends GenericDataSourceFactory
29: implements ObjectFactory {
30:
31: /**
32: * Initialize a pool as defined in a config file.
33: * The config file may contain multiple pools, but only the one specified is loaded
34: * because Tomcat does the JNDI binding itself (one datasource at a time)
35: *
36: */
37: public Object getObjectInstance(Object refObj, Name nm,
38: Context ctx, Hashtable env) throws Exception {
39: PrimroseDataSource pds = null;
40:
41: Reference ref = (Reference) refObj;
42:
43: if (ref.get("poolName") == null) {
44: throw new Exception(
45: "You must specify a 'poolName' in the <Resource> tag");
46: }
47:
48: String poolName = (String) ref.get("poolName").getContent();
49:
50: Properties poolProps = null;
51:
52: try {
53: // Load from properties defined in the Resource tag
54: // if they have not specified a config file
55: if (ref.get("primroseConfigFile") == null) {
56: poolProps = new Properties();
57:
58: for (Enumeration e = ref.getAll(); e.hasMoreElements();) {
59: StringRefAddr refAddr = (StringRefAddr) e
60: .nextElement();
61: String name = refAddr.getType();
62: String value = (String) refAddr.getContent();
63:
64: poolProps.setProperty(name, value);
65: }
66:
67: pds = loadPool(poolName, poolProps);
68:
69: // Load pool from a file
70: } else {
71: String primroseConfigFile = (String) ref.get(
72: "primroseConfigFile").getContent();
73: pds = loadPool(poolName, primroseConfigFile);
74: }
75: } catch (GeneralException ge) {
76: throw new Exception("Error loading pool '" + poolName
77: + "' : " + ge);
78: }
79:
80: if (pds == null) {
81: throw new Exception(
82: "Could not generate PrimroseDataSource object due to previous errors - check the settings in your config");
83: }
84:
85: return pds;
86: }
87:
88: }
|