/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed 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 com.proofpoint.jmx;

import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.proofpoint.configuration.ConfigurationModule;
import com.proofpoint.discovery.client.announce.ServiceAnnouncement;

import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;

import static com.proofpoint.discovery.client.DiscoveryBinder.discoveryBinder;
import static com.proofpoint.discovery.client.announce.ServiceAnnouncement.serviceAnnouncement;
import static org.weakref.jmx.guice.ExportBinder.newExporter;

public class JmxModule
        implements Module
{
    @Override
    public void configure(Binder binder)
    {
        binder.disableCircularProxies();

        binder.bind(MBeanServer.class).toInstance(ManagementFactory.getPlatformMBeanServer());
        binder.bind(JmxAgent.class).in(Scopes.SINGLETON);
        ConfigurationModule.bindConfig(binder).to(JmxConfig.class);

        newExporter(binder).export(StackTraceMBean.class).withGeneratedName();
        binder.bind(StackTraceMBean.class).in(Scopes.SINGLETON);

        discoveryBinder(binder).bindServiceAnnouncement(JmxAnnouncementProvider.class);
    }

    static class JmxAnnouncementProvider
            implements Provider<ServiceAnnouncement>
    {
        private JmxAgent jmxAgent;

        @Inject
        public void setJmxAgent(JmxAgent jmxAgent)
        {
            this.jmxAgent = jmxAgent;
        }

        @Override
        public ServiceAnnouncement get()
        {
            return serviceAnnouncement("jmx")
                    .addProperty("jmx", jmxAgent.getUrl().toString())
                    .build();
        }
    }
}

@@ -684,82 +684,14 @@
 ect.
-Inject;%0Aimport com.google.inject.Module;%0Aimport com.google.inject.Provider
+Module
 ;%0Aim
@@ -781,77 +781,8 @@
 ule;
-%0Aimport com.proofpoint.discovery.client.announce.ServiceAnnouncement;
 %0A%0Aim
@@ -868,183 +868,8 @@
 y;%0A%0A
-import static com.proofpoint.discovery.client.DiscoveryBinder.discoveryBinder;%0Aimport static com.proofpoint.discovery.client.announce.ServiceAnnouncement.serviceAnnouncement;%0A
 impo
@@ -1378,24 +1378,24 @@
 atedName();%0A
+
         bind
@@ -1450,591 +1450,8 @@
 ON);
-%0A%0A        discoveryBinder(binder).bindServiceAnnouncement(JmxAnnouncementProvider.class);%0A    %7D%0A%0A    static class JmxAnnouncementProvider%0A            implements Provider%3CServiceAnnouncement%3E%0A    %7B%0A        private JmxAgent jmxAgent;%0A%0A        @Inject%0A        public void setJmxAgent(JmxAgent jmxAgent)%0A        %7B%0A            this.jmxAgent = jmxAgent;%0A        %7D%0A%0A        @Override%0A        public ServiceAnnouncement get()%0A        %7B%0A            return serviceAnnouncement(%22jmx%22)%0A                    .addProperty(%22jmx%22, jmxAgent.getUrl().toString())%0A                    .build();%0A        %7D
 %0A   

Stop announcing the jmx service to discovery