KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > datasource > MasterPoolDataSourceFactory


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * 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 */

21
22 package uk.org.primrose.pool.datasource;
23
24 import uk.org.primrose.pool.jmx.*;
25 import java.io.* ;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import javax.sql.DataSource JavaDoc;
31 import java.util.*;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.Name JavaDoc;
35 import javax.naming.Reference JavaDoc;
36 import javax.naming.spi.ObjectFactory JavaDoc;
37
38 public class MasterPoolDataSourceFactory implements ObjectFactory JavaDoc, Serializable {
39     static PoolController pc;
40
41     public MasterPoolDataSourceFactory() {
42     }
43
44         public static Queue[] getPoolQueues() {
45         ArrayList al = pc.getQueues();
46         if (al.size() == 0) return null;
47
48         Queue[] queues = new Queue[al.size()];
49         for (int i = 0; i < queues.length; i++) {
50             queues[i] = (Queue)al.get(i);
51         }
52
53         return queues;
54     }
55
56     public static void shutdownPools() {
57         pc.stopAllPoolQueues();
58         pc.destroy();
59     }
60
61
62     public Object JavaDoc getObjectInstance(Object JavaDoc refObj, Name JavaDoc nm, Context JavaDoc ctx, Hashtable env) throws Exception JavaDoc {
63
64         Reference JavaDoc ref = (Reference JavaDoc) refObj;
65         String JavaDoc className = ref.getClassName();
66
67         Object JavaDoc configType = null;
68         if (ref.get("configFile") != null) configType = ref.get("configFile").getContent();
69
70         Object JavaDoc dataSourceConfigType = null;
71         if (ref.get("dataSourceConfigFile") != null) dataSourceConfigType = ref.get("dataSourceConfigFile").getContent();
72
73         Object JavaDoc adminConfigFileType = null;
74         if (ref.get("adminConfigFile") != null) adminConfigFileType = ref.get("adminConfigFile").getContent();
75         if (configType == null && (dataSourceConfigType == null || adminConfigFileType == null)) {
76             System.out.println("[Primrose::MasterPoolDataSourceFactory] ERROR ! Cannot find either 'configFile' or ('dataSourceConfigFile' and 'adminConfigFile') parameters - check you have configured Primrose correctly");
77             return null;
78         }
79
80         if (configType != null) {
81             dataSourceConfigType = configType;
82             adminConfigFileType = configType;
83         }
84
85         if (configType instanceof String JavaDoc || dataSourceConfigType instanceof String JavaDoc) {
86 // System.err.println("[MasterPoolDataSourceFactory] Loading pools.");
87

88             //if (pc == null) { //removed 2.7.1 so we reset everything on a stop/start of entire pool
89
pc = new PoolController();
90                 pc.setPoolConfigFile((String JavaDoc)dataSourceConfigType);
91                 // get the pool controller log file loc
92
pc.setAdminDetails((String JavaDoc)adminConfigFileType);
93 // System.err.println("[MasterPoolDataSourceFactory] Initializing PoolController.");
94
pc.init(new String JavaDoc[]{});
95 // System.err.println("[MasterPoolDataSourceFactory] JMX PoolController loaded.");
96
//}
97
// System.err.println("[MasterPoolDataSourceFactory] Loading pools from " +configFile +".");
98
pc.loadPoolsFromConfigFile((String JavaDoc)dataSourceConfigType, true);
99 // System.err.println("[MasterPoolDataSourceFactory] Retrieving pool instances.");
100
Object JavaDoc pools = pc.getQueues();
101 // System.err.println("[MasterPoolDataSourceFactory] Pools initialized.");
102
return pools;
103         } else if (configType instanceof java.util.Properties JavaDoc) {
104             if (pc == null) {
105                 //System.err.println("[MasterPoolDataSourceFactory] Constructing PoolController.");
106
pc = new PoolController();
107
108                 pc.setPoolConfigFile(null);
109
110                 // OK, we can load a pool just with a properties file
111
// but since 2.6.1, we do need a pool controller log file
112
// If this the the first time they are firing up the pool, the
113
// first properties object must contain the admin details,
114
// so load that, then init() the pool controller
115

116                 Properties props = (Properties)configType;
117
118                 pc.loadPoolsFromConfigFile(props);
119                 //System.err.println("[MasterPoolDataSourceFactory] Initializing PoolController.");
120

121
122                 pc.init(new String JavaDoc[]{props.getProperty("encryptionKeyFile")});
123                 //System.err.println("[MasterPoolDataSourceFactory] JMX PoolController loaded.");
124
} else {
125
126                 Properties props = (Properties)configType;
127                 //CharArrayWriter caw = new CharArrayWriter();
128
//for (Enumeration e = props.propertyNames() ; e.hasMoreElements() ;) {
129
// String key = (String)e.nextElement();
130
// caw.write(key +"=" +props.getProperty(key) +"\n");
131
//}
132

133                 //System.err.println("[MasterPoolDataSourceFactory] Loading pools from runtime properties file.");
134
pc.loadPoolsFromConfigFile(props);
135                 //System.err.println("[MasterPoolDataSourceFactory] Retrieving pool instances.");
136
}
137
138             Object JavaDoc pools = pc.getQueues();
139             //System.err.println("[MasterPoolDataSourceFactory] Pools initialized.");
140
return pools;
141
142
143         }
144
145         return null;
146     }
147
148
149 }
150
Popular Tags