diff --git a/pom.xml b/pom.xml index 19ad584..45974d1 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,12 @@ commons-io 2.5 - + + org.reflections + reflections + 0.9.10 + runtime + 1.8 diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 6f42b26..1ddddb4 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -1,7 +1,14 @@ package io.github.norbipeti.chat.server; +import java.lang.reflect.Modifier; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.Set; + +import org.reflections.Reflections; +import org.reflections.scanners.SubTypesScanner; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; import com.sun.net.httpserver.HttpServer; @@ -22,9 +29,23 @@ public class Main { } System.out.println("Starting webserver..."); HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); - addPage(server, new IndexPage()); - addPage(server, new RegisterPage()); - addPage(server, new LoginPage()); + Reflections rf = new Reflections( + new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(Page.class.getClassLoader())) + .addClassLoader(Page.class.getClassLoader()).addScanners(new SubTypesScanner()) + .filterInputsBy((String pkg) -> pkg.contains("io.github.norbipeti.chat.server.page"))); + Set> pages = rf.getSubTypesOf(Page.class); + for (Class page : pages) { + try { + if (Modifier.isAbstract(page.getModifiers())) + continue; + Page p = page.newInstance(); + addPage(server, p); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } server.start(); System.out.println("Ready... Press Enter to stop."); System.in.read(); diff --git a/src/io/github/norbipeti/chat/server/page/Page.java b/src/io/github/norbipeti/chat/server/page/Page.java index 1132045..b300ebf 100644 --- a/src/io/github/norbipeti/chat/server/page/Page.java +++ b/src/io/github/norbipeti/chat/server/page/Page.java @@ -22,11 +22,10 @@ public abstract class Page implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { - if (exchange.getRequestURI().getPath().equals("/" + GetName())) + if (!getDo404() || exchange.getRequestURI().getPath().equals("/" + GetName())) handlePage(exchange); else { - if (!IOHelper.SendPage(404, NotFoundPage.Instance, exchange)) - ; + IOHelper.SendPage(404, NotFoundPage.Instance, exchange); } }