Skip to content

Commit

Permalink
Merge pull request #4833 from Pandrex247/FISH-59
Browse files Browse the repository at this point in the history
FISH-59 Payara Micro --enableRequestTracing argument not accepting values
  • Loading branch information
Pandrex247 authored Aug 18, 2020
2 parents 2a93501 + a101c2b commit c879b92
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public enum RUNTIME_OPTION {
accessloginterval(true),
accesslogsuffix(true),
accesslogprefix(true),
enablerequesttracing(false),
enablerequesttracing(true, new RequestTracingValidator(), true),
requesttracingthresholdunit(true),
requesttracingthresholdvalue(true),
enablerequesttracingadaptivesampling(false),
Expand All @@ -114,13 +114,22 @@ public enum RUNTIME_OPTION {
contextroot(true),
warmup(false);

private RUNTIME_OPTION(boolean hasValue) {
RUNTIME_OPTION(boolean hasValue) {
this(hasValue, new Validator());
}

private RUNTIME_OPTION(boolean hasValue, Validator validator) {
RUNTIME_OPTION(boolean hasValue, boolean optionalValue) {
this(hasValue, new Validator(), optionalValue);
}

RUNTIME_OPTION(boolean hasValue, Validator validator) {
this(hasValue, validator, false);
}

RUNTIME_OPTION(boolean hasValue, Validator validator, boolean optionalValue) {
this.value = hasValue;
this.validator = validator;
this.optional = optionalValue;
}

boolean validate(String optionValue) throws ValidationException {
Expand All @@ -130,9 +139,17 @@ boolean validate(String optionValue) throws ValidationException {
boolean hasFollowingValue() {
return value;
}

boolean followingValueIsOptional() {
return optional;
}

private final Validator validator;

// Indicates the runtime option requires a value
private final boolean value;

// Indicates the runtime option is optional
private final boolean optional;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2020 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.micro.cmd.options;

import java.text.MessageFormat;

public class RequestTracingValidator extends Validator {

@Override
boolean validate(String optionValue) throws ValidationException {
if (optionValue != null) {
String[] requestTracingValues = optionValue.split("(?<=\\d)(?=\\D)|(?=\\d)(?<=\\D)");
// If valid, there should be no more than 2 entries
if (requestTracingValues.length <= 2) {
// If the first entry is a number, the second entry should be a String
if (requestTracingValues[0].matches("\\d+") &&
(requestTracingValues.length == 2 && !requestTracingValues[1].matches("\\D+"))) {
// If there is a second entry, and it's not a String
throw new ValidationException(MessageFormat.format(
RuntimeOptions.commandlogstrings.getString("requestTracingIncorrect"), optionValue));
}

// If the first entry is a String, there shouldn't be a second entry
if (requestTracingValues[0].matches("\\D+") && (requestTracingValues.length == 2)) {
throw new ValidationException(MessageFormat.format(
RuntimeOptions.commandlogstrings.getString("requestTracingIncorrect"), optionValue));
}
} else {
throw new ValidationException(MessageFormat.format(
RuntimeOptions.commandlogstrings.getString("requestTracingIncorrect"), optionValue));
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2020 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -94,13 +94,22 @@ public RuntimeOptions(String args[]) throws ValidationException {
RUNTIME_OPTION option = RUNTIME_OPTION.valueOf(arg.substring(2).toLowerCase());
String value = null;
if (option.hasFollowingValue()) {
// there is a second value
value = args[i+1];
i++;
if (value.startsWith("--")) {
throw new IndexOutOfBoundsException();
if (!option.followingValueIsOptional()) {
// there is a second value
value = args[i+1];
i++;
if (value.startsWith("--")) {
throw new IndexOutOfBoundsException();
}
option.validate(value);
} else {
// If we're not at the end, and the next arg isn't a command option, validate it
if (i + 1 != args.length && !args[i + 1].startsWith("--")) {
value = args[i+1];
i++;
option.validate(value);
}
}
option.validate(value);
}
List<String> values = options.get(option);
if (values == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,18 +1293,11 @@ private void scanArgs(String[] args) {
if (requestTracing.length == 2 && requestTracing[1].matches("\\D+")) {
requestTracingThresholdUnit = parseTimeUnit(requestTracing[1],
"request tracing threshold unit").name();
} // If there is a second entry, and it's not a String
else if (requestTracing.length == 2 && !requestTracing[1].matches("\\D+")) {
throw new IllegalArgumentException();
}
} // If the first entry is a String
else if (requestTracing[0].matches("\\D+")) {
requestTracingThresholdUnit = parseTimeUnit(requestTracing[0],
"request tracing threshold unit").name();
// There shouldn't be a second entry
if (requestTracing.length == 2) {
throw new IllegalArgumentException();
}
}
} else {
throw new IllegalArgumentException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved.
# Copyright (c) 2016-2020 Payara Foundation and/or its affiliates. All rights reserved.
#
# The contents of this file are subject to the terms of either the GNU
# General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -51,4 +51,5 @@ notValidMC={0} is not a valid multicast address
notValidPort={0} is not a valid port number it must be > 1 and < 65535
integerIncorrect={0} must be a valid integer
integerRangeIncorrect={0} must be a valid integer > {1} and < {2}
valueIsNotAllowed={0} must be one of {1}
valueIsNotAllowed={0} must be one of {1}
requestTracingIncorrect={0} is not a valid request tracing option
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2020 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/

package fish.payara.micro.cmd.options;


import org.junit.Test;

import static org.junit.Assert.fail;

public class RequestTracingValidatorTest {


@Test
public void test_no_following_option_no_following_value() {
try {
new RuntimeOptions(new String[]{"--enableRequestTracing"});
} catch (ValidationException e) {
fail(e.getMessage());
}
}

@Test
public void test_following_option_no_following_value() {
try {
new RuntimeOptions(new String[]{"--enableRequestTracing", "--noCluster"});
} catch (ValidationException e) {
fail(e.getMessage());
}
}

@Test
public void test_following_option_following_value() {
try {
new RuntimeOptions(new String[]{"--enableRequestTracing", "3MINUTES", "--noCluster"});
} catch (ValidationException e) {
fail(e.getMessage());
}
}

@Test
public void test_no_following_option_following_value() {
try {
new RuntimeOptions(new String[]{"--enableRequestTracing", "8H"});
} catch (ValidationException e) {
fail(e.getMessage());
}
}

}

0 comments on commit c879b92

Please sign in to comment.