1 package org.codehaus.plexus.component.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
38
39
40
41
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 }