From 57d0c9e7572a9de13c7e26f57e98e9a0cdd564a7 Mon Sep 17 00:00:00 2001 From: youbowu Date: Wed, 1 Mar 2017 10:55:46 +0800 Subject: Fix the sonar detected error Issue-ID:HOLMES-51 Change-Id: I1ac160651d7cd1382076a447d33482219cdb0def Signed-off-by: youbowu --- .../org/openo/holmes/common/api/stat/Alarm.java | 126 +++++++-------------- .../holmes/common/i18n/correlation-i18n-en-US.json | 2 +- .../holmes/common/i18n/correlation-i18n-zh-CN.json | 2 +- .../openo/holmes/common/producer/MQProducer.java | 48 +++----- .../org/openo/holmes/common/utils/I18nProxy.java | 2 +- .../org/openo/holmes/common/utils/JacksonUtil.java | 15 --- .../openo/holmes/common/utils/LanguageUtil.java | 4 +- .../org/openo/holmes/common/utils/UserUtil.java | 2 +- 8 files changed, 66 insertions(+), 135 deletions(-) (limited to 'holmes-actions/src/main') diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/api/stat/Alarm.java b/holmes-actions/src/main/java/org/openo/holmes/common/api/stat/Alarm.java index e5eda70..3ea3715 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/api/stat/Alarm.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/api/stat/Alarm.java @@ -46,78 +46,83 @@ public class Alarm implements AplusData, Cloneable, Serializable { public static final byte EVENT_RAISED = 0; private static final long serialVersionUID = 4520003737132012000L; - + private final static Date clearedServerTime = null; + private final Map linkIdNodeIdxMap = new HashMap(); private byte eventType = EVENT_RAISED; - private long id = 0L; - private String alarmKey = ""; - private String network = ""; - private String neType = ""; - private String equipType = ""; - private String position1 = ""; - private String subPosition1 = null; - private String position2 = null; - private String subPosition2 = null; - private byte severity = -1; - private byte alarmType = -1; - private long probableCause = -1; - private String specificProblem = null; - private String additionalText = null; - private Date raisedTime = new Date(); - private Date raisedServerTime = new Date(); - private Date clearedTime = null; - - private final Date clearedServerTime = null; - private String region = null; - private String site = null; - private String aid = null; - private short systemType = -1; - private boolean rootAlarmFlag = false; - private int linkId = -1; - private int nodeIdx = -1; - private Set linkIds = new HashSet(); - private HashMap priorityMap = new HashMap(); - private HashMap rootAlarmTypeMap = new HashMap(); - private int rootAlarmType = -1; - private boolean keyAlarmFlag = false; - private int keyAlarmType = -1; - private int networkLevel = -1; - private int linkType = -1; - private int centerType; - private final Map linkIdNodeIdxMap = new HashMap(); + public static Alarm valueOf(String xmlString) { + Element element; + try { + StringReader sb = new StringReader(xmlString); + element = new SAXBuilder().build(sb).getRootElement(); + sb.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + Alarm alarm = new Alarm(); + @SuppressWarnings("unchecked") + List list = element.getAttributes(); + for (Attribute attr : list) { + String attrName = attr.getName(); + try { + Field field = Alarm.class.getDeclaredField(attrName); + if (!attrName.endsWith("Time")) { + String type = field.getType().getSimpleName(); + if ("byte".equals(type)) { + field.set(alarm, Byte.parseByte(attr.getValue())); + } else if ("long".equals(type)) { + field.set(alarm, Long.parseLong(attr.getValue())); + } else if ("String".equals(type)) { + field.set(alarm, attr.getValue()); + } else { + throw new RuntimeException("unknow attr type: " + type.toString()); + } + } else { + Date date = new Date(); + date.setTime(Long.parseLong(attr.getValue())); + field.set(alarm, date); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + return alarm; + } public void addLinkIdNodeIdx(int linkId, int index) { linkIdNodeIdxMap.put(linkId, index); @@ -146,11 +151,7 @@ public class Alarm implements AplusData, Cloneable, Serializable { } public boolean containNode(int linkId, int index) { - if (linkIdNodeIdxMap.containsKey(linkId) && linkIdNodeIdxMap.get(linkId) == index) { - return true; - } else { - return false; - } + return linkIdNodeIdxMap.containsKey(linkId) && linkIdNodeIdxMap.get(linkId) == index; } @Override @@ -193,47 +194,6 @@ public class Alarm implements AplusData, Cloneable, Serializable { return new XMLOutputter().outputString(el); } - public static Alarm valueOf(String xmlString) { - Element element = null; - try { - StringReader sb = new StringReader(xmlString); - element = new SAXBuilder().build(sb).getRootElement(); - sb.close(); - } catch (Exception e) { - throw new RuntimeException(e); - } - - Alarm alarm = new Alarm(); - @SuppressWarnings("unchecked") - List list = element.getAttributes(); - for (Attribute attr : list) { - String attrName = attr.getName(); - try { - Field field = Alarm.class.getDeclaredField(attrName); - if (!attrName.endsWith("Time")) { - String type = field.getType().getSimpleName(); - if (type.equals("byte")) { - field.set(alarm, Byte.parseByte(attr.getValue())); - } else if (type.equals("long")) { - field.set(alarm, Long.parseLong(attr.getValue())); - } else if (type.equals("String")) { - field.set(alarm, attr.getValue()); - } else { - throw new RuntimeException("unknow attr type: " + type.toString()); - } - } else { - Date date = new Date(); - date.setTime(Long.parseLong(attr.getValue())); - field.set(alarm, date); - } - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - return alarm; - } - @Override public int hashCode() { return this.getAlarmKey().hashCode(); diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-en-US.json b/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-en-US.json index 8288f6e..0192d01 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-en-US.json +++ b/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-en-US.json @@ -13,7 +13,7 @@ "EXCEPTION_REQUEST_IS_EMPTY": "The request object is empty", "RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED": "Delete rules from engine call interface failure ", "RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED": "From the rules engine calls the deployment of interface failure", - "RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED": "From the rules engine calls the check of interface failure", + "RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED": "From the rules engine calls the check of interface failure", "RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED": "Create query exception", "RULE_MANAGEMENT_QUERY_RULE_FAILED": "Query rule failed", "RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY": "Request object is empty", diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-zh-CN.json b/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-zh-CN.json index 347986a..0b0d007 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-zh-CN.json +++ b/holmes-actions/src/main/java/org/openo/holmes/common/i18n/correlation-i18n-zh-CN.json @@ -9,7 +9,7 @@ "ENGINE_QUERY_CACHE_FAILED":"查询缓存失败", "RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED": "从引擎调用删除规则接口失败", "RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED": "从引擎调用部署规则接口失败", - "RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED": "从引擎调用校验规则接口失败", + "RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED": "从引擎调用校验规则接口失败", "RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED": "创建查询语句异常", "RULE_MANAGEMENT_QUERY_RULE_FAILED": "查询规则失败", "RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY":"请求对象为空", diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java b/holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java index e9c38b9..8cb3d2b 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java @@ -15,6 +15,7 @@ */ package org.openo.holmes.common.producer; +import java.io.Serializable; import javax.inject.Inject; import javax.jms.Connection; import javax.jms.ConnectionFactory; @@ -41,22 +42,33 @@ public class MQProducer { private ConnectionFactory connectionFactory; public void sendAlarmMQTopicMsg(Alarm alarm) { + sendMQTopicMsg(alarm); + } + + public void sendCorrelationMQTopicMsg(String ruleId, long createTimeL, Alarm parentAlarm, + Alarm childAlarm) { + CorrelationResult correlationResult = getCorrelationResult(ruleId, createTimeL, parentAlarm, childAlarm); + sendMQTopicMsg(correlationResult); + } + private void sendMQTopicMsg(T t) { + Serializable msgEntity = (Serializable) t; Connection connection = null; Session session; Destination destination; MessageProducer messageProducer; + try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); - destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM); + destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARMS_CORRELATION); messageProducer = session.createProducer(destination); - ObjectMessage message = session.createObjectMessage(alarm); + ObjectMessage message = session.createObjectMessage(msgEntity); messageProducer.send(message); session.commit(); } catch (Exception e) { - log.error("Failed send alarm." + e.getMessage(), e); + log.error("Failed send correlation." + e.getMessage(), e); } finally { if (connection != null) { try { @@ -68,39 +80,13 @@ public class MQProducer { } } - public void sendCorrelationMQTopicMsg(String ruleId, long createTimeL, Alarm parentAlarm, + private CorrelationResult getCorrelationResult(String ruleId, long createTimeL, Alarm parentAlarm, Alarm childAlarm) { - CorrelationResult correlationResult = new CorrelationResult(); correlationResult.setRuleId(ruleId); correlationResult.setCreateTimeL(createTimeL); correlationResult.setResultType(AplusResult.APLUS_CORRELATION); correlationResult.setAffectedAlarms(new Alarm[]{parentAlarm, childAlarm}); - - Connection connection = null; - Session session; - Destination destination; - MessageProducer messageProducer; - - try { - connection = connectionFactory.createConnection(); - connection.start(); - session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); - destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARMS_CORRELATION); - messageProducer = session.createProducer(destination); - ObjectMessage message = session.createObjectMessage(correlationResult); - messageProducer.send(message); - session.commit(); - } catch (Exception e) { - log.error("Failed send correlation." + e.getMessage(), e); - } finally { - if (connection != null) { - try { - connection.close(); - } catch (JMSException e) { - log.error("Failed close connection." + e.getMessage(), e); - } - } - } + return correlationResult; } } diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/utils/I18nProxy.java b/holmes-actions/src/main/java/org/openo/holmes/common/utils/I18nProxy.java index 75aacb9..c30eff1 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/utils/I18nProxy.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/utils/I18nProxy.java @@ -38,7 +38,7 @@ public class I18nProxy { /*-----------------------Rule Management------------------------------- */ public static final String RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED = "RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED"; public static final String RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED = "RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED"; - public static final String RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED = "RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED"; + public static final String RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED = "RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED"; public static final String RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED = "RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED"; public static final String RULE_MANAGEMENT_QUERY_RULE_FAILED = "RULE_MANAGEMENT_QUERY_RULE_FAILED"; public static final String RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY = "RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY"; diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/utils/JacksonUtil.java b/holmes-actions/src/main/java/org/openo/holmes/common/utils/JacksonUtil.java index caef0b0..f030d3d 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/utils/JacksonUtil.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/utils/JacksonUtil.java @@ -15,10 +15,7 @@ */ package org.openo.holmes.common.utils; -import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; @@ -40,16 +37,4 @@ public class JacksonUtil { } return objectMapper.readValue(json, cls); } - - public static T jsonToBeanByMatchAttribute(String json, Class cls) - throws JsonParseException, - JsonMappingException, IOException { - if (JudgeNullUtil.isEmpty(json)) { - return null; - } - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - return objectMapper.readValue(json, cls); - } - } diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/utils/LanguageUtil.java b/holmes-actions/src/main/java/org/openo/holmes/common/utils/LanguageUtil.java index 1c8f72c..930905a 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/utils/LanguageUtil.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/utils/LanguageUtil.java @@ -25,7 +25,7 @@ public class LanguageUtil { } public static String getLanguage(HttpServletRequest servletRequest) { - String language = (String) servletRequest.getHeader("language-option"); + String language = servletRequest.getHeader("language-option"); if (JudgeNullUtil.isEmpty(language)) { language = AlarmConst.I18N_EN; } @@ -38,7 +38,7 @@ public class LanguageUtil { } public static Locale getLocale(HttpServletRequest servletRequest) { - String language = (String) servletRequest.getHeader("language-option"); + String language = servletRequest.getHeader("language-option"); if (JudgeNullUtil.isEmpty(language)) { language = AlarmConst.I18N_EN; } diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/utils/UserUtil.java b/holmes-actions/src/main/java/org/openo/holmes/common/utils/UserUtil.java index 723b489..ec8fda6 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/utils/UserUtil.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/utils/UserUtil.java @@ -26,7 +26,7 @@ public class UserUtil { public static String getUserName(HttpServletRequest request) { String userName = AlarmConst.ADMIN; - String sessionName = (String) request.getHeader("username"); + String sessionName = request.getHeader("username"); if (sessionName != null) { userName = sessionName.toLowerCase(); } -- cgit 1.2.3-korg