Skip to content

Commit cee2c81

Browse files
authored
ref: Adapt to jicoco changes, move CmdLine to jigasi. (#575)
* ref: Adapt to jicoco changes, move CmdLine to jigasi. * chore: Update jicoco.
1 parent 83029d2 commit cee2c81

File tree

5 files changed

+307
-3
lines changed

5 files changed

+307
-3
lines changed

pom.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<smack.version>4.4.8</smack.version>
2323
<jxmppVersion>1.0.3</jxmppVersion>
2424
<!-- Match jicoco's jetty version. -->
25-
<jicoco.version>1.1-140-g8f45a9f</jicoco.version>
25+
<jicoco.version>1.1-150-g57913c0</jicoco.version>
2626
<jetty.version>11.0.20</jetty.version>
2727
<oci-sdk.version>3.45.0</oci-sdk.version>
2828
</properties>
@@ -130,7 +130,12 @@
130130
<!-- org.jitsi -->
131131
<dependency>
132132
<groupId>${project.groupId}</groupId>
133-
<artifactId>jicoco</artifactId>
133+
<artifactId>jicoco-jetty</artifactId>
134+
<version>${jicoco.version}</version>
135+
</dependency>
136+
<dependency>
137+
<groupId>${project.groupId}</groupId>
138+
<artifactId>jicoco-mucclient</artifactId>
134139
<version>${jicoco.version}</version>
135140
</dependency>
136141
<dependency>

src/main/java/org/jitsi/jigasi/Main.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import net.java.sip.communicator.service.protocol.media.*;
4040
import net.java.sip.communicator.util.*;
4141
import org.apache.commons.lang3.*;
42-
import org.jitsi.cmd.*;
4342
import org.jitsi.impl.osgi.framework.launch.*;
43+
import org.jitsi.jigasi.cmd.*;
4444
import org.jitsi.jigasi.osgi.*;
4545
import org.jitsi.jigasi.rest.*;
4646
import org.jitsi.jigasi.version.*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright @ 2015 - present, 8x8 Inc
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+
package org.jitsi.jigasi.cmd;
17+
18+
19+
import org.jitsi.utils.logging.*;
20+
21+
import java.util.*;
22+
23+
/**
24+
* Utility class for parsing command line arguments that take some value.
25+
* Arguments can have one of the following formats:
26+
* <ul>
27+
* <li>"arg=value"</li>
28+
* <li>"-arg=value"</li>
29+
* <li>"--arg=value"</li>
30+
* </ul>
31+
* It's also possible to specify required arguments. If any of required
32+
* arguments is not found {@link ParseException} will be thrown by
33+
* {@link #parse(String[])}.
34+
*
35+
* @author Pawel Domas
36+
*/
37+
public class CmdLine
38+
{
39+
/**
40+
* The logger
41+
*/
42+
private final static Logger logger = Logger.getLogger(CmdLine.class);
43+
44+
/**
45+
* Map of argument values.
46+
*/
47+
private Map<String, String> argMap = new HashMap<String, String>();
48+
49+
/**
50+
* The list of required arguments.
51+
*/
52+
private List<String> requiredArgs = new ArrayList<String>();
53+
54+
/**
55+
* Adds argument name to the list of required arguments.
56+
* @param reqArg "arg", "-arg" or "--arg" argument name to be added.
57+
*/
58+
public void addRequiredArgument(String reqArg)
59+
{
60+
reqArg = cleanHyphens(reqArg);
61+
62+
if (!requiredArgs.contains(reqArg))
63+
requiredArgs.add(reqArg);
64+
}
65+
66+
/**
67+
* Removes given argument name from the list of required arguments.
68+
* @param reqArg "arg", "-arg" or "--arg" argument name.
69+
*/
70+
public void removeRequiredArgument(String reqArg)
71+
{
72+
reqArg = cleanHyphens(reqArg);
73+
74+
requiredArgs.remove(reqArg);
75+
}
76+
77+
/**
78+
* Returns the list of required arguments. Names are stripped from hyphens.
79+
*/
80+
public List<String> getRequiredArguments()
81+
{
82+
return Collections.unmodifiableList(requiredArgs);
83+
}
84+
85+
private String cleanHyphens(String arg)
86+
{
87+
if (arg.startsWith("--"))
88+
return arg.substring(2);
89+
else if (arg.startsWith("-"))
90+
return arg.substring(1);
91+
else
92+
return arg;
93+
}
94+
95+
/**
96+
* Parses the array of command line arguments.
97+
*
98+
* @param args String array which should come from the "main" method.
99+
*
100+
* @throws ParseException if any of required arguments has not been found
101+
* in <tt>args</tt>.
102+
*/
103+
public void parse(String[] args) throws ParseException
104+
{
105+
for (String arg : args)
106+
{
107+
arg = cleanHyphens(arg);
108+
109+
int eqIdx = arg.indexOf("=");
110+
if (eqIdx <= 0)
111+
{
112+
logger.warn("Skipped invalid cmd line argument: " + arg);
113+
continue;
114+
}
115+
else if (eqIdx == arg.length() - 1)
116+
{
117+
logger.warn("Skipped empty cmd line argument: " + arg);
118+
continue;
119+
}
120+
121+
String key = arg.substring(0, eqIdx);
122+
String val = arg.substring(eqIdx+1);
123+
argMap.put(key, val);
124+
}
125+
126+
List<String> leftReqArgs = new ArrayList<String>(requiredArgs);
127+
leftReqArgs.removeAll(argMap.keySet());
128+
if (!leftReqArgs.isEmpty())
129+
{
130+
throw new ParseException(
131+
"Some of required arguments were not specified: "
132+
+ leftReqArgs.toString());
133+
}
134+
}
135+
136+
/**
137+
* Returns the value of cmd line argument for given name. <tt>null</tt>
138+
* if there was no value or it was empty.
139+
* @param opt the name of command line argument which value we want to get.
140+
*/
141+
public String getOptionValue(String opt)
142+
{
143+
return argMap.get(cleanHyphens(opt));
144+
}
145+
146+
/**
147+
* Returns the value of cmd line argument for given name.
148+
* <tt>defaultValue</tt> if there was no value or it was empty.
149+
* @param opt the name of command line argument which value we want to get.
150+
* @param defaultValue the default value which should be returned if the
151+
* argument value is missing.
152+
*/
153+
public String getOptionValue(String opt, String defaultValue)
154+
{
155+
String val = getOptionValue(opt);
156+
return val != null ? val : defaultValue;
157+
}
158+
159+
/**
160+
* Returns <tt>int</tt> value of cmd line argument for given name.
161+
* <tt>defaultValue</tt> if there was no valid value for that argument.
162+
* @param opt the name of command line argument which value we want to get.
163+
* @param defaultValue the default value which should be returned if the
164+
* argument value is missing.
165+
*/
166+
public int getIntOptionValue(String opt, int defaultValue)
167+
{
168+
String val = getOptionValue(opt);
169+
if (val == null)
170+
return defaultValue;
171+
try
172+
{
173+
return Integer.parseInt(val);
174+
}
175+
catch (NumberFormatException fmt)
176+
{
177+
return defaultValue;
178+
}
179+
}
180+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright @ 2015 - present, 8x8 Inc
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+
package org.jitsi.jigasi.cmd;
17+
18+
/**
19+
* An exception thrown when unrecoverable parsing error occurs.
20+
*
21+
* @author Pawel Domas
22+
*/
23+
public class ParseException
24+
extends Exception
25+
{
26+
/**
27+
* Creates new instance of <tt>ParseException</tt>.
28+
* @param message parse exception message.
29+
*/
30+
public ParseException(String message)
31+
{
32+
super(message);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright @ 2015 - present, 8x8 Inc
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+
package org.jitsi.jigasi.cmd;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.*;
21+
22+
public class CmdLineArgsTest
23+
{
24+
/**
25+
* A basic test for {@link CmdLine} class.
26+
*/
27+
@Test
28+
public void testJvbArgs()
29+
throws ParseException
30+
{
31+
String[] args = {
32+
"--apis=xmpp,rest",
33+
"-blablaarg=",
34+
"--domain=example.com",
35+
"-max-port=21000",
36+
"secret=secretpass",
37+
"--port=5275",
38+
"somegarbagearg",
39+
"-=dsf="
40+
};
41+
42+
// create the parser
43+
CmdLine parser = new CmdLine();
44+
45+
// parse the command line arguments
46+
parser.parse(args);
47+
48+
assertEquals("example.com", parser.getOptionValue("domain"));
49+
assertEquals(21000, parser.getIntOptionValue("max-port", 1));
50+
assertEquals("secretpass", parser.getOptionValue("secret"));
51+
assertEquals(5275, parser.getIntOptionValue("port", 1));
52+
assertEquals("xmpp,rest", parser.getOptionValue("apis"));
53+
54+
// Default value
55+
assertEquals(
56+
"localhost", parser.getOptionValue("host", "localhost"));
57+
58+
// Parsed default value
59+
assertEquals(10000, parser.getIntOptionValue("min-port", 10000));
60+
}
61+
62+
@Test
63+
public void testRequiredArg()
64+
{
65+
String[] args = { "--min-port=23423" };
66+
67+
CmdLine parser = new CmdLine();
68+
69+
parser.addRequiredArgument("-max-port");
70+
parser.addRequiredArgument("min-port");
71+
72+
try
73+
{
74+
parser.parse(args);
75+
76+
fail("Missed required argument");
77+
}
78+
catch (ParseException e)
79+
{
80+
assertEquals(
81+
"Some of required arguments were not specified: [max-port]",
82+
e.getMessage());
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)