01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Contact: sequoia@continuent.org
06: *
07: * Licensed under the Apache License, Version 2.0 (the "License");
08: * you may not use this file except in compliance with the License.
09: * 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: */package org.continuent.sequoia.driver;
22:
23: import java.util.Hashtable;
24:
25: import javax.naming.Context;
26: import javax.naming.Name;
27: import javax.naming.Reference;
28: import javax.naming.spi.ObjectFactory;
29:
30: /**
31: * <code>DataSource</code> factory for to implement <code>Referenceable</code>.
32: * The factory serves for the {@link DataSource},<code>PooledDataSource</code>,
33: * and <code>XADataSource</code> classes.
34: *
35: * @author <a href="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka</a>
36: * @version 1.0
37: */
38: public class DataSourceFactory implements ObjectFactory {
39: /** DataSource classnames. */
40: protected final String dataSourceClassName = "org.continuent.sequoia.driver.DataSource";
41: protected final String poolDataSourceName = "org.continuent.sequoia.driver.PoolDataSource";
42: protected final String xaDataSourceName = "org.continuent.sequoia.driver.XADataSource";
43:
44: /**
45: * Gets an instance of the requested <code>DataSource</code> object.
46: *
47: * @param objRef object containing location or reference information that is
48: * used to create an object instance (could be <code>null</code>).
49: * @param name name of this object relative to specified <code>nameCtx</code>
50: * (could be <code>null</code>).
51: * @param nameCtx name context (could ne null if the default initial context
52: * is used).
53: * @param env environment to use (could be null if default is used)
54: * @return a newly created instance of Sequoia DataSource, <code>null</code>
55: * if an error occurred.
56: * @throws Exception if an error occurs when creating the object instance.
57: */
58: public Object getObjectInstance(Object objRef, Name name,
59: Context nameCtx, Hashtable env) throws Exception {
60: // Check the requested object class
61: Reference ref = (Reference) objRef;
62: String className = ref.getClassName();
63: if ((className == null)
64: || !(className.equals(dataSourceClassName)
65: | className.equals(poolDataSourceName) | className
66: .equals(xaDataSourceName))) {
67: // Wrong class
68: return null;
69: }
70: DataSource ds = null;
71: try {
72: ds = (DataSource) Class.forName(className).newInstance();
73: } catch (Exception e) {
74: throw new RuntimeException("Error when creating Sequoia "
75: + className + " instance: " + e);
76: }
77:
78: ds.setUrl((String) ref.get(DataSource.URL_PROPERTY)
79: .getContent());
80: ds.setUser((String) ref.get(DataSource.USER_PROPERTY)
81: .getContent());
82: ds.setPassword((String) ref.get(DataSource.PASSWORD_PROPERTY)
83: .getContent());
84: return ds;
85: }
86: }
|