KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > connector > ra > fos > FosManagedConnectionFactory


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

23
24 package org.objectweb.perseus.connector.ra.fos;
25
26 import org.objectweb.perseus.fos.api.FosException;
27 import org.objectweb.perseus.fos.api.FosLoggerFactory;
28 import org.objectweb.perseus.fos.api.FosManager;
29 import org.objectweb.perseus.fos.lib.FosTxContextFactory;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32 import org.objectweb.util.monolog.api.MonologFactory;
33 import org.objectweb.util.monolog.api.Loggable;
34 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
35 import org.objectweb.util.monolog.Monolog;
36
37 import java.io.PrintWriter JavaDoc;
38 import java.util.Set JavaDoc;
39 import javax.resource.ResourceException JavaDoc;
40 import javax.resource.cci.ConnectionFactory JavaDoc;
41 import javax.resource.spi.ConnectionManager JavaDoc;
42 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
43 import javax.resource.spi.ManagedConnection JavaDoc;
44 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
45 import javax.security.auth.Subject JavaDoc;
46
47 /**
48  * @author S. Chassande-Barrioz, P. Dechamboux
49  */

50 public class FosManagedConnectionFactory
51     implements ManagedConnectionFactory JavaDoc, FosLoggerFactory, FosAttributeControler {
52     /**
53      * The logger into which traces about FosManagedConnectionFactory are
54      * produced.
55      */

56     private Logger logger;
57     /**
58      * The logger into which traces about FosManagedConnection are
59      * produced.
60      */

61     private Logger entityLogger;
62     /**
63      * It is assumed that only one ConnectionFactory is actually created by
64      * a ManagedConnectionFactory.
65      */

66     private FosConnectionFactory connectionFactory;
67     /**
68      * The factory for managing FOS XA resources.
69      */

70     private FosXAResourceFactory xaResourceFactory = null;
71     /**
72      * The URL associated with the FOS controled by this resource adapter.
73      */

74     private String JavaDoc connectionURL = null;
75     /**
76      * The status wrt initialization at next start the FOS controled by this resource adapter.
77      */

78     private boolean initializeAtStartUp = false;
79     /**
80      * The factory for managing FOS transaction contexts.
81      */

82     private FosManager txContextFactory = null;
83     /**
84      * The factory for managing FOS transaction contexts.
85      */

86     private ConnectionManager JavaDoc connectionManager = null;
87
88     private MonologFactory monologFactory = null;
89
90     private PrintWriter JavaDoc printWriter = null;
91
92     /**
93      * Starts this FosManagedConnectionFactory.
94      */

95     public void start() throws ResourceException JavaDoc {
96         //Logging initialisation
97
if (monologFactory == null) {
98             if (Monolog.monologFactory == Monolog.getDefaultMonologFactory()) {
99                 monologFactory = Monolog.initialize();
100             } else {
101                 monologFactory = Monolog.monologFactory;
102             }
103         }
104         if (logger == null) {
105             logger = monologFactory.getLogger("org.objectweb.perseus.jdo.ra");
106         }
107
108         try {
109             if (txContextFactory == null) {
110                 txContextFactory = new FosTxContextFactory();
111                 txContextFactory.setDbDir(connectionURL);
112                 txContextFactory.setMonologFactory(monologFactory);
113                 if (initializeAtStartUp) {
114                     initializeAtStartUp = false;
115                     txContextFactory.initialize();
116                 }
117             }
118             txContextFactory.start();
119             logger = getLogger(FosLoggerFactory.MANAGEDCONNECTION, true);
120             entityLogger = getLogger(FosLoggerFactory.MANAGEDCONNECTION, false);
121             if (FosLoggerFactory.DEBUG)
122                 logger.log(BasicLevel.DEBUG,
123                     "Starts a FosManagedConnectionFactory - ConnectionManager: "
124                     + connectionManager);
125             connectionFactory = new FosConnectionFactory(this, connectionManager);
126         } catch (FosException fe) {
127             ResourceException JavaDoc re
128                 = new ResourceException JavaDoc("Cannot start the ManagedConnectionFactory.");
129             re.setLinkedException(fe);
130             throw re;
131         }
132     }
133
134     /**
135      * Stops this FosManagedConnectionFactory.
136      */

137     public void stop() throws ResourceException JavaDoc {
138         try {
139             txContextFactory.stop();
140         } catch (FosException fe) {
141             ResourceException JavaDoc re
142                 = new ResourceException JavaDoc("Cannot stop the ManagedConnectionFactory.");
143             re.setLinkedException(fe);
144             throw re;
145         }
146     }
147
148     /**
149      * Delegates the creation of a Connection to the ConnectionFactory.
150      */

151     public Object JavaDoc createConnection() throws ResourceException JavaDoc {
152         return connectionFactory.createConnection();
153     }
154
155     /**
156      * Retrieves the FosTxContextFactory.
157      */

158     FosManager getTxContextFactory() {
159         if (FosLoggerFactory.DEBUG)
160             logger.log(BasicLevel.DEBUG, "TxContextFactory is: " + txContextFactory);
161         return txContextFactory;
162     }
163
164     /**
165      * Retrieves the ConnectionFactory.
166      */

167     ConnectionFactory JavaDoc getConnectionFactory() {
168         if (FosLoggerFactory.DEBUG)
169             logger.log(BasicLevel.DEBUG, "ConnectionFactory is: " + connectionFactory);
170         return connectionFactory;
171     }
172
173     /**
174      * Retrieves the XAResourceFactory. If null, creates it. This means that no
175      * object related to XAResource management is created if not executing in
176      * XA-aware runtime environment.
177      */

178     FosXAResourceFactory getXAResourceFactory() {
179         if (xaResourceFactory == null) {
180             if (FosLoggerFactory.DEBUG)
181                 logger.log(BasicLevel.DEBUG, "First time retrieving the XAResourceFactory: creates it!");
182             xaResourceFactory = new FosXAResourceFactory(this, txContextFactory);
183         }
184         if (FosLoggerFactory.DEBUG)
185             logger.log(BasicLevel.DEBUG, "XAResourceFactory is: " + xaResourceFactory);
186         return xaResourceFactory;
187     }
188
189     // IMPLEMENTATION OF METHODS FROM THE (cci)ManagedConnectionFactory INTERFACE
190

191     /**
192      * Creates a FosConnectionFactory; yields the existing one if any.
193      * @param cm The ConnectionManager to be used by the created
194      * ConnectionFactory (may be null).
195      */

196     public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cm)
197         throws ResourceException JavaDoc {
198         if (connectionFactory == null) {
199             connectionFactory = new FosConnectionFactory(this, cm);
200             if (FosLoggerFactory.DEBUG)
201                 logger.log(BasicLevel.DEBUG,
202                     "Creation of the ConnectionFactory.");
203         } else {
204             if (FosLoggerFactory.DEBUG)
205                 logger.log(BasicLevel.DEBUG,
206                     "The ConnectionFactory has already been created.");
207         }
208         return connectionFactory;
209     }
210
211     /**
212      * Creates a FosConnectionFactory; yields the existing one if any.
213      */

