Java tutorial
/* Copyright (C) 2006 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.repository.factory; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.SnmpSecurityLevelConstant; import com.clustercontrol.bean.SnmpVersionConstant; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.fault.FacilityDuplicate; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.InvalidSetting; import com.clustercontrol.maintenance.util.HinemosPropertyUtil; import com.clustercontrol.repository.NodeSearchTask; import com.clustercontrol.repository.bean.NodeInfoDeviceSearch; import com.clustercontrol.repository.model.NodeInfo; import com.clustercontrol.repository.session.RepositoryControllerBean; import com.clustercontrol.repository.util.FacilityIdCacheInitCallback; import com.clustercontrol.repository.util.FacilityTreeCacheRefreshCallback; import com.clustercontrol.repository.util.RepositoryChangedNotificationCallback; import com.clustercontrol.repository.util.RepositoryUtil; import com.clustercontrol.util.HinemosTime; import com.clustercontrol.util.MessageConstant; /** * ???? Session Bean <BR> * */ public class NodeSearcher { /** ?<BR> */ private static Log m_log = LogFactory.getLog(NodeSearcher.class); // ?????? private static final Semaphore duplicateExec = new Semaphore(1); public static final String MaxSearchNodeKey = "repository.node.search.max.node"; private final ExecutorService _executorService = Executors.newCachedThreadPool(new ThreadFactory() { private volatile int _count = 0; @Override public Thread newThread(Runnable r) { String threadName = "NodeSearchWorker-" + _count++; m_log.debug("new thread=" + threadName); return new Thread(r, threadName); } }); public List<NodeInfoDeviceSearch> searchNode(String ownerRoleId, String ipAddrFrom, String ipAddrTo, int port, String community, int version, String facilityID, String securityLevel, String user, String authPass, String privPass, String authProtocol, String privProtocol) throws HinemosUnknown, FacilityDuplicate, InvalidSetting { long startTime = HinemosTime.currentTimeMillis(); // ?(60)??????? int maxMsec = HinemosPropertyUtil .getHinemosPropertyNum("repository.node.search.timeout", Long.valueOf(50 * 1000)).intValue(); List<NodeInfoDeviceSearch> nodeList = new ArrayList<NodeInfoDeviceSearch>(); if (duplicateExec.tryAcquire()) { try { String errMsg = MessageConstant.MESSAGE_PLEASE_SET_IPADDR_CORRECT_FORMAT.getMessage(); //? if (ipAddrFrom == null || ipAddrFrom.equals("") || ipAddrTo == null || ipAddrTo.equals("")) { throw new HinemosUnknown(MessageConstant.MESSAGE_PLEASE_SET_SEARCH_IPADDR.getMessage()); } else if (version == SnmpVersionConstant.TYPE_V3 && securityLevel.equals(SnmpSecurityLevelConstant.NOAUTH_NOPRIV) == false) { if (user == null || user.equals("")) { throw new HinemosUnknown(MessageConstant.MESSAGE_PLEASE_SET_USER_NAME.getMessage()); } else if (authPass == null || authPass.equals("")) { throw new HinemosUnknown( MessageConstant.MESSAGE_PLEASE_SET_AUTHPASS_8CHARA_MINIMUM.getMessage()); } else if (securityLevel.equals(SnmpSecurityLevelConstant.AUTH_PRIV)) { if (privPass == null || privPass.equals("")) { throw new HinemosUnknown( MessageConstant.MESSAGE_PLEASE_SET_PRIVPASS_8CHARA_MINIMUM.getMessage()); } } } List<String> ipAddressList = null; // IP? InetAddress addressFrom; InetAddress addressTo; try { addressFrom = InetAddress.getByName(ipAddrFrom); addressTo = InetAddress.getByName(ipAddrTo); if (addressFrom instanceof Inet4Address && addressTo instanceof Inet4Address) { //IPv4?????String? if (!ipAddrFrom.matches(".{1,3}?\\..{1,3}?\\..{1,3}?\\..{1,3}?")) { m_log.info(errMsg); throw new HinemosUnknown(errMsg); } ipAddressList = RepositoryUtil.getIpList(ipAddrFrom, ipAddrTo, 4); } else if (addressFrom instanceof Inet6Address && addressTo instanceof Inet6Address) { //IPv6????String??? ipAddressList = RepositoryUtil.getIpList(ipAddrFrom, ipAddrTo, 6); } else { m_log.info(errMsg); throw new HinemosUnknown(errMsg); } } catch (UnknownHostException e) { m_log.warn(errMsg); throw new HinemosUnknown(errMsg); } if (m_log.isDebugEnabled()) { StringBuilder str = new StringBuilder(); for (String ipAddress : ipAddressList) { if (str.length() != 0) { str.append(", "); } str.append(ipAddress); } m_log.debug("ipAddress=" + str); } List<NodeInfoDeviceSearch> searchList = new ArrayList<>(); try { //? //256????? if (ipAddressList.size() > HinemosPropertyUtil.getHinemosPropertyNum(MaxSearchNodeKey, Long.valueOf(256))) { m_log.info(MessageConstant.MESSAGE_EXCEED_LIMIT_NUMBER_256NODES.getMessage()); throw new HinemosUnknown(MessageConstant.MESSAGE_EXCEED_LIMIT_NUMBER_256NODES.getMessage()); } //???? //delay????????????(60??????) List<Future<NodeInfoDeviceSearch>> list = new ArrayList<>(); for (String ipAddress : ipAddressList) { if (list.size() > 0) { Thread.sleep(HinemosPropertyUtil.getHinemosPropertyNum("repository.node.search.delay", Long.valueOf(10))); } try { InetAddress address = InetAddress.getByName(ipAddress); List<String> facilityList = new RepositoryControllerBean() .getFacilityIdByIpAddress(address); if (facilityList != null && 0 < facilityList.size()) { //??IP???? m_log.info("ipAddress " + address + " is already registered."); continue; } } catch (UnknownHostException e) { m_log.warn("UnknownHostException : " + e.getMessage()); // ??????? continue; } NodeSearchTask task = new NodeSearchTask(ipAddress, port, community, version, facilityID, securityLevel, user, authPass, privPass, authProtocol, privProtocol); list.add(_executorService.submit(task)); } for (Future<NodeInfoDeviceSearch> future : list) { if (future != null) { try { // SNMP??5?????????get?????? searchList.add(future.get(maxMsec, TimeUnit.MILLISECONDS)); } catch (TimeoutException e) { m_log.warn("searchNode : " + e.getClass().getName() + ", " + e.getMessage()); } } } } catch (InterruptedException e) { m_log.warn("searchNode : " + e.getClass().getName() + ", " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage(), e); } catch (ExecutionException e) { m_log.warn("searchNode : " + e.getClass().getName() + ", " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage(), e); } // RepositoryControllerBean controller = new RepositoryControllerBean(); boolean commitFlag = false; for (NodeInfoDeviceSearch searchInfo : searchList) { try { if (searchInfo == null) { m_log.warn("searchInfo is null"); continue; } nodeList.add(searchInfo); NodeInfo nodeInfo = searchInfo.getNodeInfo(); nodeInfo.setOwnerRoleId(ownerRoleId); m_log.info("nodeInfo " + nodeInfo); if (searchInfo.getErrorMessage() != null) { continue; } //? long msec = HinemosTime.currentTimeMillis() - startTime; if (msec > maxMsec) { m_log.info(MessageConstant.MESSAGE_TIME_OUT.getMessage() + " msec=" + msec); throw new HinemosUnknown(MessageConstant.MESSAGE_TIME_OUT.getMessage()); } try { //???????? controller.addNodeWithoutRefresh(nodeInfo); commitFlag = true; } catch (FacilityDuplicate | InvalidSetting | HinemosUnknown e) { String errorMessage = "" + e.getMessage(); searchInfo.setErrorMessage(errorMessage); } finally { } } catch (Exception e) { m_log.warn("searchNode : " + e.getClass().getName() + ", " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage()); } } if (commitFlag) { new FacilityIdCacheInitCallback().postCommit(); //FacilityTreeCache?????? JpaTransactionManager jtm = new JpaTransactionManager(); try { jtm.begin(); new FacilityTreeCacheRefreshCallback().postCommit(); jtm.commit(); } catch (Exception e1) { jtm.rollback(); throw e1; } finally { jtm.close(); } new RepositoryChangedNotificationCallback().postCommit(); } } catch (HinemosUnknown e) { throw e; } finally { m_log.info("node search : " + "ipAddrFrom=" + ipAddrFrom + ", ipAddrTo=" + ipAddrTo + ", time=" + (HinemosTime.currentTimeMillis() - startTime) + "ms"); duplicateExec.release(); } } else { m_log.warn("runningCheck is busy !!"); } return nodeList; } }