1 package org.asteriskjava;
2
3 import org.asteriskjava.fastagi.DefaultAgiServer;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Properties;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class Cli
28 {
29 private void parseOptions(String[] args) throws Exception
30 {
31 if (args.length == 0)
32 {
33 startAgiServer();
34 return;
35 }
36
37 final String arg = args[0];
38 if ("-h".equals(arg) || "-help".equals(arg))
39 {
40 showHelp();
41 }
42 else if ("-v".equals(arg) || "-version".equals(arg))
43 {
44 showVersion();
45 }
46 else if ("-a".equals(arg) || "-agi".equals(arg))
47 {
48 if (args.length >= 2)
49 {
50 Integer port = null;
51 try
52 {
53 port = new Integer(args[1]);
54 }
55 catch (NumberFormatException e)
56 {
57 System.err.println("Invalid port '" + args[1] + "'. Port must be a number.");
58 exit(1);
59 }
60 startAgiServer(port);
61 }
62 }
63 else
64 {
65 showHelp();
66 }
67 }
68
69 private void showHelp()
70 {
71 showVersion();
72 System.err.println();
73 System.err.println("-a, -agi [port]\n\tStarts a FastAGI server");
74 System.err.println("-h, -help\n\tDisplays the available options\n");
75 System.err.println("-v, -version\n\tDisplays the version of Asterisk-Java\n");
76 }
77
78 private void showVersion()
79 {
80 System.out.println("Asterisk-Java " + getVersion());
81 }
82
83 private String getVersion()
84 {
85 String version = "<unknown>";
86 final InputStream is;
87 final Properties properties;
88
89 is = getClass().getResourceAsStream("/META-INF/maven/org.asteriskjava/asterisk-java/pom.properties");
90 if (is == null)
91 {
92 return version;
93 }
94
95 properties = new Properties();
96 try
97 {
98 properties.load(is);
99 }
100 catch (IOException e)
101 {
102 return version;
103 }
104 finally
105 {
106 try
107 {
108 is.close();
109 }
110 catch (IOException e)
111 {
112
113 }
114 }
115
116 version = properties.getProperty("version", version);
117 return version;
118 }
119
120 private void startAgiServer() throws IOException
121 {
122 startAgiServer(null);
123 }
124
125 private void startAgiServer(Integer port) throws IOException
126 {
127 final DefaultAgiServer server;
128
129 server = new DefaultAgiServer();
130 if (port != null)
131 {
132 server.setPort(port);
133 }
134 server.startup();
135 }
136
137 private void exit(int code)
138 {
139 System.exit(code);
140 }
141
142 public static void main(String[] args) throws Exception
143 {
144 new Cli().parseOptions(args);
145 }
146 }