/*
|
neon - Embeddable HTTP Server based on jdk.httpserver
|
Copyright (C) 2024 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.neon;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.MalformedURLException;
|
import java.net.URI;
|
import java.net.URISyntaxException;
|
import java.net.URL;
|
import java.net.URLClassLoader;
|
import java.util.Enumeration;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipFile;
|
|
/**
|
* Die Klasse JarScanner enthaelt Methoden, um fuer eine Klasse zu bestimmen, in welcher JAR-Datei
|
* sie liegt und diese JAR-Datei nach Klassen zu durchsuchen.
|
*
|
* @author Ulrich Hilger
|
* @version 0.1, 30.11.2024
|
*/
|
public final class JarScanner {
|
|
private final URI path;
|
private final Class annotation;
|
private final Class cls;
|
private final ClassLoader urlCL;
|
|
/**
|
* Einen JarScanner erzeugen, der das Archiv, in dem sich eine gegebene Klasse befindet, nach
|
* Klassen durchsucht, die eine bestimmte Annotation besitzen
|
*
|
* @param c eine Klasse die sich im Archiv befindet, das durchsucht werden soll
|
* @param annotation die Annotation, nach der gesucht wird
|
*/
|
public JarScanner(Class c, Class annotation) {
|
this.annotation = annotation;
|
this.cls = c;
|
this.urlCL = getUrlClassLoader(cls);
|
this.path = getPath(c);
|
}
|
|
/**
|
* Den Inhalt einer Jar-Datei nach Klassen durchsuchen, die die dem Konstruktor gegebene
|
* Annotation besitzen.
|
*
|
* @param packageName Name der Package, die einschl. Unterpackages durchsucht wird, nur Klassen
|
* dieser Package und ihrer Unterpackages werden geladen und auf die Anotation ueberprueft
|
* @param l eine Klasse, die verstaendigt wird, wenn eine annotierte Klasse gefunden wurde
|
* @param h der Handler, dem die gefundene Klasse hinzugefuegt werden soll
|
* @param contextName Name des Kontext, dem gefundene Klassen hinzugefuegt werden sollen
|
*/
|
public void processZipContent(String packageName, JarScannerListener l, Handler h, String contextName) {
|
try {
|
ZipFile zipfile = new ZipFile(new File(path));
|
Enumeration en = zipfile.entries();
|
//ClassLoader cl = getUrlClassLoader(cls);
|
while (en.hasMoreElements()) {
|
ZipEntry zipentry = (ZipEntry) en.nextElement();
|
if (!zipentry.isDirectory()) {
|
processZipEntry(zipentry, packageName, l, h, contextName);
|
} else {
|
// ZIP-Dir muss nicht bearbeitet werden
|
}
|
}
|
} catch (IOException ex) {
|
log(Level.SEVERE, ex.getLocalizedMessage());
|
}
|
}
|
|
@SuppressWarnings("unchecked")
|
private void processZipEntry(ZipEntry zipentry, String packageName, JarScannerListener l, Handler h, String contextName) {
|
finest(zipentry.getName());
|
String zName = zipentry.getName();
|
if (zName.toLowerCase().endsWith(".class")) {
|
int pos = zName.indexOf(".class");
|
String fullClassName = zName.substring(0, pos);
|
finest("full class name: " + zName);
|
String fullClassNameDots = fullClassName.replace('/', '.');
|
finest("full class name dots: " + fullClassNameDots);
|
String pkgName = getPackageName(fullClassNameDots);
|
finest(" -- package name: " + pkgName);
|
if (null != urlCL && pkgName.toLowerCase().startsWith(packageName)) {
|
try {
|
Class c = urlCL.loadClass(fullClassNameDots);
|
if (c != null) {
|
if (c.isAnnotationPresent(annotation)) {
|
finest(" ---- ACTOR ---- " + fullClassNameDots);
|
l.annotationFound(c, h, contextName);
|
} else {
|
finest("kein Actor " + fullClassNameDots);
|
}
|
} else {
|
finest("class NOT loaded: " + zName);
|
}
|
} catch (ClassNotFoundException ex) {
|
finest(" +++++ Class not found: " + ex.getMessage());
|
}
|
}
|
}
|
}
|
|
private String getPackageName(String fullClassName) {
|
String packageName;
|
int pos = fullClassName.lastIndexOf(".");
|
if (pos > 0) {
|
packageName = fullClassName.substring(0, pos);
|
} else {
|
packageName = fullClassName;
|
}
|
return packageName;
|
}
|
|
public ClassLoader getUrlClassLoader(Class c) {
|
ClassLoader cl = null;
|
try {
|
URL url = getPath(c).toURL();
|
finer("url: " + url.getPath());
|
cl = new URLClassLoader(new URL[]{url});
|
} catch (MalformedURLException ex) {
|
log(Level.SEVERE, ex.getMessage());
|
} finally {
|
return cl;
|
}
|
}
|
|
public String getPathStr() {
|
if (path != null) {
|
return path.toString();
|
} else {
|
return "";
|
}
|
}
|
|
public boolean isJar() {
|
return !getPathStr().toLowerCase().endsWith(".class");
|
}
|
|
private URI getPath(Class c) {
|
String className = c.getName();
|
finest("this name: " + className);
|
String classNameWoPkg = c.getSimpleName();//className.substring(className.lastIndexOf(".") + 1);
|
finest("Class name: " + classNameWoPkg);
|
String classPath = c.getResource(classNameWoPkg + ".class").getPath();
|
int pos = classPath.indexOf("!");
|
String jarPath;
|
if (pos > -1) {
|
jarPath = /*"jar:" + */ classPath.substring(0, pos);
|
} else {
|
jarPath = classPath;
|
}
|
finest("path: " + jarPath);
|
try {
|
return new URI(jarPath);
|
} catch (URISyntaxException ex) {
|
Logger.getLogger(JarScanner.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
|
return null;
|
}
|
}
|
|
private void finest(String msg) {
|
log(Level.FINEST, msg);
|
}
|
|
private void finer(String msg) {
|
log(Level.FINER, msg);
|
}
|
|
private void log(Level l, String msg) {
|
Logger.getLogger(JarScanner.class.getName()).log(l, msg);
|
}
|
|
public interface JarScannerListener {
|
|
public void annotationFound(Class foundClass, Handler h, String contextName);
|
}
|
|
}
|