1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.xml.util.resolvers;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.exolab.castor.mapping.ClassDescriptor;
31 import org.exolab.castor.xml.ResolverException;
32 import org.exolab.castor.xml.XMLConstants;
33
34
35
36
37
38
39
40
41
42
43 public class ByCDR extends AbstractResolverPackageCommand {
44 private static final Log LOG = LogFactory.getLog(ByCDR.class);
45
46 private List<String> _loadedPackages = new ArrayList<String>();
47
48
49
50
51 public ByCDR() {
52 super();
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67 private Properties getProperties(URL url) throws java.io.IOException {
68 Properties cdrList = new Properties();
69
70 java.io.InputStream stream = url.openStream();
71 cdrList.load(stream);
72 stream.close();
73
74 return cdrList;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 protected Map<String, ClassDescriptor> internalResolve(final String packageName, final ClassLoader classLoader,
95 final Map properties) throws ResolverException {
96
97 Map<String, ClassDescriptor> results = new HashMap<String, ClassDescriptor>();
98 if (!isEmptyPackageName(packageName) && _loadedPackages.contains(packageName)) {
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("Package: " + packageName + " has already been loaded.");
101 }
102 return results;
103 }
104 if (!isEmptyPackageName(packageName)) {
105 _loadedPackages.add(packageName);
106 }
107
108 URL url = classLoader.getResource(ResolveHelpers.getQualifiedFileName(
109 XMLConstants.PKG_CDR_LIST_FILE, packageName));
110 if (url == null) {
111 return results;
112 }
113
114 try {
115 Properties cdrList = this.getProperties(url);
116
117 for (Object clazz : cdrList.keySet()) {
118 String clazzName = (String) clazz;
119 String descriptorClassName = (String) cdrList.get(clazzName);
120 try {
121 Class<?> descriptorClass = classLoader.loadClass(descriptorClassName);
122 if (LOG.isDebugEnabled()) {
123 LOG.debug("Found descriptor: " + descriptorClass);
124 }
125 if (descriptorClass != null) {
126 ClassDescriptor instance = (ClassDescriptor) descriptorClass.newInstance();
127 results.put(clazzName, instance);
128 } else if (LOG.isDebugEnabled()) {
129 LOG.debug("Loading of descriptor class: " + descriptorClassName
130 + " for class: " + clazzName + " has failed - continue without");
131 }
132 } catch (Exception e) {
133 if (LOG.isDebugEnabled()) {
134 LOG.debug("Ignored problem at loading of: " + descriptorClassName
135 + " with exception: " + e);
136 }
137 }
138 }
139 } catch (IOException iox) {
140 String message = "Failed to load package: " + packageName + " with exception: " + iox;
141 LOG.warn(message);
142 throw new ResolverException(message);
143 }
144 return results;
145 }
146 }