Quadcap Embeddable Server

com/quadcap/services/DataSources.java

Go to the documentation of this file.
00001 package com.quadcap.services; 00002 00003 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.FileReader; 00042 00043 import java.util.HashMap; 00044 import java.util.Hashtable; 00045 import java.util.Properties; 00046 00047 import java.lang.ref.WeakReference; 00048 00049 import javax.naming.Context; 00050 import javax.naming.InitialContext; 00051 import javax.naming.Name; 00052 import javax.naming.NameAlreadyBoundException; 00053 import javax.naming.NamingException; 00054 import javax.naming.RefAddr; 00055 import javax.naming.Reference; 00056 import javax.naming.spi.ObjectFactory; 00057 00058 import com.quadcap.server.Service; 00059 import com.quadcap.server.ServiceContainer; 00060 00061 import com.quadcap.util.Debug; 00062 00063 /** 00064 * Read datasources.xml; create any specified data sources, and bind them 00065 * to JNDI names 00066 * 00067 * @author Stan Bailes 00068 */ 00069 public class DataSources implements Service, ObjectFactory { 00070 static HashMap dss = new HashMap(); 00071 Context context; 00072 Properties props; 00073 00074 public DataSources() {} 00075 00076 private static DataSources globalDs = null; 00077 00078 // public static void init(String initFile) throws Exception { 00079 // if (globalDs == null) { 00080 // globalDs = new DataSources(); 00081 // Properties p = new Properties(); 00082 // p.put("service.config", "datasources.xml"); 00083 // globalDs.init(null, p); 00084 // } 00085 // } 00086 00087 public void init(ServiceContainer c, Properties p) 00088 throws Exception 00089 { 00090 this.props = p; 00091 this.context = new InitialContext(); 00092 String fileName = p.getProperty("service.config"); 00093 DataSourcesParser dp = new DataSourcesParser(); 00094 FileReader in = new FileReader(fileName); 00095 dp.parse(in, this); 00096 } 00097 00098 public Properties getProperties() { return props; } 00099 00100 public void addDataSource(DataSource ds) throws NamingException { 00101 addAttribute(ds.getName(), ds); 00102 } 00103 00104 public static DataSource getDataSource(String name) { 00105 return (DataSource)dss.get(name); 00106 } 00107 00108 public void addAttribute(String name, Object attr) { 00109 dss.put(name, attr); 00110 try { 00111 try { 00112 context.bind(name, attr); 00113 } catch (NameAlreadyBoundException ne) { 00114 context.rebind(name, attr); 00115 } 00116 } catch (NamingException ex) { 00117 Debug.print(ex); 00118 } 00119 } 00120 00121 public Object getAttribute(String name) { 00122 return dss.get(name); 00123 } 00124 00125 public void stop() { 00126 } 00127 00128 public Object getObjectInstance(Object obj, Name name, 00129 Context ctx, Hashtable env) 00130 throws Exception 00131 { 00132 Object ret = null; 00133 if (obj instanceof Reference) { 00134 Reference ref = (Reference)obj; 00135 if (ref.getClassName().equals(DataSource.class.getName())) { 00136 RefAddr addr = ref.get("name"); 00137 if (addr != null) { 00138 ret = getDataSource(addr.getContent().toString()); 00139 } 00140 } 00141 } 00142 return ret; 00143 } 00144 }