Author: matthieu Date: Tue Nov 3 16:49:32 2015 New Revision: 1712336 URL: http://svn.apache.org/viewvc?rev=1712336&view=rev Log: JAMES-1626 Add JMX support to cassandra-guice application
Added: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServer.java james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServerModule.java james/project/trunk/server/container/cassandra-guice/src/test/resources/jmx.properties Modified: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java james/project/trunk/server/container/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java Modified: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java?rev=1712336&r1=1712335&r2=1712336&view=diff ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java (original) +++ james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java Tue Nov 3 16:49:32 2015 @@ -34,6 +34,7 @@ import org.apache.james.modules.server.A import org.apache.james.modules.server.ConfigurationPerformerModule; import org.apache.james.modules.server.DNSServiceModule; import org.apache.james.modules.server.FileSystemModule; +import org.apache.james.modules.server.JMXServerModule; import org.apache.james.modules.server.QuotaModule; import com.google.inject.Module; @@ -60,7 +61,9 @@ public class CassandraJamesServerMain { new FileSystemModule()); public static void main(String[] args) throws Exception { - CassandraJamesServer server = new CassandraJamesServer(defaultModule); + CassandraJamesServer server = new CassandraJamesServer(Modules.combine( + defaultModule, + new JMXServerModule())); server.start(); } Added: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServer.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServer.java?rev=1712336&view=auto ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServer.java (added) +++ james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServer.java Tue Nov 3 16:49:32 2015 @@ -0,0 +1,118 @@ +/**************************************************************** + * 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 com.google.common.base.Throwables; +import com.google.common.collect.ImmutableMap; +import org.apache.james.util.RestrictingRMISocketFactory; +import org.apache.james.utils.PropertiesReader; + +import javax.annotation.PreDestroy; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import javax.management.remote.JMXConnectorServer; +import javax.management.remote.JMXConnectorServerFactory; +import javax.management.remote.JMXServiceURL; +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.rmi.registry.LocateRegistry; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class JMXServer { + + private final Set<String> registeredKeys; + private final Object lock; + private JMXConnectorServer jmxConnectorServer; + private boolean isStarted; + + public JMXServer() { + isStarted = false; + registeredKeys = new HashSet<>(); + lock = new Object(); + } + + public void start() { + synchronized (lock) { + if (isStarted) { + return; + } + isStarted = true; + doStart(); + } + } + + @PreDestroy + public void stop() { + synchronized (lock) { + if (!isStarted) { + return; + } + isStarted = false; + doStop(); + } + } + + public void register(String key, Object remote) throws Exception { + ManagementFactory.getPlatformMBeanServer().registerMBean(remote, new ObjectName(key)); + synchronized (lock) { + registeredKeys.add(key); + } + } + + private void doStart() { + PropertiesReader propertiesReader = new PropertiesReader("jmx.properties"); + String address = propertiesReader.getProperty("jmx.address"); + int port = Integer.parseInt(propertiesReader.getProperty("jmx.port")); + String serviceURL = "service:jmx:rmi://" + address + "/jndi/rmi://" + address+ ":" + port +"/jmxrmi"; + try { + RestrictingRMISocketFactory restrictingRMISocketFactory = new RestrictingRMISocketFactory(address); + LocateRegistry.createRegistry(port, restrictingRMISocketFactory, restrictingRMISocketFactory); + + Map<String, ?> environment = ImmutableMap.of(); + jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(new JMXServiceURL(serviceURL), + environment, + ManagementFactory.getPlatformMBeanServer()); + + jmxConnectorServer.start(); + } catch (Exception e) { + throw Throwables.propagate(e); + } + } + + private void doStop() { + try { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + registeredKeys.forEach(key -> { + try { + mBeanServer.unregisterMBean(new ObjectName(key)); + } catch (Exception e) { + throw Throwables.propagate(e); + } + }); + registeredKeys.clear(); + jmxConnectorServer.stop(); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } + +} Added: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServerModule.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServerModule.java?rev=1712336&view=auto ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServerModule.java (added) +++ james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/modules/server/JMXServerModule.java Tue Nov 3 16:49:32 2015 @@ -0,0 +1,114 @@ +/**************************************************************** + * 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 org.apache.james.adapter.mailbox.MailboxCopierManagement; +import org.apache.james.adapter.mailbox.MailboxCopierManagementMBean; +import org.apache.james.adapter.mailbox.MailboxManagerManagement; +import org.apache.james.adapter.mailbox.MailboxManagerManagementMBean; +import org.apache.james.adapter.mailbox.MailboxManagerResolver; +import org.apache.james.domainlist.api.DomainListManagementMBean; +import org.apache.james.domainlist.lib.DomainListManagement; +import org.apache.james.mailbox.cassandra.CassandraMailboxManager; +import org.apache.james.mailbox.copier.MailboxCopier; +import org.apache.james.mailbox.copier.MailboxCopierImpl; +import org.apache.james.mailetcontainer.api.jmx.MailSpoolerMBean; +import org.apache.james.mailetcontainer.impl.JamesMailSpooler; +import org.apache.james.rrt.api.RecipientRewriteTableManagementMBean; +import org.apache.james.rrt.lib.RecipientRewriteTableManagement; +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.MailboxManagerDefinition; + +import com.google.inject.AbstractModule; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; +import com.google.inject.name.Names; + +public class JMXServerModule extends AbstractModule { + + 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"; + private static final String JMX_COMPONENT_NAME_MAILBOXMANAGERBEAN = "org.apache.james:type=component,name=mailboxmanagerbean"; + private static final String JMX_COMPONENT_MAILBOXCOPIER = "org.apache.james:type=component,name=mailboxcopier"; + + @Override + protected void configure() { + bind(MailboxCopier.class).annotatedWith(Names.named("mailboxcopier")).to(MailboxCopierImpl.class); + bind(MailboxCopierManagementMBean.class).to(MailboxCopierManagement.class); + bind(MailboxManagerResolver.class).to(GuiceMailboxManagerResolver.class); + bind(DomainListManagementMBean.class).to(DomainListManagement.class); + bind(UsersRepositoryManagementMBean.class).to(UsersRepositoryManagement.class); + bind(MailboxManagerManagementMBean.class).to(MailboxManagerManagement.class); + bind(RecipientRewriteTableManagementMBean.class).to(RecipientRewriteTableManagement.class); + bind(MailSpoolerMBean.class).to(JamesMailSpooler.class); + Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(JMXModuleConfigurationPerformer.class); + Multibinder.newSetBinder(binder(), MailboxManagerDefinition.class).addBinding().to(CassandraMailboxManagerDefinition.class); + } + + @Singleton + private static class CassandraMailboxManagerDefinition extends MailboxManagerDefinition { + @Inject + private CassandraMailboxManagerDefinition(CassandraMailboxManager manager) { + super("cassandra-mailboxmanager", manager); + } + } + + @Singleton + public static class JMXModuleConfigurationPerformer implements ConfigurationPerformer { + + private final JMXServer jmxServer; + private final DomainListManagementMBean domainListManagementMBean; + private final UsersRepositoryManagementMBean usersRepositoryManagementMBean; + private final RecipientRewriteTableManagementMBean recipientRewriteTableManagementMBean; + private final MailboxManagerManagementMBean mailboxManagerManagementMBean; + private final MailboxCopierManagementMBean mailboxCopierManagementMBean; + + @Inject + public JMXModuleConfigurationPerformer(JMXServer jmxServer, + DomainListManagementMBean domainListManagementMBean, + UsersRepositoryManagementMBean usersRepositoryManagementMBean, + RecipientRewriteTableManagementMBean recipientRewriteTableManagementMBean, + MailboxManagerManagementMBean mailboxManagerManagementMBean, + MailboxCopierManagementMBean mailboxCopierManagementMBean) { + this.jmxServer = jmxServer; + this.domainListManagementMBean = domainListManagementMBean; + this.usersRepositoryManagementMBean = usersRepositoryManagementMBean; + this.recipientRewriteTableManagementMBean = recipientRewriteTableManagementMBean; + this.mailboxManagerManagementMBean = mailboxManagerManagementMBean; + this.mailboxCopierManagementMBean = mailboxCopierManagementMBean; + } + + @Override + public void initModule() throws Exception { + jmxServer.start(); + jmxServer.register(JMX_COMPONENT_DOMAINLIST, domainListManagementMBean); + jmxServer.register(JMX_COMPONENT_USERS_REPOSITORY, usersRepositoryManagementMBean); + jmxServer.register(JMX_COMPONENT_RECIPIENTREWRITETABLE, recipientRewriteTableManagementMBean); + jmxServer.register(JMX_COMPONENT_NAME_MAILBOXMANAGERBEAN, mailboxManagerManagementMBean); + jmxServer.register(JMX_COMPONENT_MAILBOXCOPIER, mailboxCopierManagementMBean); + } + } + +} Added: james/project/trunk/server/container/cassandra-guice/src/test/resources/jmx.properties URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/test/resources/jmx.properties?rev=1712336&view=auto ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/test/resources/jmx.properties (added) +++ james/project/trunk/server/container/cassandra-guice/src/test/resources/jmx.properties Tue Nov 3 16:49:32 2015 @@ -0,0 +1,28 @@ +# 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. +# + +# This template file can be used as example for James Server configuration +# DO NOT USE IT AS SUCH AND ADAPT IT TO YOUR NEEDS + +# This template file can be used as example for James Server configuration +# DO NOT USE IT AS SUCH AND ADAPT IT TO YOUR NEEDS + +# See http://james.apache.org/server/3/config.html for usage + +jmx.address=127.0.0.1 +jmx.port=9999 Modified: james/project/trunk/server/container/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java?rev=1712336&r1=1712335&r2=1712336&view=diff ============================================================================== --- james/project/trunk/server/container/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java (original) +++ james/project/trunk/server/container/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java Tue Nov 3 16:49:32 2015 @@ -44,12 +44,12 @@ import org.apache.james.user.api.UsersRe public class JmxServerProbe implements ServerProbe { // TODO: Move this to somewhere else - private final static String DOMAINLIST_OBJECT_NAME = "org.apache.james:type=component,name=domainlist"; - private final static String VIRTUALUSERTABLE_OBJECT_NAME = "org.apache.james:type=component,name=recipientrewritetable"; - private final static String USERSREPOSITORY_OBJECT_NAME = "org.apache.james:type=component,name=usersrepository"; - private final static String MAILBOXCOPIER_OBJECT_NAME = "org.apache.james:type=component,name=mailboxcopier"; - private final static String MAILBOXMANAGER_OBJECT_NAME = "org.apache.james:type=component,name=mailboxmanagerbean"; - private final static String QUOTAMANAGER_OBJECT_NAME = "org.apache.james:type=component,name=quotamanagerbean"; + public final static String DOMAINLIST_OBJECT_NAME = "org.apache.james:type=component,name=domainlist"; + public final static String VIRTUALUSERTABLE_OBJECT_NAME = "org.apache.james:type=component,name=recipientrewritetable"; + public final static String USERSREPOSITORY_OBJECT_NAME = "org.apache.james:type=component,name=usersrepository"; + public final static String MAILBOXCOPIER_OBJECT_NAME = "org.apache.james:type=component,name=mailboxcopier"; + public final static String MAILBOXMANAGER_OBJECT_NAME = "org.apache.james:type=component,name=mailboxmanagerbean"; + public final static String QUOTAMANAGER_OBJECT_NAME = "org.apache.james:type=component,name=quotamanagerbean"; private JMXConnector jmxc; --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org