KickJava   Java API By Example, From Geeks To Geeks.

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


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.jca.cfg.ResourceAdapterConfig;
32 import com.caucho.loader.EnvironmentClassLoader;
33 import com.caucho.loader.EnvironmentLocal;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Manages the resource archives.
42  */

43 public class ResourceArchiveManager {
44   static final L10N L = new L10N(ResourceArchiveManager.class);
45   static final Logger JavaDoc log = Log.open(ResourceArchiveManager.class);
46
47   private static final EnvironmentLocal<ResourceArchiveManager> _localManager =
48     new EnvironmentLocal<ResourceArchiveManager>();
49
50   private ArrayList JavaDoc<ResourceArchive> _resources =
51     new ArrayList JavaDoc<ResourceArchive>();
52
53   /**
54    * Creates the application.
55    */

56   private ResourceArchiveManager()
57   {
58   }
59
60   /**
61    * Adds a new resource.
62    */

63   static void addResourceArchive(ResourceArchive rar)
64   {
65     ResourceArchiveManager raManager = _localManager.getLevel();
66
67     if (raManager == null) {
68       raManager = new ResourceArchiveManager();
69       _localManager.set(raManager);
70     }
71
72     raManager._resources.add(rar);
73   }
74
75   /**
76    * Finds a resource.
77    */

78   static ResourceArchive findResourceArchive(String JavaDoc name)
79   {
80     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
81     
82     for (; loader != null; loader = loader.getParent()) {
83       if (loader instanceof EnvironmentClassLoader) {
84     EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
85
86     ResourceArchiveManager manager = _localManager.getLevel(envLoader);
87
88     if (manager != null) {
89       ResourceArchive ra = manager.getResourceArchive(name);
90
91       if (ra != null)
92         return ra;
93     }
94       }
95     }
96
97     return null;
98   }
99
100   /**
101    * Returns the resource archive in the manager.
102    */

103   private ResourceArchive getResourceArchive(String JavaDoc type)
104   {
105     for (int i = 0; i < _resources.size(); i++) {
106       ResourceArchive ra = _resources.get(i);
107
108       if (type.equals(ra.getDisplayName()))
109     return ra;
110     }
111     
112     for (int i = 0; i < _resources.size(); i++) {
113       ResourceArchive ra = _resources.get(i);
114
115       ResourceAdapterConfig raConfig = ra.getResourceAdapter();
116       if (raConfig == null)
117     continue;
118
119       Class JavaDoc resourceAdapterClass = raConfig.getResourceadapterClass();
120
121       if (resourceAdapterClass != null &&
122       type.equals(resourceAdapterClass.getName()))
123     return ra;
124     }
125     
126     for (int i = 0; i < _resources.size(); i++) {
127       ResourceArchive ra = _resources.get(i);
128       if (ra.getConnectionDefinition(type) != null)
129     return ra;
130     }
131
132     return null;
133   }
134 }
135
Popular Tags