KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conn;
21
22 import java.util.Hashtable JavaDoc;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.Name JavaDoc;
26 import javax.naming.RefAddr JavaDoc;
27 import javax.naming.Reference JavaDoc;
28 import javax.naming.spi.ObjectFactory JavaDoc;
29
30
31 /**
32  * <p>Basic JNDI object factory that creates an instance of
33  * <code>PoolManager</code> that has been configured based on the
34  * <code>RefAddr</code> values of the specified <code>Reference</code>.</p>
35  *
36  * <p>Here is a sample Tomcat 4.x configuration that sets this class
37  * as a default factory for javax.sql.DataSource objects:</p>
38 <code><pre>
39 &lt;ResourceParams name="jdbc/mydb"&gt;
40     &lt;parameter&gt;
41         &lt;name&gt;factory&lt;/name&gt;
42         &lt;value>org.apache.cayenne.conn.ContainerPoolFactory&lt;/value&gt;
43     &lt;/parameter&gt;
44
45     &lt;parameter>
46         &lt;name>username&lt;/name>
47         &lt;value>andrei&lt;/value>
48     &lt;/parameter>
49             
50     &lt;parameter>
51         &lt;name>password&lt;/name>
52         &lt;value>bla-bla&lt;/value>
53     &lt;/parameter>
54                 
55     &lt;parameter>
56         &lt;name>driver&lt;/name>
57         &lt;value>org.gjt.mm.mysql.Driver&lt;/value>
58     &lt;/parameter>
59             
60     &lt;parameter>
61         &lt;name>url&lt;/name>
62         &lt;value>jdbc:mysql://noise/cayenne&lt;/value>
63     &lt;/parameter>
64             
65     &lt;parameter>
66         &lt;name>min&lt;/name>
67         &lt;value>1&lt;/value>
68     &lt;/parameter>
69             
70     &lt;parameter>
71         &lt;name>max&lt;/name>
72         &lt;value>3&lt;/value>
73     &lt;/parameter>
74 &lt;/ResourceParams>
75 </pre></code>
76  *
77  * <p>After ContainerPoolFactory was configured to be used within the container
78  * (see above for Tomcat example), you can reference your "jdbc/mydb" DataSource in
79  * web application deployment descriptor like that (per Servlet Specification): </p>
80  *<code><pre>
81 &lt;resource-ref>
82     &lt;es-ref-name>jdbc/mydb&lt;/res-ref-name>
83     &lt;res-type>javax.sql.DataSource&lt;/res-type>
84     &lt;res-auth>Container&lt;/res-auth>
85 &lt;/resource-ref>
86 </pre></code>
87  *
88  * @author Andrus Adamchik
89  */

90 public class ContainerPoolFactory implements ObjectFactory JavaDoc {
91
92     /**
93      * <p>Creates and returns a new <code>PoolManager</code> instance. If no
94      * instance can be created, returns <code>null</code> instead.</p>
95      *
96      * @param obj The possibly null object containing location or
97      * reference information that can be used in creating an object
98      * @param name The name of this object relative to <code>nameCtx</code>
99      * @param nameCtx The context relative to which the <code>name</code>
100      * parameter is specified, or <code>null</code> if <code>name</code>
101      * is relative to the default initial context
102      * @param environment The possibly null environment that is used in
103      * creating this object
104      *
105      * @exception Exception if an exception occurs creating the instance
106      */

107     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
108                                     Hashtable JavaDoc environment)
109     throws Exception JavaDoc {
110         // We only know how to deal with <code>javax.naming.Reference</code>s
111
// that specify a class name of "javax.sql.DataSource"
112
if ((obj == null) || !(obj instanceof Reference JavaDoc)) {
113             return null;
114         }
115
116         Reference JavaDoc ref = (Reference JavaDoc) obj;
117         if (!"javax.sql.DataSource".equals(ref.getClassName())) {
118             return null;
119         }
120
121         // Create and configure a PoolManager instance based on the
122
// RefAddr values associated with this Reference
123
RefAddr JavaDoc ra = null;
124         String JavaDoc driver = null;
125         String JavaDoc url = null;
126         int min = 1;
127         int max = 1;
128         String JavaDoc username = null;
129         String JavaDoc password = null;
130         
131         ra = ref.get("min");
132         if (ra != null) {
133             min = Integer.parseInt(ra.getContent().toString());
134         }
135         
136         ra = ref.get("max");
137         if (ra != null) {
138             max = Integer.parseInt(ra.getContent().toString());
139         }
140
141
142         ra = ref.get("driver");
143         if (ra != null) {
144             driver = ra.getContent().toString();
145         }
146
147
148         ra = ref.get("password");
149         if (ra != null) {
150             password = ra.getContent().toString();
151         }
152
153         ra = ref.get("url");
154         if (ra != null) {
155             url = ra.getContent().toString();
156         }
157
158         ra = ref.get("username");
159         if (ra != null) {
160             username = ra.getContent().toString();
161         }
162
163         return new PoolManager(driver, url, min, max, username, password);
164     }
165 }
166
167
Popular Tags