001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jca;
030:
031: import com.caucho.config.Config;
032: import com.caucho.config.ConfigException;
033: import com.caucho.jca.cfg.ConnectorConfig;
034: import com.caucho.jca.cfg.ResourceAdapterConfig;
035: import com.caucho.log.Log;
036: import com.caucho.util.L10N;
037: import com.caucho.vfs.Path;
038:
039: import javax.annotation.PostConstruct;
040: import javax.resource.spi.ResourceAdapter;
041: import java.io.InputStream;
042: import java.util.ArrayList;
043: import java.util.logging.Logger;
044:
045: /**
046: * Configuration for the resource manager.
047: */
048: public class ResourceManagerConfig {
049: private static final L10N L = new L10N(ResourceManagerConfig.class);
050: private static final Logger log = Log
051: .open(ResourceManagerConfig.class);
052:
053: private ResourceManagerImpl _rm;
054:
055: private Path _configDirectory;
056:
057: private ArrayList<ConnectorConfig> _connList = new ArrayList<ConnectorConfig>();
058:
059: public ResourceManagerConfig() throws ConfigException {
060: _rm = ResourceManagerImpl.createLocalManager();
061: }
062:
063: /**
064: * Sets the configuration directory.
065: */
066: public void setConfigDirectory(Path path) {
067: _configDirectory = path;
068: }
069:
070: /**
071: * Returns the matching connector.
072: */
073: public ConnectorConfig getConnector(String adapterClass) {
074: for (int i = 0; i < _connList.size(); i++) {
075: ConnectorConfig conn = _connList.get(i);
076: com.caucho.jca.cfg.ResourceAdapterConfig ra = conn
077: .getResourceAdapter();
078:
079: if (ra.getResourceadapterClass() != null
080: && adapterClass.equals(ra.getResourceadapterClass()
081: .getName()))
082: return conn;
083: }
084:
085: return null;
086: }
087:
088: @PostConstruct
089: public void init() throws ConfigException {
090: if (_configDirectory == null)
091: throw new ConfigException(L
092: .l("resource-manager requires a config-directory"));
093: Thread thread = Thread.currentThread();
094: ClassLoader loader = thread.getContextClassLoader();
095:
096: try {
097: Path path = _configDirectory;
098: String[] list = path.list();
099:
100: for (int i = 0; i < list.length; i++) {
101: String name = list[i];
102:
103: if (!name.endsWith(".ra"))
104: continue;
105:
106: InputStream is = path.lookup(name).openRead();
107: try {
108: ConnectorConfig conn = new ConnectorConfig();
109:
110: new Config().configure(conn, is,
111: "com/caucho/jca/jca.rnc");
112:
113: _connList.add(conn);
114: } finally {
115: is.close();
116: }
117: }
118: } catch (ConfigException e) {
119: throw e;
120: } catch (Exception e) {
121: throw ConfigException.create(e);
122: }
123:
124: for (int i = 0; i < _connList.size(); i++) {
125: ConnectorConfig conn = _connList.get(i);
126:
127: initResource(conn);
128: }
129: }
130:
131: private void initResource(ConnectorConfig conn)
132: throws ConfigException {
133: ResourceAdapterConfig raCfg = conn.getResourceAdapter();
134:
135: try {
136: Class raClass = raCfg.getResourceadapterClass();
137:
138: ResourceAdapter ra = (ResourceAdapter) raClass
139: .newInstance();
140:
141: java.lang.reflect.Method init = raClass.getMethod("init",
142: new Class[0]);
143:
144: if (init != null)
145: init.invoke(ra, (Object[]) null);
146:
147: _rm.addResource(ra);
148: } catch (Exception e) {
149: throw ConfigException.create(e);
150: }
151: }
152: }
|