Added reflections to load pages automatically
This commit is contained in:
parent
e48c318e42
commit
495ae68e82
3 changed files with 32 additions and 7 deletions
7
pom.xml
7
pom.xml
|
@ -40,7 +40,12 @@
|
|||
<artifactId>commons-io</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.9.10</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
|
|
|
@ -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<Class<? extends Page>> pages = rf.getSubTypesOf(Page.class);
|
||||
for (Class<? extends Page> 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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue