1 16 17 package org.apache.commons.dbcp.datasources; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.IOException ; 21 import java.io.ObjectInputStream ; 22 23 import java.util.Hashtable ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Properties ; 28 29 import javax.naming.Context ; 30 import javax.naming.Name ; 31 import javax.naming.RefAddr ; 32 import javax.naming.Reference ; 33 import javax.naming.spi.ObjectFactory ; 34 35 41 abstract class InstanceKeyObjectFactory 42 implements ObjectFactory 43 { 44 private static Map instanceMap = new HashMap (); 45 46 synchronized static String registerNewInstance(InstanceKeyDataSource ds) { 47 int max = 0; 48 Iterator i = instanceMap.keySet().iterator(); 49 while (i.hasNext()) { 50 Object obj = i.next(); 51 if (obj instanceof String ) 52 { 53 try { 54 max = Math.max(max, Integer.valueOf((String )obj).intValue()); 55 } 56 catch (NumberFormatException e) { 57 } 59 } 60 } 61 String instanceKey = String.valueOf(max + 1); 62 instanceMap.put(instanceKey, ds); 65 return instanceKey; 66 } 67 68 static void removeInstance(String key) 69 { 70 instanceMap.remove(key); 71 } 72 73 76 public static void closeAll() throws Exception { 77 Iterator instanceIterator = instanceMap.entrySet().iterator(); 79 while (instanceIterator.hasNext()) { 80 ((InstanceKeyDataSource) 81 ((Map.Entry ) instanceIterator.next()).getValue()).close(); 82 } 83 instanceMap.clear(); 84 } 85 86 87 91 public Object getObjectInstance(Object refObj, Name name, 92 Context context, Hashtable env) 93 throws IOException , ClassNotFoundException { 94 Object obj = null; 97 if (refObj instanceof Reference ) { 98 Reference ref = (Reference ) refObj; 99 if (isCorrectClass(ref.getClassName())) { 100 RefAddr ra = ref.get("instanceKey"); 101 if (ra != null && ra.getContent() != null) { 102 obj = instanceMap.get(ra.getContent()); 104 } 105 else 106 { 107 String key = null; 111 if (name != null) 112 { 113 key = name.toString(); 114 obj = instanceMap.get(key); 115 } 116 if (obj == null) 117 { 118 InstanceKeyDataSource ds = getNewInstance(ref); 119 setCommonProperties(ref, ds); 120 obj = ds; 121 if (key != null) 122 { 123 instanceMap.put(key, ds); 124 } 125 } 126 } 127 } 128 } 129 return obj; 130 } 131 132 private void setCommonProperties(Reference ref, 133 InstanceKeyDataSource ikds) 134 throws IOException , ClassNotFoundException { 135 136 RefAddr ra = ref.get("dataSourceName"); 137 if (ra != null && ra.getContent() != null) { 138 ikds.setDataSourceName(ra.getContent().toString()); 139 } 140 141 ra = ref.get("defaultAutoCommit"); 142 if (ra != null && ra.getContent() != null) { 143 ikds.setDefaultAutoCommit(Boolean.valueOf( 144 ra.getContent().toString()).booleanValue()); 145 } 146 147 ra = ref.get("defaultReadOnly"); 148 if (ra != null && ra.getContent() != null) { 149 ikds.setDefaultReadOnly(Boolean.valueOf( 150 ra.getContent().toString()).booleanValue()); 151 } 152 153 ra = ref.get("description"); 154 if (ra != null && ra.getContent() != null) { 155 ikds.setDescription(ra.getContent().toString()); 156 } 157 158 ra = ref.get("jndiEnvironment"); 159 if (ra != null && ra.getContent() != null) { 160 byte[] serialized = (byte[]) ra.getContent(); 161 ikds.jndiEnvironment = 162 (Properties ) deserialize(serialized); 163 } 164 165 ra = ref.get("loginTimeout"); 166 if (ra != null && ra.getContent() != null) { 167 ikds.setLoginTimeout( 168 Integer.parseInt(ra.getContent().toString())); 169 } 170 171 ra = ref.get("testOnBorrow"); 172 if (ra != null && ra.getContent() != null) { 173 ikds.setTestOnBorrow( 174 Boolean.getBoolean(ra.getContent().toString())); 175 } 176 177 ra = ref.get("testOnReturn"); 178 if (ra != null && ra.getContent() != null) { 179 ikds.setTestOnReturn(Boolean.valueOf( 180 ra.getContent().toString()).booleanValue()); 181 } 182 183 ra = ref.get("timeBetweenEvictionRunsMillis"); 184 if (ra != null && ra.getContent() != null) { 185 ikds.setTimeBetweenEvictionRunsMillis( 186 Integer.parseInt(ra.getContent().toString())); 187 } 188 189 ra = ref.get("numTestsPerEvictionRun"); 190 if (ra != null && ra.getContent() != null) { 191 ikds.setNumTestsPerEvictionRun( 192 Integer.parseInt(ra.getContent().toString())); 193 } 194 195 ra = ref.get("minEvictableIdleTimeMillis"); 196 if (ra != null && ra.getContent() != null) { 197 ikds.setMinEvictableIdleTimeMillis( 198 Integer.parseInt(ra.getContent().toString())); 199 } 200 201 ra = ref.get("testWhileIdle"); 202 if (ra != null && ra.getContent() != null) { 203 ikds.setTestWhileIdle(Boolean.valueOf( 204 ra.getContent().toString()).booleanValue()); 205 } 206 207 ra = ref.get("validationQuery"); 208 if (ra != null && ra.getContent() != null) { 209 ikds.setValidationQuery(ra.getContent().toString()); 210 } 211 } 212 213 214 218 protected abstract boolean isCorrectClass(String className); 219 220 224 protected abstract InstanceKeyDataSource getNewInstance(Reference ref) 225 throws IOException , ClassNotFoundException ; 226 227 230 protected static final Object deserialize(byte[] data) 231 throws IOException , ClassNotFoundException { 232 ObjectInputStream in = null; 233 try { 234 in = new ObjectInputStream (new ByteArrayInputStream (data)); 235 return in.readObject(); 236 } finally { 237 try { 238 in.close(); 239 } catch (IOException ex) { 240 } 241 } 242 } 243 } 244 245 | Popular Tags |