Coverage Report - org.apache.commons.dbcp.datasources.InstanceKeyObjectFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
InstanceKeyObjectFactory
71%
62/87
37%
28/74
6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>s
 38  
  * or <code>PerUserPoolDataSource</code>s
 39  
  *
 40  
  * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $
 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  
                     // no sweat, ignore those keys
 59  6110
                 }
 60  
             }
 61  6566
         }
 62  158
         String instanceKey = String.valueOf(max + 1);
 63  
         // put a placeholder here for now, so other instances will not
 64  
         // take our key.  we will replace with a pool when ready.
 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  
      * Close all pools associated with this class.
 76  
      */
 77  
     public static void closeAll() throws Exception {
 78  
         //Get iterator to loop over all instances of this datasource.
 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  
      * implements ObjectFactory to create an instance of SharedPoolDataSource
 90  
      * or PerUserPoolDataSource
 91  
      */
 92  
     public Object getObjectInstance(Object refObj, Name name,
 93  
                                     Context context, Hashtable env)
 94  
         throws IOException, ClassNotFoundException {
 95  
         // The spec says to return null if we can't create an instance
 96  
         // of the reference
 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  
                     // object was bound to jndi via Referenceable api.
 104  2
                     obj = instanceMap.get(ra.getContent());
 105  
                 }
 106  
                 else
 107  
                 {
 108  
                     // tomcat jndi creates a Reference out of server.xml
 109  
                     // <ResourceParam> configuration and passes it to an
 110  
                     // instance of the factory given in server.xml.
 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  
      * @return true if and only if className is the value returned
 217  
      * from getClass().getName().toString()
 218  
      */
 219  
     protected abstract boolean isCorrectClass(String className);
 220  
 
 221  
     /**
 222  
      * Creates an instance of the subclass and sets any properties
 223  
      * contained in the Reference.
 224  
      */
 225  
     protected abstract InstanceKeyDataSource getNewInstance(Reference ref)
 226  
         throws IOException, ClassNotFoundException;
 227  
 
 228  
     /**
 229  
      * used to set some properties saved within a Reference
 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