KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > pm > lib > SpeedoProxyManagerInstanciator


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.pm.lib;
19
20 import org.objectweb.fractal.adl.Factory;
21 import org.objectweb.fractal.adl.FactoryFactory;
22 import org.objectweb.fractal.api.Component;
23 import org.objectweb.fractal.api.Interface;
24 import org.objectweb.fractal.api.control.BindingController;
25 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
26 import org.objectweb.fractal.api.control.LifeCycleController;
27 import org.objectweb.fractal.util.Fractal;
28 import org.objectweb.perseus.pool.api.PoolMatchFactory;
29 import org.objectweb.perseus.pool.api.PoolException;
30 import org.objectweb.perseus.persistence.api.ConnectionHolderFactory;
31 import org.objectweb.speedo.pm.api.ProxyManager;
32 import org.objectweb.speedo.workingset.api.Transaction;
33 import org.objectweb.speedo.workingset.lib.SpeedoTransaction;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Arrays JavaDoc;
40
41 /**
42  * is a primitive components in charge of the ProxyManager and the Transaction
43  * allocation. It exports the PoolMatchFactory interface in order to be used by
44  * a Pool as Factory of pool resource. The pool resource is the ProxyManager
45  * instance.
46  * It uses a ConnectionHolderFactory for allocating ConnectionHolder to the
47  * created Transaction components.
48  *
49  * @see org.objectweb.speedo.pm.api.ProxyManager
50  * @see org.objectweb.speedo.pm.lib.SpeedoProxyManager
51  * @see org.objectweb.speedo.workingset.api.Transaction
52  * @see org.objectweb.speedo.workingset.lib.SpeedoTransaction
53  * @see org.objectweb.perseus.persistence.api.ConnectionHolder
54  * @see org.objectweb.perseus.persistence.api.ConnectionHolderFactory
55  *
56  * @author S.Chassande-Barrioz
57  */

