JAMES-1759 Optional load of WebAdmin server in Guice deployement It is based on configuration
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1ab41945 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1ab41945 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1ab41945 Branch: refs/heads/master Commit: 1ab41945eb078c87d97009d7b31eea8b18767a7e Parents: 3816200 Author: Benoit Tellier <[email protected]> Authored: Fri Jun 17 11:15:23 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Wed Jun 22 15:34:19 2016 +0700 ---------------------------------------------------------------------- server/container/guice/guice-common/pom.xml | 4 + .../apache/james/modules/ProtocolsModule.java | 2 + .../modules/server/WebAdminServerModule.java | 111 +++++++++++++++++++ server/pom.xml | 5 + .../java/org/apache/james/webadmin/Routes.java | 4 +- .../apache/james/webadmin/WebAdminServer.java | 34 +++--- 6 files changed, 146 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/1ab41945/server/container/guice/guice-common/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/pom.xml b/server/container/guice/guice-common/pom.xml index b3109c9..72b7188 100644 --- a/server/container/guice/guice-common/pom.xml +++ b/server/container/guice/guice-common/pom.xml @@ -226,6 +226,10 @@ <artifactId>james-server-queue-activemq</artifactId> </dependency> <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-webadmin</artifactId> + </dependency> + <dependency> <groupId>org.apache.james.protocols</groupId> <artifactId>protocols-imap</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/1ab41945/server/container/guice/guice-common/src/main/java/org/apache/james/modules/ProtocolsModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/ProtocolsModule.java b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/ProtocolsModule.java index eca1926..c21391d 100644 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/ProtocolsModule.java +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/ProtocolsModule.java @@ -25,6 +25,7 @@ import org.apache.james.modules.protocols.ManageSieveServerModule; import org.apache.james.modules.protocols.POP3ServerModule; import org.apache.james.modules.protocols.ProtocolHandlerModule; import org.apache.james.modules.protocols.SMTPServerModule; +import org.apache.james.modules.server.WebAdminServerModule; import com.google.inject.AbstractModule; @@ -39,6 +40,7 @@ public class ProtocolsModule extends AbstractModule { install(new SMTPServerModule()); install(new LMTPServerModule()); install(new ManageSieveServerModule()); + install(new WebAdminServerModule()); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/1ab41945/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java new file mode 100644 index 0000000..4e88950 --- /dev/null +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java @@ -0,0 +1,111 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.modules.server; + +import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; +import static org.apache.james.webadmin.WebAdminServer.WEBADMIN_ENABLED; +import static org.apache.james.webadmin.WebAdminServer.WEBADMIN_PORT; + +import java.io.FileNotFoundException; +import java.util.List; +import java.util.Set; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.james.lifecycle.api.Configurable; +import org.apache.james.utils.ConfigurationPerformer; +import org.apache.james.utils.PropertiesProvider; +import org.apache.james.webadmin.FixedPort; +import org.apache.james.webadmin.Port; +import org.apache.james.webadmin.Routes; +import org.apache.james.webadmin.WebAdminServer; +import org.apache.james.webadmin.routes.DomainRoutes; +import org.apache.james.webadmin.routes.UserRoutes; +import org.apache.james.webadmin.utils.JsonTransformer; + +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; +import com.google.inject.AbstractModule; +import com.google.inject.Inject; +import com.google.inject.Provides; +import com.google.inject.Scopes; +import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; +import com.google.inject.name.Named; + +public class WebAdminServerModule extends AbstractModule { + + @Override + protected void configure() { + bind(JsonTransformer.class).in(Scopes.SINGLETON); + bind(WebAdminServer.class).in(Scopes.SINGLETON); + + Multibinder<Routes> routesMultibinder = Multibinder.newSetBinder(binder(), Routes.class); + routesMultibinder.addBinding().to(DomainRoutes.class); + routesMultibinder.addBinding().to(UserRoutes.class); + + Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(WebAdminServerModuleConfigurationPerformer.class); + } + + @Provides + @Named(WEBADMIN_PORT) + public Port provideWebAdminPort(PropertiesProvider propertiesProvider) throws Exception { + try { + return new FixedPort(propertiesProvider.getConfiguration("webadmin").getInt("port", WebAdminServer.DEFAULT_PORT)); + } catch (FileNotFoundException e) { + return new FixedPort(WebAdminServer.DEFAULT_PORT); + } + } + + @Provides + @Named(WEBADMIN_ENABLED) + public boolean provideWebAdminEnabled(PropertiesProvider propertiesProvider) throws Exception { + try { + return propertiesProvider.getConfiguration("webadmin").getBoolean("enabled", false); + } catch (FileNotFoundException e) { + return false; + } + } + + @Singleton + public static class WebAdminServerModuleConfigurationPerformer implements ConfigurationPerformer { + + private final WebAdminServer webAdminServer; + + @Inject + public WebAdminServerModuleConfigurationPerformer(WebAdminServer webAdminServer) { + this.webAdminServer = webAdminServer; + } + + @Override + public void initModule() { + try { + webAdminServer.configure(NO_CONFIGURATION); + } catch (ConfigurationException e) { + throw Throwables.propagate(e); + } + } + + @Override + public List<Class<? extends Configurable>> forClasses() { + return ImmutableList.of(WebAdminServer.class); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/1ab41945/server/pom.xml ---------------------------------------------------------------------- diff --git a/server/pom.xml b/server/pom.xml index f4cd352..c33c7c1 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -606,6 +606,11 @@ <version>${project.version}</version> </dependency> <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-webadmin</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>org.apache.james.protocols</groupId> <artifactId>protocols-smtp</artifactId> <version>${protocols.version}</version> http://git-wip-us.apache.org/repos/asf/james-project/blob/1ab41945/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Routes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Routes.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Routes.java index b8fe35c..bc8554a 100644 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Routes.java +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Routes.java @@ -19,8 +19,10 @@ package org.apache.james.webadmin; +import spark.Service; + public interface Routes { - void define(); + void define(Service service); } http://git-wip-us.apache.org/repos/asf/james-project/blob/1ab41945/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java index 307dc24..7ca5499 100644 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java @@ -19,10 +19,6 @@ package org.apache.james.webadmin; -import static spark.Spark.awaitInitialization; -import static spark.Spark.port; -import static spark.Spark.stop; - import java.io.IOException; import java.net.ServerSocket; import java.util.Set; @@ -40,14 +36,20 @@ import org.slf4j.LoggerFactory; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableSet; +import spark.Service; + public class WebAdminServer implements Configurable { private static final Logger LOGGER = LoggerFactory.getLogger(WebAdminServer.class); public static final HierarchicalConfiguration NO_CONFIGURATION = null; public static final String WEBADMIN_PORT = "webadmin_port"; + public static final String WEBADMIN_ENABLED = "webadmin_enabled"; + public static final int DEFAULT_PORT = 8080; private final int port; private final Set<Routes> routesList; + private final boolean enabled; + private final Service service; // Spark do not allow to retrieve allocated port when using a random port. Thus we generate the port. public static int findFreePort() throws IOException { @@ -57,31 +59,37 @@ public class WebAdminServer implements Configurable { } @Inject - @VisibleForTesting - public WebAdminServer(@Named(WEBADMIN_PORT)int port, Set<Routes> routesList) { + private WebAdminServer(@Named(WEBADMIN_ENABLED) boolean enabled, @Named(WEBADMIN_PORT)int port, Set<Routes> routesList) { this.port = port; this.routesList = routesList; + this.enabled = enabled; + this.service = Service.ignite(); } + @VisibleForTesting public WebAdminServer(Routes... routes) throws IOException { - this(findFreePort(), ImmutableSet.copyOf(routes)); + this(true, findFreePort(), ImmutableSet.copyOf(routes)); } @Override public void configure(HierarchicalConfiguration config) throws ConfigurationException { - port(port); - routesList.forEach(Routes::define); - LOGGER.info("Web admin server started"); + if (enabled) { + service.port(port); + routesList.forEach(routes -> routes.define(service)); + LOGGER.info("Web admin server started"); + } } @PreDestroy public void destroy() { - stop(); - LOGGER.info("Web admin server stopped"); + if (enabled) { + service.stop(); + LOGGER.info("Web admin server stopped"); + } } public void await() { - awaitInitialization(); + service.awaitInitialization(); } public int getPort() { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
