KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jca > ResourceManagerConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Configuration for the resource manager.
47  */

48 public class ResourceManagerConfig {
49   private static final L10N L = new L10N(ResourceManagerConfig.class);
50   private static final Logger JavaDoc log = Log.open(ResourceManagerConfig.class);
51
52   private ResourceManagerImpl _rm;
53
54   private Path _configDirectory;
55
56   private ArrayList JavaDoc<ConnectorConfig> _connList =
57     new ArrayList JavaDoc<ConnectorConfig>();
58   
59   public ResourceManagerConfig()
60     throws ConfigException
61   {
62     _rm = ResourceManagerImpl.createLocalManager();
63   }
64
65   /**
66    * Sets the configuration directory.
67    */

68   public void setConfigDirectory(Path path)
69   {
70     _configDirectory = path;
71   }
72
73   /**
74    * Returns the matching connector.
75    */

76   public ConnectorConfig getConnector(String JavaDoc 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 JavaDoc thread = Thread.currentThread();
97     ClassLoader JavaDoc loader = thread.getContextClassLoader();
98
99     try {
100       Path path = _configDirectory;
101       String JavaDoc []list = path.list();
102
103       for (int i = 0; i < list.length; i++) {
104     String JavaDoc name = list[i];
105
106     if (! name.endsWith(".ra"))
107       continue;
108
109     InputStream JavaDoc 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 JavaDoc 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 JavaDoc raClass = raCfg.getResourceadapterClass();
140
141       ResourceAdapter JavaDoc ra = (ResourceAdapter JavaDoc) raClass.newInstance();
142
143       java.lang.reflect.Method JavaDoc init = raClass.getMethod("init", new Class JavaDoc[0]);
144
145       if (init != null)
146     init.invoke(ra, (Object JavaDoc []) null);
147
148       _rm.addResource(ra);
149     } catch (Exception JavaDoc e) {
150       throw new ConfigException(e);
151     }
152   }
153 }
154
155
Popular Tags