-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathCli.java
More file actions
148 lines (133 loc) · 3.57 KB
/
Cli.java
File metadata and controls
148 lines (133 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.asteriskjava;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.asteriskjava.fastagi.DefaultAgiServer;
/**
* Simple command line interface for Asterisk-Java. This class is run when
* Asterisk-Java is started with {@code java -jar asterisk-java.jar}. It is
* configured as Main-Class in the manifest.
* <p>
* The command line interface supports the following options:
* <dl>
* <dt>{@code -a}, {@code -agi [port]}
* <dt>
* <dd>Starts a FastAGI server</dd>
* <dt>{@code -h}, {@code -help}
* <dt>
* <dd>Displays the available options</dd>
* <dt>{@code -v}, {@code -version}</dt>
* <dd>Displays the version of Asterisk-Java</dd>
* </dl>
* If no option is given a FastAGI server is started on the default port.
*
* @since 1.0.0
*/
public class Cli
{
private void parseOptions(String[] args) throws Exception
{
if (args.length == 0)
{
startAgiServer();
return;
}
final String arg = args[0];
if ("-h".equals(arg) || "-help".equals(arg))
{
showHelp();
}
else if ("-v".equals(arg) || "-version".equals(arg))
{
showVersion();
}
else if ("-a".equals(arg) || "-agi".equals(arg))
{
if (args.length >= 2)
{
Integer port = null;
try
{
port = new Integer(args[1]);
}
catch (NumberFormatException e)
{
System.err.println("Invalid port '" + args[1] + "'. Port must be a number.");
exit(1);
}
startAgiServer(port);
}
}
else
{
showHelp();
}
}
private void showHelp()
{
showVersion();
System.err.println();
System.err.println("-a, -agi [port]\n\tStarts a FastAGI server");
System.err.println("-h, -help\n\tDisplays the available options\n");
System.err.println("-v, -version\n\tDisplays the version of Asterisk-Java\n");
}
private void showVersion()
{
System.out.println("Asterisk-Java " + getVersion());
}
private String getVersion()
{
String version = "<unknown>";
final InputStream is;
final Properties properties;
is = getClass().getResourceAsStream("/META-INF/maven/org.asteriskjava/asterisk-java/pom.properties");
if (is == null)
{
return version;
}
properties = new Properties();
try
{
properties.load(is); // contains version, groupId and artifactId
}
catch (IOException e)
{
return version;
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
// ignore
}
}
version = properties.getProperty("version", version);
return version;
}
private void startAgiServer() throws IOException
{
startAgiServer(null);
}
private void startAgiServer(Integer port) throws IOException
{
final DefaultAgiServer server;
server = new DefaultAgiServer();
if (port != null)
{
server.setPort(port);
}
server.startup();
}
private void exit(int code)
{
System.exit(code);
}
public static void main(String[] args) throws Exception
{
new Cli().parseOptions(args);
}
}