KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > RegistryProviderAutoDetector


1 // Copyright 2007 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.io.IOException JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.jar.Attributes JavaDoc;
25 import java.util.jar.Manifest JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hivemind.ApplicationRuntimeException;
30 import org.apache.hivemind.ClassResolver;
31 import org.apache.hivemind.util.URLResource;
32
33 /**
34  * Searches for {@link org.apache.hivemind.impl.RegistryProvider} implementations
35  * that are defined in manifest files. Creates an instance of each
36  * implementation and returns them in the getProviders method.
37  * The manifest file must contain a global attribute <code>hivemind-providers</code>
38  * which contains a comma separated list of classes that implement
39  * the {@link org.apache.hivemind.impl.RegistryProvider} interface.
40  *
41  * @author Achim Huegen
42  */

43 public class RegistryProviderAutoDetector
44 {
45     private static final Log LOG = LogFactory.getLog(RegistryProviderAutoDetector.class);
46     public static final String JavaDoc MANIFEST = "META-INF/MANIFEST.MF";
47     public static final String JavaDoc HIVEMIND_SECTION_NAME = "hivemind";
48     public static final String JavaDoc PROVIDER_ATTRIBUTE_NAME = "hivemind-providers";
49     
50     private List JavaDoc _providers = new ArrayList JavaDoc();
51     
52     public RegistryProviderAutoDetector(ClassResolver resolver)
53     {
54         processManifestFiles(resolver);
55     }
56     
57     public List JavaDoc getProviders()
58     {
59         return _providers;
60     }
61     
62     /**
63      * Process all manifest files found in the classpath
64      * @param resolver the ClassResolver to use for the search
65      */

66     private void processManifestFiles(ClassResolver resolver)
67     {
68         if (LOG.isDebugEnabled())
69             LOG.debug("Processing manifest files visible to " + resolver);
70
71         ClassLoader JavaDoc loader = resolver.getClassLoader();
72         Enumeration JavaDoc e = null;
73
74         try
75         {
76             e = loader.getResources(MANIFEST);
77         }
78         catch (IOException JavaDoc ex)
79         {
80             throw new ApplicationRuntimeException(ImplMessages.unableToFindProviders(resolver, ex),
81                     ex);
82         }
83
84         while (e.hasMoreElements())
85         {
86             URL JavaDoc descriptorURL = (URL JavaDoc) e.nextElement();
87
88             processManifestFile(resolver, new URLResource(descriptorURL));
89         }
90
91     }
92
93
94     /**
95      * Process a single manifest file.
96      *
97      * @param resolver
98      * @param resource pointer to the manifest file
99      */

100     private void processManifestFile(ClassResolver resolver, URLResource resource)
101     {
102         URL JavaDoc url = resource.getResourceURL();
103         Manifest JavaDoc manifest;
104         try
105         {
106             manifest = new Manifest JavaDoc(url.openStream());
107         }
108         catch (IOException JavaDoc e)
109         {
110             throw new ApplicationRuntimeException(ImplMessages.unableToReadManifest(url, e),
111                     e);
112         }
113         // Search for an entry that defines a provider class
114
Attributes JavaDoc attributes = manifest.getMainAttributes();
115         if (attributes != null) {
116             String JavaDoc providers = attributes.getValue(PROVIDER_ATTRIBUTE_NAME);
117             if (providers != null) {
118                 if (LOG.isDebugEnabled()) {
119                     LOG.debug("Found providers '" + providers + "' defined in manifest file '" + url.toString() + "'");
120                 }
121                 handleProviderAttribute(resolver, providers);
122             }
123         }
124     }
125
126     /**
127      * Parse the provider list in an attribute and load all classes.
128      */

129     private void handleProviderAttribute(ClassResolver resolver, String JavaDoc providers)
130     {
131         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(providers, ",");
132         while (tokenizer.hasMoreTokens())
133         {
134             String JavaDoc providerClassName = tokenizer.nextToken();
135             loadProvider(resolver, providerClassName);
136         }
137     }
138     
139     /**
140      * Load a provider class and create an instance.
141      *
142      * @param resolver
143      * @param providerClassName
144      */

145     private void loadProvider(ClassResolver resolver, String JavaDoc providerClassName)
146     {
147         if (LOG.isDebugEnabled())
148             LOG.debug("Loading provider " + providerClassName);
149         Object JavaDoc provider = null;
150         try
151         {
152             Class JavaDoc providerClass = resolver.findClass(providerClassName);
153             provider = providerClass.newInstance();
154         }
155         catch (Exception JavaDoc e)
156         {
157             Exception JavaDoc cause = e;
158             if (e instanceof InvocationTargetException JavaDoc)
159             {
160                 cause = (InvocationTargetException JavaDoc) e;
161             }
162             throw new ApplicationRuntimeException(ImplMessages.unableToCreateProvider(providerClassName, e),
163                     cause);
164         }
165         // Check type of provider
166
if (!(provider instanceof RegistryProvider)) {
167             throw new ApplicationRuntimeException(ImplMessages.providerWrongType(providerClassName, RegistryProvider.class));
168         }
169         
170         _providers.add(provider);
171     }
172
173 }
174
Popular Tags