package com.topsy.jmxproxy.service;

import com.topsy.jmxproxy.domain.Host;
import com.topsy.jmxproxy.service.JMXConnectionWorker;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

// import org.springframework.jmx.export.annotation.ManagedResource;
// import org.springframework.jmx.export.annotation.ManagedOperation;
// import org.springframework.jmx.export.annotation.ManagedOperationParameters;
// import org.springframework.jmx.export.annotation.ManagedOperationParameter;
// import org.springframework.jmx.export.annotation.ManagedAttribute;

// import org.springframework.scheduling.annotation.Async;
// import org.springframework.scheduling.annotation.Scheduled;

public class JMXConnectionManager {
    private static final Logger LOG = Logger.getLogger(JMXConnectionManager.class);

    private Map<String, JMXConnectionWorker> hosts;

    public JMXConnectionManager() {
        hosts = new HashMap<String, JMXConnectionWorker>();
    }

    public synchronized Host getHost(String host) throws Exception {
        if (!hosts.containsKey(host)) {
            hosts.put(host, new JMXConnectionWorker(host));
        }

        return hosts.get(host).getHost();
    }
}
/*
@ManagedResource(objectName="jmxproxy:service=JMXConnectionManager")
public class JMXConnectionManager {
    private static final Logger logger = Logger.getLogger(JMXConnectionManager.class);

    private Map<String, JMXConnectionWorker> hosts;

    public JMXConnectionManager() {
        hosts = new HashMap<String, JMXConnectionWorker>();
    }

    @ManagedAttribute(description="List of connected hosts")
    public String[] getHosts() {
        return (String[])hosts.keySet().toArray(new String[0]);
    }

    @ManagedOperation(description="Remove a specified host")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name="host", description="host[:port]")
    })
    public void removeHost(String host) {
        if (hosts.containsKey(host)) {
            hosts.get(host).disconnect();
            synchronized(hosts) {
                hosts.remove(host);
            }
        }
    }

    @ManagedOperation(description="Get domains for specified host")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name="host", description="host[:port]")
    })
    public String[] getDomains(String host) throws Exception {
        return getJMXConnection(host).getDomains();
    }

    @ManagedOperation(description="Get mbeans for specified host and domain")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name="host", description="host[:port]"),
        @ManagedOperationParameter(name="domain", description="mbean domain")
    })
    public String[] getMBeans(String host, String domain) throws Exception {
        return getJMXConnection(host).getMBeans(domain);
    }

    public String[] getAttributes(String host, String mbean) throws Exception {
        return getJMXConnection(host).getAttributes(mbean);
    }

    public Object getAttributeValue(String host, String mbean, String attribute) throws Exception {
        return getJMXConnection(host).getAttributeValue(mbean, attribute);
    }

    private JMXConnectionWorker getJMXConnection(String host) throws Exception {
        if (!hosts.containsKey(host)) {
            hosts.put(host, new JMXConnectionWorker(host));
        }

        return hosts.get(host);
    }
}
*/

@@ -142,23 +142,25 @@
 va.util.
-HashMap
+ArrayList
 ;%0Aimport
@@ -174,269 +174,96 @@
 til.
+Hash
 Map;%0A
-%0A
 import 
-org.apache.log4j.Logger;%0A%0A// import org.springframework.jmx.export.annotation.ManagedResource;%0A// import org.springframework.jmx.export.annotation.ManagedOperation;%0A// import org.springframework.jmx.export.annotation.ManagedOperationParamet
+java.util.Map;%0Aimport java.util.List;%0A%0Aimport org.apache.log4j.Logg
 er
-s
 ;%0A
-// 
+%0A
 impo
@@ -318,31 +318,18 @@
 aged
-OperationParameter;%0A// 
+Resource;%0A
 impo
@@ -396,641 +396,8 @@
 e;%0A%0A
