commit | author | age
|
f4025a
|
1 |
/* |
U |
2 |
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
|
3 |
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template |
|
4 |
*/ |
|
5 |
|
|
6 |
package de.uhilger.neon; |
|
7 |
|
|
8 |
import java.io.File; |
|
9 |
import java.io.IOException; |
|
10 |
import java.net.MalformedURLException; |
|
11 |
import java.net.URI; |
|
12 |
import java.net.URISyntaxException; |
|
13 |
import java.net.URL; |
|
14 |
import java.net.URLClassLoader; |
|
15 |
import java.util.Enumeration; |
|
16 |
import java.util.logging.Level; |
|
17 |
import java.util.logging.Logger; |
|
18 |
import java.util.zip.ZipEntry; |
|
19 |
import java.util.zip.ZipFile; |
|
20 |
|
|
21 |
/** |
|
22 |
* Die Klasse JarScanner enthaelt Methoden, um fuer eine Klasse zu bestimmen, in |
|
23 |
* welcher JAR-Datei sie liegt und diese JAR-Datei nach Klassen zu durchsuchen. |
|
24 |
* |
|
25 |
* @author Ulrich Hilger |
|
26 |
* @version 0.1, 30.11.2024 |
|
27 |
*/ |
|
28 |
public class JarScanner { |
|
29 |
|
|
30 |
|
|
31 |
public void processZipContent(ClassLoader urlCL, File archive, String packageName, JarScannerListener l, Handler h, String contextName) { |
|
32 |
try { |
|
33 |
ZipFile zipfile = new ZipFile(archive); |
|
34 |
Enumeration en = zipfile.entries(); |
|
35 |
while (en.hasMoreElements()) { |
|
36 |
ZipEntry zipentry = (ZipEntry) en.nextElement(); |
|
37 |
if (!zipentry.isDirectory()) { |
|
38 |
processZipEntry(urlCL, zipentry, packageName, l, h, contextName); |
|
39 |
} else { |
|
40 |
// ZIP-Dir muss nicht bearbeitet werden |
|
41 |
} |
|
42 |
} |
|
43 |
zipfile.close(); |
|
44 |
} catch (IOException ex) { |
|
45 |
log(Level.SEVERE, ex.getLocalizedMessage()); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
private void processZipEntry(ClassLoader urlCL, ZipEntry zipentry, String packageName, JarScannerListener l, Handler h, String contextName) { |
|
50 |
log(Level.FINEST, zipentry.getName()); |
|
51 |
String zName = zipentry.getName(); |
|
52 |
if (zName.toLowerCase().endsWith(".class")) { |
|
53 |
int pos = zName.indexOf(".class"); |
|
54 |
String fullClassName = zName.substring(0, pos); |
|
55 |
log(Level.FINEST, "full class name: " + zName); |
|
56 |
String fullClassNameDots = fullClassName.replace('/', '.'); |
|
57 |
log(Level.FINEST, "full class name dots: " + fullClassNameDots); |
|
58 |
String pkgName = getPackageName(fullClassNameDots); |
|
59 |
log(Level.FINEST, " -- package name: " + pkgName); |
|
60 |
if (null != urlCL && pkgName.toLowerCase().startsWith(packageName)) { |
|
61 |
Class c = null; |
|
62 |
try { |
|
63 |
c = urlCL.loadClass(fullClassNameDots); |
|
64 |
if (c != null) { |
|
65 |
if (c.isAnnotationPresent(Actor.class)) { |
|
66 |
log(Level.FINER, " ---- ACTOR ---- " + fullClassNameDots); |
|
67 |
l.actorFound(c, h, contextName); |
|
68 |
} else { |
|
69 |
log(Level.FINER, "kein Actor " + fullClassNameDots); |
|
70 |
} |
|
71 |
} else { |
|
72 |
log(Level.FINER, "class NOT loaded: " + zName); |
|
73 |
} |
|
74 |
} catch (ClassNotFoundException ex) { |
|
75 |
log(Level.FINER, " +++++ Class not found: " + ex.getMessage()); |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
private String getPackageName(String fullClassName) { |
|
82 |
String packageName; |
|
83 |
int pos = fullClassName.lastIndexOf("."); |
|
84 |
if (pos > 0) { |
|
85 |
packageName = fullClassName.substring(0, pos); |
|
86 |
} else { |
|
87 |
packageName = fullClassName; |
|
88 |
} |
|
89 |
return packageName; |
|
90 |
} |
|
91 |
|
|
92 |
public ClassLoader getUrlClassLoader(Class c) { |
|
93 |
URL url; |
|
94 |
ClassLoader urlCL = null; |
|
95 |
try { |
|
96 |
url = getPath(c).toURL(); |
|
97 |
log(Level.FINER, "url: " + url.getPath()); |
|
98 |
urlCL = new URLClassLoader(new URL[]{url}); |
|
99 |
} catch (URISyntaxException ex) { |
|
100 |
log(Level.SEVERE, ex.getMessage()); |
|
101 |
} catch (MalformedURLException ex) { |
|
102 |
log(Level.SEVERE, ex.getMessage()); |
|
103 |
} finally { |
|
104 |
return urlCL; |
|
105 |
} |
|
106 |
} |
|
107 |
|
|
108 |
public URI getPath(Class c) throws URISyntaxException { |
|
109 |
//Class c = this.getClass(); |
|
110 |
String className = c.getName(); |
|
111 |
finer("this name: " + className); |
|
112 |
|
|
113 |
int pos = className.indexOf(".class"); |
|
114 |
if(pos > -1) { |
|
115 |
String classNameWoExt = className.substring(0, pos); |
|
116 |
} |
|
117 |
String classNameWoPkg = className.substring(className.lastIndexOf(".") + 1); |
|
118 |
finer("Class name: " + classNameWoPkg); |
|
119 |
String classPath = c.getResource(classNameWoPkg + ".class").getPath(); |
|
120 |
pos = classPath.indexOf("!"); |
|
121 |
String jarPath; |
|
122 |
if(pos > -1) { |
|
123 |
jarPath = /*"jar:" + */ classPath.substring(0, pos); |
|
124 |
} else { |
|
125 |
jarPath = classPath; |
|
126 |
} |
|
127 |
finer("path: " + jarPath); |
|
128 |
return new URI(jarPath); |
|
129 |
} |
|
130 |
|
|
131 |
private void finer(String msg) { |
|
132 |
log(Level.FINER, msg); |
|
133 |
} |
|
134 |
|
|
135 |
private void log(Level l, String msg) { |
|
136 |
Logger.getLogger(JarScanner.class.getName()).log(l, msg); |
|
137 |
} |
|
138 |
|
|
139 |
|
|
140 |
public interface JarScannerListener { |
|
141 |
public void actorFound(Class actorClass, Handler h, String contextName); |
|
142 |
} |
|
143 |
|
|
144 |
} |