com.clustercontrol.poller.impl.WbemPollerImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.poller.impl.WbemPollerImpl.java

Source

/*
    
 Copyright (C) 2008 NTT DATA Corporation
    
 This program is free software; you can redistribute it and/or
 Modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation, version 2.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 */

package com.clustercontrol.poller.impl;

import java.math.BigInteger;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sblim.wbem.cim.CIMDataType;
import org.sblim.wbem.cim.CIMDateTime;
import org.sblim.wbem.cim.CIMException;
import org.sblim.wbem.cim.CIMInstance;
import org.sblim.wbem.cim.CIMNameSpace;
import org.sblim.wbem.cim.CIMObjectPath;
import org.sblim.wbem.cim.CIMValue;
import org.sblim.wbem.cim.UnsignedInt16;
import org.sblim.wbem.cim.UnsignedInt32;
import org.sblim.wbem.cim.UnsignedInt64;
import org.sblim.wbem.cim.UnsignedInt8;
import org.sblim.wbem.client.CIMClient;
import org.sblim.wbem.client.PasswordCredential;
import org.sblim.wbem.client.UserPrincipal;
import org.sblim.wbem.util.SessionProperties;

import com.clustercontrol.poller.bean.PollerProtocolConstant;
import com.clustercontrol.poller.util.DataTable;
import com.clustercontrol.poller.util.TableEntry;
import com.clustercontrol.poller.util.TableEntry.ErrorType;
import com.clustercontrol.util.HinemosTime;

/**
 * WBEM???
 * 
 * @version 3.1.0
 * @since 3.1.0
 */
public class WbemPollerImpl {
    private static Log m_log = LogFactory.getLog(WbemPollerImpl.class);

    // ???
    private static final int DEFAULT_PORT = 5988;

    // ?
    private static final int DEFAULT_RETRIES = 3;

    // ?(ms)
    private static final int DEFAULT_TIMEOUT = 3000;

    // IP???
    private String m_ipAddress;

    // ????CIM??????????
    private String[] m_cimText;

    // CIM?(: http://xx.xx.xx.xx:5988)
    private String m_cimAgentAddress = null;

    // ???(: root/cimv2)
    private String m_nameSpace = "root/cimv2";

    // ???HashMapKey:CIM.??
    private HashMap<String, CIMValue> m_retMap = new HashMap<String, CIMValue>();

    // ??
    private void init() {

    }

