001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: DataSourceFactory.java 7180 2005-08-05 10:14:17Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.dbm;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Context;
029: import javax.naming.Name;
030: import javax.naming.Reference;
031: import javax.naming.spi.ObjectFactory;
032:
033: import org.objectweb.jonas.common.Log;
034: import org.objectweb.util.monolog.api.BasicLevel;
035: import org.objectweb.util.monolog.api.Logger;
036:
037: /**
038: * @author Philippe Durieux
039: * Contributor(s):
040: * <p>
041: * 03/05/23 Adriana Danes - Introduce connection pool size configuration
042: */
043: public class DataSourceFactory implements ObjectFactory {
044:
045: static private Logger logger = null;
046:
047: public Object getObjectInstance(Object refObj, Name name,
048: Context nameCtx, Hashtable env) throws Exception {
049:
050: Reference ref = (Reference) refObj;
051: String clname = ref.getClassName();
052: if (logger == null) {
053: logger = Log.getLogger(Log.JONAS_DBM_PREFIX);
054: }
055: if (logger.isLoggable(BasicLevel.DEBUG)) {
056: logger.log(BasicLevel.DEBUG, "");
057: }
058:
059: // Old fashioned Connection Manager
060: if (clname.equals("org.objectweb.jonas.dbm.ConnectionManager")) {
061: String dsname = (String) ref.get("datasource.name")
062: .getContent();
063: ConnectionManager ds = ConnectionManager
064: .getConnectionManager(dsname);
065: if (ds == null) {
066: // The DataSource was not in this JOnAS Server: Create it.
067: if (logger.isLoggable(BasicLevel.DEBUG)) {
068: logger.log(BasicLevel.DEBUG,
069: "Creating a new Connection Manager for "
070: + dsname);
071: }
072: try {
073: String jBase = System.getProperty("jonas.base");
074: if (jBase == null) {
075: //TODO : must be removed. A client can lookup a datasource without modifications
076: // pure client case
077: ds = new ConnectionManager(true);
078: ds.setDSName(dsname);
079: ds.setUrl((String) ref.get("datasource.url")
080: .getContent());
081: ds.setClassName((String) ref.get(
082: "datasource.classname").getContent());
083: ds.setUserName((String) ref.get(
084: "datasource.username").getContent());
085: ds.setPassword((String) ref.get(
086: "datasource.password").getContent());
087: return ds;
088: }
089: //build a new datasource for another server
090: ds = new ConnectionManager();
091: ds.setDSName(dsname);
092: ds.setUrl((String) ref.get("datasource.url")
093: .getContent());
094: ds.setClassName((String) ref.get(
095: "datasource.classname").getContent());
096: ds.setUserName((String) ref.get(
097: "datasource.username").getContent());
098: ds.setPassword((String) ref.get(
099: "datasource.password").getContent());
100: ds.setTransactionIsolation((String) ref.get(
101: "datasource.isolationlevel").getContent());
102: ds.setMapperName((String) ref.get(
103: "datasource.mapper").getContent());
104: ds
105: .poolConfigure((String) ref.get(
106: "connchecklevel").getContent(),
107: (String) ref.get("connmaxage")
108: .getContent(), (String) ref
109: .get("maxopentime")
110: .getContent(), (String) ref
111: .get("connteststmt")
112: .getContent(), (String) ref
113: .get("pstmtmax")
114: .getContent(), (String) ref
115: .get("minconpool")
116: .getContent(), (String) ref
117: .get("maxconpool")
118: .getContent(), (String) ref
119: .get("maxwaittime")
120: .getContent(), (String) ref
121: .get("maxwaiters")
122: .getContent(), (String) ref
123: .get("samplingperiod")
124: .getContent());
125: } catch (Exception e) {
126: logger.log(BasicLevel.ERROR,
127: "DataSourceFactory error", e);
128: }
129: } else {
130: // The DataSource is already in this JOnAS Server: Just return it.
131: }
132: return ds;
133: }
134:
135: logger.log(BasicLevel.ERROR, "no object found for " + clname);
136: return null;
137: }
138: }
|