1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.exceptions.runtime;
17
18 import java.io.BufferedInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.util.jar.JarEntry;
25 import java.util.jar.JarFile;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35 public abstract class AbstractProjectClassLoader extends ClassLoader
36 {
37
38
39
40
41
42
43
44
45
46 private final Log log = LogFactory.getLog(AbstractProjectClassLoader.class);
47
48
49
50
51
52
53
54
55
56
57 protected AbstractProjectClassLoader(final ClassLoader parent)
58 {
59 super(parent);
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 protected void ensurePackageProvided(final String className)
81 {
82 final int lastDotIndex = className.lastIndexOf('.');
83 if (lastDotIndex != -1)
84 {
85 final String packageName = className.substring(0, lastDotIndex);
86 final Package paccage = getPackage(packageName);
87 if (paccage == null)
88 {
89 definePackage(packageName, null, null, null, null, null, null, null);
90 }
91 }
92 }
93
94
95
96
97
98
99
100
101
102 protected Class<?> loadClassFile(final String className, final File classFile)
103 throws ClassNotFoundException
104 {
105 ensurePackageProvided(className);
106 InputStream in = null;
107 try
108 {
109 in = new BufferedInputStream(new FileInputStream(classFile));
110 final byte[] data = IOUtils.toByteArray(in);
111 final Class<?> clazz = defineClass(className, data, 0, data.length);
112 return clazz;
113 }
114 catch (final IOException e)
115 {
116 final String message =
117 "Cannot load class '" + className + "' from file '" + classFile
118 + "'.";
119 if (log.isDebugEnabled())
120 {
121 log.debug(message);
122 }
123
124 throw new ClassNotFoundException(message, e);
125 }
126 finally
127 {
128 IOUtils.closeQuietly(in);
129 }
130 }
131
132
133
134
135
136
137
138
139
140
141 protected Class<?> loadClassFromLibrary(final String className,
142 final String fileName, final File dirName) throws ClassNotFoundException
143 {
144 ensurePackageProvided(className);
145 try
146 {
147 final JarFile jarFile = new JarFile(dirName);
148 final JarEntry entry = jarFile.getJarEntry(fileName);
149 if (entry != null)
150 {
151 InputStream in = null;
152 try
153 {
154 in = new BufferedInputStream(jarFile.getInputStream(entry));
155 final byte[] data = IOUtils.toByteArray(in);
156 final Class<?> clazz = defineClass(className, data, 0, data.length);
157 return clazz;
158 }
159 finally
160 {
161 IOUtils.closeQuietly(in);
162 }
163 }
164 }
165 catch (final IOException e)
166 {
167 final String message =
168 "Cannot load class '" + className + "' from file '" + dirName + "'.";
169 if (log.isDebugEnabled())
170 {
171 log.debug(message);
172 }
173
174 throw new ClassNotFoundException(message, e);
175 }
176
177 final String message =
178 "Cannot load class '" + className + "' from file '" + dirName + "'.";
179 if (log.isDebugEnabled())
180 {
181 log.debug(message);
182 }
183
184 throw new ClassNotFoundException(message);
185 }
186
187
188
189
190
191
192
193
194
195
196 protected URL loadResourceFromLibrary(final String resourceName,
197 final String fileName, final File dirName) throws IOException
198 {
199 ensurePackageProvided(resourceName);
200 try
201 {
202 return new URL("jar:file:" + dirName.getName() + "!/" + fileName);
203 }
204 catch (final IOException e)
205 {
206 final String message =
207 "Cannot load class '" + resourceName + "' from file '" + dirName
208 + "'.";
209 if (log.isDebugEnabled())
210 {
211 log.debug(message);
212 }
213 final IOException ioe = new IOException(message);
214 ioe.initCause(e);
215 throw ioe;
216 }
217 }
218
219
220
221 }