View Javadoc

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      private Object instance = null;
24  
25      /** For Spring only */
26      public SingletonObjectFactory()
27      {
28          super();
29      }
30  
31      public SingletonObjectFactory(String objectClassName)
32      {
33          super(objectClassName);
34      }
35  
36      public SingletonObjectFactory(String objectClassName, Map properties)
37      {
38          super(objectClassName, properties);
39      }
40  
41      public SingletonObjectFactory(Class objectClass)
42      {
43          super(objectClass);
44      }
45  
46      public SingletonObjectFactory(Class objectClass, Map properties)
47      {
48          super(objectClass, properties);
49      }
50  
51      /**
52       * Create the singleton based on a previously created object.
53       */
54      public SingletonObjectFactory(Object instance)
55      {
56          super(instance.getClass());
57          this.instance = instance;
58      }
59  
60      // @Override
61      public void initialise() throws InitialisationException
62      {
63          super.initialise();
64          if (instance == null)
65          {
66              try
67              {
68                  instance = super.getInstance();
69              }
70              catch (Exception e)
71              {
72                  throw new InitialisationException(e, this);
73              }
74          }
75      }
76  
77      // @Override
78      public void dispose()
79      {
80          instance = null;
81          super.dispose();
82      }
83  
84      /**
85       * Always returns the same instance of the object.
86       */
87      // @Override
88      public Object getInstance() throws Exception
89      {
90          if (instance != null)
91          {
92              return instance;
93          }
94          else
95          {
96              throw new InitialisationException(
97                  MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
98          }
99      }
100 
101     // @Override
102     public Class getObjectClass()
103     {
104         if (instance != null)
105         {
106             return instance.getClass();
107         }
108         else
109         {
110             return super.getObjectClass();
111         }
112     }
113 
114     public boolean isSingleton()
115     {
116         return true;
117     }
118 
119 }