    /**
     * ?
     * IP?DataTable????
     * ????DataTable??
     * 
     * @param ipAddress IP
     * @param port ??
     * @param protocol 
     * @param user 
     * @param password 
     * @param nameSpace ???
     * @param retries ????
     * @param timeout ??
     * @param cimList CIM??
     * @param indexCheckFlg ???????????
     */
    @SuppressWarnings("unchecked")
    public DataTable polling(String ipAddress, int port, String protocol, String user, String password,
            String nameSpace, int retries, int timeout, Set<String> cimList // cim????
    ) {
        // ?
        init();

        if (port < 0) {
            m_log.debug("set Port. " + port + " to " + DEFAULT_PORT);
            port = DEFAULT_PORT;
        }

        // ?
        if (retries < 0) {
            m_log.debug("set Retries. " + retries + " to " + DEFAULT_RETRIES);
            retries = DEFAULT_RETRIES;
        }

        if (timeout < 0) {
            m_log.debug("set Timeout. " + timeout + " to " + DEFAULT_TIMEOUT);
            timeout = DEFAULT_TIMEOUT;
        }

        // ????
        DataTable dataTable = new DataTable();

        m_ipAddress = ipAddress;
        try {
            InetAddress address = InetAddress.getByName(ipAddress);
            if (address instanceof Inet6Address) {
                m_ipAddress = "[" + m_ipAddress + "]";
            }
        } catch (UnknownHostException e) {
            m_log.warn("polling() ipAddress = " + ipAddress, e);
        }
        m_cimAgentAddress = protocol + "://" + m_ipAddress + ":" + port;

        if (nameSpace != null) {
            m_nameSpace = nameSpace;
        }

        // ?
        if (m_log.isDebugEnabled()) {
            m_log.debug("polling() start : " + m_ipAddress.toString());
            m_log.debug("Port            : " + port);
            m_log.debug("Protocol        : " + protocol);
            m_log.debug("User            : " + user);
            m_log.debug("Password        : " + password);
            m_log.debug("Retries         : " + retries);
            m_log.debug("Timeout         : " + timeout);
            m_log.debug("URL             : " + m_cimAgentAddress);
        }

        long enumerationStart = HinemosTime.currentTimeMillis();

        CIMClient cimClient = null;

        try {

            // *****************************
            // 1. Create user credentials
            // *****************************
            UserPrincipal userPr = new UserPrincipal(user);
            PasswordCredential pwCred = new PasswordCredential(password.toCharArray());

            // *****************************
            // 2. Set NameSpace
            // - URL is set like: http(s)://<IP>:Port
            // - Namespace does not need to be specified in COPs if set in this constuctor
            // - There is no server authentication being done. Thus: No need for a truststore
            // *****************************
            CIMNameSpace ns = new CIMNameSpace(m_cimAgentAddress, m_nameSpace);

            // *****************************
            // 3. Create CIM Client
            // *****************************
            cimClient = new CIMClient(ns, userPr, pwCred);

            // *****************************
            // 4. Create Session Properties
            // *****************************
            SessionProperties properties = cimClient.getSessionProperties();
            if (properties == null) {
                properties = new SessionProperties();
                cimClient.setSessionProperties(properties);
            }
            properties.setHttpTimeOut(timeout);

            m_cimText = new String[cimList.size()];

            // ??????HashMap?
            HashMap<String, ArrayList<String>> requestMap = new HashMap<String, ArrayList<String>>();
            String cimClass = "";
            String cimProperty = "";
            ArrayList<String> propertyList = null;

            int i = 0;
            for (String cimText : cimList) {

                m_cimText[i] = cimText;

                String[] targetValue = m_cimText[i].split("\\.");
                cimClass = targetValue[0];
                cimProperty = targetValue[1];

                propertyList = requestMap.get(cimClass);

                // ????????
                if (propertyList != null && propertyList.size() != 0) {
                    propertyList.add(cimProperty);
                }
                // ???????????
                else {
                    propertyList = new ArrayList<String>();
                    propertyList.add(cimProperty);
                    requestMap.put(cimClass, propertyList);
                }

                i++;
            }

            CIMObjectPath cop = null;
            CIMInstance ci = null;
            CIMValue value = null;
            Enumeration<CIMInstance> enm = null;

            // ???
            for (int j = 0; j < retries; j++) {
                boolean errorFlg = false;
                m_retMap = new HashMap<String, CIMValue>();
                try {
                    for (Map.Entry<String, ArrayList<String>> cimClassEntry : requestMap.entrySet()) {
                        cimClass = cimClassEntry.getKey();
                        propertyList = cimClassEntry.getValue();

                        m_log.debug("CIMClass : " + cimClassEntry.getKey());

                        cop = new CIMObjectPath(cimClassEntry.getKey());
                        enm = cimClient.enumInstances(cop, true);

                        i = 0;
                        while (enm.hasMoreElements()) {

                            ci = enm.nextElement();

                            for (String property : propertyList) {
                                cimProperty = property;

                                if (ci.getProperty(cimProperty) != null) {

                                    value = ci.getProperty(cimProperty).getValue();

                                    // ????5?1???
                                    // CIM??????????????
                                    // 
                                    // ?????Array index out of range: 0??
                                    // test code
                                    /*
                                    if(value.getType().getType() == CIMDataType.STRING_ARRAY){
                                       testCounter ++;
                                       if (testCounter > 10) {
                                          value = new CIMValue(new Vector<String>(),
                                                new CIMDataType(CIMDataType.STRING_ARRAY));
                                          testCounter = 0;
                                       }
                                    }
                                     */
                                    // test code

                                    if (!checkCIMData(value)) {
                                        errorFlg = true;
                                        continue;
                                        /*
                                         * ??????????
                                         * (???????????????)
                                         */
                                    }
                                    m_retMap.put(cimClass + "." + cimProperty + "." + i, value);

                                }
                            }
                            if (errorFlg) {
                                break;
                            }
                            i++;
                        }
                        if (errorFlg) {
                            break;
                        }
                    }
                } catch (CIMException e) {
                    errorFlg = true;
                    for (String property : propertyList) {
                        dataTable.putValue(new TableEntry(getEntryKey(cimClass + "." + property + ".0"),
                                HinemosTime.currentTimeMillis(), ErrorType.IO_ERROR, e));
                    }
                    m_log.warn("polling() warning :" + m_ipAddress.toString() + ", cimClass=" + cimClass + ", ID="
                            + e.getID() + ", message=" + e.getMessage());
                } catch (Exception e) {
                    errorFlg = true;
                    for (String property : propertyList) {
                        dataTable.putValue(new TableEntry(getEntryKey(cimClass + "." + property + ".0"),
                                HinemosTime.currentTimeMillis(), ErrorType.IO_ERROR, e));
                    }
                    m_log.warn("polling() warning :" + m_ipAddress.toString() + ", " + cimClass
                            + " unforeseen error. " + e.getMessage(), e);
                } finally {
                    if (errorFlg) {
                        // m_retMap = new HashMap<String, CIMValue>();
                        /*
                         * ???????????m_retMap?
                         * ???
                         */
                    } else {
                        break;
                    }
                }
            }
        } catch (RuntimeException e) {
            m_log.warn("polling() warning :" + m_ipAddress.toString() + " unforeseen error. " + e.getMessage(), e);
        } finally {
            if (cimClient != null) {
                try {
                    cimClient.close();
                } catch (Exception e) {
                    m_log.warn("polling():" + m_ipAddress.toString() + " Session close failed", e);
                }
            }
        }

        long enumerationStop = HinemosTime.currentTimeMillis();

        // ?
        if (m_log.isDebugEnabled()) {
            m_log.debug("polling() end : time : " + (enumerationStop - enumerationStart));
        }

        // ***************
        // ?? (?)
        // ***************
        try {
            // ?????????????
            if (m_retMap == null || m_retMap.size() == 0) {
                m_log.debug("wbemReceived() : " + m_ipAddress.toString() + " result is empty");
                return dataTable;
            }

            long time = HinemosTime.currentTimeMillis(); // ?

            for (Map.Entry<String, CIMValue> entry : m_retMap.entrySet()) {
                String cimString = entry.getKey();
                CIMValue value = entry.getValue();

                if (value.getType().getType() == CIMDataType.UINT8) {
                    long ret = ((UnsignedInt8) value.getValue()).longValue();
                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);
                } else if (value.getType().getType() == CIMDataType.UINT16) {
                    long ret = ((UnsignedInt16) value.getValue()).longValue();
                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);
                } else if (value.getType().getType() == CIMDataType.UINT32) {
                    long ret = ((UnsignedInt32) value.getValue()).longValue();
                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);
                } else if (value.getType().getType() == CIMDataType.UINT64) {
                    BigInteger bigInt = ((UnsignedInt64) value.getValue()).bigIntValue();
                    long ret = bigInt.longValue();
                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);
                } else if (value.getType().getType() == CIMDataType.STRING) {
                    String ret = (String) value.getValue();
                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);

                } else if (value.getType().getType() == CIMDataType.STRING_ARRAY) {

                    Vector<String> ret = (Vector<String>) value.getValue();
                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);
                } else if (value.getType().getType() == CIMDataType.DATETIME) {

                    CIMDateTime sdt = (CIMDateTime) value.getValue();

                    // CIMDateTime?Calendar?
                    Calendar cal = sdt.getCalendar();

                    // Calendar??????????
                    long ret = cal.getTimeInMillis() / 1000;

                    dataTable.putValue(getEntryKey(cimString), time, ret);

                    m_log.debug("polling() dataTable put : " + "entryKey : " + getEntryKey(cimString) + ", time : "
                            + time + ", value : " + ret);

                } else {
                    m_log.debug("polling() data type is nothing");
                }

            }
        } catch (Exception e) { // ?????????
            m_log.warn("polling() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);

            // ?????
            dataTable.clear();
            for (Map.Entry<String, CIMValue> entry : m_retMap.entrySet()) {
                final String entryKey = getEntryKey(entry.getKey());
                dataTable
                        .putValue(new TableEntry(entryKey, HinemosTime.currentTimeMillis(), ErrorType.IO_ERROR, e));
            }
        }
        return dataTable;
    }

    private boolean checkCIMData(CIMValue value) {
        if (value == null) {
            m_log.info("checkCIMData : value is null");
            return false;
        }
        if (value.getType() == null) {
            m_log.info("checkCIMData : value.getType is null, " + "refClassName="
                    + value.getType().getRefClassName() + "stringType=" + value.getType().getStringType()
                    + "toString=" + value.getType().toString());
            return false;
        }
        int type = value.getType().getType();
        if (type == CIMDataType.UINT8) {
        } else if (type == CIMDataType.UINT16) {
        } else if (type == CIMDataType.UINT32) {
        } else if (type == CIMDataType.UINT64) {
        } else if (type == CIMDataType.STRING) {
        } else if (type == CIMDataType.STRING_ARRAY) {
            @SuppressWarnings("unchecked")
            Vector<String> ret = (Vector<String>) value.getValue();
            if (ret.size() == 0) {
                m_log.info("checkCIMData : CIMValue has fault. : ip=" + m_ipAddress);
                return false;
            }
        }
        return true;
    }

    /**
     * DataTable??????EntryKey?
     * 
     * @param cimString CIM??
     */
    private String getEntryKey(String cimString) {

        return PollerProtocolConstant.PROTOCOL_WBEM + "." + cimString;
    }

    /**
     * ?
     * 
     * @param args
     */
    public static void main(String[] args) {
        // 
        WbemPollerImpl poller = new WbemPollerImpl();

        String ipAddress = args[0];
        int port = Integer.parseInt(args[1]);
        String protocol = args[2];
        String user = args[3];
        String password = args[4];
        String nameSpace = args[5]; // ??
        int retries = Integer.parseInt(args[6]);
        int timeout = Integer.parseInt(args[7]);
        Set<String> cims = new HashSet<String>();

        for (int i = 8; i < args.length; i++) {
            cims.add(args[i]);
        }

        DataTable table = poller.polling(ipAddress, port, protocol, user, password, nameSpace, retries, timeout,
                cims);
        System.out.println(table);
    }
}