Coverage Report - org.mule.object.SingletonObjectFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SingletonObjectFactory
81%
25/31
100%
6/6
1.727
 
 1  
 /*
 2  
  * $Id: SingletonObjectFactory.java 11517 2008-03-31 21:34:19Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.object;
 12  
 
 13  
 import org.mule.api.lifecycle.InitialisationException;
 14  
 import org.mule.config.i18n.MessageFactory;
 15  
 
 16  
 import java.util.Map;
 17  
 
 18  
 /**
 19  
  * Creates an instance of the object once and then always returns the same instance.
 20  
  */
 21  
 public class SingletonObjectFactory extends AbstractObjectFactory
 22  
 {
 23  416
     private Object instance = null;
 24  
 
 25  
     /** For Spring only */
 26  
     public SingletonObjectFactory()
 27  
     {
 28  12
         super();
 29  12
     }
 30  
 
 31  
     public SingletonObjectFactory(String objectClassName)
 32  
     {
 33  0
         super(objectClassName);
 34  0
     }
 35  
 
 36  
     public SingletonObjectFactory(String objectClassName, Map properties)
 37  
     {
 38  0
         super(objectClassName, properties);
 39  0
     }
 40  
 
 41  
     public SingletonObjectFactory(Class objectClass)
 42  
     {
 43  14
         super(objectClass);
 44  14
     }
 45  
 
 46  
     public SingletonObjectFactory(Class objectClass, Map properties)
 47  
     {
 48  384
         super(objectClass, properties);
 49  384
     }
 50  
 
 51  
     /**
 52  
      * Create the singleton based on a previously created object.
 53  
      */
 54  
     public SingletonObjectFactory(Object instance)
 55  
     {
 56  6
         super(instance.getClass());
 57  6
         this.instance = instance;
 58  6
     }
 59  
 
 60  
     // @Override
 61  
     public void initialise() throws InitialisationException
 62  
     {
 63  790
         super.initialise();
 64  788
         if (instance == null)
 65  
         {
 66  
             try
 67  
             {
 68  402
                 instance = super.getInstance();
 69  
             }
 70  0
             catch (Exception e)
 71  
             {
 72  0
                 throw new InitialisationException(e, this);
 73  402
             }
 74  
         }
 75  788
     }
 76  
 
 77  
     // @Override
 78  
     public void dispose()
 79  
     {
 80  2
         instance = null;
 81  2
         super.dispose();
 82  2
     }
 83  
 
 84  
     /**
 85  
      * Always returns the same instance of the object.
 86  
      */
 87  
     // @Override
 88  
     public Object getInstance() throws Exception
 89  
     {
 90  30
         if (instance != null)
 91  
         {
 92  26
             return instance;
 93  
         }
 94  
         else
 95  
         {
 96  4
             throw new InitialisationException(
 97  
                 MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
 98  
         }
 99  
     }
 100  
 
 101  
     // @Override
 102  
     public Class getObjectClass()
 103  
     {
 104  16
         if (instance != null)
 105  
         {
 106  10
             return instance.getClass();
 107  
         }
 108  
         else
 109  
         {
 110  6
             return super.getObjectClass();
 111  
         }
 112  
     }
 113  
 
 114  
     public boolean isSingleton()
 115  
     {
 116  16
         return true;
 117  
     }
 118  
 
 119  
 }