View Javadoc

1   /*
2    * $Id: AbstractObjectFactory.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.InitialisationCallback;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.object.ObjectFactory;
16  import org.mule.config.i18n.MessageFactory;
17  import org.mule.util.BeanUtils;
18  import org.mule.util.ClassUtils;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
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.
30   */
31  public abstract class AbstractObjectFactory implements ObjectFactory
32  {
33      public static final String ATTRIBUTE_OBJECT_CLASS_NAME = "objectClassName";
34      public static final String ATTRIBUTE_OBJECT_CLASS = "objectClass";
35  
36      protected String objectClassName;
37      protected Class objectClass = null;
38      protected Map properties = null;
39      protected List initialisationCallbacks = new ArrayList();
40  
41      protected transient Log logger = LogFactory.getLog(getClass());
42  
43      /** For Spring only */
44      public AbstractObjectFactory()
45      {
46          // nop
47      }
48  
49      public AbstractObjectFactory(String objectClassName)
50      {
51          this(objectClassName, null);
52      }
53  
54      public AbstractObjectFactory(String objectClassName, Map properties)
55      {
56          this.objectClassName = objectClassName;
57          this.properties = properties;
58      }
59  
60      public AbstractObjectFactory(Class objectClass)
61      {
62          this(objectClass, null);
63      }
64  
65      public AbstractObjectFactory(Class objectClass, Map properties)
66      {
67          this.objectClass = objectClass;
68          this.properties = properties;
69      }
70  
71      public void initialise() throws InitialisationException
72      {
73          if (objectClass == null && objectClassName == null)
74          {
75              throw new InitialisationException(
76                  MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
77          }
78  
79          if (objectClass == null && objectClassName != null)
80          {
81              try
82              {
83                  objectClass = ClassUtils.getClass(objectClassName);
84              }
85              catch (ClassNotFoundException e)
86              {
87                  throw new InitialisationException(e, this);
88              }
89          }
90      }
91  
92      public void dispose()
93      {
94          this.objectClass = null;
95          this.objectClassName = null;
96      }
97  
98      /**
99       * Creates an initialized object instance based on the class and sets any properties.
100      */
101     public Object getInstance() throws Exception
102     {
103         if (objectClass == null)
104         {
105             throw new InitialisationException(
106                 MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
107         }
108 
109         Object object = ClassUtils.instanciateClass(objectClass, ClassUtils.NO_ARGS);
110 
111         if (properties != null)
112         {
113             BeanUtils.populate(object, properties);
114         }
115 
116         fireInitialisationCallbacks(object);
117         
118         return object;
119     }
120     
121     protected void fireInitialisationCallbacks(Object component) throws InitialisationException
122     {
123         InitialisationCallback callback;
124         for (Iterator iterator = initialisationCallbacks.iterator(); iterator.hasNext();)
125         {
126             callback = (InitialisationCallback) iterator.next();
127             callback.initialise(component);
128         }
129     }
130 
131     public Class getObjectClass()
132     {
133         return objectClass;
134     }
135 
136     public void setObjectClass(Class objectClass)
137     {
138         this.objectClass = objectClass;
139     }
140 
141     protected String getObjectClassName()
142     {
143         return objectClassName;
144     }
145 
146     public void setObjectClassName(String objectClassName)
147     {
148         this.objectClassName = objectClassName;
149     }
150 
151     protected Map getProperties()
152     {
153         return properties;
154     }
155 
156     public void setProperties(Map properties)
157     {
158         this.properties = properties;
159     }
160     
161     public void addObjectInitialisationCallback(InitialisationCallback callback)
162     {
163         initialisationCallbacks.add(callback);
164     }
165     
166     public boolean isSingleton()
167     {
168         return false;
169     }
170 
171 }