You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/06/26 09:13:12 UTC

[16/20] james-project git commit: JAMES-2439 Extract configuration management from JMXServer

JAMES-2439 Extract configuration management from JMXServer


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3c3c8fc6
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3c3c8fc6
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3c3c8fc6

Branch: refs/heads/master
Commit: 3c3c8fc6bf247727a6f3311e0626f389ccd8b51f
Parents: 81db3c2
Author: benwa <bt...@linagora.com>
Authored: Mon Jun 25 11:42:02 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue Jun 26 16:10:41 2018 +0700

----------------------------------------------------------------------
 server/container/guice/jmx/pom.xml              | 20 ++++++
 .../apache/james/modules/server/JMXServer.java  | 35 +++------
 .../james/modules/server/JMXServerModule.java   | 19 +++++
 .../james/modules/server/JmxConfiguration.java  | 75 ++++++++++++++++++++
 .../modules/server/JmxConfigurationTest.java    | 55 ++++++++++++++
 5 files changed, 177 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/3c3c8fc6/server/container/guice/jmx/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/jmx/pom.xml b/server/container/guice/jmx/pom.xml
index df9326f..aa1190f 100644
--- a/server/container/guice/jmx/pom.xml
+++ b/server/container/guice/jmx/pom.xml
@@ -77,6 +77,26 @@
             <artifactId>guice-multibindings</artifactId>
         </dependency>
         <dependency>
+            <groupId>nl.jqno.equalsverifier</groupId>
+            <artifactId>equalsverifier</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.platform</groupId>
+            <artifactId>junit-platform-launcher</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c3c8fc6/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServer.java
----------------------------------------------------------------------
diff --git a/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServer.java b/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServer.java
index 9fc69fe..130b572 100644
--- a/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServer.java
+++ b/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServer.java
@@ -19,7 +19,6 @@
 
 package org.apache.james.modules.server;
 
-import java.io.FileNotFoundException;
 import java.lang.management.ManagementFactory;
 import java.net.ServerSocket;
 import java.rmi.registry.LocateRegistry;
