| | |
| | | import com.sun.net.httpserver.HttpContext; |
| | | import com.sun.net.httpserver.HttpHandler; |
| | | import com.sun.net.httpserver.HttpServer; |
| | | import de.uhilger.neon.JarScanner.JarScannerListener; |
| | | import de.uhilger.neon.entity.ContextDescriptor; |
| | | import de.uhilger.neon.entity.NeonDescriptor; |
| | | import de.uhilger.neon.entity.ServerDescriptor; |
| | |
| | | import java.util.concurrent.Executors; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | import de.uhilger.neon.Scanner.ScannerListener; |
| | | |
| | | /** |
| | | * Einen Neon-Server aus einer Beschreibungsdatei herstellen |
| | |
| | | * @author Ulrich Hilger |
| | | * @version 1, 6.2.2024 |
| | | */ |
| | | public class Factory implements JarScannerListener { |
| | | public class Factory implements ScannerListener { |
| | | |
| | | public Factory() { |
| | | listeners = new ArrayList<>(); |
| | |
| | | |
| | | if (packageNames == null) { |
| | | packageNames = d.actorPackages; |
| | | } |
| | | addContexts(starter, d, server, sd.contexts, packageNames, sdp); |
| | | } |
| | | addContexts(new Scanner(starter, Actor.class), d, server, sd.contexts, packageNames, sdp); |
| | | |
| | | server.setExecutor(Executors.newFixedThreadPool(10)); |
| | | server.start(); |
| | |
| | | return auth; |
| | | } |
| | | |
| | | private void addContexts(Class c, NeonDescriptor d, HttpServer server, List contextList, List<String> packageNames, |
| | | private void addContexts(Scanner scn, NeonDescriptor d, HttpServer server, List contextList, List<String> packageNames, |
| | | List<DataProvider> sdp) |
| | | throws ClassNotFoundException, NoSuchMethodException, InstantiationException, |
| | | IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException { |
| | |
| | | ctxAttrs.putAll(cd.attributes); |
| | | if (h instanceof Handler) { |
| | | for (String packageName : packageNames) { |
| | | wireActors(c, |
| | | packageName, Actor.class, (Handler) h, |
| | | wireActors(scn, |
| | | packageName, (Handler) h, |
| | | cd.attributes.get("contextName")); |
| | | ctx.getAttributes().put("serverDataProviderList", sdp); |
| | | } |
| | |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | private void wireActors(Class c, String packageName, Class annotation, Handler h, String contextName) { |
| | | JarScanner js = new JarScanner(c, annotation); |
| | | if (!js.isJar()) { |
| | | ClassLoader cl = c.getClassLoader(); |
| | | InputStream stream = cl |
| | | .getResourceAsStream(packageName.replaceAll("[.]", "/")); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); |
| | | Iterator i = reader.lines().iterator(); |
| | | while (i.hasNext()) { |
| | | String line = i.next().toString(); |
| | | if (line.endsWith(".class")) { |
| | | try { |
| | | Class actorClass = cl.loadClass(packageName + "." |
| | | + line.substring(0, line.lastIndexOf('.'))); |
| | | if (actorClass != null && actorClass.isAnnotationPresent(annotation)) { |
| | | wire(h, actorClass, contextName); |
| | | } |
| | | } catch (ClassNotFoundException ex) { |
| | | // Klasse nicht gefunden. Muss das geloggt oder sonstwie behandel werden? |
| | | } |
| | | } else { |
| | | wireActors(c, packageName + "." + line, annotation, h, contextName); |
| | | } |
| | | } |
| | | private void wireActors(Scanner scn, String packageName, /*Class annotation, */Handler h, String contextName) { |
| | | if (!scn.isJar()) { |
| | | scn.processClasses(this, packageName, h, contextName); |
| | | } else { |
| | | js.processZipContent(packageName, this, h, contextName); |
| | | scn.processZipContent(packageName, this, h, contextName); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /* -------------- JarScannerListener Implementierung --------------- */ |
| | | /* -------------- ScannerListener Implementierung --------------- */ |
| | | @Override |
| | | public void annotationFound(Class foundClass, Handler h, String contextName) { |
| | | wire(h, foundClass, contextName); |