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