KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > conn > ContainerPoolFactory


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.conn;
57
58 import java.util.Hashtable JavaDoc;
59
60 import javax.naming.Context JavaDoc;
61 import javax.naming.Name JavaDoc;
62 import javax.naming.RefAddr JavaDoc;
63 import javax.naming.Reference JavaDoc;
64 import javax.naming.spi.ObjectFactory JavaDoc;
65
66 import org.apache.log4j.Logger;
67
68
69 /**
70  * <p>Basic JNDI object factory that creates an instance of
71  * <code>PoolManager</code> that has been configured based on the
72  * <code>RefAddr</code> values of the specified <code>Reference</code>.</p>
73  *
74  * <p>Here is a sample Tomcat 4.x configuration that sets this class
75  * as a default factory for javax.sql.DataSource objects:</p>
76 <code><pre>
77 &lt;ResourceParams name="jdbc/mydb"&gt;
78     &lt;parameter&gt;
79         &lt;name&gt;factory&lt;/name&gt;
80         &lt;value>org.objectstyle.cayenne.conn.ContainerPoolFactory&lt;/value&gt;
81     &lt;/parameter&gt;
82
83     &lt;parameter>
84         &lt;name>username&lt;/name>
85         &lt;value>andrei&lt;/value>
86     &lt;/parameter>
87             
88     &lt;parameter>
89         &lt;name>password&lt;/name>
90         &lt;value>bla-bla&lt;/value>
91     &lt;/parameter>
92                 
93     &lt;parameter>
94         &lt;name>driver&lt;/name>
95         &lt;value>org.gjt.mm.mysql.Driver&lt;/value>
96     &lt;/parameter>
97             
98     &lt;parameter>
99         &lt;name>url&lt;/name>
100         &lt;value>jdbc:mysql://noise/cayenne&lt;/value>
101     &lt;/parameter>
102             
103     &lt;parameter>
104         &lt;name>min&lt;/name>
105         &lt;value>1&lt;/value>
106     &lt;/parameter>
107             
108     &lt;parameter>
109         &lt;name>max&lt;/name>
110         &lt;value>3&lt;/value>
111     &lt;/parameter>
112 &lt;/ResourceParams>
113 </pre></code>
114  *
115  * <p>After ContainerPoolFactory was configured to be used within the container
116  * (see above for Tomcat example), you can reference your "jdbc/mydb" DataSource in
117  * web application deployment descriptor like that (per Servlet Specification): </p>
118  *<code><pre>
119 &lt;resource-ref>
120     &lt;es-ref-name>jdbc/mydb&lt;/res-ref-name>
121     &lt;res-type>javax.sql.DataSource&lt;/res-type>
122     &lt;res-auth>Container&lt;/res-auth>
123 &lt;/resource-ref>
124 </pre></code>
125  *
126  * @author Andrei Adamchik
127  */

128
129 public class ContainerPoolFactory implements ObjectFactory JavaDoc {
130     private static Logger logObj = Logger.getLogger(ContainerPoolFactory.class);
131
132
133     /**
134      * <p>Creates and returns a new <code>PoolManager</code> instance. If no
135      * instance can be created, returns <code>null</code> instead.</p>
136      *
137      * @param obj The possibly null object containing location or
138      * reference information that can be used in creating an object
139      * @param name The name of this object relative to <code>nameCtx</code>
140      * @param nameCtx The context relative to which the <code>name</code>
141      * parameter is specified, or <code>null</code> if <code>name</code>
142      * is relative to the default initial context
143      * @param environment The possibly null environment that is used in
144      * creating this object
145      *
146      * @exception Exception if an exception occurs creating the instance
147      */

148     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
149                                     Hashtable JavaDoc environment)
150     throws Exception JavaDoc {
151         // We only know how to deal with <code>javax.naming.Reference</code>s
152
// that specify a class name of "javax.sql.DataSource"
153
if ((obj == null) || !(obj instanceof Reference JavaDoc)) {
154             logObj.info("unsupported or null reference: " + obj);
155             return null;
156         }
157
158         Reference JavaDoc ref = (Reference JavaDoc) obj;
159         if (!"javax.sql.DataSource".equals(ref.getClassName())) {
160             logObj.info("unsupported type: " + ref.getClassName());
161             return null;
162         }
163
164         // Create and configure a PoolManager instance based on the
165
// RefAddr values associated with this Reference
166
RefAddr JavaDoc ra = null;
167         String JavaDoc driver = null;
168         String JavaDoc url = null;
169         int min = 1;
170         int max = 1;
171         String JavaDoc username = null;
172         String JavaDoc password = null;
173         
174         ra = ref.get("min");
175         if (ra != null) {
176             min = Integer.parseInt(ra.getContent().toString());
177         }
178         
179         ra = ref.get("max");
180         if (ra != null) {
181             max = Integer.parseInt(ra.getContent().toString());
182         }
183
184
185         ra = ref.get("driver");
186         if (ra != null) {
187             driver = ra.getContent().toString();
188         }
189
190
191         ra = ref.get("password");
192         if (ra != null) {
193             password = ra.getContent().toString();
194         }
195
196         ra = ref.get("url");
197         if (ra != null) {
198             url = ra.getContent().toString();
199         }
200
201         ra = ref.get("username");
202         if (ra != null) {
203             username = ra.getContent().toString();
204         }
205
206         logObj.info("Loading datasource driver: " + driver);
207         logObj.info("Connecting to URL: " + url);
208         return new PoolManager(driver, url, min, max, username, password);
209     }
210 }
211
212
Popular Tags