Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Select server to create dynamically to avoid classpath issue.
Load server classes dynamically to avoid `NoClassDefFoundError`s. Select Simple or Undertow depending on which one is available on the classpath. Closes #53
- Loading branch information
1 parent
e05e515
commit 3f93810
Showing
4 changed files
with
88 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.vtence.molecule.servers; | ||
|
||
import com.vtence.molecule.Server; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
public final class Servers { | ||
|
||
private static final List<String> supported = asList("Simple", "Undertow"); | ||
|
||
private static final List<? extends Class<? extends Server>> available = supported.stream() | ||
.map(Servers::loadClass) | ||
.filter(Objects::nonNull) | ||
.collect(toList()); | ||
|
||
private static class NoneAvailableException extends RuntimeException { | ||
public String getMessage() { | ||
return "No server implementation is available. Add Simple or Undertow jars to your classpath."; | ||
} | ||
} | ||
|
||
private static class CreationFailed extends RuntimeException { | ||
private final Class<? extends Server> clazz; | ||
|
||
public CreationFailed(Class<? extends Server> clazz, Throwable cause) { | ||
super(cause); | ||
this.clazz = clazz; | ||
} | ||
|
||
public String getMessage() { | ||
return "Failed to create " + clazz.getSimpleName(); | ||
} | ||
} | ||
|
||
private Servers() {} | ||
|
||
public static Server create(String host, int port) { | ||
return available.stream() | ||
.map(server -> instantiate(server, host, port)) | ||
.findFirst() | ||
.orElseThrow(NoneAvailableException::new); | ||
} | ||
|
||
private static Server instantiate(Class<? extends Server> clazz, String host, int port) { | ||
try { | ||
Constructor<? extends Server> constructor = clazz.getDeclaredConstructor(String.class, Integer.TYPE); | ||
return constructor.newInstance(host, port); | ||
} catch (Exception e) { | ||
throw new CreationFailed(clazz, e); | ||
} | ||
} | ||
|
||
private static Class<? extends Server> loadClass(String type) { | ||
try { | ||
return Class.forName(className(type)).asSubclass(Server.class); | ||
} catch (ClassNotFoundException | NoClassDefFoundError notAvailable) { | ||
return null; | ||
} | ||
} | ||
|
||
private static String className(String type) { | ||
return Servers.class.getPackage().getName() + "." + type + "Server"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters