From 24ad39eec2a0fdca1ef9768de0b92e45a34c1a24 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Tue, 03 Dec 2024 13:35:37 +0000
Subject: [PATCH] Anwendungsspezifische Parameter im Scanner auf generisches Object-Array umgestellt 

---
 src/de/uhilger/neon/Factory.java |   63 +++++++++++--------------------
 1 files changed, 22 insertions(+), 41 deletions(-)

diff --git a/src/de/uhilger/neon/Factory.java b/src/de/uhilger/neon/Factory.java
index 48ceb6d..3748d5b 100644
--- a/src/de/uhilger/neon/Factory.java
+++ b/src/de/uhilger/neon/Factory.java
@@ -23,7 +23,6 @@
 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;
@@ -48,6 +47,7 @@
 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
@@ -65,7 +65,7 @@
  * @author Ulrich Hilger
  * @version 1, 6.2.2024
  */
-public class Factory implements JarScannerListener {
+public class Factory implements ScannerListener {
 
   public Factory() {
     listeners = new ArrayList<>();
@@ -138,8 +138,8 @@
 
       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();
@@ -167,7 +167,7 @@
     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 {
@@ -187,9 +187,7 @@
         ctxAttrs.putAll(cd.attributes);
         if (h instanceof Handler) {
           for (String packageName : packageNames) {
-            wireActors(c,
-                    packageName, Actor.class, (Handler) h,
-                    cd.attributes.get("contextName"));
+            scn.process(this, packageName, (Handler) h, cd.attributes.get("contextName"));
             ctx.getAttributes().put("serverDataProviderList", sdp);
           }
         }
@@ -262,36 +260,6 @@
             | InvocationTargetException ex) {
       // Klasse nicht gefunden. Muss das geloggt oder sonstwie behandel werden?
       return null;
-    }
-  }
-
-  @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);
-        }
-      }
-    } else {
-      js.processZipContent(packageName, this, h, contextName);
     }
   }
 
@@ -427,9 +395,22 @@
     }
   }
 
-  /* -------------- JarScannerListener Implementierung --------------- */
+  /* -------------- ScannerListener Implementierung --------------- */
   @Override
-  public void annotationFound(Class foundClass, Handler h, String contextName) {
-    wire(h, foundClass, contextName);
+  public void annotationFound(Class foundClass, Object[] params) {
+    Handler h = null;
+    String contextName = null;
+    for(Object param : params) {
+      if(param instanceof Handler) {
+        h = (Handler) param;
+      } else if(param instanceof String) {
+        contextName = (String) param;
+      }
+    }
+    if(h == null || contextName == null) {
+      Logger.getLogger(Factory.class.getName()).log(Level.FINER, "Handler oder contextName ist null");
+    } else {
+      wire(h, foundClass, contextName);
+    }
   }
 }
\ No newline at end of file

--
Gitblit v1.9.3