com.dc.tes.adapter.startup.remote.StartUpRemoteForRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.dc.tes.adapter.startup.remote.StartUpRemoteForRequest.java

Source

package com.dc.tes.adapter.startup.remote;

import java.nio.channels.ClosedByInterruptException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dc.tes.adapter.IRequestAdapter;
import com.dc.tes.adapter.framework.DefaultRequestAdapterEnvContext;
import com.dc.tes.adapter.remote.DefaultRequestAdapterHelper;
import com.dc.tes.adapter.util.ConfigHelper;
import com.dc.tes.adapterlib.*; //Needed!

/**
 * ?"?"()
 * 
 * @author 
 * 
 */
public class StartUpRemoteForRequest extends Thread {

    private static final Log log = LogFactory.getLog(StartUpRemoteForRequest.class);

    /**
     * ???(ComLayer.config.xml)
     */
    private Properties m_props = null;

    /**
     * ??
     */
    DefaultRequestAdapterHelper m_adpterHelper = null;

    public StartUpRemoteForRequest(Properties prop) throws Exception {
        if (!((String) prop.get("adapterType")).toUpperCase().equals("REQUEST"))
            throw new Exception("?" + prop.get("adapterType") + "?!");
        m_props = prop;
    }

    public void run() {
        startAdapter(m_props);
        //  ??
        try {
            while (!Thread.interrupted()) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            log.debug("?");
            shutdownAdapter();
        }
    }

    private void shutdownAdapter() {
        if (m_adpterHelper != null) {
            // 
            m_adpterHelper.unReg2TES();

            m_adpterHelper.stopServer();
        }
        log.info("??" + m_props.getProperty("CHANNELNAME") + "?.");
    }

    private void startAdapter(Properties props) {
        String adapterName = props.getProperty("CHANNELNAME");

        IRequestAdapter adapterInstance = null;
        try {
            String clsName = "com.dc.tes.adapterlib." + (String) props.get("adapterPlugIn");
            adapterInstance = (IRequestAdapter) ConfigHelper.class.getClassLoader().loadClass(clsName)
                    .newInstance();
            System.out.println("????" + clsName);

            // License
            m_props.setProperty("SIMTYPE", adapterInstance.AdapterType());

            m_adpterHelper = new DefaultRequestAdapterHelper(m_props, adapterInstance);

            // 
            byte[] config = m_adpterHelper.reg2TES();

            // ?
            if (adapterInstance.Init(new DefaultRequestAdapterEnvContext(config))) {
                m_adpterHelper.SetConfigProperty(adapterInstance.GetAdapterConfigProperties());
                System.out.println("??" + adapterName + "?Init?.");

                log.info("???" + adapterName + "?Init?.");
                log.error("???" + adapterName + "?Init?.");

                // ???
                m_adpterHelper.startServer();
            } else {
                m_adpterHelper.unReg2TES();
                // ?m_requestAdapterHelper.stopServer()

                System.out.println("???" + adapterName + "?Init.");
                log.error("???" + adapterName + "?Init.");
            }

        } catch (InstantiationException e) {
            log.error("?!?");
            System.out.println("?!?");
            // ?stopServer
        } catch (IllegalAccessException e) {
            log.error("?!?[" + e.getMessage() + "]");
            System.out.println("?!?[" + e.getMessage() + "]");
            // ?stopServer
        } catch (ClassNotFoundException e) {
            log.error("??!?[" + e.getMessage() + "]");
            System.out.println("??!?[" + e.getMessage() + "]");
            // ?stopServer
        } catch (ClosedByInterruptException e) {
            // ??
            shutdownAdapter();
            log.info("??" + adapterName + "");
            System.out.println("??" + adapterName + "");
        } catch (Exception e) {
            // ??
            shutdownAdapter();
            log.error("?" + adapterName + "?![" + e.getMessage() + "]");
            System.out.println("?" + adapterName + "?![" + e.getMessage() + "]");
        }

        System.out.println("??" + adapterName + "??.");
        log.info("??" + adapterName + "??.");
    }
}