1 /******************************************************************************* 2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 *******************************************************************************/ 16 package it.jnrpe.events; 17 18 import java.util.HashMap; 19 import java.util.Map; 20 21 /** 22 * A basic, very simple, general purpose event. 23 * 24 * @author Massimiliano Ziccardi 25 */ 26 class SimpleEvent implements IJNRPEEvent { 27 /** 28 * The log event associated with this object. 29 */ 30 private LogEvent logEventType = null; 31 32 /** 33 * The parameters of the vent. 34 */ 35 private Map<String, Object> parametersMap = new HashMap<String, Object>(); 36 37 /** 38 * The custom event type (if it is not a log event). 39 */ 40 private String customEventType = null; 41 42 /** 43 * Builds the {@link SimpleEvent} object with a LogEvent. 44 * 45 * @param evt 46 * The Log Event 47 * @param paramsAry 48 * The Log Event parameters 49 */ 50 public SimpleEvent(final LogEvent evt, final Object[] paramsAry) { 51 if (evt == null) { 52 throw new NullPointerException("Event type can't be null"); 53 } 54 logEventType = evt; 55 for (int i = 0; paramsAry != null && i < paramsAry.length; i += 2) { 56 parametersMap.put((String) paramsAry[i], paramsAry[i + 1]); 57 } 58 } 59 60 /** 61 * Builds the {@link SimpleEvent} object with a custom event type. 62 * 63 * @param custEvtType 64 * The Custom Event Type 65 * @param paramsAry 66 * The Event parameters 67 */ 68 public SimpleEvent(final String custEvtType, final Object[] paramsAry) { 69 if (custEvtType == null) { 70 throw new NullPointerException("Event type can't be null"); 71 } 72 73 customEventType = custEvtType; 74 for (int i = 0; paramsAry != null && i < paramsAry.length; i += 2) { 75 EventParam param = (EventParam) paramsAry[i]; 76 parametersMap.put(param.getName(), param.getValue()); 77 } 78 } 79 80 /** 81 * Returns the event name. 82 * 83 * @return The event name 84 */ 85 public String getEventName() { 86 if (customEventType != null) { 87 return customEventType; 88 } 89 return logEventType.name(); 90 } 91 92 /** 93 * Returns the LogEvent type (<code>null</code> if it is not a log event). 94 * 95 * @return The LogEvent type 96 */ 97 LogEvent getLogEvent() { 98 return logEventType; 99 } 100 101 /** 102 * Returns the event parameters. 103 * 104 * @return The event parameters 105 */ 106 public Map<String, Object> getEventParams() { 107 return parametersMap; 108 } 109 110 }