package com.eactive.eai.rms.client.agent;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

import com.eactive.eai.common.util.ContainerUtil;
import com.eactive.eai.env.ElinkConfig;
import com.eactive.eai.rms.client.SocketClient;
import com.eactive.eai.rms.client.context.RmsProperties;
import com.eactive.eai.rms.client.service.JMXMonitoring;
import com.eactive.eai.rms.client.service.JMXMonitoringFactory;
//import com.eactive.eai.rms.client.service.SingleQueueMonitoring;
//import com.eactive.eai.rms.client.service.SingleServerMonitoring;
import com.eactive.eai.rms.onl.vo.JmsMonitorVo;

public class JmsQueueAgent extends AbstractScheduledAgent {

    /** The socket client. */
    private SocketClient socketClient = null;

    /** The RMS Properties */
    private RmsProperties rmsProperties;
    
    private Properties adminClientProperties;

    public void setAdminClientProperties(Properties adminClientProperties) {
        this.adminClientProperties = adminClientProperties;
    }    

    @SuppressWarnings("unchecked")
    public void run() {
    	logger.debug("JmsQueueAgent ");
        JmsMonitorVo vo = null ;
        try {
            vo = (JmsMonitorVo) applicationContext.getBean("jmsMonitorVo");
            if( vo == null ) {
                logger.error("JmsQueueAgent - Can't get bean => jmsMonitorVo");
                return ;
            }
            
            HashMap map = getLocalQueueInfo();
            if( map != null && !map.isEmpty() ) {
                Iterator it = map.keySet().iterator();
                while( it.hasNext() ) {
                    String name = (String)it.next();
                    String info[] = (String[])map.get(name);
                    
                    if(logger.isDebug()) logger.debug("JmsQueueAgent - Queue Name => "+name);
                    
                    vo.setUdpQueueName( name );
                    vo.setUdpInfo( info );
                    
                    socketClient.send(vo);
                    logger.debug("SEND JmsQueueAgent : " + vo );
                }
            }
        } catch ( Exception e ) {
            logger.error(e.getMessage());
        }        
    }
    
    public HashMap getLocalQueueInfo() throws Exception{
        
    	String instName     = rmsProperties.getStringProperty(RmsProperties.EAI_SERVER_INSTANCE_NAME);
    	JMXMonitoring monitoring = JMXMonitoringFactory.createQueueMonitoring(ContainerUtil.get());
    	if (monitoring == null) return new HashMap();
    	monitoring.init(this.adminClientProperties);
        return (HashMap)monitoring.getState(instName);

    }
    
    public static void main(String[] args) throws Exception {
        JmsQueueAgent s = new JmsQueueAgent();
        s.run();
    }

    public void setSocketClient(SocketClient socketClient) {
        this.socketClient = socketClient;
    }

    public void setRmsProperties(RmsProperties rmsProperties) {
        this.rmsProperties = rmsProperties;
    }
}