58 public class SpeedoProxyManagerInstanciator
59     implements PoolMatchFactory, BindingController, LifeCycleController {
60
61
62     public final static String JavaDoc CONNECTION_HOLDER_FACTORY_BINDING = "connection-holder-factory";
63     public final static String JavaDoc COMPONENT_BINDING = "component";
64     public final static String JavaDoc PM_TEMPLATE = "org.objectweb.speedo.pm.lib.SpeedoProxyManager";
65     public final static String JavaDoc T_TEMPLATE = "org.objectweb.speedo.workingset.lib.SpeedoTransaction";
66
67     /**
68      * The factory if ConnectionHolder used at Transaction instanciation time.
69      *
70      * @see org.objectweb.perseus.persistence.api.ConnectionHolder
71      * @see org.objectweb.perseus.persistence.api.ConnectionHolderFactory
72      */

73     private ConnectionHolderFactory chf;
74
75     /**
76      * The current component interface.
77      */

78     private Component thisC = null;
79
80     /**
81      * The template of the ProxyManager component to instanciate
82      */

83     private Component pmT;
84
85     /**
86      * The template of the Transaction component to instanciate
87      */

88     private Component tT;
89
90     /**
91      * life cycle status of the current component
92      */

93     private boolean started = false;
94
95
96     private Logger logger;
97
98     /**
99      * The array of common components required by a ProxyManager and Transaction
100      * components. At each index matches a particular component ( see the
101      * XXX_COMPONENT_IDX fields).
102      *
103      * @see #PMF_COMPONENT_IDX
104      * @see #MAPPER_COMPONENT_IDX
105      * @see #JF_COMPONENT_IDX
106      * @see #PNC_COMPONENT_IDX
107      * @see #QM_COMPONENT_IDX
108      * @see #TPM_COMPONENT_IDX
109      */

110     Object JavaDoc[] components = new Object JavaDoc[6];
111
112     private final static byte PMF_COMPONENT_IDX = 0;
113     private final static byte MAPPER_COMPONENT_IDX = PMF_COMPONENT_IDX + 1;
114     private final static byte JF_COMPONENT_IDX = MAPPER_COMPONENT_IDX + 1;
115     private final static byte PNC_COMPONENT_IDX = JF_COMPONENT_IDX + 1;
116     private final static byte QM_COMPONENT_IDX = PNC_COMPONENT_IDX + 1;
117     private final static byte TPM_COMPONENT_IDX = QM_COMPONENT_IDX + 1;
118
119     /**
120      * The reference the composite component containing the ProxyManager and
121      * Transaction components to instanciate.
122      */

123     Component speedoComponent = null;
124
125     void clearComponentRef() {
126         Arrays.fill(components, null);
127     }
128
129     /**
130      * Fills the components array with components required by a ProxyManager and
131      * Transaction components.
132      */

133     void fetchComponentRef() throws Exception JavaDoc {
134         speedoComponent = Fractal.getSuperController(thisC).getFcSuperComponents()[0];
135         Component[] children = Fractal.getContentController(speedoComponent)
136             .getFcSubComponents();
137         for (int i = 0; i < children.length; i++) {
138             String JavaDoc name = Fractal.getNameController(children[i]).getFcName();
139             if ("proxy-manager-factory".equals(name)) {
140                 components[PMF_COMPONENT_IDX] = children[i].getFcInterface("proxy-manager-factory");
141             } else if ("mapper".equals(name)) {
142                 components[MAPPER_COMPONENT_IDX] = children[i].getFcInterface("mapper");
143                 components[JF_COMPONENT_IDX] = children[i].getFcInterface("jorm-factory");
144                 components[PNC_COMPONENT_IDX] = children[i].getFcInterface("pname-coder");
145             } else if ("query-manager".equals(name)) {
146                 components[QM_COMPONENT_IDX] = children[i].getFcInterface("query-manager");
147             } else if ("tpm".equals(name)) {
148                 components[TPM_COMPONENT_IDX] = children[i].getFcInterface("transactional-persistence-manager");
149             }
150         }
151         Factory factory = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
152         Map JavaDoc ctxt = new HashMap JavaDoc();
153         ctxt.put("template", "true");
154         pmT = (Component) factory.newComponent(PM_TEMPLATE, ctxt);
155         tT = (Component) factory.newComponent(T_TEMPLATE, ctxt);
156     }
157
158     // IMPLEMENTATION OF THE UserBindingController INTERFACE //
159
//-------------------------------------------------------//
160

161     public String JavaDoc getFcState() {
162         return started ? STARTED : STOPPED;
163     }
164
165     /**
166      * Startes the component by computing the components array.
167      */

168     public void startFc() throws IllegalLifeCycleException {
169         started = true;
170         try {
171             fetchComponentRef();
172         } catch (Exception JavaDoc e) {
173             if (logger != null) {
174                 logger.log(BasicLevel.ERROR, "Impossible to fetch inner speedo components", e);
175             }
176             throw new IllegalLifeCycleException(e.getMessage());
177         }
178     }
179
180     /**
181      * Clears the components array.
182      */

183     public void stopFc() throws IllegalLifeCycleException {
184         started = false;
185         clearComponentRef();
186     }
187
188
189     // IMPLEMENTATION OF THE UserBindingController INTERFACE //
190
//-------------------------------------------------------//
191

192     public String JavaDoc[] listFc() {
193         return new String JavaDoc[]{CONNECTION_HOLDER_FACTORY_BINDING};
194     }
195
196     public Object JavaDoc lookupFc(String JavaDoc c) {
197         if (CONNECTION_HOLDER_FACTORY_BINDING.equals(c)) {
198             return chf;
199         } else {
200             return null;
201         }
202     }
203
204     public void bindFc(String JavaDoc c, Object JavaDoc s) {
205         if ("logger".equals(c)) {
206             logger = (Logger) s;
207         } else if (CONNECTION_HOLDER_FACTORY_BINDING.equals(c)) {
208             chf = (ConnectionHolderFactory) s;
209         } else if (COMPONENT_BINDING.equals(c)) {
210             thisC = (Component) s;
211         }
212     }
213
214     public void unbindFc(String JavaDoc c) {
215         if (CONNECTION_HOLDER_FACTORY_BINDING.equals(c)) {
216             chf = null;
217         }
218     }
219
220
221     // IMPLEMENTATION OF THE PoolMathcFactory INTERFACE //
222
//--------------------------------------------------//
223

224     /**
225      * Creates a new <code>PoolResource</code>.
226      * This methos is invoked by the owned <code>Pool</code>.
227      * A new SpeedoProxyManager and a new SpeedoTransaction component are
228      * created, and added into the Speedo composite.
229      *
230      * @param o params used to build a new PoolResource
231      * @return the build <code>SpeedoProxyManager</code>
232      */

233     public Object JavaDoc createResource(Object JavaDoc o) throws PoolException {
234         try {
235             //instanciate the ProxyManager
236
Component pmC = Fractal.getFactory(pmT).newFcInstance();
237             ProxyManager pm = (ProxyManager) pmC.getFcInterface("proxy-manager");
238             Fractal.getNameController(pmC).setFcName("proxy-manager");
239             BindingController pmBC = Fractal.getBindingController(pmC);
240
241             //instanciate the Transaction
242
Component tC = Fractal.getFactory(tT).newFcInstance();
243             Fractal.getNameController(tC).setFcName("transaction");
244             Transaction t = (Transaction) tC.getFcInterface("transaction");
245             BindingController tBC = Fractal.getBindingController(tC);
246
247             //Add the new components into the Speedo composite
248
Fractal.getContentController(speedoComponent).addFcSubComponent(pmC);
249             Fractal.getContentController(speedoComponent).addFcSubComponent(tC);
250
251             //add bindings on the proxy manager
252
pmBC.bindFc(SpeedoProxyManager.JORM_FACTORY_BINDING,
253                 components[JF_COMPONENT_IDX]);
254             pmBC.bindFc(SpeedoProxyManager.PNAME_CODER_BINDING,
255                 components[PNC_COMPONENT_IDX]);
256             pmBC.bindFc(SpeedoProxyManager.PROXY_MANAGER_FACTORY_BINDING,
257                 components[PMF_COMPONENT_IDX]);
258             pmBC.bindFc(SpeedoProxyManager.QUERY_MANAGER_BINDING,
259                 components[QM_COMPONENT_IDX]);
260             pmBC.bindFc(SpeedoProxyManager.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING,
261                 components[TPM_COMPONENT_IDX]);
262             pmBC.bindFc(SpeedoProxyManager.TRANSACTION_BINDING, t);
263
264             //add bindings on the transaction
265
tBC.bindFc(SpeedoTransaction.PROXY_MANAGER_BINDING, pm);
266             tBC.bindFc(SpeedoTransaction.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING,
267                 components[TPM_COMPONENT_IDX]);
268             tBC.bindFc(SpeedoTransaction.MAPPER_BINDING,
269                 components[MAPPER_COMPONENT_IDX]);
270
271             //Add a ConenctionHolder to the transaction
272
t.setConnectionHolder(chf.createConnectionHolder());
273
274             //start components
275
Fractal.getLifeCycleController(pmC).startFc();
276             Fractal.getLifeCycleController(tC).startFc();
277
278             return pm;
279         } catch (Exception JavaDoc e) {
280             PoolException pe = new PoolException(
281                 "Error during the allocation of a new ProxyManager and its WorkingSet", e);
282             logger.log(BasicLevel.ERROR, pe.getMessage(), pe);
283             throw pe;
284         }
285     }
286
287     public boolean matchResource(Object JavaDoc resource, Object JavaDoc o) {
288         return true;
289     }
290
291     /**
292      * It removes the ProxyManager and the Transaction components from the
293      * Speedo composite.
294      * @param resource is the ProxyManager to destroy.
295      */

296     public void destroyResource(Object JavaDoc resource) {
297         try {
298             //fetch the PM component
299
Component pmC = ((Interface) resource).getFcItfOwner();
300             BindingController pmBC = Fractal.getBindingController(pmC);
301
302             //fetch the Tx component
303
Component tC = ((Interface)
304                 pmBC.lookupFc(SpeedoProxyManager.TRANSACTION_BINDING))
305                 .getFcItfOwner();
306             BindingController tBC = Fractal.getBindingController(tC);
307
308             //Stop compoenents
309
Fractal.getLifeCycleController(pmC).stopFc();
310             Fractal.getLifeCycleController(tC).stopFc();
311
312             //remove bindings of the PM component
313
pmBC.unbindFc(SpeedoProxyManager.JORM_FACTORY_BINDING);
314             pmBC.unbindFc(SpeedoProxyManager.PNAME_CODER_BINDING);
315             pmBC.unbindFc(SpeedoProxyManager.PROXY_MANAGER_FACTORY_BINDING);
316             pmBC.unbindFc(SpeedoProxyManager.QUERY_MANAGER_BINDING);
317             pmBC.unbindFc(SpeedoProxyManager.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING);
318             pmBC.unbindFc(SpeedoProxyManager.TRANSACTION_BINDING);
319
320             //remove bindings of the Tx component
321
tBC.unbindFc(SpeedoTransaction.PROXY_MANAGER_BINDING);
322             tBC.unbindFc(SpeedoTransaction.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING);
323             tBC.unbindFc(SpeedoTransaction.MAPPER_BINDING);
324
325             //remove both components from the composite
326
//TODO: find a way to remove sub components without stopping Speedo !!!
327
//Fractal.getContentController(speedoComponent).removeFcSubComponent(pmC);
328
//Fractal.getContentController(speedoComponent).removeFcSubComponent(tC);
329
} catch (Exception JavaDoc e) {
330             if (logger != null) {
331                 logger.log(BasicLevel.WARN, "Error during the destroying of a ProxyManager", e);
332             }
333         }
334     }
335
336 }
337
Popular Tags