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.io.Serializable;
20  import java.lang.reflect.Method;
21  import java.util.Date;
22  import java.util.EventObject;
23  import java.util.Map;
24  
25  import org.asteriskjava.util.ReflectionUtil;
26  
27  /***
28   * Abstract base class for all Events that can be received from the Asterisk
29   * server.
30   * <p>
31   * Events contain data pertaining to an event generated from within the Asterisk
32   * core or an extension module.
33   * <p>
34   * There is one conrete subclass of ManagerEvent per each supported Asterisk
35   * Event.
36   * 
37   * @author srt
38   * @version $Id: ManagerEvent.java 564 2006-09-26 22:54:31Z srt $
39   */
40  public abstract class ManagerEvent extends EventObject implements Serializable
41  {
42      /***
43       * Serializable version identifier
44       */
45      static final long serialVersionUID = 4299374743315152040L;
46  
47      /***
48       * AMI authorization class.
49       */
50      private String privilege;
51  
52      /***
53       * The point in time this event has been received from the Asterisk server.
54       */
55      private Date dateReceived;
56  
57      private Double timestamp;
58  
59      /***
60       * @param source
61       */
62      public ManagerEvent(Object source)
63      {
64          super(source);
65  
66      }
67  
68      /***
69       * Returns the point in time this event was received from the Asterisk
70       * server.
71       * <p>
72       * Pseudo events that are not directly received from the asterisk server
73       * (for example ConnectEvent and DisconnectEvent) may return
74       * <code>null</code>.
75       */
76      public Date getDateReceived()
77      {
78          return dateReceived;
79      }
80  
81      /***
82       * Sets the point in time this event was received from the asterisk server.
83       */
84      public void setDateReceived(Date dateReceived)
85      {
86          this.dateReceived = dateReceived;
87      }
88  
89      /***
90       * Returns the AMI authorization class of this event.
91       * <p>
92       * This is one or more of system, call, log, verbose, command, agent or
93       * user. Multiple privileges are separated by comma.
94       * <p>
95       * Note: This property is not available from Asterisk 1.0 servers.
96       * 
97       * @since 0.2
98       */
99      public String getPrivilege()
100     {
101         return privilege;
102     }
103 
104     /***
105      * Sets the AMI authorization class of this event.
106      * 
107      * @since 0.2
108      */
109     public void setPrivilege(String privilege)
110     {
111         this.privilege = privilege;
112     }
113 
114     /***
115      * Returns the timestamp for this event.
116      * <p>
117      * The timestamp property is available in Asterisk since 1.4 if enabled in
118      * <code>manager.conf</code> by setting <code>timestampevents = yes</code>.
119      * <p>
120      * In contains the time the event was generated in seconds since the epoch.
121      * <p>
122      * Example: 1159310429.569108
123      * 
124      * @return
125      * @since 0.3
126      */
127     public final Double getTimestamp()
128     {
129         return timestamp;
130     }
131 
132     /***
133      * Sets the timestamp for this event.
134      * 
135      * @param timestamp the timestamp to set.
136      * @since 0.3
137      */
138     public final void setTimestamp(Double timestamp)
139     {
140         this.timestamp = timestamp;
141     }
142 
143     @Override
144     public String toString()
145     {
146         StringBuffer sb;
147         Map<String, Method> getters;
148 
149         sb = new StringBuffer(getClass().getName() + "[");
150         sb.append("dateReceived=" + getDateReceived() + ",");
151         if (getPrivilege() != null)
152         {
153             sb.append("privilege='" + getPrivilege() + "',");
154         }
155         getters = ReflectionUtil.getGetters(getClass());
156         for (String attribute : getters.keySet())
157         {
158             if ("class".equals(attribute) || "datereceived".equals(attribute) || "privilege".equals(attribute)
159                     || "source".equals(attribute))
160             {
161                 continue;
162             }
163 
164             try
165             {
166                 Object value;
167                 value = getters.get(attribute).invoke(this);
168                 sb.append(attribute + "='" + value + "',");
169             }
170             catch (Exception e) // NOPMD
171             {
172             }
173         }
174         sb.append("systemHashcode=" + System.identityHashCode(this));
175         sb.append("]");
176 
177         return sb.toString();
178     }
179 }