1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
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
100
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 }