From 39821731527eeeb029b861c1a578aea97a57e978 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Tue, 10 Dec 2024 19:24:13 -0600 Subject: [PATCH 1/2] ref: Adapt to jicoco changes, move CmdLine to jigasi. --- pom.xml | 7 +- src/main/java/org/jitsi/jigasi/Main.java | 2 +- .../java/org/jitsi/jigasi/cmd/CmdLine.java | 180 ++++++++++++++++++ .../org/jitsi/jigasi/cmd/ParseException.java | 34 ++++ .../org/jitsi/jigasi/cmd/CmdLineArgsTest.java | 85 +++++++++ 5 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/jitsi/jigasi/cmd/CmdLine.java create mode 100644 src/main/java/org/jitsi/jigasi/cmd/ParseException.java create mode 100644 src/test/java/org/jitsi/jigasi/cmd/CmdLineArgsTest.java diff --git a/pom.xml b/pom.xml index 6115b19c..67fd1e48 100644 --- a/pom.xml +++ b/pom.xml @@ -130,7 +130,12 @@ ${project.groupId} - jicoco + jicoco-jetty + ${jicoco.version} + + + ${project.groupId} + jicoco-mucclient ${jicoco.version} diff --git a/src/main/java/org/jitsi/jigasi/Main.java b/src/main/java/org/jitsi/jigasi/Main.java index 1c172665..a8cc55fa 100644 --- a/src/main/java/org/jitsi/jigasi/Main.java +++ b/src/main/java/org/jitsi/jigasi/Main.java @@ -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.*; diff --git a/src/main/java/org/jitsi/jigasi/cmd/CmdLine.java b/src/main/java/org/jitsi/jigasi/cmd/CmdLine.java new file mode 100644 index 00000000..f19375d3 --- /dev/null +++ b/src/main/java/org/jitsi/jigasi/cmd/CmdLine.java @@ -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: + * + * 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 argMap = new HashMap(); + + /** + * The list of required arguments. + */ + private List requiredArgs = new ArrayList(); + + /** + * 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 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 args. + */ + 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 leftReqArgs = new ArrayList(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. null + * 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. + * defaultValue 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 int value of cmd line argument for given name. + * defaultValue 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; + } + } +} diff --git a/src/main/java/org/jitsi/jigasi/cmd/ParseException.java b/src/main/java/org/jitsi/jigasi/cmd/ParseException.java new file mode 100644 index 00000000..758beaea --- /dev/null +++ b/src/main/java/org/jitsi/jigasi/cmd/ParseException.java @@ -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 ParseException. + * @param message parse exception message. + */ + public ParseException(String message) + { + super(message); + } +} diff --git a/src/test/java/org/jitsi/jigasi/cmd/CmdLineArgsTest.java b/src/test/java/org/jitsi/jigasi/cmd/CmdLineArgsTest.java new file mode 100644 index 00000000..108f1fd4 --- /dev/null +++ b/src/test/java/org/jitsi/jigasi/cmd/CmdLineArgsTest.java @@ -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()); + } + } +} From cbf5a21787a7decefd77516ecd04cb4614c5051b Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Wed, 11 Dec 2024 11:18:13 -0600 Subject: [PATCH 2/2] chore: Update jicoco. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 67fd1e48..e16b67dd 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ 4.4.8 1.0.3 - 1.1-140-g8f45a9f + 1.1-150-g57913c0 11.0.20 3.45.0