KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > standalone > PoolControllerStandalone


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 package uk.org.primrose.pool.standalone;
22
23 import java.util.*;
24 import java.io.*;
25 import javax.naming.Reference JavaDoc;
26 import javax.naming.StringRefAddr JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import java.lang.reflect.*;
30 import uk.org.primrose.pool.jmx.PoolController;
31 import uk.org.primrose.pool.jmx.MBeanUtil;
32 import uk.org.primrose.pool.datasource.MasterPoolDataSourceFactory;
33
34 public class PoolControllerStandalone {
35     static PoolControllerStandalone pcs;
36
37
38     public static void main(String JavaDoc args[]) {
39         if (args.length != 1) {
40             //System.err.println("Usuage : java -Djava.naming.factory.url.pkgs=uk.org.primrose.pool.standalone -Djava.naming.factory.initial=uk.org.primrose.pool.standalone.PoolInitialContextFactory uk.org.primrose.pool.standalone.PoolControllerStandalone <poolConfig.properties location>");
41
System.err.println("Usuage : java uk.org.primrose.pool.standalone.PoolControllerStandalone <poolConfig.properties location>");
42             System.exit(1);
43         }
44
45         //System.out.println("[PoolControllerStandalone] Loading primrose ...");
46
PoolControllerStandalone.load(args[0]);
47     }
48
49     public static void shutdown() {
50         MasterPoolDataSourceFactory.shutdownPools();
51     }
52
53     public static void load(Properties properties) {
54         if (pcs == null) pcs = new PoolControllerStandalone();
55         else {
56             MasterPoolDataSourceFactory.shutdownPools();
57             pcs = new PoolControllerStandalone();
58         }
59
60         pcs.checkSystemJNDIProperties();
61         Hashtable props = new Hashtable();
62         props.put("jndiName", "masterPool");
63         props.put("className", "uk.org.primrose.pool.datasource.MasterPoolDataSourceFactory");
64         props.put("configFile", properties);
65         pcs.jndiBind(props, true);
66     }
67
68
69     public static void load(String JavaDoc poolConfigFile) {
70         if (pcs == null) pcs = new PoolControllerStandalone();
71
72         pcs.checkSystemJNDIProperties();
73         Hashtable props = new Hashtable();
74         props.put("jndiName", "masterPool");
75         props.put("className", "uk.org.primrose.pool.datasource.MasterPoolDataSourceFactory");
76         props.put("configFile", poolConfigFile);
77         pcs.jndiBind(props, true);
78     }
79
80     private void checkSystemJNDIProperties() {
81         String JavaDoc initialContextPackage = System.getProperty("java.naming.factory.url.pkgs");
82         String JavaDoc initialContextFactory = System.getProperty("java.naming.factory.initial");
83         if (initialContextPackage == null ||
84             initialContextFactory == null) {
85             //(!initialContextPackage.equals("uk.org.primrose.pool.standalone")) ||
86
//(!initialContextFactory.equals("uk.org.primrose.pool.standalone.PoolInitialContextFactory"))) {
87

88             /*
89             System.out.println("\n\n--------------------------------------------------------\n" +
90                 "[PoolControllerStandalone] INFO : \nThe following JVM proerties are not set :" +
91                 "\n\tjava.naming.factory.url.pkgs\n\t" +
92                 "java.naming.factory.initial\n\n" +
93                 "So am using internal JNDI context provider ...\n" +
94                 "Setting System.properties as :\n\t" +
95                 "java.naming.factory.url.pkgs=uk.org.primrose.pool.standalone\n\t" +
96                 "java.naming.factory.initial=uk.org.primrose.pool.standalone.PoolInitialContextFactory" +
97                 "\n--------------------------------------------------------\n\n");
98             */

99
100                 System.setProperty("java.naming.factory.url.pkgs", "uk.org.primrose.pool.standalone");
101                 System.setProperty("java.naming.factory.initial", "uk.org.primrose.pool.standalone.PoolInitialContextFactory");
102         }
103
104     }
105
106     private void jndiBind(Hashtable ht, boolean inst) {
107         Reference JavaDoc ref = new Reference JavaDoc((String JavaDoc)(ht.get("className")));
108         for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) {
109             String JavaDoc refType = (String JavaDoc)e.nextElement();
110             if (ht.get(refType) instanceof String JavaDoc) {
111                 String JavaDoc refAddr = (String JavaDoc)ht.get(refType);
112                 StringRefAddr JavaDoc sra = new StringRefAddr JavaDoc(refType, refAddr);
113                 ref.add(sra);
114             } else {
115                 String JavaDoc refAddr = "runtime_properties_load";
116                 ObjectRefAddr sra = new ObjectRefAddr(refType, "runtime_properties_load".getBytes());
117                 sra.setContent(ht.get(refType));
118                 //StringRefAddr sra = new StringRefAddr(refType, refAddr);
119
ref.add(sra);
120             }
121         }
122
123         if (inst) instantiate(ref.getClassName(), ref);
124     }
125
126     private void instantiate(String JavaDoc className, Reference JavaDoc ref) {
127         try {
128             Class JavaDoc targetClass = Class.forName(className);
129             Object JavaDoc obj = targetClass.newInstance();
130             Method[] publicMethods = targetClass.getMethods();
131             for (int j = 0; j < publicMethods.length; j++) {
132                 String JavaDoc fieldName = publicMethods[j].getName();
133                 Class JavaDoc typeClass = publicMethods[j].getReturnType();
134                 String JavaDoc fieldType = typeClass.getName();
135                 if ("getObjectInstance".equalsIgnoreCase(fieldName)) {
136                     Object JavaDoc o = publicMethods[j].invoke(obj, new Object JavaDoc[]{ref, null, null, null});
137                     Context JavaDoc ctx = new InitialContext JavaDoc();
138                     Object JavaDoc comp = null;
139
140                     // Bind the masterPool (but not twice)
141
//try {
142
// comp = ctx.lookup("java:comp/env/masterPool");
143
//} catch (javax.naming.NamingException ne) {
144
//}
145

146                     //if (comp == null) {
147
// ctx.bind("java:comp/env/" +(ref.get("jndiName").getContent()), o);
148
// ctx.bind((String)(ref.get("jndiName").getContent()), o);
149
//}
150
ctx.rebind("java:comp/env/" +(ref.get("jndiName").getContent()), o);
151                     ctx.rebind((String JavaDoc)(ref.get("jndiName").getContent()), o);
152
153                 }
154             }
155
156         } catch (Exception JavaDoc e) {
157             e.printStackTrace(System.err);
158         }
159
160
161     }
162
163
164 // private void destroy() {
165
// pc.destroy();
166
// }
167
}
Popular Tags