1 21 package uk.org.primrose.pool.standalone; 22 23 import java.util.*; 24 import java.io.*; 25 import javax.naming.Reference ; 26 import javax.naming.StringRefAddr ; 27 import javax.naming.InitialContext ; 28 import javax.naming.Context ; 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 args[]) { 39 if (args.length != 1) { 40 System.err.println("Usuage : java uk.org.primrose.pool.standalone.PoolControllerStandalone <poolConfig.properties location>"); 42 System.exit(1); 43 } 44 45 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 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 initialContextPackage = System.getProperty("java.naming.factory.url.pkgs"); 82 String initialContextFactory = System.getProperty("java.naming.factory.initial"); 83 if (initialContextPackage == null || 84 initialContextFactory == null) { 85 88 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 ref = new Reference ((String )(ht.get("className"))); 108 for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) { 109 String refType = (String )e.nextElement(); 110 if (ht.get(refType) instanceof String ) { 111 String refAddr = (String )ht.get(refType); 112 StringRefAddr sra = new StringRefAddr (refType, refAddr); 113 ref.add(sra); 114 } else { 115 String refAddr = "runtime_properties_load"; 116 ObjectRefAddr sra = new ObjectRefAddr(refType, "runtime_properties_load".getBytes()); 117 sra.setContent(ht.get(refType)); 118 ref.add(sra); 120 } 121 } 122 123 if (inst) instantiate(ref.getClassName(), ref); 124 } 125 126 private void instantiate(String className, Reference ref) { 127 try { 128 Class targetClass = Class.forName(className); 129 Object obj = targetClass.newInstance(); 130 Method[] publicMethods = targetClass.getMethods(); 131 for (int j = 0; j < publicMethods.length; j++) { 132 String fieldName = publicMethods[j].getName(); 133 Class typeClass = publicMethods[j].getReturnType(); 134 String fieldType = typeClass.getName(); 135 if ("getObjectInstance".equalsIgnoreCase(fieldName)) { 136 Object o = publicMethods[j].invoke(obj, new Object []{ref, null, null, null}); 137 Context ctx = new InitialContext (); 138 Object comp = null; 139 140 146 ctx.rebind("java:comp/env/" +(ref.get("jndiName").getContent()), o); 151 ctx.rebind((String )(ref.get("jndiName").getContent()), o); 152 153 } 154 } 155 156 } catch (Exception e) { 157 e.printStackTrace(System.err); 158 } 159 160 161 } 162 163 164 } | Popular Tags |