Clover coverage report - Code Coverage for hivemind-lib release 1.0
Coverage timestamp: Wed Sep 22 2004 08:05:53 EDT
file stats: LOC: 173   Methods: 6
NCLOC: 116   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
BeanFactoryImpl.java 95.5% 100% 100% 98.6%
coverage coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.lib.factory;
 16   
 
 17   
 import java.lang.reflect.Constructor;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 import java.util.Map;
 22   
 
 23   
 import org.apache.commons.logging.Log;
 24   
 import org.apache.hivemind.ApplicationRuntimeException;
 25   
 import org.apache.hivemind.ErrorHandler;
 26   
 import org.apache.hivemind.HiveMind;
 27   
 import org.apache.hivemind.impl.BaseLocatable;
 28   
 import org.apache.hivemind.lib.BeanFactory;
 29   
 
 30   
 /**
 31   
  * Implementation of {@link org.apache.hivemind.lib.BeanFactory}.
 32   
  *
 33   
  * @author Howard Lewis Ship
 34   
  */
 35   
 public class BeanFactoryImpl extends BaseLocatable implements BeanFactory
 36   
 {
 37   
     private Log _log;
 38   
     private ErrorHandler _errorHandler;
 39   
     private Class _vendType;
 40   
     private Map _contributions = new HashMap();
 41   
     private Map _cache = new HashMap();
 42   
     private boolean _defaultCacheable;
 43   
 
 44  13
     public BeanFactoryImpl(
 45   
         Log log,
 46   
         ErrorHandler errorHandler,
 47   
         Class vendType,
 48   
         List contributions,
 49   
         boolean defaultCacheable)
 50   
     {
 51  13
         _log = log;
 52  13
         _errorHandler = errorHandler;
 53  13
         _vendType = vendType;
 54  13
         _defaultCacheable = defaultCacheable;
 55   
 
 56  13
         processContributions(contributions);
 57   
     }
 58   
 
 59  2
     public boolean contains(String locator)
 60   
     {
 61  2
         int commax = locator.indexOf(',');
 62   
 
 63  2
         String name = commax < 0 ? locator.trim() : locator.substring(0, commax);
 64   
 
 65  2
         return _contributions.containsKey(name);
 66   
     }
 67   
 
 68  13
     private void processContributions(List list)
 69   
     {
 70  13
         Iterator i = list.iterator();
 71   
 
 72  13
         while (i.hasNext())
 73   
         {
 74  15
             BeanFactoryContribution c = (BeanFactoryContribution) i.next();
 75   
 
 76  15
             Class beanClass = c.getBeanClass();
 77   
 
 78  15
             if (beanClass.isInterface() || beanClass.isArray() || beanClass.isPrimitive())
 79   
             {
 80  3
                 _errorHandler.error(
 81   
                     _log,
 82   
                     FactoryMessages.invalidContributionClass(c),
 83   
                     c.getLocation(),
 84   
                     null);
 85  3
                 continue;
 86   
             }
 87   
 
 88  12
             if (!_vendType.isAssignableFrom(beanClass))
 89   
             {
 90  1
                 _errorHandler.error(
 91   
                     _log,
 92   
                     FactoryMessages.wrongContributionType(c, _vendType),
 93   
                     c.getLocation(),
 94   
                     null);
 95  1
                 continue;
 96   
             }
 97   
 
 98  11
             String name = c.getName();
 99   
 
 100  11
             BeanFactoryContribution existing = (BeanFactoryContribution) _contributions.get(name);
 101   
 
 102  11
             if (existing != null)
 103   
             {
 104  1
                 _errorHandler.error(
 105   
                     _log,
 106   
                     FactoryMessages.dupeContribution(existing),
 107   
                     existing.getLocation(),
 108   
                     null);
 109  1
                 continue;
 110   
             }
 111   
 
 112   
             // Passed the checks, good to go.
 113   
 
 114  10
             _contributions.put(name, c);
 115   
         }
 116   
     }
 117   
 
 118  14
     public synchronized Object get(String locator)
 119   
     {
 120  14
         Object result = _cache.get(locator);
 121   
 
 122  14
         if (result == null)
 123  13
             result = create(locator);
 124   
 
 125  9
         return result;
 126   
     }
 127   
 
 128   
     // Implicitly synchronized by get()
 129   
 
 130  13
     private Object create(String locator)
 131   
     {
 132  13
         int commax = locator.indexOf(',');
 133   
 
 134  13
         String name = commax < 0 ? locator.trim() : locator.substring(0, commax);
 135  13
         String initializer = commax < 0 ? null : locator.substring(commax + 1).trim();
 136   
 
 137  13
         BeanFactoryContribution c = (BeanFactoryContribution) _contributions.get(name);
 138   
 
 139  13
         if (c == null)
 140  4
             throw new ApplicationRuntimeException(FactoryMessages.unknownContribution(name));
 141   
 
 142  9
         Object result = construct(c, initializer);
 143   
 
 144  8
         if (c.getStoreResultInCache(_defaultCacheable))
 145  6
             _cache.put(locator, result);
 146   
 
 147  8
         return result;
 148   
     }
 149   
 
 150  9
     private Object construct(BeanFactoryContribution contribution, String initializer)
 151   
     {
 152  9
         Class beanClass = contribution.getBeanClass();
 153   
 
 154  9
         try
 155   
         {
 156  9
             if (HiveMind.isBlank(initializer))
 157  5
                 return beanClass.newInstance();
 158   
 
 159  4
             Constructor c = beanClass.getConstructor(new Class[] { String.class });
 160   
 
 161  4
             return c.newInstance(new Object[] { initializer });
 162   
         }
 163   
         catch (Exception ex)
 164   
         {
 165  1
             throw new ApplicationRuntimeException(
 166   
                 FactoryMessages.unableToInstantiate(beanClass, ex),
 167   
                 contribution.getLocation(),
 168   
                 ex);
 169   
 
 170   
         }
 171   
     }
 172   
 
 173   
 }