1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.asterisk.fastagi;
18
19 import java.io.IOException;
20
21 /***
22 * Runs an AGIServer in a separate Thread.<br>
23 * You can use this class to run an AGIServer in the background of your
24 * application or run it in your webcontainer or application server.
25 *
26 * @author srt
27 * @version $Id: AGIServerThread.java,v 1.2 2005/10/25 22:26:21 srt Exp $
28 * @since 0.2
29 */
30 public class AGIServerThread
31 {
32 private AGIServer agiServer;
33 private Thread thread;
34
35 /***
36 * Sets the AGIServer to run.
37 *
38 * @param agiServer the AGIServer to run.
39 */
40 public void setAgiServer(AGIServer agiServer)
41 {
42 this.agiServer = agiServer;
43 }
44
45 /***
46 * Starts the AGIServer in its own thread.<br>
47 * Note: The AGIServerThread is designed to handle on AGIServer instance at
48 * a time so calling this method twice without stopping the AGIServer in
49 * between will result in a RuntimeException.
50 */
51 public synchronized void startup()
52 {
53 if (agiServer == null)
54 {
55 throw new RuntimeException(
56 "Mandatory property agiServer is not set.");
57 }
58
59 if (thread != null)
60 {
61 throw new RuntimeException("AGIServer is already started");
62 }
63
64 thread = new Thread(new Runnable()
65 {
66 public void run()
67 {
68 try
69 {
70 agiServer.startup();
71 }
72 catch (IOException e)
73 {
74 throw new RuntimeException("Unable to start AGIServer.", e);
75 }
76 }
77 });
78 thread.setName("AGIServer Thread");
79 thread.start();
80 }
81
82 /***
83 * Stops the AGIServer.
84 */
85 public synchronized void shutdown()
86 {
87 if (agiServer == null)
88 {
89 throw new RuntimeException(
90 "Mandatory property agiServer is not set.");
91 }
92
93 try
94 {
95 agiServer.shutdown();
96 }
97 catch (IOException e)
98 {
99 throw new RuntimeException("Unable to stop AGIServer.", e);
100 }
101
102 thread = null;
103 }
104 }