Example usage for javax.net.ssl SSLSessionContext setSessionTimeout

List of usage examples for javax.net.ssl SSLSessionContext setSessionTimeout

Introduction

In this page you can find the example usage for javax.net.ssl SSLSessionContext setSessionTimeout.

Prototype

public void setSessionTimeout(int seconds) throws IllegalArgumentException;

Source Link

Document

Sets the timeout limit for SSLSession objects grouped under this SSLSessionContext.

Usage

From source file:com.clustercontrol.agent.Agent.java

/**
 * /*w w w . j ava2s. c  o m*/
 */
public Agent(String propFileName) throws Exception {

    //------------
    //-- ??
    //------------

    //???
    AgentProperties.init(propFileName);

    // ?IP????
    getAgentInfo();
    m_log.info(getAgentStr());

    // log4j???
    String log4jFileName = System.getProperty("hinemos.agent.conf.dir") + File.separator + "log4j.properties";
    m_log.info("log4j.properties = " + log4jFileName);
    m_log4jFileName = log4jFileName;

    int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
    int requestTimeout = DEFAULT_REQUEST_TIMEOUT;

    // 
    String proxyHost = DEFAULT_PROXY_HOST;
    int proxyPort = DEFAULT_PROXY_PORT;
    String proxyUser = DEFAULT_PROXY_USER;
    String proxyPassword = DEFAULT_PROXY_PASSWORD;

    // ???hostnameVerifier?HTTPS????
    try {
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, javax.net.ssl.SSLSession session) {
                return true;
            }
        };

        // Create the trust manager.
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        class AllTrustManager implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                    throws java.security.cert.CertificateException {
                return;
            }

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                    throws java.security.cert.CertificateException {
                return;
            }
        }
        javax.net.ssl.TrustManager tm = new AllTrustManager();
        trustAllCerts[0] = tm;
        // Create the SSL context
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
        // Create the session context
        javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
        // Initialize the contexts; the session context takes the
        // trust manager.
        sslsc.setSessionTimeout(0);
        sc.init(null, trustAllCerts, null);
        // Use the default socket factory to create the socket for
        // the secure
        // connection
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Set the default host name verifier to enable the connection.
        HttpsURLConnection.setDefaultHostnameVerifier(hv);

    } catch (Throwable e) {
        m_log.warn("hostname verifier (all trust) disable : " + e.getMessage(), e);
    }

    try {
        String strConnect = AgentProperties.getProperty("connect.timeout");
        if (strConnect != null) {
            connectTimeout = Integer.parseInt(strConnect);
        }
        String strRequest = AgentProperties.getProperty("request.timeout");
        if (strRequest != null) {
            requestTimeout = Integer.parseInt(strRequest);
        }
        String strProxyHost = AgentProperties.getProperty("http.proxy.host");
        if (strProxyHost != null) {
            proxyHost = strProxyHost;
        }
        String strProxyPort = AgentProperties.getProperty("http.proxy.port");
        if (strProxyPort != null) {
            proxyPort = Integer.parseInt(strProxyPort);
        }
        String strProxyUser = AgentProperties.getProperty("http.proxy.user");
        if (strProxyUser != null) {
            proxyUser = strProxyUser;
        }
        String strProxyPassword = AgentProperties.getProperty("http.proxy.password");
        if (strProxyPassword != null) {
            proxyPassword = strProxyPassword;
        }
    } catch (Exception e) {
        m_log.warn(e.getMessage());
    }

    if (!"".equals(proxyHost)) {
        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", Integer.toString(proxyPort));
        BasicAuth basicAuth = new BasicAuth(proxyUser, proxyPassword);
        Authenticator.setDefault(basicAuth);
        m_log.info("proxy.host=" + System.getProperty("http.proxyHost") + ", proxy.port="
                + System.getProperty("http.proxyPort") + ", proxy.user=" + proxyUser);
    }

    // ?? ${ManagerIP} ?????????????
    // ??PING????????IP????FacilityID??????
    String managerAddress = AgentProperties.getProperty("managerAddress");
    URL url = new URL(managerAddress);
    boolean replacePropFileSuccess = true;
    String errMsg = "";
    if (REPLACE_VALUE_MANAGER_IP.equals((url.getHost()))) {
        try {

            // ???PING?????PING???????
            Map<String, String> discoveryInfoMap = new HashMap<String, String>();
            while (true) {
                m_log.info("waiting for manager connection...");
                String recvMsg = receiveManagerDiscoveryInfo();

                // ????key=value,key=value ??????Map??
                try {
                    discoveryInfoMap.clear();
                    String[] commaSplittedRecvMsgArray = recvMsg.split(",");
                    for (String keyvalueset : commaSplittedRecvMsgArray) {
                        String key = keyvalueset.split("=")[0];
                        String value = keyvalueset.split("=")[1];
                        discoveryInfoMap.put(key, value);
                    }
                } catch (Exception e) {
                    m_log.error("can't parse receive message : " + e.toString());
                    continue;
                }
                if (discoveryInfoMap.containsKey("agentFacilityId")
                        && discoveryInfoMap.containsKey("managerIp")) {
                    break;
                } else {
                    m_log.error("receive message is invalid");
                }
            }
            // Agent.properties?????????????
            {
                String managerIp = discoveryInfoMap.get("managerIp");
                String key = "managerAddress";
                String value = url.getProtocol() + "://" + managerIp + ":" + url.getPort() + "/HinemosWS/";
                m_log.info("Rewrite property. key : " + key + ", value : " + value);
                PropertiesFileUtil.replacePropertyFile(propFileName, key, managerAddress, value);
                AgentProperties.setProperty(key, value);
            }

            // Agent.properties?ID??????????
            {
                String key = "facilityId";
                String value = discoveryInfoMap.get("agentFacilityId");
                m_log.info("Rewrite property. key : " + key + ", value : " + value);
                PropertiesFileUtil.replacePropertyFile(propFileName, key, "", value);
                AgentProperties.setProperty(key, value);
            }

            // log4j.properties?????Windows??
            {
                String managerIp = discoveryInfoMap.get("managerIp");
                String key = "log4j.appender.syslog.SyslogHost";
                PropertiesFileUtil.replacePropertyFile(log4jFileName, "log4j.appender.syslog.SyslogHost",
                        REPLACE_VALUE_MANAGER_IP, managerIp);
                if (REPLACE_VALUE_MANAGER_IP.equals(AgentProperties.getProperty(key))) {
                    m_log.info("Rewrite property. key : " + key + ", value : " + managerIp);
                    PropertiesFileUtil.replacePropertyFile(log4jFileName, key, REPLACE_VALUE_MANAGER_IP,
                            managerIp);
                }
            }
        } catch (HinemosUnknown e) {
            // ????????????
            errMsg = e.getMessage();
            m_log.warn(errMsg, e);
            replacePropFileSuccess = false;
        } catch (Exception e) {
            m_log.warn(e.getMessage(), e);
            throw e;
        }
    }

    try {
        EndpointManager.init(AgentProperties.getProperty("user"), AgentProperties.getProperty("password"),
                AgentProperties.getProperty("managerAddress"), connectTimeout, requestTimeout);
    } catch (Exception e) {
        m_log.error("EndpointManager.init error : " + e.getMessage(), e);
        m_log.error("current-dir=" + (new File(".")).getAbsoluteFile().getParent());
        throw e;
    }

    if (!replacePropFileSuccess) {
        OutputBasicInfo output = new OutputBasicInfo();
        output.setPluginId("AGT_UPDATE_CONFFILE");
        output.setPriority(PriorityConstant.TYPE_WARNING);
        output.setApplication(MessageConstant.AGENT.getMessage());
        String[] args = { errMsg };
        output.setMessage(MessageConstant.MESSAGE_AGENT_REPLACE_FILE_FAULURE_NOTIFY_MSG.getMessage());
        output.setMessageOrg(
                MessageConstant.MESSAGE_AGENT_REPLACE_FILE_FAULURE_NOTIFY_ORIGMSG.getMessage(args));
        output.setGenerationDate(HinemosTime.getDateInstance().getTime());
        output.setMonitorId("SYS");
        output.setFacilityId(""); // ???
        output.setScopeText(""); // ???
        m_sendQueue.put(output);
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            terminate();
            m_log.info("Hinemos agent stopped");
        }
    });
}