KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > jdbcpool > DataSourceFactory


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
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.1 of the License, or 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
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: DataSourceFactory.java 1022 2006-08-04 11:02:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.jdbcpool;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.spi.ObjectFactory JavaDoc;
34
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37
38 /**
39  * Class which is used for binding the ConnectionManager object. getReference()
40  * method of ConnectionManager redirect to this class. The getObjectInstance()
41  * method will be called for the JNDI lookup
42  * @author Florent Benoit
43  */

44 public class DataSourceFactory implements ObjectFactory JavaDoc {
45
46     /**
47      * Logger.
48      */

49     private static JLog logger = JLogFactory.getLog(DataSourceFactory.class);
50
51     /**
52      * Creates an object using the location or reference information specified.
53      * It gets a connection manager of this server.
54      * @param obj The possibly null object containing location or reference
55      * information that can be used in creating an object.
56      * @param name The name of this object relative to <code>nameCtx</code>,
57      * or null if no name is specified.
58      * @param nameCtx The context relative to which the <code>name</code>
59      * parameter is specified, or null if <code>name</code> is relative
60      * to the default initial context.
61      * @param environment The possibly null environment that is used in creating
62      * the object.
63      * @return The object created; null if an object cannot be created.
64      * @throws Exception if this object factory encountered an exception while
65      * attempting to create an object, and no other object factories are
66      * to be tried.
67      */

68     public Object JavaDoc getObjectInstance(final Object JavaDoc obj, final Name JavaDoc name, final Context JavaDoc nameCtx, final Hashtable JavaDoc environment)
69             throws Exception JavaDoc {
70
71         Reference JavaDoc ref = (Reference JavaDoc) obj;
72
73         String JavaDoc dsname = (String JavaDoc) ref.get("datasource.name").getContent();
74         ConnectionManager ds = ConnectionManager.getConnectionManager(dsname);
75         if (ds == null) {
76             // The DataSource was not in the EasyBeans Server: Create it.
77
logger.debug("Creating a new Connection Manager for {0}", dsname);
78             try {
79                 // build a new datasource for another server
80
ds = new ConnectionManager();
81                 ds.setDSName(dsname);
82                 ds.setUrl((String JavaDoc) ref.get("datasource.url").getContent());
83                 ds.setClassName((String JavaDoc) ref.get("datasource.classname").getContent());
84                 ds.setUserName((String JavaDoc) ref.get("datasource.username").getContent());
85                 ds.setPassword((String JavaDoc) ref.get("datasource.password").getContent());
86                 ds.setTransactionIsolation((String JavaDoc) ref.get("datasource.isolationlevel").getContent());
87                 ds.poolConfigure((String JavaDoc) ref.get("connchecklevel").getContent(),
88                         (String JavaDoc) ref.get("connmaxage").getContent(), (String JavaDoc) ref.get("maxopentime").getContent(),
89                         (String JavaDoc) ref.get("connteststmt").getContent(), (String JavaDoc) ref.get("pstmtmax").getContent(),
90                         (String JavaDoc) ref.get("minconpool").getContent(), (String JavaDoc) ref.get("maxconpool").getContent(),
91                         (String JavaDoc) ref.get("maxwaittime").getContent(), (String JavaDoc) ref.get("maxwaiters").getContent(),
92                         (String JavaDoc) ref.get("samplingperiod").getContent());
93             } catch (Exception JavaDoc e) {
94                 logger.error("DataSourceFactory error", e);
95             }
96         }
97         return ds;
98     }
99 }
100
Popular Tags