package com.topsy.jmxproxy.service;

import java.util.Date;
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;

import com.topsy.jmxproxy.service.JMXConnectionWorker;

@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 last access time for specified host")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name="host", description="host[:port]")
    })
    public String getAccessTime(String host) throws Exception {
        return new Date(getJMXConnection(host).getAccessTime()).toString();
    }

    @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);
    }

    @Scheduled(fixedRate=60000)
    private void fetchAttributeValues() {
        for (String hostKey : hosts.keySet()) {
            JMXConnectionWorker host = hosts.get(hostKey);

            try {
                if (System.currentTimeMillis() - host.getAccessTime() > 1000 * 60 * 60) {
                    logger.info("Host " + hostKey + " access time expired, disconnecting");
                    host.disconnect();
                    synchronized(hosts) {
                        hosts.remove(host);
                    }
                } else {
                    hosts.get(hostKey).fetchAttributeValues();
                }
            } catch (Exception e) {
                removeHost(hostKey);
            }
        }
    }
}

@@ -3693,32 +3693,35 @@
 osts.remove(host
+Key
 );%0A             

Fix host expiration.