View Javadoc
1   package org.codehaus.plexus.component.discovery;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.Reader;
21  import java.net.URL;
22  import java.net.URLConnection;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.List;
27  
28  import org.codehaus.plexus.classworlds.realm.ClassRealm;
29  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
30  import org.codehaus.plexus.configuration.PlexusConfigurationException;
31  import org.codehaus.plexus.context.Context;
32  import org.codehaus.plexus.context.ContextMapAdapter;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.InterpolationFilterReader;
35  import org.codehaus.plexus.util.ReaderFactory;
36  
37  // TODO: this should be a default strategy of searching through classloaders. a discoverer should really not have to be
38  // concerned finding a particular resource and how to turn it into a set of component descriptors.
39  
40  /**
41   * @author Jason van Zyl
42   */
43  public abstract class AbstractResourceBasedComponentDiscoverer implements ComponentDiscoverer {
44      protected abstract String getComponentDescriptorLocation();
45  
46      protected abstract ComponentSetDescriptor createComponentDescriptors(Reader reader, String source, ClassRealm realm)
47              throws PlexusConfigurationException;
48  
49      public List<ComponentSetDescriptor> findComponents(Context context, ClassRealm realm)
50              throws PlexusConfigurationException {
51          List<ComponentSetDescriptor> componentSetDescriptors = new ArrayList<ComponentSetDescriptor>();
52  
53          Enumeration<URL> resources;
54  
55          try {
56              resources = realm.getResources(getComponentDescriptorLocation());
57          } catch (IOException e) {
58              throw new PlexusConfigurationException("Unable to retrieve resources for: "
59                      + getComponentDescriptorLocation() + " in class realm: " + realm.getId());
60          }
61  
62          for (URL url : Collections.list(resources)) {
63              Reader reader = null;
64  
65              try {
66                  URLConnection conn = url.openConnection();
67  
68                  conn.setUseCaches(false);
69  
70                  conn.connect();
71  
72                  reader = ReaderFactory.newXmlReader(conn.getInputStream());
73  
74                  InterpolationFilterReader interpolationFilterReader =
75                          new InterpolationFilterReader(reader, new ContextMapAdapter(context));
76  
77                  ComponentSetDescriptor componentSetDescriptor =
78                          createComponentDescriptors(interpolationFilterReader, url.toString(), realm);
79  
80                  componentSetDescriptors.add(componentSetDescriptor);
81              } catch (IOException ex) {
82                  throw new PlexusConfigurationException("Error reading configuration " + url, ex);
83              } finally {
84                  IOUtil.close(reader);
85              }
86          }
87  
88          return componentSetDescriptors;
89      }
90  }