001: package com.quadcap.services;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.FileReader;
042:
043: import java.util.HashMap;
044: import java.util.Hashtable;
045: import java.util.Properties;
046:
047: import java.lang.ref.WeakReference;
048:
049: import javax.naming.Context;
050: import javax.naming.InitialContext;
051: import javax.naming.Name;
052: import javax.naming.NameAlreadyBoundException;
053: import javax.naming.NamingException;
054: import javax.naming.RefAddr;
055: import javax.naming.Reference;
056: import javax.naming.spi.ObjectFactory;
057:
058: import com.quadcap.server.Service;
059: import com.quadcap.server.ServiceContainer;
060:
061: import com.quadcap.util.Debug;
062:
063: /**
064: * Read datasources.xml; create any specified data sources, and bind them
065: * to JNDI names
066: *
067: * @author Stan Bailes
068: */
069: public class DataSources implements Service, ObjectFactory {
070: static HashMap dss = new HashMap();
071: Context context;
072: Properties props;
073:
074: public DataSources() {
075: }
076:
077: private static DataSources globalDs = null;
078:
079: // public static void init(String initFile) throws Exception {
080: // if (globalDs == null) {
081: // globalDs = new DataSources();
082: // Properties p = new Properties();
083: // p.put("service.config", "datasources.xml");
084: // globalDs.init(null, p);
085: // }
086: // }
087:
088: public void init(ServiceContainer c, Properties p) throws Exception {
089: this .props = p;
090: this .context = new InitialContext();
091: String fileName = p.getProperty("service.config");
092: DataSourcesParser dp = new DataSourcesParser();
093: FileReader in = new FileReader(fileName);
094: dp.parse(in, this );
095: }
096:
097: public Properties getProperties() {
098: return props;
099: }
100:
101: public void addDataSource(DataSource ds) throws NamingException {
102: addAttribute(ds.getName(), ds);
103: }
104:
105: public static DataSource getDataSource(String name) {
106: return (DataSource) dss.get(name);
107: }
108:
109: public void addAttribute(String name, Object attr) {
110: dss.put(name, attr);
111: try {
112: try {
113: context.bind(name, attr);
114: } catch (NameAlreadyBoundException ne) {
115: context.rebind(name, attr);
116: }
117: } catch (NamingException ex) {
118: Debug.print(ex);
119: }
120: }
121:
122: public Object getAttribute(String name) {
123: return dss.get(name);
124: }
125:
126: public void stop() {
127: }
128:
129: public Object getObjectInstance(Object obj, Name name, Context ctx,
130: Hashtable env) throws Exception {
131: Object ret = null;
132: if (obj instanceof Reference) {
133: Reference ref = (Reference) obj;
134: if (ref.getClassName().equals(DataSource.class.getName())) {
135: RefAddr addr = ref.get("name");
136: if (addr != null) {
137: ret = getDataSource(addr.getContent().toString());
138: }
139: }
140: }
141: return ret;
142: }
143: }
|