All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.factoryfx.jetty.JettyServerFactory Maven / Gradle / Ivy

package io.github.factoryfx.jetty;

import io.github.factoryfx.factory.FactoryBase;
import io.github.factoryfx.factory.attribute.dependency.FactoryAttribute;
import io.github.factoryfx.factory.attribute.dependency.FactoryListAttribute;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *  usage example.
 *
 *  
{@code
 *       public class SimpleHttpServer extends SimpleFactoryBase {
 *
 *           public final FactoryAttribute> server = new FactoryAttribute<>(JettyServerFactory.class);
 *
 *           {@literal @}Override
 *           public Server createImpl() {
 *               return server.instance();
 *           }
 *       }
 *
 *       builder.addFactory(JettyServerFactory.class, Scope.SINGLETON, ctx-> new JettyServerBuilder<>(new JettyServerFactory())
 *           .withHost("localhost").withPort(8005)
 *           .withResource(ctx.get(CustomResourceFactory.class)).build());
 *
 *  }
*/ public class JettyServerFactory> extends FactoryBase { private static final Logger jerseyLogger1 = Logger.getLogger(org.glassfish.jersey.internal.inject.Providers.class.getName()); private static final Logger jerseyLogger2 = Logger.getLogger(org.glassfish.jersey.internal.Errors.class.getName()); static { jerseyLogger1.setLevel(Level.SEVERE); //another useless warning https://github.com/jersey/jersey/issues/3700 jerseyLogger2.setLevel(Level.SEVERE);//warning about generic parameters, works fine and no fix available so the warnings are just useless } public final FactoryListAttribute> connectors = new FactoryListAttribute>().labelText("Connectors").userNotSelectable(); public final FactoryAttribute> handler = new FactoryAttribute>().labelText("Handler collection"); public JettyServerFactory(){ configLifeCycle().setCreator(this::createJetty); configLifeCycle().setUpdater(this::update); configLifeCycle().setStarter(this::start); configLifeCycle().setDestroyer(this::stop); config().setDisplayTextProvider(() -> "Microservice REST server"); } //api for customizing JettyServer creation protected Server createJetty() { Server server = new Server(); connectors.instances().forEach(httpServerConnector -> httpServerConnector.addToServer(server)); handler.instance().setServer(server); server.setHandler(handler.instance()); return server; } private void update(Server server){ for (Connector connector : server.getConnectors()) { server.removeConnector(connector); } for (HttpServerConnector connector : connectors.instances()) { connector.addToServer(server); } } /** * model navigation shortcut, only works with th e default setup form the builder * @param clazz resource clazz * @param resource type * @return resource */ public final T getResource(Class clazz){ return getDefaultJerseyServlet().resources.get(clazz); } /** * model navigation shortcut, only works with th e default setup form the builder * @param resource resource * @param resource type */ public final > void setResource(T resource){ JerseyServletFactory jerseyServletFactory = getDefaultJerseyServlet(); jerseyServletFactory.resources.removeIf(factoryBase -> factoryBase.getClass()==resource.getClass()); jerseyServletFactory.resources.add(resource); } /** * model navigation shortcut, only works with th e default setup form the builder * @param clazz servlet class * @param servlet type * @return servlet */ @SuppressWarnings("unchecked") public final T getServlet(Class clazz){ ServletContextHandlerFactory servletContextHandler = (ServletContextHandlerFactory) handler.get().handlers.get(GzipHandlerFactory.class).handler.get(); for (ServletAndPathFactory servletAndPath : servletContextHandler.updatableRootServlet.get().servletAndPaths) { if (servletAndPath.servlet.get().getClass()==clazz){ return (T)servletAndPath.servlet.get(); } } return null; } public final void clearResource(Class resource){ getDefaultJerseyServlet().resources.removeIf(factoryBase -> factoryBase.getClass()==resource); } @SuppressWarnings("unchecked") private JerseyServletFactory getDefaultJerseyServlet() { ServletContextHandlerFactory servletContextHandler = (ServletContextHandlerFactory) handler.get().handlers.get(GzipHandlerFactory.class).handler.get(); ServletAndPathFactory servletAndPathFactory = servletContextHandler.updatableRootServlet.get().servletAndPaths.get(0); return (JerseyServletFactory) servletAndPathFactory.servlet.get(); } public void start(Server server) throws Error { try { server.start(); } catch (Exception e) { throw new RuntimeException(e); } } public void stop(Server server) { try { if (Thread.interrupted()) throw new RuntimeException("Interrupted"); server.setStopTimeout(1L); server.stop(); //because stop call can be inside this one of jetty's threads, we need to clear the interrupt //to let the rest of the factory updates run. They might be interrupt-sensitive Thread.interrupted(); } catch (Exception e) { throw new RuntimeException(e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy