1 28 29 package com.caucho.jca; 30 31 import com.caucho.config.Config; 32 import com.caucho.config.ConfigException; 33 import com.caucho.jca.cfg.ConnectorConfig; 34 import com.caucho.jca.cfg.ResourceAdapterConfig; 35 import com.caucho.log.Log; 36 import com.caucho.util.L10N; 37 import com.caucho.vfs.Path; 38 39 import javax.annotation.PostConstruct; 40 import javax.resource.spi.ResourceAdapter ; 41 import java.io.InputStream ; 42 import java.util.ArrayList ; 43 import java.util.logging.Logger ; 44 45 48 public class ResourceManagerConfig { 49 private static final L10N L = new L10N(ResourceManagerConfig.class); 50 private static final Logger log = Log.open(ResourceManagerConfig.class); 51 52 private ResourceManagerImpl _rm; 53 54 private Path _configDirectory; 55 56 private ArrayList <ConnectorConfig> _connList = 57 new ArrayList <ConnectorConfig>(); 58 59 public ResourceManagerConfig() 60 throws ConfigException 61 { 62 _rm = ResourceManagerImpl.createLocalManager(); 63 } 64 65 68 public void setConfigDirectory(Path path) 69 { 70 _configDirectory = path; 71 } 72 73 76 public ConnectorConfig getConnector(String adapterClass) 77 { 78 for (int i = 0; i < _connList.size(); i++) { 79 ConnectorConfig conn = _connList.get(i); 80 com.caucho.jca.cfg.ResourceAdapterConfig ra = conn.getResourceAdapter(); 81 82 if (ra.getResourceadapterClass() != null && 83 adapterClass.equals(ra.getResourceadapterClass().getName())) 84 return conn; 85 } 86 87 return null; 88 } 89 90 @PostConstruct 91 public void init() 92 throws ConfigException 93 { 94 if (_configDirectory == null) 95 throw new ConfigException(L.l("resource-manager requires a config-directory")); 96 Thread thread = Thread.currentThread(); 97 ClassLoader loader = thread.getContextClassLoader(); 98 99 try { 100 Path path = _configDirectory; 101 String []list = path.list(); 102 103 for (int i = 0; i < list.length; i++) { 104 String name = list[i]; 105 106 if (! name.endsWith(".ra")) 107 continue; 108 109 InputStream is = path.lookup(name).openRead(); 110 try { 111 ConnectorConfig conn = new ConnectorConfig(); 112 113 new Config().configure(conn, is, "com/caucho/jca/jca.rnc"); 114 115 _connList.add(conn); 116 } finally { 117 is.close(); 118 } 119 } 120 } catch (ConfigException e) { 121 throw e; 122 } catch (Throwable e) { 123 throw new ConfigException(e); 124 } 125 126 for (int i = 0; i < _connList.size(); i++) { 127 ConnectorConfig conn = _connList.get(i); 128 129 initResource(conn); 130 } 131 } 132 133 private void initResource(ConnectorConfig conn) 134 throws ConfigException 135 { 136 ResourceAdapterConfig raCfg = conn.getResourceAdapter(); 137 138 try { 139 Class raClass = raCfg.getResourceadapterClass(); 140 141 ResourceAdapter ra = (ResourceAdapter ) raClass.newInstance(); 142 143 java.lang.reflect.Method init = raClass.getMethod("init", new Class [0]); 144 145 if (init != null) 146 init.invoke(ra, (Object []) null); 147 148 _rm.addResource(ra); 149 } catch (Exception e) { 150 throw new ConfigException(e); 151 } 152 } 153 } 154 155 | Popular Tags |