Skip to content

Commit

Permalink
ref: Adapt to jicoco changes, move CmdLine to jigasi. (#575)
Browse files Browse the repository at this point in the history
* ref: Adapt to jicoco changes, move CmdLine to jigasi.

* chore: Update jicoco.
  • Loading branch information
bgrozev authored Dec 11, 2024
1 parent 83029d2 commit cee2c81
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<smack.version>4.4.8</smack.version>
<jxmppVersion>1.0.3</jxmppVersion>
<!-- Match jicoco's jetty version. -->
<jicoco.version>1.1-140-g8f45a9f</jicoco.version>
<jicoco.version>1.1-150-g57913c0</jicoco.version>
<jetty.version>11.0.20</jetty.version>
<oci-sdk.version>3.45.0</oci-sdk.version>
</properties>
Expand Down Expand Up @@ -130,7 +130,12 @@
<!-- org.jitsi -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jicoco</artifactId>
<artifactId>jicoco-jetty</artifactId>
<version>${jicoco.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jicoco-mucclient</artifactId>
<version>${jicoco.version}</version>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jitsi/jigasi/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import net.java.sip.communicator.service.protocol.media.*;
import net.java.sip.communicator.util.*;
import org.apache.commons.lang3.*;
import org.jitsi.cmd.*;
import org.jitsi.impl.osgi.framework.launch.*;
import org.jitsi.jigasi.cmd.*;
import org.jitsi.jigasi.osgi.*;
import org.jitsi.jigasi.rest.*;
import org.jitsi.jigasi.version.*;
Expand Down
180 changes: 180 additions & 0 deletions src/main/java/org/jitsi/jigasi/cmd/CmdLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright @ 2015 - present, 8x8 Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.jigasi.cmd;


import org.jitsi.utils.logging.*;

import java.util.*;

/**
* Utility class for parsing command line arguments that take some value.
* Arguments can have one of the following formats:
* <ul>
* <li>"arg=value"</li>
* <li>"-arg=value"</li>
* <li>"--arg=value"</li>
* </ul>
* It's also possible to specify required arguments. If any of required
* arguments is not found {@link ParseException} will be thrown by
* {@link #parse(String[])}.
*
* @author Pawel Domas
*/
public class CmdLine
{
/**
* The logger
*/
private final static Logger logger = Logger.getLogger(CmdLine.class);

/**
* Map of argument values.
*/
private Map<String, String> argMap = new HashMap<String, String>();

/**
* The list of required arguments.
*/
private List<String> requiredArgs = new ArrayList<String>();

/**
* Adds argument name to the list of required arguments.
* @param reqArg "arg", "-arg" or "--arg" argument name to be added.
*/
public void addRequiredArgument(String reqArg)
{
reqArg = cleanHyphens(reqArg);

if (!requiredArgs.contains(reqArg))
requiredArgs.add(reqArg);
}

/**
* Removes given argument name from the list of required arguments.
* @param reqArg "arg", "-arg" or "--arg" argument name.
*/
public void removeRequiredArgument(String reqArg)
{
reqArg = cleanHyphens(reqArg);

requiredArgs.remove(reqArg);
}

/**
* Returns the list of required arguments. Names are stripped from hyphens.
*/
public List<String> getRequiredArguments()
{
return Collections.unmodifiableList(requiredArgs);
}

private String cleanHyphens(String arg)
{
if (arg.startsWith("--"))
return arg.substring(2);
else if (arg.startsWith("-"))
return arg.substring(1);
else
return arg;
}

/**
* Parses the array of command line arguments.
*
* @param args String array which should come from the "main" method.
*
* @throws ParseException if any of required arguments has not been found
* in <tt>args</tt>.
*/
public void parse(String[] args) throws ParseException
{
for (String arg : args)
{
arg = cleanHyphens(arg);

int eqIdx = arg.indexOf("=");
if (eqIdx <= 0)
{
logger.warn("Skipped invalid cmd line argument: " + arg);
continue;
}
else if (eqIdx == arg.length() - 1)
{
logger.warn("Skipped empty cmd line argument: " + arg);
continue;
}

String key = arg.substring(0, eqIdx);
String val = arg.substring(eqIdx+1);
argMap.put(key, val);
}

List<String> leftReqArgs = new ArrayList<String>(requiredArgs);
leftReqArgs.removeAll(argMap.keySet());
if (!leftReqArgs.isEmpty())
{
throw new ParseException(
"Some of required arguments were not specified: "
+ leftReqArgs.toString());
}
}

/**
* Returns the value of cmd line argument for given name. <tt>null</tt>
* if there was no value or it was empty.
* @param opt the name of command line argument which value we want to get.
*/
public String getOptionValue(String opt)
{
return argMap.get(cleanHyphens(opt));
}

/**
* Returns the value of cmd line argument for given name.
* <tt>defaultValue</tt> if there was no value or it was empty.
* @param opt the name of command line argument which value we want to get.
* @param defaultValue the default value which should be returned if the
* argument value is missing.
*/
public String getOptionValue(String opt, String defaultValue)
{
String val = getOptionValue(opt);
return val != null ? val : defaultValue;
}

/**
* Returns <tt>int</tt> value of cmd line argument for given name.
* <tt>defaultValue</tt> if there was no valid value for that argument.
* @param opt the name of command line argument which value we want to get.
* @param defaultValue the default value which should be returned if the
* argument value is missing.
*/
public int getIntOptionValue(String opt, int defaultValue)
{
String val = getOptionValue(opt);
if (val == null)
return defaultValue;
try
{
return Integer.parseInt(val);
}
catch (NumberFormatException fmt)
{
return defaultValue;
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/jitsi/jigasi/cmd/ParseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright @ 2015 - present, 8x8 Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.jigasi.cmd;

/**
* An exception thrown when unrecoverable parsing error occurs.
*
* @author Pawel Domas
*/
public class ParseException
extends Exception
{
/**
* Creates new instance of <tt>ParseException</tt>.
* @param message parse exception message.
*/
public ParseException(String message)
{
super(message);
}
}
85 changes: 85 additions & 0 deletions src/test/java/org/jitsi/jigasi/cmd/CmdLineArgsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright @ 2015 - present, 8x8 Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.jigasi.cmd;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.*;

public class CmdLineArgsTest
{
/**
* A basic test for {@link CmdLine} class.
*/
@Test
public void testJvbArgs()
throws ParseException
{
String[] args = {
"--apis=xmpp,rest",
"-blablaarg=",
"--domain=example.com",
"-max-port=21000",
"secret=secretpass",
"--port=5275",
"somegarbagearg",
"-=dsf="
};

// create the parser
CmdLine parser = new CmdLine();

// parse the command line arguments
parser.parse(args);

assertEquals("example.com", parser.getOptionValue("domain"));
assertEquals(21000, parser.getIntOptionValue("max-port", 1));
assertEquals("secretpass", parser.getOptionValue("secret"));
assertEquals(5275, parser.getIntOptionValue("port", 1));
assertEquals("xmpp,rest", parser.getOptionValue("apis"));

// Default value
assertEquals(
"localhost", parser.getOptionValue("host", "localhost"));

// Parsed default value
assertEquals(10000, parser.getIntOptionValue("min-port", 10000));
}

@Test
public void testRequiredArg()
{
String[] args = { "--min-port=23423" };

CmdLine parser = new CmdLine();

parser.addRequiredArgument("-max-port");
parser.addRequiredArgument("min-port");

try
{
parser.parse(args);

fail("Missed required argument");
}
catch (ParseException e)
{
assertEquals(
"Some of required arguments were not specified: [max-port]",
e.getMessage());
}
}
}

0 comments on commit cee2c81

Please sign in to comment.