001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.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 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.jdbcpool;
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.ow2.util.log.Log;
034: import org.ow2.util.log.LogFactory;
035:
036: /**
037: * Class which is used for binding the ConnectionManager object. getReference()
038: * method of ConnectionManager redirect to this class. The getObjectInstance()
039: * method will be called for the JNDI lookup
040: * @author Florent Benoit
041: */
042: public class DataSourceFactory implements ObjectFactory {
043:
044: /**
045: * Logger.
046: */
047: private static Log logger = LogFactory
048: .getLog(DataSourceFactory.class);
049:
050: /**
051: * Creates an object using the location or reference information specified.
052: * It gets a connection manager of this server.
053: * @param obj The possibly null object containing location or reference
054: * information that can be used in creating an object.
055: * @param name The name of this object relative to <code>nameCtx</code>,
056: * or null if no name is specified.
057: * @param nameCtx The context relative to which the <code>name</code>
058: * parameter is specified, or null if <code>name</code> is relative
059: * to the default initial context.
060: * @param environment The possibly null environment that is used in creating
061: * the object.
062: * @return The object created; null if an object cannot be created.
063: * @throws Exception if this object factory encountered an exception while
064: * attempting to create an object, and no other object factories are
065: * to be tried.
066: */
067: public Object getObjectInstance(final Object obj, final Name name,
068: final Context nameCtx, final Hashtable environment)
069: throws Exception {
070:
071: Reference ref = (Reference) obj;
072:
073: String dsname = (String) ref.get("datasource.name")
074: .getContent();
075: ConnectionManager ds = ConnectionManager
076: .getConnectionManager(dsname);
077: if (ds == null) {
078: // The DataSource was not in the EasyBeans Server: Create it.
079: logger.debug("Creating a new Connection Manager for {0}",
080: dsname);
081: try {
082: // build a new datasource for another server
083: ds = new ConnectionManager();
084: ds.setDSName(dsname);
085: ds.setUrl((String) ref.get("datasource.url")
086: .getContent());
087: ds.setClassName((String) ref
088: .get("datasource.classname").getContent());
089: ds.setUserName((String) ref.get("datasource.username")
090: .getContent());
091: ds.setPassword((String) ref.get("datasource.password")
092: .getContent());
093: ds.setTransactionIsolation((String) ref.get(
094: "datasource.isolationlevel").getContent());
095: ds.poolConfigure((String) ref.get("connchecklevel")
096: .getContent(), (String) ref.get("connmaxage")
097: .getContent(), (String) ref.get("maxopentime")
098: .getContent(), (String) ref.get("connteststmt")
099: .getContent(), (String) ref.get("pstmtmax")
100: .getContent(), (String) ref.get("minconpool")
101: .getContent(), (String) ref.get("maxconpool")
102: .getContent(), (String) ref.get("maxwaittime")
103: .getContent(), (String) ref.get("maxwaiters")
104: .getContent(), (String) ref.get(
105: "samplingperiod").getContent());
106: } catch (Exception e) {
107: logger.error("DataSourceFactory error", e);
108: }
109: }
110: return ds;
111: }
112: }
|