Source code: org/objectstyle/cayenne/conn/ContainerPoolFactory.java
1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, Version 1.0
4 *
5 * Copyright (c) 2002-2003 The ObjectStyle Group
6 * and individual authors of the software. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution, if
21 * any, must include the following acknowlegement:
22 * "This product includes software developed by the
23 * ObjectStyle Group (http://objectstyle.org/)."
24 * Alternately, this acknowlegement may appear in the software itself,
25 * if and wherever such third-party acknowlegements normally appear.
26 *
27 * 4. The names "ObjectStyle Group" and "Cayenne"
28 * must not be used to endorse or promote products derived
29 * from this software without prior written permission. For written
30 * permission, please contact andrus@objectstyle.org.
31 *
32 * 5. Products derived from this software may not be called "ObjectStyle"
33 * nor may "ObjectStyle" appear in their names without prior written
34 * permission of the ObjectStyle Group.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the ObjectStyle Group. For more
52 * information on the ObjectStyle Group, please see
53 * <http://objectstyle.org/>.
54 *
55 *
56 * Other copyright notes:
57 *
58 * 1. Original idea and implementation: Jakarta DBCP project,
59 * copyright (c) The Apache Software Foundation,
60 * author Craig R. McClanahan
61 *
62 */
63 package org.objectstyle.cayenne.conn;
64
65 import java.util.Hashtable;
66
67 import javax.naming.Context;
68 import javax.naming.Name;
69 import javax.naming.RefAddr;
70 import javax.naming.Reference;
71 import javax.naming.spi.ObjectFactory;
72
73 import org.apache.log4j.Logger;
74
75
76 /**
77 * <p>Basic JNDI object factory that creates an instance of
78 * <code>PoolManager</code> that has been configured based on the
79 * <code>RefAddr</code> values of the specified <code>Reference</code>.</p>
80 *
81 * <p>Here is a sample Tomcat 4.0.x configuration that sets this class
82 * as a default factory for javax.sql.DataSource objects:</p>
83 <code><pre>
84 <ResourceParams name="jdbc/mydb">
85 <parameter>
86 <name>factory</name>
87 <value>org.objectstyle.cayenne.conn.ContainerPoolFactory</value>
88 </parameter>
89
90 <parameter>
91 <name>username</name>
92 <value>andrei</value>
93 </parameter>
94
95 <parameter>
96 <name>password</name>
97 <value>bla-bla</value>
98 </parameter>
99
100 <parameter>
101 <name>driver</name>
102 <value>org.gjt.mm.mysql.Driver</value>
103 </parameter>
104
105 <parameter>
106 <name>url</name>
107 <value>jdbc:mysql://noise/cayenne</value>
108 </parameter>
109
110 <parameter>
111 <name>min</name>
112 <value>1</value>
113 </parameter>
114
115 <parameter>
116 <name>max</name>
117 <value>3</value>
118 </parameter>
119 </ResourceParams>
120 </pre></code>
121 *
122 * <p>After ContainerPoolFactory was configured to be used within the container
123 * (see above for Tomcat example), you can reference your "jdbc/mydb" DataSource in
124 * web application deployment descriptor like that (per Servlet Specification): </p>
125 *<code><pre>
126 <resource-ref>
127 <es-ref-name>jdbc/mydb</res-ref-name>
128 <res-type>javax.sql.DataSource</res-type>
129 <res-auth>Container</res-auth>
130 </resource-ref>
131 </pre></code>
132 *
133 * @author Andrei Adamchik
134 */
135
136 public class ContainerPoolFactory implements ObjectFactory {
137 private static Logger logObj = Logger.getLogger(ContainerPoolFactory.class);
138
139
140 /**
141 * <p>Creates and returns a new <code>PoolManager</code> instance. If no
142 * instance can be created, returns <code>null</code> instead.</p>
143 *
144 * @param obj The possibly null object containing location or
145 * reference information that can be used in creating an object
146 * @param name The name of this object relative to <code>nameCtx</code>
147 * @param nameCts The context relative to which the <code>name</code>
148 * parameter is specified, or <code>null</code> if <code>name</code>
149 * is relative to the default initial context
150 * @param environment The possibly null environment that is used in
151 * creating this object
152 *
153 * @exception Exception if an exception occurs creating the instance
154 */
155 public Object getObjectInstance(Object obj, Name name, Context nameCtx,
156 Hashtable environment)
157 throws Exception {
158 // We only know how to deal with <code>javax.naming.Reference</code>s
159 // that specify a class name of "javax.sql.DataSource"
160 if ((obj == null) || !(obj instanceof Reference)) {
161 logObj.info("unsupported or null reference: " + obj);
162 return null;
163 }
164
165 Reference ref = (Reference) obj;
166 if (!"javax.sql.DataSource".equals(ref.getClassName())) {
167 logObj.info("unsupported type: " + ref.getClassName());
168 return null;
169 }
170
171 // Create and configure a PoolManager instance based on the
172 // RefAddr values associated with this Reference
173 RefAddr ra = null;
174 String driver = null;
175 String url = null;
176 int min = 1;
177 int max = 1;
178 String username = null;
179 String password = null;
180
181 ra = ref.get("min");
182 if (ra != null) {
183 min = Integer.parseInt(ra.getContent().toString());
184 }
185
186 ra = ref.get("max");
187 if (ra != null) {
188 max = Integer.parseInt(ra.getContent().toString());
189 }
190
191
192 ra = ref.get("driver");
193 if (ra != null) {
194 driver = ra.getContent().toString();
195 }
196
197
198 ra = ref.get("password");
199 if (ra != null) {
200 password = ra.getContent().toString();
201 }
202
203 ra = ref.get("url");
204 if (ra != null) {
205 url = ra.getContent().toString();
206 }
207
208 ra = ref.get("username");
209 if (ra != null) {
210 username = ra.getContent().toString();
211 }
212
213 logObj.info("Loading datasource driver: " + driver);
214 logObj.info("Connecting to URL: " + url);
215 return new PoolManager(driver, url, min, max, username, password);
216 }
217 }
218