Clover coverage report -
Coverage timestamp: Sun Feb 29 2004 21:42:04 CET
file stats: LOC: 209   Methods: 14
NCLOC: 141   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractComponentFactory.java 40% 71.4% 100% 72.7%
coverage coverage
 1   
 /* ====================================================================
 2   
  The Jicarilla Software License
 3   
 
 4   
  Copyright (c) 2003 Leo Simons.
 5   
  All rights reserved.
 6   
 
 7   
  Permission is hereby granted, free of charge, to any person obtaining
 8   
  a copy of this software and associated documentation files (the
 9   
  "Software"), to deal in the Software without restriction, including
 10   
  without limitation the rights to use, copy, modify, merge, publish,
 11   
  distribute, sublicense, and/or sell copies of the Software, and to
 12   
  permit persons to whom the Software is furnished to do so, subject to
 13   
  the following conditions:
 14   
 
 15   
  The above copyright notice and this permission notice shall be
 16   
  included in all copies or substantial portions of the Software.
 17   
 
 18   
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19   
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 20   
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 21   
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 22   
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 23   
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 24   
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 25   
 ==================================================================== */
 26   
 package org.jicarilla.container.factories;
 27   
 
 28   
 import org.jicarilla.container.CyclicDependencyException;
 29   
 import org.jicarilla.container.Factory;
 30   
 import org.jicarilla.container.JicarillaClassNotFoundException;
 31   
 import org.jicarilla.container.JicarillaException;
 32   
 import org.jicarilla.container.JicarillaIllegalAccessException;
 33   
 import org.jicarilla.container.JicarillaInstantiationException;
 34   
 import org.jicarilla.container.JicarillaInvocationTargetException;
 35   
 import org.jicarilla.container.Resolver;
 36   
 import org.jicarilla.container.util.ReflectionUtil;
 37   
 import org.jicarilla.framework.Assert;
 38   
 import org.jicarilla.framework.LifecycleUtil;
 39   
 
 40   
 import java.lang.reflect.InvocationTargetException;
 41   
 
 42   
 /**
 43   
  * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 44   
  * @version $Id: AbstractComponentFactory.java,v 1.9 2004/01/25 22:12:53 lsimons Exp $
 45   
  */
 46   
 public abstract class AbstractComponentFactory implements Factory
 47   
 {
 48   
     // ----------------------------------------------------------------------
 49   
     //  Properties
 50   
     // ----------------------------------------------------------------------
 51   
     protected final Resolver m_resolver;
 52   
     protected final String m_className;
 53   
     protected Class m_clazz;
 54   
     protected boolean m_inTheProcessOfConstructingAnInstance = false;
 55   
 
 56   
     // ----------------------------------------------------------------------
 57   
     //  Constructors
 58   
     // ----------------------------------------------------------------------
 59  356
     public AbstractComponentFactory( final Resolver resolver,
 60   
             final String className )
 61   
     {
 62  356
         Assert.assertNotNull( "resolver argument may not be null", resolver );
 63  356
         Assert.assertNotNull( "className argument may not be null", className );
 64   
 
 65  356
         m_resolver = resolver;
 66  356
         m_className = className;
 67   
     }
 68   
 
 69   
     // ----------------------------------------------------------------------
 70   
     //  Interface: Factory
 71   
     // ----------------------------------------------------------------------
 72   
 
 73  250
     public Object newInstance()
 74   
     {
 75  250
         lazyInitialization();
 76  248
         Object instance = null;
 77   
 
 78  248
         try
 79   
         {
 80  248
             try
 81   
             {
 82  248
                 instance = doNewInstance();
 83   
             }
 84   
             catch( InstantiationException e )
 85   
             {
 86  0
                 throw new JicarillaInstantiationException( e );
 87   
             }
 88   
             catch( IllegalAccessException e )
 89   
             {
 90  0
                 throw new JicarillaIllegalAccessException( e );
 91   
             }
 92   
             catch( InvocationTargetException e )
 93   
             {
 94  0
                 throw new JicarillaInvocationTargetException( e );
 95   
             }
 96   
         }
 97   
         catch( CyclicDependencyException cde )
 98   
         {
 99  5
             handleCyclicDependencyException( cde );
 100   
         }
 101   
         finally
 102   
         {
 103  248
             disableCyclicDependencyChecking();
 104   
         }
 105  243
         initialize( instance );
 106  243
         return instance;
 107   
     }
 108   
 
 109  8
     public void releaseInstance( final Object component ) throws Exception
 110   
     {
 111  8
         doReleaseInstance( component );
 112   
     }
 113   
 
 114  6
     protected void doReleaseInstance( final Object component ) throws Exception
 115   
     {
 116  6
         dispose( component );
 117   
     }
 118   
 
 119   
     protected abstract Object doNewInstance() throws IllegalAccessException, InvocationTargetException, InstantiationException;
 120   
 
 121   
     // ----------------------------------------------------------------------
 122   
     //  Getters/Setters
 123   
     // ----------------------------------------------------------------------
 124  596
     protected Resolver getResolver()
 125   
     {
 126  596
         return m_resolver;
 127   
     }
 128   
 
 129  243
     protected String getClassName()
 130   
     {
 131  243
         return m_className;
 132   
     }
 133   
 
 134  641
     protected Class getClazz()
 135   
     {
 136  641
         return m_clazz;
 137   
     }
 138   
 
 139   
     // ----------------------------------------------------------------------
 140   
     //  Helper Methods
 141   
     // ----------------------------------------------------------------------
 142  246
     protected void enableCyclicDependencyChecking()
 143   
     {
 144  246
         m_inTheProcessOfConstructingAnInstance = true;
 145   
     }
 146   
 
 147  248
     protected void checkCyclicDependency( final Class[] parameterTypes )
 148   
     {
 149  248
         if( m_inTheProcessOfConstructingAnInstance )
 150  2
             throw new CyclicDependencyException( parameterTypes );
 151   
     }
 152   
 
 153  248
     protected void disableCyclicDependencyChecking()
 154   
     {
 155  248
         m_inTheProcessOfConstructingAnInstance = false;
 156   
     }
 157   
 
 158  5
     protected void handleCyclicDependencyException(
 159   
             final CyclicDependencyException cde )
 160   
     {
 161  5
         throw cde;
 162   
     }
 163   
 
 164  371
     protected void lazyInitialization()
 165   
     {
 166  371
         if( m_clazz == null )
 167  243
             try
 168   
             {
 169  243
                 m_clazz = ReflectionUtil.loadClass( getClassName() );
 170   
             }
 171   
             catch( ClassNotFoundException e )
 172   
             {
 173  0
                 throw new JicarillaClassNotFoundException( e );
 174   
             }
 175   
     }
 176   
 
 177  243
     protected void initialize( Object instance )
 178   
     {
 179  243
         try
 180   
         {
 181  243
             LifecycleUtil.initialize( instance );
 182   
         }
 183   
         catch( Throwable t )
 184   
         {
 185  0
             throw new JicarillaInstantiationException( t );
 186   
         }
 187   
     }
 188   
 
 189  6
     protected void dispose( Object instance ) throws Exception
 190   
     {
 191  6
         try
 192   
         {
 193  6
             LifecycleUtil.dispose( instance );
 194   
         }
 195   
         catch( Throwable t )
 196   
         {
 197  0
             if( t instanceof Exception )
 198  0
                 throw (Exception)t;
 199  0
             if( t instanceof RuntimeException )
 200  0
                 throw (RuntimeException)t;
 201  0
             if( t instanceof Error )
 202  0
                 throw (Error)t;
 203   
 
 204  0
             throw new JicarillaException( t );
 205   
         }
 206   
 
 207   
     }
 208   
 }
 209