diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2017-03-22 15:43:26 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2017-03-22 15:43:26 +0100 |
commit | bb1caf716a18b65fa42d7bed2369b66a0837d977 (patch) | |
tree | 2c53f175ac19f5aec1c5e003b21a2fa1fddeb7bf /java/vaadin/src/run | |
parent | 6437ef7aff1291f74ddbcf0ab04aa0a7e346a591 (diff) | |
download | share-bb1caf716a18b65fa42d7bed2369b66a0837d977.zip share-bb1caf716a18b65fa42d7bed2369b66a0837d977.tar.gz |
java : vaadin : use my own VaadinJettyServer to run my own VaadinServlet
Diffstat (limited to 'java/vaadin/src/run')
-rw-r--r-- | java/vaadin/src/run/java/ch/asynk/Main.java | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/java/vaadin/src/run/java/ch/asynk/Main.java b/java/vaadin/src/run/java/ch/asynk/Main.java index cff6ee9..e0aeb15 100644 --- a/java/vaadin/src/run/java/ch/asynk/Main.java +++ b/java/vaadin/src/run/java/ch/asynk/Main.java @@ -1,14 +1,54 @@ package ch.asynk; -import org.vaadin.jetty.VaadinJettyServer; +import com.vaadin.server.DefaultUIProvider; +import com.vaadin.server.UIProvider; +import com.vaadin.server.VaadinServlet; +import com.vaadin.ui.UI; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.webapp.WebAppContext; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +class MyVaadinJettyServer extends Server +{ + public MyVaadinJettyServer(int port, Class<? extends VaadinServlet> servletClass, Class<? extends UI> uiClass, String webappDirectory) + throws IOException, InstantiationException, IllegalAccessException + { + super(port); + + createIfDoesntExists(webappDirectory); + WebAppContext context = new WebAppContext(webappDirectory, "/"); + context.addServlet(buildVaadinServlet(servletClass.newInstance(), uiClass), "/*"); + setHandler(context); + } + + private ServletHolder buildVaadinServlet(VaadinServlet servlet, Class<? extends UI> uiClass) + { + ServletHolder servletHolder = new ServletHolder(servlet); + servletHolder.setInitParameter(VaadinServlet.SERVLET_PARAMETER_UI_PROVIDER, DefaultUIProvider.class.getName()); + servletHolder.setInitParameter("UI", uiClass.getName()); + return servletHolder; + } + + private void createIfDoesntExists(String directory) throws IOException + { + Path path = Paths.get(directory); + if (!Files.exists(path)) { + Files.createDirectory(path); + } + } +} public class Main { public static void main(String[] args) throws Exception { - System.err.println("!!! org.vaadin.jetty.VaadinJettyServer uses it's own VaadinServlet instance instead of HelloWorldServlet.\n"); String webRoot = System.getProperty("WEBROOT"); if (webRoot == null) webRoot = "./src/main/WebContent/"; - new VaadinJettyServer(8080, HelloWorld.class, webRoot).start(); + new MyVaadinJettyServer(8081, HelloWorldServlet.class, HelloWorld.class, webRoot).start(); } } |