@@ -35,21 +34,13 @@ import javax.management.remote.JMXConnectorServer;
 import javax.management.remote.JMXConnectorServerFactory;
 import javax.management.remote.JMXServiceURL;
 
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.james.util.RestrictingRMISocketFactory;
-import org.apache.james.utils.PropertiesProvider;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import com.github.fge.lambdas.Throwing;
 import com.google.common.collect.ImmutableMap;
 
 public class JMXServer {
-
-    private static final Logger LOGGER = LoggerFactory.getLogger(JMXServer.class);
-
-    private final PropertiesProvider propertiesProvider;
+    private final JmxConfiguration jmxConfiguration;
     private final Set<String> registeredKeys;
     private final Object lock;
     private JMXConnectorServer jmxConnectorServer;
@@ -57,8 +48,8 @@ public class JMXServer {
     private RestrictingRMISocketFactory restrictingRMISocketFactory;
 
     @Inject
-    public JMXServer(PropertiesProvider propertiesProvider) {
-        this.propertiesProvider = propertiesProvider;
+    public JMXServer(JmxConfiguration jmxConfiguration) {
+        this.jmxConfiguration = jmxConfiguration;
         isStarted = false;
         registeredKeys = new HashSet<>();
         lock = new Object();
@@ -94,12 +85,11 @@ public class JMXServer {
 
     private void doStart() {
         try {
-            PropertiesConfiguration configuration = getPropertiesConfiguration();
-            String address = configuration.getString("jmx.address", "localhost");
-            int port = configuration.getInt("jmx.port", 9999);
-            String serviceURL = "service:jmx:rmi://" + address + "/jndi/rmi://" + address + ":" + port + "/jmxrmi";
-            restrictingRMISocketFactory = new RestrictingRMISocketFactory(address);
-            LocateRegistry.createRegistry(port, restrictingRMISocketFactory, restrictingRMISocketFactory);
+            String serviceURL = "service:jmx:rmi://" + jmxConfiguration.getHost().getHostName()
+                + "/jndi/rmi://" + jmxConfiguration.getHost().getHostName()
+                + ":" + jmxConfiguration.getHost().getPort() + "/jmxrmi";
+            restrictingRMISocketFactory = new RestrictingRMISocketFactory(jmxConfiguration.getHost().getHostName());
+            LocateRegistry.createRegistry(jmxConfiguration.getHost().getPort(), restrictingRMISocketFactory, restrictingRMISocketFactory);
 
             Map<String, ?> environment = ImmutableMap.of();
             jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(new JMXServiceURL(serviceURL),
@@ -112,15 +102,6 @@ public class JMXServer {
         }
     }
 
-    private PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException {
-        try {
-            return propertiesProvider.getConfiguration("jmx");
-        } catch (FileNotFoundException e) {
-            LOGGER.warn("Could not locate configuration file for JMX. Defaults to rmi://127.0.0.1:9999");
-            return new PropertiesConfiguration();
-        }
-    }
-
     private void doStop() {
         try {
             MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c3c8fc6/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServerModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServerModule.java b/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServerModule.java
index ca6938f..92105a5 100644
--- a/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServerModule.java
+++ b/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JMXServerModule.java
@@ -19,8 +19,10 @@
 
 package org.apache.james.modules.server;
 
+import java.io.FileNotFoundException;
 import java.util.List;
 
+import org.apache.commons.configuration.ConfigurationException;
 import org.apache.james.adapter.mailbox.MailboxCopierManagement;
 import org.apache.james.adapter.mailbox.MailboxCopierManagementMBean;
 import org.apache.james.adapter.mailbox.MailboxManagerManagement;
@@ -47,10 +49,14 @@ import org.apache.james.user.api.UsersRepositoryManagementMBean;
 import org.apache.james.user.lib.UsersRepositoryManagement;
 import org.apache.james.utils.ConfigurationPerformer;
 import org.apache.james.utils.GuiceMailboxManagerResolver;
+import org.apache.james.utils.PropertiesProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 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;
@@ -58,6 +64,8 @@ import com.google.inject.name.Names;
 
 public class JMXServerModule extends AbstractModule {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(JMXServerModule.class);
+
     private static final String JMX_COMPONENT_DOMAINLIST = "org.apache.james:type=component,name=domainlist";
     private static final String JMX_COMPONENT_USERS_REPOSITORY = "org.apache.james:type=component,name=usersrepository";
     private static final String JMX_COMPONENT_RECIPIENTREWRITETABLE = "org.apache.james:type=component,name=recipientrewritetable";
@@ -94,6 +102,17 @@ public class JMXServerModule extends AbstractModule {
         configurationMultibinder.addBinding().to(JMXModuleConfigurationPerformer.class);
     }
 
+    @Provides
+    @Singleton
+    public JmxConfiguration provideConfiguration(PropertiesProvider propertiesProvider) throws ConfigurationException {
+        try {
+            return JmxConfiguration.fromProperties(propertiesProvider.getConfiguration("jmx"));
+        } catch (FileNotFoundException e) {
+            LOGGER.warn("Could not locate configuration file for JMX. Defaults to rmi://127.0.0.1:9999");
+            return JmxConfiguration.DEFAULT_CONFIGURATION;
+        }
+    }
+
     @Singleton
     public static class JMXModuleConfigurationPerformer implements ConfigurationPerformer {
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c3c8fc6/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JmxConfiguration.java
----------------------------------------------------------------------
diff --git a/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JmxConfiguration.java b/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JmxConfiguration.java
new file mode 100644
index 0000000..793e930
--- /dev/null
+++ b/server/container/guice/jmx/src/main/java/org/apache/james/modules/server/JmxConfiguration.java
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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 java.util.Objects;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.james.util.Host;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
+
+public class JmxConfiguration {
+
+    public static final String LOCALHOST = "localhost";
+    public static final int DEFAULT_PORT = 9999;
+
+    public static final JmxConfiguration DEFAULT_CONFIGURATION = new JmxConfiguration(Host.from(LOCALHOST, DEFAULT_PORT));
+
+    public static JmxConfiguration fromProperties(PropertiesConfiguration configuration) {
+        String address = configuration.getString("jmx.address", LOCALHOST);
+        int port = configuration.getInt("jmx.port", DEFAULT_PORT);
+        return new JmxConfiguration(Host.from(address, port));
+    }
+
+    private final Host host;
+
+    @VisibleForTesting
+    JmxConfiguration(Host host) {
+        this.host = host;
+    }
+
+    public Host getHost() {
+        return host;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof JmxConfiguration) {
+            JmxConfiguration that = (JmxConfiguration) o;
+
+            return Objects.equals(this.host, that.host);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(host);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+            .add("host", host)
+            .toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c3c8fc6/server/container/guice/jmx/src/test/java/org/apache/james/modules/server/JmxConfigurationTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/jmx/src/test/java/org/apache/james/modules/server/JmxConfigurationTest.java b/server/container/guice/jmx/src/test/java/org/apache/james/modules/server/JmxConfigurationTest.java
new file mode 100644
index 0000000..a727b58
--- /dev/null
+++ b/server/container/guice/jmx/src/test/java/org/apache/james/modules/server/JmxConfigurationTest.java
@@ -0,0 +1,55 @@
+/****************************************************************
+ * 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.assertj.core.api.Assertions.assertThat;
+
+import java.io.StringReader;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.james.util.Host;
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+public class JmxConfigurationTest {
+    @Test
+    void shouldMatchBeanContract() {
+        EqualsVerifier.forClass(JmxConfiguration.class)
+            .verify();
+    }
+
+    @Test
+    void fromPropertiesShouldReturnDefaultWhenEmpty() {
+        assertThat(JmxConfiguration.fromProperties(new PropertiesConfiguration()))
+            .isEqualTo(JmxConfiguration.DEFAULT_CONFIGURATION);
+    }
+
+    @Test
+    void fromPropertiesShouldReturnConfiguredValues() throws Exception {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.load(new StringReader(
+                "jmx.address=172.0.0.5\n" +
+                "jmx.port=889\n"));
+
+        assertThat(JmxConfiguration.fromProperties(configuration))
+            .isEqualTo(new JmxConfiguration(Host.from("172.0.0.5", 889)));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org