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