KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > dbm > DataSourceFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@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,v 1.31 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.dbm;
28
29 import java.util.Hashtable JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.spi.ObjectFactory JavaDoc;
35
36 import org.objectweb.jonas.common.Log;
37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.objectweb.util.monolog.api.Logger;
39
40 /**
41  * @author Philippe Durieux
42  * Contributor(s):
43  * <p>
44  * 03/05/23 Adriana Danes - Introduce connection pool size configuration
45  */

46 public class DataSourceFactory implements ObjectFactory JavaDoc {
47
48     static private Logger logger = null;
49
50     public Object JavaDoc getObjectInstance(Object JavaDoc refObj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc env) throws Exception JavaDoc {
51
52         Reference JavaDoc ref = (Reference JavaDoc) refObj;
53         String JavaDoc clname = ref.getClassName();
54         if (logger == null) {
55             logger = Log.getLogger(Log.JONAS_DBM_PREFIX);
56         }
57         if (logger.isLoggable(BasicLevel.DEBUG)) {
58             logger.log(BasicLevel.DEBUG, "");
59         }
60
61         // Old fashioned Connection Manager
62
if (clname.equals("org.objectweb.jonas.dbm.ConnectionManager")) {
63             String JavaDoc dsname = (String JavaDoc)ref.get("datasource.name").getContent();
64             ConnectionManager ds = ConnectionManager.getConnectionManager(dsname);
65             if (ds == null) {
66                 // The DataSource was not in this JOnAS Server: Create it.
67
if (logger.isLoggable(BasicLevel.DEBUG)) {
68                     logger.log(BasicLevel.DEBUG, "Creating a new Connection Manager for " + dsname);
69                 }
70                 try {
71                     String JavaDoc jBase = System.getProperty("jonas.base");
72                     if (jBase == null) {
73                         //TODO : must be removed. A client can lookup a datasource without modifications
74
// pure client case
75
ds = new ConnectionManager(true);
76                         ds.setDSName(dsname);
77                         ds.setUrl((String JavaDoc)ref.get("datasource.url").getContent());
78                         ds.setClassName((String JavaDoc)ref.get("datasource.classname").getContent());
79                         ds.setUserName((String JavaDoc)ref.get("datasource.username").getContent());
80                         ds.setPassword((String JavaDoc)ref.get("datasource.password").getContent());
81                         return ds;
82                     }
83                     //build a new datasource for another server
84
ds = new ConnectionManager();
85                     ds.setDSName(dsname);
86                     ds.setUrl((String JavaDoc)ref.get("datasource.url").getContent());
87                     ds.setClassName((String JavaDoc)ref.get("datasource.classname").getContent());
88                     ds.setUserName((String JavaDoc)ref.get("datasource.username").getContent());
89                     ds.setPassword((String JavaDoc)ref.get("datasource.password").getContent());
90                     ds.setTransactionIsolation((String JavaDoc)ref.get("datasource.isolationlevel").getContent());
91                     ds.setMapperName((String JavaDoc)ref.get("datasource.mapper").getContent());
92                     ds.poolConfigure((String JavaDoc)ref.get("connchecklevel").getContent(),
93                                      (String JavaDoc)ref.get("connmaxage").getContent(),
94                                      (String JavaDoc)ref.get("maxopentime").getContent(),
95                                      (String JavaDoc)ref.get("connteststmt").getContent(),
96                                      (String JavaDoc)ref.get("minconpool").getContent(),
97                                      (String JavaDoc)ref.get("maxconpool").getContent(),
98                                      (String JavaDoc)ref.get("maxwaittime").getContent(),
99                                      (String JavaDoc)ref.get("maxwaiters").getContent(),
100                                      (String JavaDoc)ref.get("samplingperiod").getContent());
101                 } catch (Exception JavaDoc e) {
102                     logger.log(BasicLevel.ERROR, "DataSourceFactory error", e);
103                 }
104             } else {
105                 // The DataSource is already in this JOnAS Server: Just return it.
106
}
107             return ds;
108         }
109
110         logger.log(BasicLevel.ERROR, "no object found for " + clname);
111         return null;
112     }
113 }
114
Popular Tags