Java tutorial
/* Copyright (C) 2012 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; import java.util.ArrayList; import java.util.Date; import java.util.concurrent.Callable; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.accesscontrol.bean.UserIdConstant; import com.clustercontrol.accesscontrol.session.AccessControllerBean; import com.clustercontrol.bean.HinemosModuleConstant; import com.clustercontrol.bean.PriorityConstant; import com.clustercontrol.commons.util.HinemosSessionContext; import com.clustercontrol.fault.SnmpResponseError; import com.clustercontrol.maintenance.util.HinemosPropertyUtil; import com.clustercontrol.monitor.bean.EventConfirmConstant; import com.clustercontrol.notify.bean.OutputBasicInfo; import com.clustercontrol.notify.session.NotifyControllerBean; import com.clustercontrol.repository.bean.DeviceSearchMessageInfo; import com.clustercontrol.repository.bean.NodeInfoDeviceSearch; import com.clustercontrol.repository.model.NodeInfo; import com.clustercontrol.repository.session.RepositoryControllerBean; import com.clustercontrol.util.HinemosTime; import com.clustercontrol.util.MessageConstant; /** * ???? */ public class DeviceSearchTask implements Callable<Boolean> { private static Log m_log = LogFactory.getLog(DeviceSearchTask.class); private String facilityId; /** * * @param facilityId ??ID */ public DeviceSearchTask(String facilityId) { this.facilityId = facilityId; } /** * ???? */ @Override public Boolean call() { m_log.debug("run() start"); boolean isExceptionOccur = false; boolean isOutPutLog = false; NodeInfoDeviceSearch nodeDeviceSearch = null; try { String user = HinemosPropertyUtil.getHinemosPropertyStr("repository.auto.device.user", UserIdConstant.HINEMOS); HinemosSessionContext.instance().setProperty(HinemosSessionContext.LOGIN_USER_ID, user); HinemosSessionContext.instance().setProperty(HinemosSessionContext.IS_ADMINISTRATOR, new AccessControllerBean().isAdministrator()); RepositoryControllerBean controller = new RepositoryControllerBean(); //???SNMP??? NodeInfo node = controller.getNode(facilityId); if (node.getAutoDeviceSearch() == false) { return isOutPutLog; } nodeDeviceSearch = controller.getNodePropertyBySNMP(node.getAvailableIpAddress(), node.getSnmpPort(), node.getSnmpCommunity(), node.getSnmpVersion(), facilityId, node.getSnmpSecurityLevel(), node.getSnmpUser(), node.getSnmpAuthPassword(), node.getSnmpPrivPassword(), node.getSnmpAuthProtocol(), node.getSnmpPrivProtocol()); if (nodeDeviceSearch.getDeviceSearchMessageInfo() != null && nodeDeviceSearch.getDeviceSearchMessageInfo().size() > 0) { //????DB controller.modifyNode(nodeDeviceSearch.getNewNodeInfo()); //???? isOutPutLog = true; } } catch (SnmpResponseError e) { //SNMP??? isOutPutLog = HinemosPropertyUtil.getHinemosPropertyBool("repository.auto.device.find.log", false); } catch (Exception e) { m_log.warn("run() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); isExceptionOccur = true; //SNMP??? isOutPutLog = HinemosPropertyUtil.getHinemosPropertyBool("repository.auto.device.find.log", false); } finally { //? if (isOutPutLog) { String details = ""; ArrayList<DeviceSearchMessageInfo> list; if (nodeDeviceSearch != null) { list = nodeDeviceSearch.getDeviceSearchMessageInfo(); } else { return isOutPutLog; } for (DeviceSearchMessageInfo msgInfo : list) { details = details.length() > 0 ? details + ", " : details; details = details + msgInfo.getItemName() + " " + MessageConstant.LASTTIME.getMessage() + ":" + msgInfo.getLastVal() + " " + MessageConstant.THISTIME.getMessage() + ":" + msgInfo.getThisVal(); } putEvent(isExceptionOccur, facilityId, details); } } return isOutPutLog; } private void putEvent(boolean isExceptionOccur, String facilityID, String details) { //?? Date nowDate = HinemosTime.getDateInstance(); String msg = null; int priority = 0; if (!isExceptionOccur) { msg = MessageConstant.MESSAGE_EXECUTED_AUTO_SEARCH_DEVICES.getMessage() + " " + details; priority = PriorityConstant.TYPE_INFO; } else { msg = MessageConstant.MESSAGE_FAILED_AUTO_SEARCH_DEVICES.getMessage(); priority = PriorityConstant.TYPE_WARNING; } //? OutputBasicInfo output = new OutputBasicInfo(); output.setPluginId(HinemosModuleConstant.REPOSITORY_DEVICE_SEARCH); output.setMonitorId(HinemosModuleConstant.SYSYTEM); output.setFacilityId(facilityID); output.setScopeText(facilityID); output.setApplication(""); output.setMessage(msg); output.setMessageOrg(msg); output.setPriority(priority); output.setGenerationDate(nowDate.getTime()); try { new NotifyControllerBean().insertEventLog(output, EventConfirmConstant.TYPE_UNCONFIRMED); } catch (Exception e) { m_log.error(e.getMessage(), e); } } }