KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > DataSourceFactory


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Marek Prochazka.
22  * Contributor(s):
23  */

24 package org.objectweb.cjdbc.driver;
25
26 import java.util.Hashtable JavaDoc;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.naming.spi.ObjectFactory JavaDoc;
32
33 /**
34  * <code>DataSource</code> factory for to implement <code>Referenceable</code>.
35  * The factory serves for the {@link DataSource},<code>PooledDataSource</code>,
36  * and <code>XADataSource</code> classes.
37  *
38  * @author <a HREF="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka</a>
39  * @version 1.0
40  */

41 public class DataSourceFactory implements ObjectFactory JavaDoc
42 {
43   /** DataSource classnames. */
44   protected final String JavaDoc dataSourceClassName = "org.objectweb.cjdbc.driver.DataSource";
45   protected final String JavaDoc poolDataSourceName = "org.objectweb.cjdbc.driver.PoolDataSource";
46   protected final String JavaDoc xaDataSourceName = "org.objectweb.cjdbc.driver.XADataSource";
47
48   /**
49    * Gets an instance of the requested <code>DataSource</code> object.
50    *
51    * @param objRef object containing location or reference information that is
52    * used to create an object instance (could be <code>null</code>).
53    * @param name name of this object relative to specified <code>nameCtx</code>
54    * (could be <code>null</code>).
55    * @param nameCtx name context (could ne null if the default initial context
56    * is used).
57    * @param env environment to use (could be null if default is used)
58    * @return a newly created instance of C-JDBC DataSource, <code>null</code>
59    * if an error occurred.
60    * @throws Exception if an error occurs when creating the object instance.
61    */

62   public Object JavaDoc getObjectInstance(Object JavaDoc objRef, Name JavaDoc name, Context JavaDoc nameCtx,
63       Hashtable JavaDoc env) throws Exception JavaDoc
64   {
65     // Check the requested object class
66
Reference JavaDoc ref = (Reference JavaDoc) objRef;
67     String JavaDoc className = ref.getClassName();
68     if ((className == null)
69         || !(className.equals(dataSourceClassName)
70             | className.equals(poolDataSourceName) | className
71             .equals(xaDataSourceName)))
72     {
73       // Wrong class
74
return null;
75     }
76     DataSource ds = null;
77     try
78     {
79       ds = (DataSource) Class.forName(className).newInstance();
80     }
81     catch (Exception JavaDoc e)
82     {
83       throw new RuntimeException JavaDoc("Error when creating C-JDBC" + className
84           + " instance: " + e);
85     }
86
87     ds.setUrl((String JavaDoc) ref.get(DataSource.URL_PROPERTY).getContent());
88     ds.setUser((String JavaDoc) ref.get(DataSource.USER_PROPERTY).getContent());
89     ds.setPassword((String JavaDoc) ref.get(DataSource.PASSWORD_PROPERTY).getContent());
90     return ds;
91   }
92 }
93
Popular Tags