00001
package com.quadcap.services;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00065
00066
00067
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
00079
00080
00081
00082
00083
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 }