View Javadoc

1   /*
2    * Copyright  2004-2005 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 net.sf.asterisk.manager;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.List;
23  
24  /***
25   * @author srt
26   * @version $Id: Channel.java,v 1.13 2005/09/01 19:13:20 srt Exp $
27   */
28  public class Channel implements Serializable
29  {
30      /***
31       * Serializable version identifier
32       */
33      private static final long serialVersionUID = -6919877370396385380L;
34  
35      private AsteriskServer asteriskServer;
36  
37      /***
38       * Unique id of this channel.
39       */
40      private final String id;
41  
42      /***
43       * Name of this channel.
44       */
45      private String name;
46  
47      /***
48       * Caller ID of this channel.
49       */
50      private String callerId;
51  
52      /***
53       * Caller ID Name of this channel.
54       */
55      private String callerIdName;
56  
57      /***
58       * State of this channel.
59       */
60      private ChannelStateEnum state;
61  
62      /***
63       * Account code used to bill this channel.
64       */
65      private String account;
66      private final List extensions;
67  
68      /***
69       * Date this channel has been created.
70       */
71      private Date dateOfCreation;
72  
73      /***
74       * If this channel is bridged to another channel, the linkedChannel contains
75       * the channel this channel is bridged with.
76       */
77      private Channel linkedChannel;
78  
79      /***
80       * Indicates if this channel was linked to another channel at least once.
81       */
82      private boolean wasLinked;
83  
84      /***
85       * Creates a new Channel.
86       * 
87       * @param name name of this channel, for example "SIP/1310-20da".
88       * @param id unique id of this channel, for example "1099015093.165".
89       */
90      public Channel(final String name, final String id)
91      {
92          this(name, id, null);
93      }
94  
95      /***
96       * Creates a new Channel on the given server.
97       * 
98       * @param name name of this channel, for example "SIP/1310-20da".
99       * @param id unique id of this channel, for example "1099015093.165".
100      * @param server the Asterisk server this channel exists on.
101      */
102     public Channel(final String name, final String id,
103             final AsteriskServer server)
104     {
105         this.name = name;
106         this.id = id;
107         this.asteriskServer = server;
108         this.extensions = new ArrayList();
109     }
110 
111     /***
112      * Returns the Asterisk server.
113      * 
114      * @return the Asterisk server.
115      */
116     public final AsteriskServer getAsteriskServer()
117     {
118         return asteriskServer;
119     }
120 
121     /***
122      * Sets the Asterisk server.
123      * 
124      * @param asteriskServer the Asterisk server to set.
125      */
126     public final void setAsteriskServer(final AsteriskServer asteriskServer)
127     {
128         this.asteriskServer = asteriskServer;
129     }
130 
131     /***
132      * Returns the unique id of this channel, for example "1099015093.165".
133      * 
134      * @return the unique id of this channel.
135      */
136     public final String getId()
137     {
138         return id;
139     }
140 
141     /***
142      * Returns the name of this channel, for example "SIP/1310-20da".
143      * 
144      * @return the name of this channel.
145      */
146     public final String getName()
147     {
148         return name;
149     }
150 
151     /***
152      * Sets the name of this channel.
153      * 
154      * @param name the name of this channel.
155      */
156     public final void setName(final String name)
157     {
158         this.name = name;
159     }
160 
161     /***
162      * Returns the caller id of this channel.
163      * 
164      * @return the caller id of this channel.
165      */
166     public final String getCallerId()
167     {
168         return callerId;
169     }
170 
171     /***
172      * Sets the caller id of this channel.
173      * 
174      * @param callerId the caller id of this channel.
175      */
176     public final void setCallerId(final String callerId)
177     {
178         this.callerId = callerId;
179     }
180 
181     /***
182      * Returns the caller id name of this channel.
183      * 
184      * @return the caller id name of this channel.
185      */
186     public final String getCallerIdName()
187     {
188         return callerIdName;
189     }
190 
191     /***
192      * Sets the caller id of this channel.
193      * 
194      * @param callerIdName the caller id name of this channel.
195      */
196     public final void setCallerIdName(final String callerIdName)
197     {
198         this.callerIdName = callerIdName;
199     }
200 
201     /***
202      * Returns the state of this channel.
203      * 
204      * @return the state of this channel.
205      */
206     public final ChannelStateEnum getState()
207     {
208         return state;
209     }
210 
211     /***
212      * Sets the state of this channel.
213      * 
214      * @param state the state of this channel.
215      */
216     public final void setState(final ChannelStateEnum state)
217     {
218         this.state = state;
219     }
220 
221     /***
222      * Returns the account code used to bill this channel.
223      * 
224      * @return the account code used to bill this channel.
225      */
226     public final String getAccount()
227     {
228         return account;
229     }
230 
231     /***
232      * Sets the account code used to bill this channel.
233      * 
234      * @param account the account code used to bill this channel.
235      */
236     public final void setAccount(final String account)
237     {
238         this.account = account;
239     }
240 
241     /***
242      * Returns the last visited dialplan entry.
243      * 
244      * @return the last visited dialplan entry.
245      * @since 0.2
246      */
247     public final Extension getCurrentExtension()
248     {
249         Extension extension;
250 
251         synchronized (extensions)
252         {
253             if (extensions.isEmpty())
254             {
255                 extension = null;
256             }
257             else
258             {
259                 extension = (Extension) extensions.get(extensions.size() - 1);
260             }
261         }
262 
263         return extension;
264     }
265 
266     /***
267      * Returns the first visited dialplan entry.
268      * 
269      * @return the first visited dialplan entry.
270      * @since 0.2
271      */
272     public Extension getFirstExtension()
273     {
274         Extension extension;
275 
276         synchronized (extensions)
277         {
278             if (extensions.isEmpty())
279             {
280                 extension = null;
281             }
282             else
283             {
284                 extension = (Extension) extensions.get(0);
285             }
286         }
287 
288         return extension;
289     }
290 
291     /***
292      * Returns the context of the current extension. This is a shortcut for
293      * <code>getCurrentExtension().getContext()</code>.
294      * 
295      * @return the context of the current extension.
296      */
297     public String getContext()
298     {
299         Extension currentExtension;
300         
301         currentExtension = getCurrentExtension();
302         return currentExtension == null ? null : currentExtension.getContext();
303     }
304 
305     /***
306      * Returns the extension of the current extension. This is a shortcut for
307      * <code>getCurrentExtension().getExtension()</code>.
308      * 
309      * @return the extension of the current extension.
310      */
311     public String getExtension()
312     {
313         Extension currentExtension;
314         
315         currentExtension = getCurrentExtension();
316         return currentExtension == null ? null : currentExtension.getExtension();
317     }
318 
319     /***
320      * Returns the priority of the current extension. This is a shortcut for
321      * <code>getCurrentExtension().getPriority()</code>.
322      * 
323      * @return the priority of the current extension.
324      */
325     public Integer getPriority()
326     {
327         Extension currentExtension;
328         
329         currentExtension = getCurrentExtension();
330         return currentExtension == null ? null : currentExtension.getPriority();
331     }
332     
333     /***
334      * Returns a list of all visited dialplan entries.
335      * 
336      * @return a list of all visited dialplan entries.
337      * @since 0.2
338      */
339     public List getExtensions()
340     {
341         List extensionsCopy;
342 
343         synchronized (extensions)
344         {
345             extensionsCopy = new ArrayList(extensions);
346         }
347 
348         return extensionsCopy;
349     }
350 
351     /***
352      * Adds a visted dialplan entry to the history.
353      * 
354      * @param extension the visted dialplan entry to add.
355      * @since 0.2
356      */
357     public void addExtension(Extension extension)
358     {
359         synchronized (extensions)
360         {
361             extensions.add(extension);
362         }
363     }
364 
365     /***
366      * Returns the date this channel has been created.
367      * 
368      * @return the date this channel has been created.
369      */
370     public final Date getDateOfCreation()
371     {
372         return dateOfCreation;
373     }
374 
375     /***
376      * Sets the date this channel has been created.
377      * 
378      * @param dateOfCreation the date this channel has been created.
379      */
380     public final void setDateOfCreation(final Date dateOfCreation)
381     {
382         this.dateOfCreation = dateOfCreation;
383     }
384 
385     /***
386      * Returns the channel this channel is bridged with, if any.
387      * 
388      * @return the channel this channel is bridged with, or <code>null</code>
389      *         if this channel is currently not bridged to another channel.
390      */
391     public final Channel getLinkedChannel()
392     {
393         return linkedChannel;
394     }
395 
396     /***
397      * Sets the channel this channel is bridged with.
398      * 
399      * @param linkedChannel the channel this channel is bridged with.
400      */
401     public final void setLinkedChannel(final Channel linkedChannel)
402     {
403         this.linkedChannel = linkedChannel;
404         if (linkedChannel != null)
405         {
406             this.wasLinked = true;
407         }
408     }
409 
410     /***
411      * Indicates if this channel was linked to another channel at least once.
412      * 
413      * @return <code>true</code> if this channel was linked to another channel
414      *         at least once, <code>false</code> otherwise.
415      * @since 0.2
416      */
417     public final boolean getWasLinked()
418     {
419         return wasLinked;
420     }
421 
422     public String toString()
423     {
424         StringBuffer sb;
425         Channel linkedChannel;
426         int systemHashcode;
427 
428         sb = new StringBuffer(getClass().getName() + ": ");
429 
430         synchronized (this)
431         {
432             sb.append("id='" + getId() + "'; ");
433             sb.append("name='" + getName() + "'; ");
434             sb.append("callerId='" + getCallerId() + "'; ");
435             sb.append("state='" + getState() + "'; ");
436             sb.append("account='" + getAccount() + "'; ");
437             sb.append("dateOfCreation=" + getDateOfCreation() + "; ");
438             linkedChannel = this.linkedChannel;
439             systemHashcode = System.identityHashCode(this);
440         }
441         if (linkedChannel == null)
442         {
443             sb.append("linkedChannel=null; ");
444         }
445         else
446         {
447             sb.append("linkedChannel=[");
448             synchronized (linkedChannel)
449             {
450                 sb.append(linkedChannel.getClass().getName() + ": ");
451                 sb.append("id='" + linkedChannel.getId() + "'; ");
452                 sb.append("name='" + linkedChannel.getName() + "'; ");
453                 sb.append("systemHashcode="
454                         + System.identityHashCode(linkedChannel));
455             }
456             sb.append("]; ");
457         }
458         sb.append("systemHashcode=" + systemHashcode);
459 
460         return sb.toString();
461     }
462 }