Coverage Report - org.mule.object.AbstractObjectFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractObjectFactory
0%
0/65
0%
0/20
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.object;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.construct.FlowConstruct;
 11  
 import org.mule.api.construct.FlowConstructAware;
 12  
 import org.mule.api.lifecycle.InitialisationCallback;
 13  
 import org.mule.api.lifecycle.InitialisationException;
 14  
 import org.mule.api.object.ObjectFactory;
 15  
 import org.mule.api.service.Service;
 16  
 import org.mule.api.service.ServiceAware;
 17  
 import org.mule.config.i18n.MessageFactory;
 18  
 import org.mule.util.BeanUtils;
 19  
 import org.mule.util.ClassUtils;
 20  
 
 21  
 import java.util.ArrayList;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * Creates object instances based on the class and sets any properties.  This factory is also responsible for applying
 30  
  * any object processors on the object before the lifecycle callbacks are called.
 31  
  */
 32  
 public abstract class AbstractObjectFactory implements ObjectFactory, FlowConstructAware
 33  
 {
 34  
 
 35  
     public static final String ATTRIBUTE_OBJECT_CLASS_NAME = "objectClassName";
 36  
     public static final String ATTRIBUTE_OBJECT_CLASS = "objectClass";
 37  
 
 38  
     protected String objectClassName;
 39  
     protected Class<?> objectClass;
 40  0
     protected Map properties = null;
 41  0
     protected List<InitialisationCallback> initialisationCallbacks = new ArrayList<InitialisationCallback>();
 42  
     protected FlowConstruct flowConstruct;
 43  0
     protected boolean disposed = false;
 44  
 
 45  0
     protected transient Log logger = LogFactory.getLog(getClass());
 46  
 
 47  
     /**
 48  
      * For Spring only
 49  
      */
 50  
     public AbstractObjectFactory()
 51  0
     {
 52  
         // nop
 53  0
     }
 54  
 
 55  
     public AbstractObjectFactory(String objectClassName)
 56  
     {
 57  0
         this(objectClassName, null);
 58  0
     }
 59  
 
 60  
     public AbstractObjectFactory(String objectClassName, Map properties)
 61  
     {
 62  0
         super();
 63  0
         this.objectClassName = objectClassName;
 64  0
         this.properties = properties;
 65  0
         setupObjectClassFromObjectClassName();
 66  0
     }
 67  
 
 68  
     public AbstractObjectFactory(Class<?> objectClass)
 69  
     {
 70  0
         this(objectClass, null);
 71  0
     }
 72  
 
 73  
     public AbstractObjectFactory(Class<?> objectClass, Map properties)
 74  
     {
 75  0
         super();
 76  0
         this.objectClassName = objectClass.getName();
 77  0
         this.objectClass = objectClass;
 78  0
         this.properties = properties;
 79  0
     }
 80  
 
 81  
     protected Class<?> setupObjectClassFromObjectClassName()
 82  
     {
 83  
         try
 84  
         {
 85  0
             Class<?> klass = ClassUtils.getClass(objectClassName);
 86  0
             objectClass = klass;
 87  0
             return klass;
 88  
         }
 89  0
         catch (ClassNotFoundException e)
 90  
         {
 91  0
             throw new IllegalArgumentException(e);
 92  
         }
 93  
     }
 94  
 
 95  
     public void setFlowConstruct(FlowConstruct flowConstruct)
 96  
     {
 97  0
         this.flowConstruct = flowConstruct;
 98  0
     }
 99  
 
 100  
     public void initialise() throws InitialisationException
 101  
     {
 102  0
         if ((objectClassName == null) || (objectClass == null))
 103  
         {
 104  0
             throw new InitialisationException(
 105  
                     MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
 106  
         }
 107  0
         disposed = false;
 108  0
     }
 109  
 
 110  
     public void dispose()
 111  
     {
 112  0
         disposed = true;
 113  
         //Don't reset the component config state i.e. objectClass since service objects can be recycled
 114  0
     }
 115  
 
 116  
     /**
 117  
      * Creates an initialized object instance based on the class and sets any properties.
 118  
      * This method handles all injection of properties for the resulting object
 119  
      *
 120  
      * @param muleContext the current {@link org.mule.api.MuleContext} instance. This can be used for performing registry lookups
 121  
      *                    applying processors to newly created objects or even firing custom notifications
 122  
      * @throws Exception Can throw any type of exception while creating a new object
 123  
      */
 124  
     public Object getInstance(MuleContext muleContext) throws Exception
 125  
     {
 126  0
         if (objectClass == null || disposed)
 127  
         {
 128  0
             throw new InitialisationException(
 129  
                     MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
 130  
         }
 131  
 
 132  0
         Object object = ClassUtils.instanciateClass(objectClass);
 133  
 
 134  0
         if (properties != null)
 135  
         {
 136  0
             BeanUtils.populateWithoutFail(object, properties, true);
 137  
         }
 138  
 
 139  0
         if (object instanceof FlowConstructAware)
 140  
         {
 141  0
             ((FlowConstructAware) object).setFlowConstruct(flowConstruct);
 142  
         }
 143  
 
 144  0
         if (object instanceof ServiceAware && flowConstruct instanceof Service)
 145  
         {
 146  0
             ((ServiceAware) object).setService((Service) flowConstruct);
 147  
         }
 148  
 
 149  0
         if (isAutoWireObject())
 150  
         {
 151  0
             muleContext.getRegistry().applyProcessors(object);
 152  
         }
 153  0
         fireInitialisationCallbacks(object);
 154  
 
 155  0
         return object;
 156  
     }
 157  
 
 158  
     protected void fireInitialisationCallbacks(Object component) throws InitialisationException
 159  
     {
 160  0
         for (InitialisationCallback callback : initialisationCallbacks)
 161  
         {
 162  0
             callback.initialise(component);
 163  
         }
 164  0
     }
 165  
 
 166  
     public Class<?> getObjectClass()
 167  
     {
 168  0
         return objectClass;
 169  
     }
 170  
 
 171  
     public void setObjectClass(Class<?> objectClass)
 172  
     {
 173  0
         this.objectClass = objectClass;
 174  0
         this.objectClassName = objectClass.getName();
 175  0
     }
 176  
 
 177  
     protected String getObjectClassName()
 178  
     {
 179  0
         return objectClassName;
 180  
     }
 181  
 
 182  
     public void setObjectClassName(String objectClassName)
 183  
     {
 184  0
         this.objectClassName = objectClassName;
 185  0
         setupObjectClassFromObjectClassName();
 186  0
     }
 187  
 
 188  
     protected Map getProperties()
 189  
     {
 190  0
         return properties;
 191  
     }
 192  
 
 193  
     public void setProperties(Map properties)
 194  
     {
 195  0
         this.properties = properties;
 196  0
     }
 197  
 
 198  
     public void addObjectInitialisationCallback(InitialisationCallback callback)
 199  
     {
 200  0
         initialisationCallbacks.add(callback);
 201  0
     }
 202  
 
 203  
     public boolean isSingleton()
 204  
     {
 205  0
         return false;
 206  
     }
 207  
 
 208  
     public boolean isExternallyManagedLifecycle()
 209  
     {
 210  0
         return false;
 211  
     }
 212  
 
 213  
     public boolean isAutoWireObject()
 214  
     {
 215  0
         return true;
 216  
     }
 217  
 }