You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/02/16 04:55:22 UTC
cvs commit: jakarta-jetspeed-2/cps/src/java/org/apache/jetspeed/cps/components/jndi JNDIComponent.java TyrexJNDIComponent.java
weaver 2004/02/15 19:55:22
Added: cps/src/java/org/apache/jetspeed/cps/components/hsql
HSQLServerComponent.java
cps/src/test/org/apache/jetspeed/cps/components
TestHSQL.java TestJNDIComponent.java
TestDBCPDatasource.java
cps/src/java/org/apache/jetspeed/cps/components/datasource
DatasourceComponent.java
DBCPDatasourceComponent.java
cps/src/java/org/apache/jetspeed/cps/components/jndi
JNDIComponent.java TyrexJNDIComponent.java
Log:
Converted into components:
- datasource
- JNDI
- HSQL
This makes testing these compoenents very simple and they are now easily
re-usable and are pico-ready.
The next step to is to make their respective service use the components
instead of the current code in those services.
Revision Changes Path
1.1 jakarta-jetspeed-2/cps/src/java/org/apache/jetspeed/cps/components/hsql/HSQLServerComponent.java
Index: HSQLServerComponent.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components.hsql;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hsqldb.Server;
import org.picocontainer.Startable;
/**
* <p>
* HSQLServerComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public class HSQLServerComponent implements Startable
{
public static final String KEY_USE_JNDI_DS = "use.jndi.datasource";
public static final String SERVICE_NAME = "HSQLDBServer";
public static final String NAMING_ROOT = "java:comp/env/";
private static final Log log = LogFactory.getLog(HSQLServerComponent.class);
private String password;
private int port;
private boolean silent;
private boolean trace;
private String user;
private String fqPath;
public HSQLServerComponent(int port, String user, String password, String dbScriptPath, boolean trace, boolean silent)
throws IOException
{
this.port = port;
this.user = user;
this.password = password;
// allow override by a system property
String dbPath = System.getProperty("services.HSQLDBServer.db.path", dbScriptPath);
fqPath = null;
if (System.getProperty("services.HSQLDBServer.db.path") == null)
{
fqPath = new File(dbPath).getCanonicalPath();
}
else
{
fqPath = dbPath;
}
log.info("Using HSQL script file: " + fqPath);
this.trace = trace;
this.silent = silent;
}
/**
* <p>
* start
* </p>
*
* @see org.picocontainer.Startable#start()
*
*/
public void start()
{
HSQLServer serverThread = new HSQLServer(port, fqPath);
serverThread.start();
}
/**
* <p>
* stop
* </p>
*
* @see org.picocontainer.Startable#stop()
*
*/
public void stop()
{
try
{
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:hsql://127.0.0.1:" + port;
Connection con = DriverManager.getConnection(url, user, password);
String sql = "SHUTDOWN";
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
}
catch (Exception e)
{
log.error("Unable to safely shutdown HSQLDB Server: " + e.toString(), e);
}
}
class HSQLServer extends Thread
{
private String[] args;
HSQLServer(int port, String dbPath)
{
args =
new String[] {
"-database",
dbPath,
"-port",
String.valueOf(port),
"-no_system_exit",
"true",
"-silent",
String.valueOf(silent),
"-trace",
String.valueOf(trace)};
setName("Jetspeed HSQLDB Thread");
}
/**
* @see java.lang.Runnable#run()
*/
public void run()
{
log.info("Starting HSQLDB server on localhost: " + args);
Server.main(args);
try
{
join();
}
catch (InterruptedException e)
{
}
}
}
}
1.1 jakarta-jetspeed-2/cps/src/test/org/apache/jetspeed/cps/components/TestHSQL.java
Index: TestHSQL.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.naming.InitialContext;
import org.apache.jetspeed.cps.components.hsql.HSQLServerComponent;
import org.apache.jetspeed.cps.components.jndi.JNDIComponent;
import org.apache.jetspeed.cps.components.jndi.TyrexJNDIComponent;
import org.hsqldb.jdbcDriver;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
import org.picocontainer.defaults.ConstantParameter;
import org.picocontainer.defaults.DefaultPicoContainer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* <p>
* TestJNDIComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public class TestHSQL extends TestCase
{
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestHSQL.class);
}
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public TestHSQL(String name)
{
super(name);
}
public void testHSQL_1() throws Exception
{
MutablePicoContainer container = startTestServer();
// sleep make sure that the server starts
container.start();
// / Thread.sleep(2000);
Class.forName(jdbcDriver.class.getName());
Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://127.0.0.1","sa", "");
assertNotNull(conn);
assertFalse(conn.isClosed());
conn.close();
container.stop();
}
/**
*
* <p>
* startTestServer
* </p>
* Conviniently creates and return a
* container that will run the HSQLServerComponent.
*
* You should manually call stop() to properly shut down the server.
*
* @return
*
*/
public static MutablePicoContainer startTestServer()
{
MutablePicoContainer container = new DefaultPicoContainer();
Parameter[] params = new Parameter[] {
new ConstantParameter(new Integer(9001)),
new ConstantParameter("sa"),
new ConstantParameter(""),
new ConstantParameter("../portal/test/db/hsql/Registry"),
new ConstantParameter(new Boolean(false)),
new ConstantParameter(new Boolean(true)),
};
container.registerComponentImplementation("HSQLServer", HSQLServerComponent.class, params);
return container;
}
}
1.1 jakarta-jetspeed-2/cps/src/test/org/apache/jetspeed/cps/components/TestJNDIComponent.java
Index: TestJNDIComponent.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components;
import javax.naming.InitialContext;
import org.apache.jetspeed.cps.components.jndi.JNDIComponent;
import org.apache.jetspeed.cps.components.jndi.TyrexJNDIComponent;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.defaults.DefaultPicoContainer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* <p>
* TestJNDIComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public class TestJNDIComponent extends TestCase
{
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestJNDIComponent.class);
}
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public TestJNDIComponent(String name)
{
super(name);
}
public void testJNDI_1() throws Exception
{
MutablePicoContainer container = new DefaultPicoContainer();
container.registerComponentImplementation(JNDIComponent.class, TyrexJNDIComponent.class);
container.start();
JNDIComponent jndi = (JNDIComponent) container.getComponentInstance(JNDIComponent.class);
assertNotNull(jndi);
// Bind a simple string into the context
jndi.bindObject("comp/env/foo", "bar");
InitialContext ctx = new InitialContext();
// Look the string we just bound
String bar = (String) ctx.lookup("java:comp/env/foo");
assertNotNull(bar);
assertEquals("bar", bar);
}
}
1.1 jakarta-jetspeed-2/cps/src/test/org/apache/jetspeed/cps/components/TestDBCPDatasource.java
Index: TestDBCPDatasource.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components;
import java.sql.Connection;
import javax.sql.DataSource;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.jetspeed.cps.components.datasource.DBCPDatasourceComponent;
import org.apache.jetspeed.cps.components.datasource.DatasourceComponent;
import org.hsqldb.jdbcDriver;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.defaults.ConstantParameter;
/**
* <p>
* TestJNDIComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public class TestDBCPDatasource extends TestCase
{
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestDBCPDatasource.class);
}
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public TestDBCPDatasource(String name)
{
super(name);
}
public void testDBCP_1() throws Exception
{
// Need to have DB to test on ;)
MutablePicoContainer container = TestHSQL.startTestServer();
Parameter[] params =
new Parameter[] {
new ConstantParameter("sa"),
new ConstantParameter(""),
new ConstantParameter(jdbcDriver.class.getName()),
new ConstantParameter("jdbc:hsqldb:hsql://127.0.0.1"),
new ConstantParameter(new Integer(5)),
new ConstantParameter(new Integer(5000)),
new ConstantParameter(new Byte(GenericObjectPool.WHEN_EXHAUSTED_GROW)),
new ConstantParameter(new Boolean(true))
};
container.registerComponentImplementation(DatasourceComponent.class, DBCPDatasourceComponent.class, params);
container.start();
DatasourceComponent dsc = (DatasourceComponent) container.getComponentInstance(DatasourceComponent.class);
assertNotNull(dsc);
DataSource ds = dsc.getDatasource();
assertNotNull(ds);
Connection conn = ds.getConnection();
assertNotNull(conn);
assertFalse(conn.isClosed());
conn.close();
container.stop();
}
}
1.1 jakarta-jetspeed-2/cps/src/java/org/apache/jetspeed/cps/components/datasource/DatasourceComponent.java
Index: DatasourceComponent.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components.datasource;
import javax.sql.DataSource;
/**
* <p>
* DatasourceComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public interface DatasourceComponent
{
/**
*
* <p>
* getDatasource
* </p>
*
* <p>
* returns the datasource created by this component
* </p>
* @return
*
*/
public abstract DataSource getDatasource();
}
1.1 jakarta-jetspeed-2/cps/src/java/org/apache/jetspeed/cps/components/datasource/DBCPDatasourceComponent.java
Index: DBCPDatasourceComponent.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components.datasource;
import javax.sql.DataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.picocontainer.Startable;
/**
* <p>
* DBCPDatasourceComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public class DBCPDatasourceComponent implements DatasourceComponent, Startable
{
private static final Log log = LogFactory.getLog(DBCPDatasourceComponent.class);
protected PoolingDataSource dataSource;
private String user;
private String password;
private String driverName;
private String connectURI;
private int maxActive;
private int maxWait;
private byte whenExhausted;
private boolean autoCommit;
private PoolableConnectionFactory dsConnectionFactory;
/**
*
* Creates a simple commons DBCP connection pool using the following
* parameters.
* <p>
* If you need to bind the datasource of this component to
* JNDI please @see org.apache.jetspeed.cps.components.jndi.JNDIComponent
* </p>
*
* @param user User name that will be used to connect to the DB
* @param password Password that will be used to connect to the DB
* @param driverName Fully qualified driver to be used by the connection pool
* @param connectURI Fully qualified URI to the DB.
* @param maxActive Maximum active connection
* @param maxWait if <code>whenExhausted</code> is set to GenericObjectPool.WHEN_EXHAUSTED_BLOCK
* the length of time to block while waiting for a connection to become
* available.
* @param whenExhausted GenericObjectPool.WHEN_EXHAUSTED_BLOCK, GenericObjectPool.WHEN_EXHAUSTED_GROW or
* GenericObjectPool.WHEN_EXHAUSTED_FAIL. @see org.apache.commons.pooling.GenericObjectPool
* for more information on these settings
* @param autoCommit Whether or not this datasource will autocommit
* @throws ClassNotFoundException If the <code>driverName</code> could not be
* located within any classloaders.
*/
public DBCPDatasourceComponent(
String user,
String password,
String driverName,
String connectURI,
int maxActive,
int maxWait,
byte whenExhausted,
boolean autoCommit)
{
log.info("Setting up data source pooling for " + driverName);
log.info("Max active connnections set to: " + maxActive);
log.info("Pool is set to \"" + whenExhausted + "\" when all connections are exhausted.");
this.user = user;
this.password = password;
this.driverName = driverName;
this.connectURI = connectURI;
this.maxActive = maxActive;
this.maxWait = maxWait;
this.autoCommit = autoCommit;
}
/**
*
* <p>
* getDatasource
* </p>
*
* <p>
* returns the datasource created by this component
* </p>
* @return
*
*/
public DataSource getDatasource()
{
return dataSource;
}
/**
* <p>
* start
* </p>
*
* @see org.picocontainer.Startable#start()
*
*/
public void start()
{
try
{
Class.forName(driverName);
ObjectPool connectionPool = new GenericObjectPool(null, maxActive, whenExhausted, maxWait);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password);
dsConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
dataSource = new PoolingDataSource(connectionPool);
}
catch (ClassNotFoundException e)
{
throw new IllegalStateException("Could not locate class for driver "+driverName+": "+e.toString());
}
}
/**
* <p>
* stop
* </p>
*
* @see org.picocontainer.Startable#stop()
*
*/
public void stop()
{
try
{
dsConnectionFactory.getPool().close();
}
catch (Exception e)
{
IllegalStateException ise =
new IllegalStateException("Unable to sfaely shutdown the DBCPConnection pool: " + e.toString());
ise.initCause(e);
}
}
}
1.1 jakarta-jetspeed-2/cps/src/java/org/apache/jetspeed/cps/components/jndi/JNDIComponent.java
Index: JNDIComponent.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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 APACHE SOFTWARE FOUNDATION 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 on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jetspeed.cps.components.jndi;
import javax.naming.Context;
import javax.naming.NamingException;
/**
* <p>
* JNDIComponent
* </p>
*
*
* @
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $ $
*
*/
public interface JNDIComponent
{
Context getRootContext();
void bindToCurrentThread() throws NamingException;
void unbindFromCurrentThread() throws NamingException;
void bindObject(String bindToName, Object obj) throws NamingException;
}
1.1 jakarta-jetspeed-2/cps/src/java/org/apache/jetspeed/cps/components/jndi/TyrexJNDIComponent.java
Index: TyrexJNDIComponent.java
===================================================================
/**
* Created on Feb 4, 2004
*
*
* @author
*/
package org.apache.jetspeed.cps.components.jndi;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fulcrum.InitializationException;
import org.apache.jetspeed.cps.components.datasource.DatasourceComponent;
import org.picocontainer.Startable;
import tyrex.naming.MemoryContext;
import tyrex.tm.RuntimeContext;
/**
* <p>
* TyrexJNDIComponent
* </p>
* <p>
*
* </p>
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $Id: TyrexJNDIComponent.java,v 1.1 2004/02/16 03:55:22 weaver Exp $
*
*/
public class TyrexJNDIComponent implements JNDIComponent
{
private static final Log log = LogFactory.getLog(TyrexJNDIComponent.class);
private MemoryContext rootJNDIContext;
/**
* @see org.apache.fulcrum.Service#init()
*/
public TyrexJNDIComponent() throws NamingException
{
Context ctx = null;
// Construct a non-shared memory context
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "tyrex.naming.MemoryContextFactory");
rootJNDIContext = new MemoryContext(null);
ctx = rootJNDIContext.createSubcontext("comp");
ctx = ctx.createSubcontext("env");
ctx = ctx.createSubcontext("jdbc");
// Associate the memory context with a new
// runtime context and associate the runtime context
// with the current thread
bindToCurrentThread();
log.info("JNDI successfully initiallized");
}
/**
* Convenience constructor to immediately bind a datasource
* to a jndi context.
* @param dsComponent
* @param bindToName
* @throws InitializationException
* @throws NamingException
*/
public TyrexJNDIComponent(DatasourceComponent dsComponent, String bindToName) throws InitializationException, NamingException
{
this();
bindObject("comp/env/" + bindToName, dsComponent.getDatasource());
}
/**
* @see org.apache.jetspeed.cps.jndi.JNDIService#getRootContext()
*/
public Context getRootContext()
{
return rootJNDIContext;
}
/**
* @see org.apache.jetspeed.cps.jndi.JNDIService#bindToCurrentThread()
*/
public void bindToCurrentThread() throws NamingException
{
RuntimeContext runCtx = RuntimeContext.newRuntimeContext(rootJNDIContext, null);
RuntimeContext.setRuntimeContext(runCtx);
}
/**
*
* <p>
* bindObject
* </p>
*
* @see org.apache.jetspeed.cps.jndi.JNDIComponent#bindObject(java.lang.String, java.lang.Object)
* @param bindToName
* @param obj
* @throws NamingException
*/
public void bindObject(String bindToName, Object obj) throws NamingException
{
Context ctx = getRootContext();
ctx.bind(bindToName, obj);
}
/**
* <p>
* unbindFromCurrentThread
* </p>
*
* @see org.apache.jetspeed.cps.components.jndi.JNDIComponent#unbindFromCurrentThread()
* @throws NamingException
*/
public void unbindFromCurrentThread() throws NamingException
{
RuntimeContext.unsetRuntimeContext();
RuntimeContext.cleanup(Thread.currentThread());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org