| | |
| | | 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, |
| | | cd.attributes.get("contextName")); |
| | | scn.process(this, packageName, (Handler) h, cd.attributes.get("contextName")); |
| | | ctx.getAttributes().put("serverDataProviderList", sdp); |
| | | } |
| | | } |
| | |
| | | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /* -------------- 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); |
| | | } |
| | | } |
| | | } |