001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.components.datasource;
018:
019: import javax.naming.NamingException;
020:
021: import org.apache.jetspeed.components.jndi.JNDIComponent;
022:
023: /**
024: * Bound DBCP Data Source
025: *
026: * @author <a href="mailto:sweaver@apache.org">Scott Weaver</a>
027: * @version $Id: BoundDBCPDatasourceComponent.java 516448 2007-03-09 16:25:47Z ate $
028: */
029: public class BoundDBCPDatasourceComponent extends
030: DBCPDatasourceComponent {
031: private JNDIComponent jndi;
032: private String bindName;
033:
034: /* (non-Javadoc)
035: * @see java.lang.Object#finalize()
036: */
037: protected void finalize() throws Throwable {
038: stop();
039: super .finalize();
040: }
041:
042: /**
043: *
044: * @param user
045: * @param password
046: * @param driverName
047: * @param connectURI
048: * @param maxActive
049: * @param maxWait
050: * @param whenExhausted
051: * @param autoCommit
052: * @param bindName JNDI name to bind this <code>javax.sql.DataSource</code>
053: * created by this class to.
054: * @param jndi JNDIComponent we will use to bind.
055: */
056: public BoundDBCPDatasourceComponent(String user, String password,
057: String driverName, String connectURI, int maxActive,
058: int maxWait, byte whenExhausted, boolean autoCommit,
059: String bindName, JNDIComponent jndi) {
060: super (user, password, driverName, connectURI, maxActive,
061: maxWait, whenExhausted, autoCommit);
062: if (jndi == null) {
063: throw new IllegalArgumentException(
064: "jndi argument cannot be null for BoundDBCPDatasourceComponent");
065: }
066:
067: if (bindName == null) {
068: throw new IllegalArgumentException(
069: "bindName argument cannot be null for BoundDBCPDatasourceComponent");
070: }
071:
072: this .jndi = jndi;
073: this .bindName = bindName;
074:
075: }
076:
077: /**
078: * Same as {@link DBCPDatasourceComponent#start()}
079: * but also binds these <code>javax.sql.DataSource</code>
080: * created to the <code>bindName</code>.
081: *
082: * @see org.picocontainer.Startable#start()
083: */
084: public void start() {
085: super .start();
086: try {
087: jndi.bindObject("comp/env/jdbc/" + bindName,
088: getDatasource());
089: jndi.bindObject("jdbc/" + bindName, getDatasource());
090: } catch (NamingException e) {
091: IllegalStateException ise = new IllegalStateException(
092: "Naming exception " + e.toString());
093: ise.initCause(e);
094: throw ise;
095: }
096: }
097:
098: /* (non-Javadoc)
099: * @see org.picocontainer.Startable#stop()
100: */
101: public void stop() {
102: try {
103: jndi.unbindObject("comp/env/jdbc/" + bindName);
104: jndi.unbindObject("jdbc/" + bindName);
105: } catch (NamingException e) {
106: throw new IllegalStateException("Naming exception "
107: + e.toString());
108: }
109: super.stop();
110: }
111: }
|