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.glassfish;
21:
22: import uk.org.primrose.*;
23: import uk.org.primrose.pool.core.*;
24: import uk.org.primrose.pool.datasource.*;
25: import java.util.*;
26: import javax.naming.*;
27: import javax.naming.spi.ObjectFactory;
28:
29: //C:\java\glassfish\bin>asadmin create-custom-resource --restype javax.naming.spi.ObjectFactory --factoryclass uk.org.primrose.vendor.glassfish.PrimroseDataSourceFactory --property poolName=hcr:primroseConfigFile=/java/primrose_3.0/dummy.conf jdbc/hcr
30: public class PrimroseDataSourceFactory extends GenericDataSourceFactory
31: implements ObjectFactory {
32:
33: /**
34: * Initialize a pool as defined in a config file.
35: * The config file may contain multiple pools, but only the one specified is loaded
36: * because Glassfish does the JNDI binding itself (one datasource at a time)
37: *
38: */
39: public Object getObjectInstance(Object refObj, Name nm,
40: Context ctx, Hashtable env) throws Exception {
41: PrimroseDataSource pds = null;
42:
43: Reference ref = (Reference) refObj;
44:
45: if (ref.get("poolName") == null) {
46: throw new Exception(
47: "You must specify a 'poolName' in the <Resource> tag");
48: }
49:
50: String poolName = (String) ref.get("poolName").getContent();
51:
52: // Glassfish seems to call the factory each time you
53: // look up the JNDI name ... so don't load the pool twice !
54: Pool alreadyLoadedPool = PoolLoader.findExistingPool(poolName);
55: if (alreadyLoadedPool != null) {
56: pds = new PrimroseDataSource();
57: pds.setPoolName(poolName);
58: } else {
59: Properties poolProps = null;
60:
61: try {
62: // Load from properties defined in the Resource tag
63: // if they have not specified a config file
64: if (ref.get("primroseConfigFile") == null) {
65: poolProps = new Properties();
66:
67: for (Enumeration e = ref.getAll(); e
68: .hasMoreElements();) {
69: StringRefAddr refAddr = (StringRefAddr) e
70: .nextElement();
71: String name = refAddr.getType();
72: String value = (String) refAddr.getContent();
73:
74: poolProps.setProperty(name, value);
75: }
76:
77: pds = loadPool(poolName, poolProps);
78:
79: // Load pool from a file
80: } else {
81: String primroseConfigFile = (String) ref.get(
82: "primroseConfigFile").getContent();
83: pds = loadPool(poolName, primroseConfigFile);
84: }
85:
86: } catch (GeneralException ge) {
87: throw new Exception("Error loading pool '" + poolName
88: + "' : " + ge);
89: }
90:
91: if (pds == null) {
92: throw new Exception(
93: "Could not generate PrimroseDataSource object due to previous errors - check the settings in your config");
94: }
95: }
96:
97: return pds;
98: }
99: }
|