214     public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc {
215         return createConnectionFactory(null);
216     }
217
218     /**
219      * Creates a new FosManagedConnection.
220      */

221     public ManagedConnection JavaDoc createManagedConnection(
222         Subject JavaDoc subject,
223         ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
224         return new FosManagedConnection(entityLogger, this);
225     }
226
227     /**
228      * No matching rules supported. Always yields the first element of the set
229      * if any.
230      */

231     public ManagedConnection JavaDoc matchManagedConnections(
232         Set JavaDoc set,
233         Subject JavaDoc subject,
234         ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
235         if (set.size() == 0)
236             return null;
237         return (ManagedConnection JavaDoc) set.iterator().next();
238     }
239
240     /**
241      * Not supported yet.
242      */

243     public void setLogWriter(PrintWriter JavaDoc writer) throws ResourceException JavaDoc {
244         if (logger == null) {
245             if (writer instanceof Loggable) {
246                 logger = ((Loggable) writer).getLogger();
247                 monologFactory = (MonologFactory)
248                         ((Loggable) writer).getLoggerFactory();
249             } else {
250                 LoggerImpl li = new LoggerImpl(writer);
251                 logger = li;
252                 monologFactory = li;
253             }
254         }
255         printWriter = writer;
256     }
257
258     /**
259      * Not supported yet.
260      */

261     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
262         return printWriter;
263     }
264
265     // IMPLEMENTATION OF METHODS FROM THE FosLoggerFactory INTERFACE
266

267     /**
268      * Gives access to the logger associated to a given kind of entities managed
269      * by this FOS adapter, which are further classified into factories or
270      * non-factories.
271      * @param entity The kind of entity (see CONNECTION, MANAGEDCONNECTION,
272      * XARESOURCE and XARESOURCE constants defined above).
273      * @param factory Specifies if the concerned entity is a factory.
274      * @return The relevant logger.
275      */

276     public Logger getLogger(byte entity, boolean factory) {
277         return txContextFactory.getLogger(entity, factory);
278     }
279
280     public void setMonologFactory(MonologFactory mf) {
281         monologFactory = mf;
282         txContextFactory.setMonologFactory(mf);
283     }
284     // IMPLEMENTATION OF METHODS FROM THE FosAttributeControler INTERFACE
285

286     /**
287      * Gives access to the URL associated with a FOS adapter.
288      */

289     public String JavaDoc getConnectionURL() {
290         return connectionURL;
291     }
292
293     /**
294      * Changes the URL associated with a FOS adapter.
295      */

296     public void setConnectionURL(String JavaDoc url) {
297         connectionURL = url;
298     }
299
300     /**
301      * Gives access to the status wrt initialization at next start a FOS adapter.
302      */

303     public boolean getInitializeAtStartUp() {
304         return initializeAtStartUp;
305     }
306
307     /**
308      * Changes the status wrt initialization at next start a FOS adapter.
309      */

310     public void setInitializeAtStartUp(boolean init) {
311         initializeAtStartUp = init;
312     }
313
314     public ConnectionManager JavaDoc getConnectionManager() {
315         return connectionManager;
316     }
317
318     /**
319      * Changes the ConnectionManager associated with a FOS adapter.
320      */

321     public void setConnectionManager(ConnectionManager JavaDoc cm) {
322         connectionManager = cm;
323     }
324 }
Popular Tags