-// import org.springframework.scheduling.annotation.Async;%0A// import org.springframework.scheduling.annotation.Scheduled;%0A%0Apublic class JMXConnectionManager %7B%0A    private static final Logger LOG = Logger.getLogger(JMXConnectionManager.class);%0A%0A    private Map%3CString, JMXConnectionWorker%3E hosts;%0A%0A    public JMXConnectionManager() %7B%0A        hosts = new HashMap%3CString, JMXConnectionWorker%3E();%0A    %7D%0A%0A    public synchronized Host getHost(String host) throws Exception %7B%0A        if (!hosts.containsKey(host)) %7B%0A            hosts.put(host, new JMXConnectionWorker(host));%0A        %7D%0A%0A        return hosts.get(host).getHost();%0A    %7D%0A%7D%0A/*%0A
 @Man
@@ -533,14 +533,11 @@
 ger 
-logger
+LOG
  = L
@@ -639,111 +639,8 @@
 s;%0A%0A
-    public JMXConnectionManager() %7B%0A        hosts = new HashMap%3CString, JMXConnectionWorker%3E();%0A    %7D%0A%0A
     
@@ -679,22 +679,19 @@
 ist of c
-onnect
+ach
 ed hosts
@@ -700,32 +700,36 @@
 %0A    public 
+List%3C
 String
-%5B%5D
+%3E
  getHosts() 
@@ -749,1634 +749,326 @@
 urn 
-(String%5B%5D)hosts.keySet().toArray(new String%5B0%5D);%0A    %7D%0A%0A    @ManagedOperation(description=%22Remove a specified host%22)%0A    @ManagedOperationParameters(%7B%0A        @ManagedOperationParameter(name=%22host%22, description=%22host%5B:port%5D%22)%0A    %7D)%0A    public void removeHost(
+new ArrayList%3C
 String
- host) %7B%0A        if (hosts.containsKey(host)) %7B%0A            hosts.get(host).disconnect();%0A            synchronized(hosts) %7B%0A                hosts.remove(host);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @ManagedOperation(description=%22Get domains for specified host%22)%0A    @ManagedOperationParameters(%7B%0A        @ManagedOperationParameter(name=%22host%22, description=%22host%5B:port%5D%22)%0A    %7D)%0A    public String%5B%5D getDomains(String host) throws Exception %7B%0A        return getJMXConnection(host).getDomains();%0A    %7D%0A%0A    @ManagedOperation(description=%22Get mbeans for specified host and domain%22)%0A    @ManagedOperationParameters(%7B%0A        @ManagedOperationParameter(name=%22host%22, description=%22host%5B:port%5D%22),%0A        @ManagedOperationParameter(name=%22domain%22, description=%22mbean domain%22)%0A    %7D)%0A    public String%5B%5D getMBeans(String host, String domain) throws Exception %7B%0A        return getJMXConnection(host).getMBeans(domain);%0A    %7D%0A%0A    public String%5B%5D getAttributes(String host, String mbean) throws Exception %7B%0A        return getJMXConnection(host).getAttributes(mbean);%0A    %7D%0A%0A    public Object getAttributeValue(String host, String mbean, String attribute) throws Exception %7B%0A        return getJMXConnection(host).getAttributeValue(mbean, attribute);%0A    %7D%0A%0A    private JMXConnectionWorker getJMXConnection(String host) throws Exception %7B%0A        if (!hosts.containsKey(host)) %7B
+%3E(hosts.keySet());%0A    %7D%0A%0A    public JMXConnectionManager() %7B%0A        hosts = new HashMap%3CString, JMXConnectionWorker%3E();%0A    %7D%0A%0A    public synchronized Host getHost(String host) throws Exception %7B%0A        if (!hosts.containsKey(host)) %7B%0A            LOG.debug(%22creating new jmx connection worker%22);
 %0A   
@@ -1165,16 +1165,26 @@
 et(host)
+.getHost()
 ;%0A    %7D%0A
@@ -1189,7 +1189,4 @@
 %7D%0A%7D%0A
-*/%0A

Clean up the manager too.