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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jca;
031:
032: import com.caucho.jca.cfg.ResourceAdapterConfig;
033: import com.caucho.loader.EnvironmentClassLoader;
034: import com.caucho.loader.EnvironmentLocal;
035: import com.caucho.log.Log;
036: import com.caucho.util.L10N;
037:
038: import java.util.ArrayList;
039: import java.util.logging.Logger;
040:
041: /**
042: * Manages the resource archives.
043: */
044: public class ResourceArchiveManager {
045: static final L10N L = new L10N(ResourceArchiveManager.class);
046: static final Logger log = Logger
047: .getLogger(ResourceArchiveManager.class.getName());
048:
049: private static final EnvironmentLocal<ResourceArchiveManager> _localManager = new EnvironmentLocal<ResourceArchiveManager>();
050:
051: private ArrayList<ResourceArchive> _resources = new ArrayList<ResourceArchive>();
052:
053: /**
054: * Creates the application.
055: */
056: private ResourceArchiveManager() {
057: }
058:
059: /**
060: * Adds a new resource.
061: */
062: static void addResourceArchive(ResourceArchive rar) {
063: ResourceArchiveManager raManager = _localManager.getLevel();
064:
065: if (raManager == null) {
066: raManager = new ResourceArchiveManager();
067: _localManager.set(raManager);
068: }
069:
070: raManager._resources.add(rar);
071: }
072:
073: /**
074: * Adds a new resource.
075: */
076: static void removeResourceArchive(String name) {
077: ResourceArchiveManager raManager = _localManager.getLevel();
078:
079: if (raManager == null)
080: return;
081:
082: ResourceArchive rar = raManager.getResourceArchive(name);
083:
084: if (rar != null) {
085: raManager._resources.remove(rar);
086:
087: rar.destroy();
088: }
089: }
090:
091: /**
092: * Finds a resource.
093: */
094: public static ResourceArchive findResourceArchive(String name) {
095: ClassLoader loader = Thread.currentThread()
096: .getContextClassLoader();
097:
098: for (; loader != null; loader = loader.getParent()) {
099: if (loader instanceof EnvironmentClassLoader) {
100: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
101:
102: ResourceArchiveManager manager = _localManager
103: .getLevel(envLoader);
104:
105: if (manager != null) {
106: ResourceArchive ra = manager
107: .getResourceArchive(name);
108:
109: if (ra != null)
110: return ra;
111: }
112: }
113: }
114:
115: return null;
116: }
117:
118: /**
119: * Returns the resource archive in the manager.
120: */
121: private ResourceArchive getResourceArchive(String type) {
122: for (int i = 0; i < _resources.size(); i++) {
123: ResourceArchive ra = _resources.get(i);
124:
125: if (type.equals(ra.getDisplayName()))
126: return ra;
127: }
128:
129: for (int i = 0; i < _resources.size(); i++) {
130: ResourceArchive ra = _resources.get(i);
131:
132: ResourceAdapterConfig raConfig = ra.getResourceAdapter();
133: if (raConfig == null)
134: continue;
135:
136: Class resourceAdapterClass = raConfig
137: .getResourceadapterClass();
138: if (resourceAdapterClass != null
139: && type.equals(resourceAdapterClass.getName()))
140: return ra;
141: }
142:
143: for (int i = 0; i < _resources.size(); i++) {
144: ResourceArchive ra = _resources.get(i);
145: if (ra.getConnectionDefinition(type) != null)
146: return ra;
147: }
148:
149: for (int i = 0; i < _resources.size(); i++) {
150: ResourceArchive ra = _resources.get(i);
151:
152: if (ra.getMessageListener(type) != null)
153: return ra;
154: }
155:
156: return null;
157: }
158:
159: /**
160: * Returns the resource archive in the manager.
161: */
162: private ResourceArchive getResourceArchiveByType(Class type) {
163: for (int i = 0; i < _resources.size(); i++) {
164: ResourceArchive ra = _resources.get(i);
165:
166: ResourceAdapterConfig raConfig = ra.getResourceAdapter();
167: if (raConfig == null)
168: continue;
169:
170: Class resourceAdapterClass = raConfig
171: .getResourceadapterClass();
172:
173: if (type.equals(resourceAdapterClass))
174: return ra;
175: }
176:
177: for (int i = 0; i < _resources.size(); i++) {
178: ResourceArchive ra = _resources.get(i);
179: if (ra.getConnectionDefinition(type.getName()) != null)
180: return ra;
181: }
182:
183: for (int i = 0; i < _resources.size(); i++) {
184: ResourceArchive ra = _resources.get(i);
185:
186: if (ra.getMessageListener(type.getName()) != null)
187: return ra;
188: }
189:
190: return null;
191: }
192: }
|