KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > logic > ControllerManager


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ControllerManager.java,v 1.11 2007/01/07 06:15:14 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.logic;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import org.opensubsystems.core.error.OSSException;
30 import org.opensubsystems.core.error.OSSInternalErrorException;
31 import org.opensubsystems.core.util.ClassFactory;
32 import org.opensubsystems.core.util.GlobalConstants;
33 import org.opensubsystems.core.util.J2EEUtils;
34 import org.opensubsystems.core.util.Log;
35
36 /**
37  * Class responsible for instantiation of controllers. This class determines
38  * what controller should be used based on currently used component model,
39  * creates the controller instance if it wasn't created yet and caches created
40  * instances. This of course assumes that the controllers are implemented to
41  * be stateless and reentrant.
42  *
43  * @version $Id: ControllerManager.java,v 1.11 2007/01/07 06:15:14 bastafidli Exp $
44  * @author Miro Halas
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.9 2006/04/05 04:58:16 bastafidli
47  */

48 public class ControllerManager
49 {
50    // Constants ////////////////////////////////////////////////////////////////
51

52    /**
53     * Lock used in synchronized sections.
54     */

55    private static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
56
57    // Attributes ///////////////////////////////////////////////////////////////
58

59    /**
60     * Class factory used to instantiate controller.
61     */

62    protected ClassFactory m_controllerClassFactory;
63    
64    /**
65     * Cache where already instantiated controllers will be cached. We can
66     * cache them since data factorys should be reentrant.
67     */

68    private Map JavaDoc m_mpControllerCache;
69    
70    // Cached values ////////////////////////////////////////////////////////////
71

72    /**
73     * Logger for this class
74     */

75    private static Logger JavaDoc s_logger = Log.getInstance(ControllerManager.class);
76    
77    /**
78     * Reference to the instance actually in use.
79     */

80    private static ControllerManager s_defaultInstance;
81
82    // Constructors /////////////////////////////////////////////////////////////
83

84    /**
85     * Default constructor.
86     */

87    public ControllerManager(
88    )
89    {
90       m_controllerClassFactory = new ControllerClassFactory();
91       m_mpControllerCache = new HashMap JavaDoc();
92    }
93    
94    // Public methods ///////////////////////////////////////////////////////////
95

96    /**
97     * Create controller for specified class.
98     *
99     * @param controller - the controller interface for which we want
100     * applicable controller. This is usually component
101     * model independent class and we will try to create
102     * dependent class.
103     * @return DataFactory - the controller to use for given interface
104     * @throws OSSException - an error has occured
105     */

106    public static Object JavaDoc getInstance(
107       Class JavaDoc controller
108    ) throws OSSException
109    {
110       return getManagerInstance().getControllerInstance(controller);
111    }
112    
113    /**
114     * Get the default instance. This method is here to make the controller manager
115     * configurable. Once can specify in configuration file derived class to used
116     * instead of this one [ControllerManager.class]=new class to use.
117     *
118     * @return ControllerManager
119     * @throws OSSException - cannot get current database
120     */

121    public static ControllerManager getManagerInstance(
122    ) throws OSSException
123    {
124       if (s_defaultInstance == null)
125       {
126          // Only if the default instance wasn't set by other means create a new one
127
// Synchronize just for the creation
128
synchronized (IMPL_LOCK)
129          {
130             Class JavaDoc defaultControllerManager = ControllerManager.class;
131             
132             // Find out if we are running under a j2ee server. If yes, redefine
133
// default variable defaultControllerManager for using
134
// j2ee controller manager.
135
if (J2EEUtils.getJ2EEServerType() != J2EEUtils.J2EE_SERVER_NO)
136             {
137                defaultControllerManager = J2EEControllerManager.class;
138             }
139
140             setManagerInstance((ControllerManager)
141                ClassFactory.getInstance().createInstance(
142                   defaultControllerManager, defaultControllerManager));
143          }
144       }
145       
146       return s_defaultInstance;
147    }
148    
149    /**
150     * Set default instance. This instance will be returned by getInstance
151     * method until it is changed.
152     *
153     * @param defaultInstance - new default instance
154     * @see #getInstance
155     */

156    public static void setManagerInstance(
157       ControllerManager defaultInstance
158    )
159    {
160       if (GlobalConstants.ERROR_CHECKING)
161       {
162          assert defaultInstance != null : "Default instance cannot be null";
163       }
164       
165       synchronized (IMPL_LOCK)
166       {
167          s_defaultInstance = defaultInstance;
168          s_logger.fine("Default controller manager is "
169                        + s_defaultInstance.getClass().getName());
170       }
171    }
172
173    /**
174     * Method to create actual controllers based on specified class. This method can
175     * be overriden and new manager can be setup either through setManagerInstance
176     * or through configuration file if different strategy is desired.
177     *
178     * @param controller - the controller interface for which we want
179     * applicable controller. This is usually component
180     * model independent class and we will try to create
181     * dependent class.
182     * @return DataFactory - the data factory to use for given interface
183     * @throws OSSException - an error has occured
184     */

185    public Object JavaDoc getControllerInstance(
186       Class JavaDoc controller
187    ) throws OSSException
188    {
189       Object JavaDoc control;
190       
191       control = m_mpControllerCache.get(controller.getName());
192       if (control == null)
193       {
194          synchronized (m_mpControllerCache)
195          {
196             control = m_controllerClassFactory.createInstance(controller);
197             // First cache the created instance and only then call the
198
// constructor() in case there is some circular reference
199
// which would resolve to the controller of the same type
200
m_mpControllerCache.put(controller.getName(), control);
201             // Initialize the object
202
try
203             {
204                ((StatelessController)control).constructor();
205             }
206             catch (RemoteException JavaDoc rExc)
207             {
208                // We cannot propagate this exception otherwise XDoclet would generate
209
// the local interface incorrectly since it would include the declared
210
// RemoteException in it (to propagate we would have to declare it)
211
throw new OSSInternalErrorException("Remote error occured", rExc);
212             }
213          }
214       }
215       
216       return control;
217    }
218 }
219
Popular Tags