1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.admin.program.esjp;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27
28 import org.apache.commons.io.FileUtils;
29 import org.efaps.admin.AppConfigHandler;
30 import org.efaps.ci.CIAdminProgram;
31 import org.efaps.ci.CIType;
32 import org.efaps.db.Checkout;
33 import org.efaps.db.InstanceQuery;
34 import org.efaps.db.QueryBuilder;
35 import org.efaps.util.EFapsException;
36 import org.joda.time.DateTime;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47 public final class EFapsClassLoader
48 extends ClassLoader
49 {
50
51
52
53
54 private static EFapsClassLoader CLASSLOADER;
55
56
57
58
59 private static final Logger LOG = LoggerFactory.getLogger(EFapsClassLoader.class);
60
61
62
63
64 private static File TMPFOLDER;
65
66
67
68
69 private final CIType classType;
70
71
72
73
74 private final boolean offline;
75
76
77
78
79
80
81
82 private EFapsClassLoader(final ClassLoader _parentClassLoader,
83 final boolean _offline)
84 {
85 super(_parentClassLoader);
86 this.offline = _offline;
87 this.classType = CIAdminProgram.JavaClass;
88 }
89
90
91
92
93
94
95
96 @Override
97 public Class<?> findClass(final String _name)
98 throws ClassNotFoundException
99 {
100 Class<?> ret = null;
101 if (this.offline) {
102 throw new ClassNotFoundException(_name);
103 } else {
104 final byte[] b = loadClassData(_name);
105 if (b != null) {
106 ret = defineClass(_name, b, 0, b.length);
107 } else {
108 throw new ClassNotFoundException(_name);
109 }
110 }
111 return ret;
112 }
113
114
115
116
117
118
119
120 @Override
121 public URL findResource(final String _name)
122 {
123 URL ret = null;
124 final String name = _name.replaceAll(System.getProperty("file.separator"), ".").replaceAll(".class", "");
125 final byte[] data = loadClassData(name);
126 if (data != null && data.length > 0) {
127 final File file = FileUtils.getFile(EFapsClassLoader.getTempFolder(), name);
128 try {
129 if (!file.exists() || FileUtils.isFileOlder(file, new DateTime().minusHours(1).toDate())) {
130 FileUtils.writeByteArrayToFile(file, data);
131 }
132 ret = file.toURI().toURL();
133 } catch (final IOException e) {
134 LOG.error("Could not geneate File for reading from URL: {}", name);
135 }
136 }
137 return ret;
138 }
139
140
141
142
143
144
145
146
147 protected byte[] loadClassData(final String _resourceName)
148 {
149 EFapsClassLoader.LOG.debug("Loading Class '{}' from Database.", _resourceName);
150 final byte[] x = read(_resourceName);
151 return x;
152 }
153
154
155
156
157
158
159
160
161
162 public byte[] read(final String _resourceName)
163 {
164 byte[] ret = null;
165 EFapsClassLoader.LOG.debug("read ''", _resourceName);
166 try {
167 final QueryBuilder queryBuilder = new QueryBuilder(this.classType);
168 queryBuilder.addWhereAttrEqValue("Name", _resourceName);
169 final InstanceQuery query = queryBuilder.getCachedQuery("esjp");
170 query.execute();
171 if (query.next()) {
172 final Checkout checkout = new Checkout(query.getCurrentValue());
173 final InputStream is = checkout.executeWithoutAccessCheck();
174
175 ret = new byte[is.available()];
176 is.read(ret);
177 is.close();
178 }
179 } catch (final EFapsException e) {
180 EFapsClassLoader.LOG.error("could not access the Database for reading '{}'", e , _resourceName);
181 } catch (final IOException e) {
182 EFapsClassLoader.LOG.error("could not read the Javaclass '{}'", e, _resourceName);
183 }
184 return ret;
185 }
186
187
188
189
190 private static File getTempFolder()
191 {
192 if (EFapsClassLoader.TMPFOLDER == null || !EFapsClassLoader.TMPFOLDER.exists()) {
193 File tmpfld = AppConfigHandler.get().getTempFolder();
194 if (tmpfld == null) {
195 File temp;
196 try {
197 temp = File.createTempFile("eFaps", ".tmp");
198 tmpfld = temp.getParentFile();
199 temp.delete();
200 } catch (final IOException e) {
201 LOG.error("Cannot create temp file", e);
202 }
203 }
204 EFapsClassLoader.TMPFOLDER = new File(tmpfld, "eFaps-ClassFiles");
205 if (!EFapsClassLoader.TMPFOLDER.exists()) {
206 final boolean mkdir = EFapsClassLoader.TMPFOLDER.mkdir();
207 if (!mkdir) {
208 LOG.error("Temp folder was not created");
209 }
210 }
211 }
212 return EFapsClassLoader.TMPFOLDER;
213 }
214
215
216
217
218
219
220
221
222
223 public static synchronized EFapsClassLoader getInstance()
224 {
225 if (EFapsClassLoader.CLASSLOADER == null) {
226 EFapsClassLoader.CLASSLOADER = new EFapsClassLoader(EFapsClassLoader.class.getClassLoader(), false);
227 }
228 return EFapsClassLoader.CLASSLOADER;
229 }
230
231
232
233
234
235
236
237
238
239
240 public static synchronized EFapsClassLoader getOfflineInstance(final ClassLoader _parent)
241 {
242 if (EFapsClassLoader.CLASSLOADER == null) {
243 EFapsClassLoader.CLASSLOADER = new EFapsClassLoader(_parent, true);
244 }
245 return EFapsClassLoader.CLASSLOADER;
246 }
247
248
249
250
251
252 public static boolean isInitialized()
253 {
254 return EFapsClassLoader.CLASSLOADER != null;
255 }
256 }