1 package com.quadcap.services; 2 3 40 41 import java.io.FileReader ; 42 43 import java.util.HashMap ; 44 import java.util.Hashtable ; 45 import java.util.Properties ; 46 47 import java.lang.ref.WeakReference ; 48 49 import javax.naming.Context ; 50 import javax.naming.InitialContext ; 51 import javax.naming.Name ; 52 import javax.naming.NameAlreadyBoundException ; 53 import javax.naming.NamingException ; 54 import javax.naming.RefAddr ; 55 import javax.naming.Reference ; 56 import javax.naming.spi.ObjectFactory ; 57 58 import com.quadcap.server.Service; 59 import com.quadcap.server.ServiceContainer; 60 61 import com.quadcap.util.Debug; 62 63 69 public class DataSources implements Service, ObjectFactory { 70 static HashMap dss = new HashMap (); 71 Context context; 72 Properties props; 73 74 public DataSources() {} 75 76 private static DataSources globalDs = null; 77 78 87 public void init(ServiceContainer c, Properties p) 88 throws Exception 89 { 90 this.props = p; 91 this.context = new InitialContext (); 92 String fileName = p.getProperty("service.config"); 93 DataSourcesParser dp = new DataSourcesParser(); 94 FileReader in = new FileReader (fileName); 95 dp.parse(in, this); 96 } 97 98 public Properties getProperties() { return props; } 99 100 public void addDataSource(DataSource ds) throws NamingException { 101 addAttribute(ds.getName(), ds); 102 } 103 104 public static DataSource getDataSource(String name) { 105 return (DataSource)dss.get(name); 106 } 107 108 public void addAttribute(String name, Object attr) { 109 dss.put(name, attr); 110 try { 111 try { 112 context.bind(name, attr); 113 } catch (NameAlreadyBoundException ne) { 114 context.rebind(name, attr); 115 } 116 } catch (NamingException ex) { 117 Debug.print(ex); 118 } 119 } 120 121 public Object getAttribute(String name) { 122 return dss.get(name); 123 } 124 125 public void stop() { 126 } 127 128 public Object getObjectInstance(Object obj, Name name, 129 Context ctx, Hashtable env) 130 throws Exception 131 { 132 Object ret = null; 133 if (obj instanceof Reference ) { 134 Reference ref = (Reference )obj; 135 if (ref.getClassName().equals(DataSource.class.getName())) { 136 RefAddr addr = ref.get("name"); 137 if (addr != null) { 138 ret = getDataSource(addr.getContent().toString()); 139 } 140 } 141 } 142 return ret; 143 } 144 } 145 | Popular Tags |