From bb1caf716a18b65fa42d7bed2369b66a0837d977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 22 Mar 2017 15:43:26 +0100 Subject: java : vaadin : use my own VaadinJettyServer to run my own VaadinServlet --- java/vaadin/ivy.xml | 2 +- java/vaadin/src/run/java/ch/asynk/Main.java | 46 +++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/java/vaadin/ivy.xml b/java/vaadin/ivy.xml index ad13fce..b1a4b3b 100644 --- a/java/vaadin/ivy.xml +++ b/java/vaadin/ivy.xml @@ -21,6 +21,6 @@ - + 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 servletClass, Class 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 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(); } } -- cgit v1.1-2-g2b99