KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > driver > DataSourceFactory


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Marek Prochazka.
20  * Contributor(s):
21  */

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

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

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