All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.asteriskjava.manager.event.ManagerEvent Maven / Gradle / Ivy

/*
 * Copyright 2004-2006 Stefan Reuter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.asteriskjava.manager.event;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.asteriskjava.util.AstState;
import org.asteriskjava.util.ReflectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Abstract base class for all Events that can be received from the Asterisk
 * server. 
* Events contain data pertaining to an event generated from within the Asterisk * core or an extension module.
* There is one conrete subclass of ManagerEvent per each supported Asterisk * Event. * * @author srt * @version $Id$ */ public abstract class ManagerEvent extends EventObject { private static final long serialVersionUID = 5463789563138128619L; private static final Logger logger = LoggerFactory.getLogger(ManagerEvent.class); protected String callerIdName; protected String callerIdNum; protected Integer channelState; protected String channelStateDesc; protected String connectedLineName; protected String connectedLineNum; protected String context; /** * The point in time this event has been received from the Asterisk server. */ private Date dateReceived; protected String exten; // AJ-213 only used when debugging is turned on private String file; private String func; private Integer line; protected Integer priority; /** * AMI authorization class. */ private String privilege; private Integer sequenceNumber; /** * The server from which this event has been received (only used with * AstManProxy). */ private String server; private String systemName; private Double timestamp; public ManagerEvent(Object source) { super(source); } private void appendProperty(StringBuilder sb, String property, Object value) { sb.append(property) .append("="); if (value == null) { sb.append("null"); } else { sb.append("'") .append(value) .append("'"); } sb.append(","); } protected void appendPropertyIfNotNull(StringBuilder sb, String property, Object value) { if (value != null) { appendProperty(sb, property, value); } } /** * Returns the Caller ID name of the caller's channel. * * @return the Caller ID name of the caller's channel or "unknown" if none * has been set. * @since 0.2 */ public String getCallerIdName() { return callerIdName; } public String getCallerIdNum() { return callerIdNum; } public Integer getChannelState() { return channelState == null ? AstState.str2state(channelStateDesc) : channelState; } public String getChannelStateDesc() { return channelStateDesc; } public String getConnectedLineName() { return connectedLineName; } public String getConnectedLineNum() { return connectedLineNum; } public String getContext() { return context; } /** * Returns the point in time this event was received from the Asterisk server.
* Pseudo events that are not directly received from the asterisk server * (for example ConnectEvent and DisconnectEvent) may return * null. * * @return time this event was received from the Asterisk server */ public Date getDateReceived() { return dateReceived; } public String getExten() { return exten; } /** * Returns the name of the file in Asterisk's source code that triggered * this event. For example pbx.c. *

* This property is only available if debugging for the Manager API has been * turned on in Asterisk. This can be done by calling * manager debug on on Asterisk's command line interface or by * adding debug=on to Asterisk's manager.conf. * This feature is availble in Asterisk since 1.6.0. * * @return the name of the file in that triggered this event or * null if debgging is turned off. * @see #getFunc() * @see #getLine() * @since 1.0.0 */ public String getFile() { return file; } /** * Returns the name of the C function in Asterisk's source code that * triggered this event. For example pbx_builtin_setvar_helper *

* This property is only available if debugging for the Manager API has been * turned on in Asterisk. This can be done by calling * manager debug on on Asterisk's command line interface or by * adding debug=on to Asterisk's manager.conf. * This feature is availble in Asterisk since 1.6.0. * * @return the name of the C function that triggered this event or * null if debgging is turned off. * @see #getFile() * @see #getLine() * @since 1.0.0 */ public String getFunc() { return func; } /** * Returns the line number in Asterisk's source code where this event was * triggered. *

* This property is only available if debugging for the Manager API has been * turned on in Asterisk. This can be done by calling * manager debug on on Asterisk's command line interface or by * adding debug=on to Asterisk's manager.conf. * This feature is availble in Asterisk since 1.6.0. * * @return the line number where this event was triggered or * null if debgging is turned off. * @see #getFile() * @see #getFunc() * @since 1.0.0 */ public Integer getLine() { return line; } public Integer getPriority() { return priority; } /** * Returns the AMI authorization class of this event.
* This is one or more of system, call, log, verbose, command, agent or * user. Multiple privileges are separated by comma.
* Note: This property is not available from Asterisk 1.0 servers. * * @since 0.2 * * @return privilege */ public String getPrivilege() { return privilege; } /** * Returns the sequence numbers of this event. Sequence numbers are only * incremented while debugging is enabled. *

* This property is only available if debugging for the Manager API has been * turned on in Asterisk. This can be done by calling * manager debug on on Asterisk's command line interface or by * adding debug=on to Asterisk's manager.conf. * This feature is availble in Asterisk since 1.6.0. * * @return the sequence number of this event or null if * debgging is turned off. * @see #getFile() * @see #getLine() * @since 1.0.0 */ public Integer getSequenceNumber() { return sequenceNumber; } /** * Returns the name of the Asterisk server from which this event has been * received.
* This property is only available when using to AstManProxy. * * @return the name of the Asterisk server from which this event has been * received or null when directly connected to an * Asterisk server instead of AstManProxy. * @since 1.0.0 */ public final String getServer() { return server; } public String getSystemName() { return systemName; } /** * Returns the timestamp for this event.
* The timestamp property is available in Asterisk since 1.4 if enabled in * manager.conf by setting timestampevents = yes.
* In contains the time the event was generated in seconds since the epoch.
* Example: 1159310429.569108 * * @return the timestamp for this event. * @since 0.3 */ public final Double getTimestamp() { return timestamp; } public void setCallerIdName(String callerIdName) { this.callerIdName = callerIdName; } public void setCallerIdNum(String callerIdNum) { this.callerIdNum = callerIdNum; } public void setChannelState(Integer channelState) { this.channelState = channelState; } public void setChannelStateDesc(String channelStateDesc) { this.channelStateDesc = channelStateDesc; } public void setConnectedLineName(String connectedLineName) { this.connectedLineName = connectedLineName; } public void setConnectedLineNum(String connectedLineNum) { this.connectedLineNum = connectedLineNum; } public void setContext(String context) { this.context = context; } /** * Sets the point in time this event was received from the asterisk server. * * @param dateReceived * time this event was received from the asterisk server */ public void setDateReceived(Date dateReceived) { this.dateReceived = dateReceived; } public void setExten(String exten) { this.exten = exten; } public void setFile(String file) { this.file = file; } public void setFunc(String func) { this.func = func; } public void setLine(Integer line) { this.line = line; } public void setPriority(Integer priority) { this.priority = priority; } /** * Sets the AMI authorization class of this event. * * @since 0.2 * * @param privilege * privilege */ public void setPrivilege(String privilege) { this.privilege = privilege; } public void setSequenceNumber(Integer sequenceNumber) { this.sequenceNumber = sequenceNumber; } /** * Sets the name of the Asterisk server from which this event has been * received. * * @param server * the name of the Asterisk server from which this event has * been received. * @since 1.0.0 */ public final void setServer(String server) { this.server = server; } public void setSystemName(String systemName) { this.systemName = systemName; } /** * Sets the timestamp for this event. * * @param timestamp * the timestamp to set. * @since 0.3 */ public final void setTimestamp(Double timestamp) { this.timestamp = timestamp; } public String toJsonString() { final List ignoredProperties = Arrays.asList("file", "func", "line", "sequenceNumber", "datereceived", "privilege", "source", "class"); final Map rootMap = new HashMap<>(); rootMap.put("eventName", getClass().getSimpleName()); rootMap.put("file", getFile()); rootMap.put("line", getLine()); final Map getters = ReflectionUtil.getGetters(getClass()); for (Map.Entry entry : getters.entrySet()) { final String property = entry.getKey(); if (ignoredProperties.contains(property)) { continue; } try { final Object value = entry.getValue() .invoke(this); rootMap.put(property, value); } catch (Exception e) // NOPMD { // swallow } } String mapAsJson = ""; try { mapAsJson = new ObjectMapper().writeValueAsString(rootMap); } catch (Exception e) { logger.error(e.getMessage()); } return mapAsJson; } @Override public String toString() { final List ignoredProperties = Arrays.asList("file", "func", "line", "sequenceNumber", "datereceived", "privilege", "source", "class"); final StringBuilder sb = new StringBuilder(getClass().getName() + "["); appendPropertyIfNotNull(sb, "file", getFile()); appendPropertyIfNotNull(sb, "func", getFunc()); appendPropertyIfNotNull(sb, "line", getLine()); appendPropertyIfNotNull(sb, "sequenceNumber", getSequenceNumber()); appendPropertyIfNotNull(sb, "dateReceived", getDateReceived()); appendPropertyIfNotNull(sb, "privilege", getPrivilege()); final Map getters = ReflectionUtil.getGetters(getClass()); for (Map.Entry entry : getters.entrySet()) { final String property = entry.getKey(); if (ignoredProperties.contains(property)) { continue; } try { final Object value = entry.getValue() .invoke(this); appendProperty(sb, property, value); } catch (Exception e) // NOPMD { // swallow } } sb.append("systemHashcode=") .append(System.identityHashCode(this)); sb.append("]"); return sb.toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy