View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
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   */
17  package org.asteriskjava.manager.event;
18  
19  import java.lang.reflect.Method;
20  import java.util.*;
21  
22  import org.asteriskjava.util.ReflectionUtil;
23  
24  /**
25   * Abstract base class for all Events that can be received from the Asterisk
26   * server.
27   * <p/>
28   * Events contain data pertaining to an event generated from within the Asterisk
29   * core or an extension module.
30   * <p/>
31   * There is one conrete subclass of ManagerEvent per each supported Asterisk
32   * Event.
33   *
34   * @author srt
35   * @version $Id$
36   */
37  public abstract class ManagerEvent extends EventObject
38  {
39      /**
40       * Serializable version identifier.
41       */
42      static final long serialVersionUID = 2L;
43  
44      /**
45       * AMI authorization class.
46       */
47      private String privilege;
48  
49      /**
50       * The point in time this event has been received from the Asterisk server.
51       */
52      private Date dateReceived;
53  
54      private Double timestamp;
55  
56      /**
57       * The server from which this event has been received (only used with AstManProxy).
58       */
59      private String server;
60  
61      // AJ-213 only used when debugging is turned on
62      private String file;
63      private Integer line;
64      private String func;
65      private Integer sequenceNumber;
66  
67      public ManagerEvent(Object source)
68      {
69          super(source);
70  
71      }
72  
73      /**
74       * Returns the point in time this event was received from the Asterisk
75       * server.
76       * <p/>
77       * Pseudo events that are not directly received from the asterisk server
78       * (for example ConnectEvent and DisconnectEvent) may return
79       * <code>null</code>.
80       */
81      public Date getDateReceived()
82      {
83          return dateReceived;
84      }
85  
86      /**
87       * Sets the point in time this event was received from the asterisk server.
88       */
89      public void setDateReceived(Date dateReceived)
90      {
91          this.dateReceived = dateReceived;
92      }
93  
94      /**
95       * Returns the AMI authorization class of this event.
96       * <p/>
97       * This is one or more of system, call, log, verbose, command, agent or
98       * user. Multiple privileges are separated by comma.
99       * <p/>
100      * Note: This property is not available from Asterisk 1.0 servers.
101      *
102      * @since 0.2
103      */
104     public String getPrivilege()
105     {
106         return privilege;
107     }
108 
109     /**
110      * Sets the AMI authorization class of this event.
111      *
112      * @since 0.2
113      */
114     public void setPrivilege(String privilege)
115     {
116         this.privilege = privilege;
117     }
118 
119     /**
120      * Returns the timestamp for this event.
121      * <p/>
122      * The timestamp property is available in Asterisk since 1.4 if enabled in
123      * <code>manager.conf</code> by setting <code>timestampevents = yes</code>.
124      * <p/>
125      * In contains the time the event was generated in seconds since the epoch.
126      * <p/>
127      * Example: 1159310429.569108
128      *
129      * @return the timestamp for this event.
130      * @since 0.3
131      */
132     public final Double getTimestamp()
133     {
134         return timestamp;
135     }
136 
137     /**
138      * Sets the timestamp for this event.
139      *
140      * @param timestamp the timestamp to set.
141      * @since 0.3
142      */
143     public final void setTimestamp(Double timestamp)
144     {
145         this.timestamp = timestamp;
146     }
147 
148     /**
149      * Returns the name of the Asterisk server from which this event has been received.
150      * <p/>
151      * This property is only available when using to AstManProxy.
152      *
153      * @return the name of the Asterisk server from which this event has been received
154      *         or <code>null</code> when directly connected to an Asterisk server
155      *         instead of AstManProxy.
156      * @since 1.0.0
157      */
158     public final String getServer()
159     {
160         return server;
161     }
162 
163     /**
164      * Sets the name of the Asterisk server from which this event has been received.
165      *
166      * @param server the name of the Asterisk server from which this event has been received.
167      * @since 1.0.0
168      */
169     public final void setServer(String server)
170     {
171         this.server = server;
172     }
173 
174     /**
175      * Returns the name of the file in Asterisk's source code that triggered this event. For example
176      * <code>pbx.c</code>.<p>
177      * This property is only available if debugging for the Manager API has been turned on in Asterisk. This can be
178      * done by calling <code>manager debug on</code> on Asterisk's command line interface or by adding
179      * <code>debug=on</code> to Asterisk's <code>manager.conf</code>. This feature is availble in Asterisk since 1.6.0.
180      *
181      * @return the name of the file in that triggered this event or <code>null</code> if debgging is turned off.
182      * @see #getFunc()
183      * @see #getLine()
184      * @since 1.0.0
185      */
186     public String getFile()
187     {
188         return file;
189     }
190 
191     public void setFile(String file)
192     {
193         this.file = file;
194     }
195 
196     /**
197      * Returns the line number in Asterisk's source code where this event was triggered.<p>
198      * This property is only available if debugging for the Manager API has been turned on in Asterisk. This can be
199      * done by calling <code>manager debug on</code> on Asterisk's command line interface or by adding
200      * <code>debug=on</code> to Asterisk's <code>manager.conf</code>. This feature is availble in Asterisk since 1.6.0.
201      *
202      * @return the line number where this event was triggered or <code>null</code> if debgging is turned off.
203      * @see #getFile()
204      * @see #getFunc()
205      * @since 1.0.0
206      */
207     public Integer getLine()
208     {
209         return line;
210     }
211 
212     public void setLine(Integer line)
213     {
214         this.line = line;
215     }
216 
217     /**
218      * Returns the name of the C function in Asterisk's source code that triggered this event. For example
219      * <code>pbx_builtin_setvar_helper</code><p>
220      * This property is only available if debugging for the Manager API has been turned on in Asterisk. This can be
221      * done by calling <code>manager debug on</code> on Asterisk's command line interface or by adding
222      * <code>debug=on</code> to Asterisk's <code>manager.conf</code>. This feature is availble in Asterisk since 1.6.0.
223      *
224      * @return the name of the C function that triggered this event or <code>null</code> if debgging is turned off.
225      * @see #getFile()
226      * @see #getLine()
227      * @since 1.0.0
228      */
229     public String getFunc()
230     {
231         return func;
232     }
233 
234     public void setFunc(String func)
235     {
236         this.func = func;
237     }
238 
239     /**
240      * Returns the sequence numbers of this event. Sequence numbers are only incremented while debugging is enabled.<p>
241      * This property is only available if debugging for the Manager API has been turned on in Asterisk. This can be
242      * done by calling <code>manager debug on</code> on Asterisk's command line interface or by adding
243      * <code>debug=on</code> to Asterisk's <code>manager.conf</code>. This feature is availble in Asterisk since 1.6.0.
244      *
245      * @return the sequence number of this event or <code>null</code> if debgging is turned off.
246      * @see #getFile()
247      * @see #getLine()
248      * @since 1.0.0
249      */
250     public Integer getSequenceNumber()
251     {
252         return sequenceNumber;
253     }
254 
255     public void setSequenceNumber(Integer sequenceNumber)
256     {
257         this.sequenceNumber = sequenceNumber;
258     }
259 
260     @Override
261     public String toString()
262     {
263         final List<String> ignoredProperties = Arrays.asList(
264                 "file", "func", "line", "sequenceNumber", "datereceived", "privilege", "source", "class");
265         final StringBuilder sb = new StringBuilder(getClass().getName() + "[");
266         appendPropertyIfNotNull(sb, "file", getFile());
267         appendPropertyIfNotNull(sb, "func", getFunc());
268         appendPropertyIfNotNull(sb, "line", getLine());
269         appendPropertyIfNotNull(sb, "sequenceNumber", getSequenceNumber());
270         appendPropertyIfNotNull(sb, "dateReceived", getDateReceived());
271         appendPropertyIfNotNull(sb, "privilege", getPrivilege());
272 
273         final Map<String, Method> getters = ReflectionUtil.getGetters(getClass());
274         for (Map.Entry<String, Method> entry : getters.entrySet())
275         {
276             final String property = entry.getKey();
277             if (ignoredProperties.contains(property))
278             {
279                 continue;
280             }
281 
282             try
283             {
284                 final Object value = entry.getValue().invoke(this);
285                 appendProperty(sb, property, value);
286             }
287             catch (Exception e) // NOPMD
288             {
289                 // swallow
290             }
291         }
292         sb.append("systemHashcode=").append(System.identityHashCode(this));
293         sb.append("]");
294 
295         return sb.toString();
296     }
297 
298     protected void appendPropertyIfNotNull(StringBuilder sb, String property, Object value)
299     {
300         if (value != null)
301         {
302             appendProperty(sb, property, value);
303         }
304     }
305 
306     private void appendProperty(StringBuilder sb, String property, Object value)
307     {
308         sb.append(property).append("=");
309         if (value == null)
310         {
311             sb.append("null");
312         }
313         else
314         {
315             sb.append("'").append(value).append("'");
316         }
317         sb.append(",");
318 
319     }
320 }