View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.components.datasource;
18  
19  import javax.naming.NamingException;
20  
21  import org.apache.jetspeed.components.jndi.JNDIComponent;
22  
23  
24  /***
25   * Bound DBCP Data Source
26   *
27   * @author <a href="mailto:sweaver@apache.org">Scott Weaver</a>
28   * @version $Id: BoundDBCPDatasourceComponent.java 516448 2007-03-09 16:25:47Z ate $
29   */
30  public class BoundDBCPDatasourceComponent extends DBCPDatasourceComponent
31  {
32      private JNDIComponent jndi;
33      private String bindName;
34  
35      
36      /* (non-Javadoc)
37       * @see java.lang.Object#finalize()
38       */
39      protected void finalize() throws Throwable
40      {
41          stop();
42          super.finalize();
43      }
44      /***
45       * 
46       * @param user
47       * @param password
48       * @param driverName
49       * @param connectURI
50       * @param maxActive
51       * @param maxWait
52       * @param whenExhausted
53       * @param autoCommit
54       * @param bindName JNDI name to bind this <code>javax.sql.DataSource</code>
55       * created by this class to.
56       * @param jndi JNDIComponent we will use to bind.
57       */
58      public BoundDBCPDatasourceComponent(String user, String password, String driverName, String connectURI,
59              int maxActive, int maxWait, byte whenExhausted, boolean autoCommit, String bindName, JNDIComponent jndi)
60      {
61          super(user, password, driverName, connectURI, maxActive, maxWait, whenExhausted, autoCommit);
62          if(jndi == null)
63          {
64              throw new IllegalArgumentException("jndi argument cannot be null for BoundDBCPDatasourceComponent");
65          }
66          
67          if(bindName == null)
68          {
69              throw new IllegalArgumentException("bindName argument cannot be null for BoundDBCPDatasourceComponent");
70          }
71          
72          this.jndi = jndi;
73          this.bindName = bindName;
74  
75      }
76      /***
77       * Same as {@link DBCPDatasourceComponent#start()}
78       * but also binds these <code>javax.sql.DataSource</code>
79       * created to the <code>bindName</code>.
80       * 
81       * @see org.picocontainer.Startable#start()
82       */
83      public void start()
84      {        
85          super.start();
86          try
87          {
88              jndi.bindObject("comp/env/jdbc/"+bindName, getDatasource());
89              jndi.bindObject("jdbc/"+bindName, getDatasource());
90          }
91          catch (NamingException e)
92          {
93              IllegalStateException ise = new IllegalStateException("Naming exception "+e.toString());
94              ise.initCause(e);
95              throw ise;
96          }
97      }
98  
99      /* (non-Javadoc)
100      * @see org.picocontainer.Startable#stop()
101      */
102     public void stop()
103     {        
104         try
105         {
106             jndi.unbindObject("comp/env/jdbc/"+bindName);
107             jndi.unbindObject("jdbc/" + bindName);
108         }
109         catch (NamingException e)
110         {
111              throw new IllegalStateException("Naming exception "+e.toString());
112         }
113         super.stop();
114     }
115 }