org.objectstyle.cayenne.conn.ContainerPoolFactory Maven / Gradle / Ivy
/* ====================================================================
*
* The ObjectStyle Group Software License, version 1.1
* ObjectStyle Group - http://objectstyle.org/
*
* Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
* of the software. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if any,
* must include the following acknowlegement:
* "This product includes software developed by independent contributors
* and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
* or promote products derived from this software without prior written
* permission. For written permission, email
* "andrus at objectstyle dot org".
*
* 5. Products derived from this software may not be called "ObjectStyle"
* or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
* names without prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals and hosted on ObjectStyle Group web site. For more
* information on the ObjectStyle Group, please see
* .
*/
package org.objectstyle.cayenne.conn;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import org.apache.log4j.Logger;
/**
* Basic JNDI object factory that creates an instance of
* PoolManager
that has been configured based on the
* RefAddr
values of the specified Reference
.
*
* Here is a sample Tomcat 4.x configuration that sets this class
* as a default factory for javax.sql.DataSource objects:
<ResourceParams name="jdbc/mydb">
<parameter>
<name>factory</name>
<value>org.objectstyle.cayenne.conn.ContainerPoolFactory</value>
</parameter>
<parameter>
<name>username</name>
<value>andrei</value>
</parameter>
<parameter>
<name>password</name>
<value>bla-bla</value>
</parameter>
<parameter>
<name>driver</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://noise/cayenne</value>
</parameter>
<parameter>
<name>min</name>
<value>1</value>
</parameter>
<parameter>
<name>max</name>
<value>3</value>
</parameter>
</ResourceParams>
*
* After ContainerPoolFactory was configured to be used within the container
* (see above for Tomcat example), you can reference your "jdbc/mydb" DataSource in
* web application deployment descriptor like that (per Servlet Specification):
*
<resource-ref>
<es-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
*
* @author Andrei Adamchik
*/
public class ContainerPoolFactory implements ObjectFactory {
private static Logger logObj = Logger.getLogger(ContainerPoolFactory.class);
/**
* Creates and returns a new PoolManager
instance. If no
* instance can be created, returns null
instead.
*
* @param obj The possibly null object containing location or
* reference information that can be used in creating an object
* @param name The name of this object relative to nameCtx
* @param nameCtx The context relative to which the name
* parameter is specified, or null
if name
* is relative to the default initial context
* @param environment The possibly null environment that is used in
* creating this object
*
* @exception Exception if an exception occurs creating the instance
*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable environment)
throws Exception {
// We only know how to deal with javax.naming.Reference
s
// that specify a class name of "javax.sql.DataSource"
if ((obj == null) || !(obj instanceof Reference)) {
logObj.info("unsupported or null reference: " + obj);
return null;
}
Reference ref = (Reference) obj;
if (!"javax.sql.DataSource".equals(ref.getClassName())) {
logObj.info("unsupported type: " + ref.getClassName());
return null;
}
// Create and configure a PoolManager instance based on the
// RefAddr values associated with this Reference
RefAddr ra = null;
String driver = null;
String url = null;
int min = 1;
int max = 1;
String username = null;
String password = null;
ra = ref.get("min");
if (ra != null) {
min = Integer.parseInt(ra.getContent().toString());
}
ra = ref.get("max");
if (ra != null) {
max = Integer.parseInt(ra.getContent().toString());
}
ra = ref.get("driver");
if (ra != null) {
driver = ra.getContent().toString();
}
ra = ref.get("password");
if (ra != null) {
password = ra.getContent().toString();
}
ra = ref.get("url");
if (ra != null) {
url = ra.getContent().toString();
}
ra = ref.get("username");
if (ra != null) {
username = ra.getContent().toString();
}
logObj.info("Loading datasource driver: " + driver);
logObj.info("Connecting to URL: " + url);
return new PoolManager(driver, url, min, max, username, password);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy