Browse Source

fix Permission denied if not found dolphinscheder-env.sh on 1.3.1 (#3128)

* fix Permission denied if not found dolphinscheder-env.sh

* update some comments on method
pull/3/MERGE
dailidong 4 years ago committed by GitHub
parent
commit
4a4e940379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/CommonUtils.java
  2. 249
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/Preconditions.java
  3. 115
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/PreconditionsTest.java
  4. 55
      dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/exceptions/RemotingException.java
  5. 30
      dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/future/ResponseFuture.java

2
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/CommonUtils.java

@ -48,7 +48,7 @@ public class CommonUtils {
envPath = envDefaultPath.getPath(); envPath = envDefaultPath.getPath();
logger.debug("env path :{}", envPath); logger.debug("env path :{}", envPath);
}else{ }else{
envPath = System.getProperty("user.home") + File.separator + ".bash_profile"; envPath = "/etc/profile";
} }
} }

249
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/Preconditions.java

@ -18,266 +18,55 @@ package org.apache.dolphinscheduler.common.utils;
/** /**
* A collection of static utility methods to validate input. * utility methods for validating input
* *
* <p>This class is modelled after Google Guava's Preconditions class, and partly takes code
* from that class. We add this code to here base in order to reduce external
* dependencies.
*/ */
public final class Preconditions { public final class Preconditions {
// ------------------------------------------------------------------------ private Preconditions() {}
// Null checks
// ------------------------------------------------------------------------
/** /**
* Ensures that the given object reference is not null. * if obj is null will throw NPE
* Upon violation, a {@code NullPointerException} with no message is thrown.
* *
* @param reference reference * @param obj obj
* @param <T> T * @param <T> T
* @return T * @return T
*/ */
public static <T> T checkNotNull(T reference) { public static <T> T checkNotNull(T obj) {
if (reference == null) { if (obj == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
return reference; return obj;
} }
/** /**
* Ensures that the given object reference is not null. * if obj is null will throw NullPointerException with error message
* Upon violation, a {@code NullPointerException} with the given message is thrown. * @param obj obj
* @param reference reference * @param errorMsg error message
* @param errorMessage errorMessage
* @param <T> T * @param <T> T
* @return T * @return T
*/ */
public static <T> T checkNotNull(T reference, String errorMessage) { public static <T> T checkNotNull(T obj, String errorMsg) {
if (reference == null) { if (obj == null) {
throw new NullPointerException(String.valueOf(errorMessage)); throw new NullPointerException(errorMsg);
}
return reference;
}
/**
* Ensures that the given object reference is not null.
* Upon violation, a {@code NullPointerException} with the given message is thrown.
*
* <p>The error message is constructed from a template and an arguments array, after
* a similar fashion as {@link String#format(String, Object...)}, but supporting only
* {@code %s} as a placeholder.
*
* @param reference The object reference
* @param errorMessageTemplate The message template for the {@code NullPointerException}
* that is thrown if the check fails. The template substitutes its
* {@code %s} placeholders with the error message arguments.
* @param errorMessageArgs The arguments for the error message, to be inserted into the
* message template for the {@code %s} placeholders.
*
* @param <T> T
* @return The object reference itself (generically typed).
*/
public static <T> T checkNotNull(T reference,
String errorMessageTemplate,
Object... errorMessageArgs) {
if (reference == null) {
throw new NullPointerException(format(errorMessageTemplate, errorMessageArgs));
}
return reference;
}
// ------------------------------------------------------------------------
// Boolean Condition Checking (Argument)
// ------------------------------------------------------------------------
/**
* Checks the given boolean condition, and throws an {@code IllegalArgumentException} if
* the condition is not met (evaluates to {@code false}).
*
* @param condition The condition to check
*
* @throws IllegalArgumentException Thrown, if the condition is violated.
*/
public static void checkArgument(boolean condition) {
if (!condition) {
throw new IllegalArgumentException();
} }
return obj;
} }
/**
* Checks the given boolean condition, and throws an {@code IllegalArgumentException} if
* the condition is not met (evaluates to {@code false}). The exception will have the
* given error message.
*
* @param condition The condition to check
* @param errorMessage The message for the {@code IllegalArgumentException} that is thrown if the check fails.
*
* @throws IllegalArgumentException Thrown, if the condition is violated.
*/
public static void checkArgument(boolean condition, Object errorMessage) {
if (!condition) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
/** /**
* Checks the given boolean condition, and throws an {@code IllegalArgumentException} if * if condition is false will throw an IllegalArgumentException with the given message
* the condition is not met (evaluates to {@code false}).
* *
* @param condition The condition to check * @param condition condition
* @param errorMessageTemplate The message template for the {@code IllegalArgumentException} * @param errorMsg error message
* that is thrown if the check fails. The template substitutes its
* {@code %s} placeholders with the error message arguments.
* @param errorMessageArgs The arguments for the error message, to be inserted into the
* message template for the {@code %s} placeholders.
* *
* @throws IllegalArgumentException Thrown, if the condition is violated. * @throws IllegalArgumentException Thrown, if the condition is violated.
*/ */
public static void checkArgument(boolean condition, public static void checkArgument(boolean condition, Object errorMsg) {
String errorMessageTemplate,
Object... errorMessageArgs) {
if (!condition) {
throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs));
}
}
// ------------------------------------------------------------------------
// Boolean Condition Checking (State)
// ------------------------------------------------------------------------
/**
* Checks the given boolean condition, and throws an {@code IllegalStateException} if
* the condition is not met (evaluates to {@code false}).
*
* @param condition The condition to check
*
* @throws IllegalStateException Thrown, if the condition is violated.
*/
public static void checkState(boolean condition) {
if (!condition) {
throw new IllegalStateException();
}
}
/**
* Checks the given boolean condition, and throws an {@code IllegalStateException} if
* the condition is not met (evaluates to {@code false}). The exception will have the
* given error message.
*
* @param condition The condition to check
* @param errorMessage The message for the {@code IllegalStateException} that is thrown if the check fails.
*
* @throws IllegalStateException Thrown, if the condition is violated.
*/
public static void checkState(boolean condition, Object errorMessage) {
if (!condition) { if (!condition) {
throw new IllegalStateException(String.valueOf(errorMessage)); throw new IllegalArgumentException(String.valueOf(errorMsg));
}
}
/**
* Checks the given boolean condition, and throws an {@code IllegalStateException} if
* the condition is not met (evaluates to {@code false}).
*
* @param condition The condition to check
* @param errorMessageTemplate The message template for the {@code IllegalStateException}
* that is thrown if the check fails. The template substitutes its
* {@code %s} placeholders with the error message arguments.
* @param errorMessageArgs The arguments for the error message, to be inserted into the
* message template for the {@code %s} placeholders.
*
* @throws IllegalStateException Thrown, if the condition is violated.
*/
public static void checkState(boolean condition,
String errorMessageTemplate,
Object... errorMessageArgs) {
if (!condition) {
throw new IllegalStateException(format(errorMessageTemplate, errorMessageArgs));
}
}
/**
* Ensures that the given index is valid for an array, list or string of the given size.
*
* @param index index to check
* @param size size of the array, list or string
*
* @throws IllegalArgumentException Thrown, if size is negative.
* @throws IndexOutOfBoundsException Thrown, if the index negative or greater than or equal to size
*/
public static void checkElementIndex(int index, int size) {
checkArgument(size >= 0, "Size was negative.");
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
} }
} }
/**
* Ensures that the given index is valid for an array, list or string of the given size.
*
* @param index index to check
* @param size size of the array, list or string
* @param errorMessage The message for the {@code IndexOutOfBoundsException} that is thrown if the check fails.
*
* @throws IllegalArgumentException Thrown, if size is negative.
* @throws IndexOutOfBoundsException Thrown, if the index negative or greater than or equal to size
*/
public static void checkElementIndex(int index, int size, String errorMessage) {
checkArgument(size >= 0, "Size was negative.");
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(errorMessage) + " Index: " + index + ", Size: " + size);
}
}
// ------------------------------------------------------------------------
// Utilities
// ------------------------------------------------------------------------
/**
* A simplified formatting method. Similar to {@link String#format(String, Object...)}, but
* with lower overhead (only String parameters, no locale, no format validation).
*
* <p>This method is taken quasi verbatim from the Guava Preconditions class.
*/
private static String format( String template, Object... args) {
final int numArgs = args == null ? 0 : args.length;
template = String.valueOf(template); // null -> "null"
// start substituting the arguments into the '%s' placeholders
StringBuilder builder = new StringBuilder(template.length() + 16 * numArgs);
int templateStart = 0;
int i = 0;
while (i < numArgs) {
int placeholderStart = template.indexOf("%s", templateStart);
if (placeholderStart == -1) {
break;
}
builder.append(template.substring(templateStart, placeholderStart));
builder.append(args[i++]);
templateStart = placeholderStart + 2;
}
builder.append(template.substring(templateStart));
// if we run out of placeholders, append the extra args in square braces
if (i < numArgs) {
builder.append(" [");
builder.append(args[i++]);
while (i < numArgs) {
builder.append(", ");
builder.append(args[i++]);
}
builder.append(']');
}
return builder.toString();
}
// ------------------------------------------------------------------------
/** Private constructor to prevent instantiation. */
private Preconditions() {}
} }

115
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/PreconditionsTest.java

@ -33,11 +33,9 @@ public class PreconditionsTest {
*/ */
@Test @Test
public void testCheckNotNull() throws Exception { public void testCheckNotNull() throws Exception {
String testReference = "test reference"; String testReference = "test object";
//test reference is not null
Assert.assertEquals(testReference, Preconditions.checkNotNull(testReference)); Assert.assertEquals(testReference, Preconditions.checkNotNull(testReference));
Assert.assertEquals(testReference,Preconditions.checkNotNull(testReference,"reference is null")); Assert.assertEquals(testReference,Preconditions.checkNotNull(testReference,"object is null"));
Assert.assertEquals(testReference,Preconditions.checkNotNull(testReference,"%s is null",testReference));
//test reference is null //test reference is null
try { try {
@ -51,120 +49,19 @@ public class PreconditionsTest {
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
assertNull(ex.getMessage()); assertNull(ex.getMessage());
} }
//test reference is null ,expect contains errorMessage
try {
Preconditions.checkNotNull(null,"reference is null");
} catch (NullPointerException ex) {
assertThat(ex.getMessage(), containsString("reference is null"));
}
try {
Preconditions.checkNotNull("","reference is null");
} catch (NullPointerException ex) {
assertThat(ex.getMessage(), containsString("reference is null"));
}
//test reference is null ,expect contains errorMessageTemplate and errorMessageArgs
try { try {
Preconditions.checkNotNull(null,"%s is null",testReference); Preconditions.checkNotNull(null,"object is null");
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
assertThat(ex.getMessage(), containsString(testReference + " is null")); assertThat(ex.getMessage(), containsString("object is null"));
} }
try { try {
Preconditions.checkNotNull("","%s is null",testReference); Preconditions.checkNotNull("","object is null");
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
assertThat(ex.getMessage(), containsString(testReference + " is null")); assertThat(ex.getMessage(), containsString("object is null"));
}
}
/**
* Test checkArgument
*/
@Test
public void testCheckArgument() throws Exception {
int argument = 100;
//boolean condition is true
Preconditions.checkArgument(argument > 0 && argument < 200);
//boolean condition is false
try {
Preconditions.checkArgument(argument > 0 && argument < 50);
} catch (IllegalArgumentException ex) {
assertNull(ex.getMessage());
}
//boolean condition is false ,expect contains errorMessage
try {
Preconditions.checkArgument(argument > 300, "argument is error");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("argument is error"));
}
//boolean condition is false,expect contains errorMessageTemplate and errorMessageArgs
try {
Preconditions.checkArgument(argument > 0 && argument < 99, "argument %s is error",argument);
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString( "argument " + argument + " is error"));
}
} }
/**
* Test checkState
*/
@Test
public void testCheckState() throws Exception {
int state = 1;
//boolean condition is true
Preconditions.checkState(state == 1);
Preconditions.checkState(state > -1);
//boolean condition is false
try {
Preconditions.checkState(state > 2);
} catch (IllegalStateException ex) {
assertNull(ex.getMessage());
}
//boolean condition is false ,expect contains errorMessage
try {
Preconditions.checkState(state < 1, "state is error");
} catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString("state is error"));
}
//boolean condition is false,expect contains errorMessageTemplate and errorMessageArgs
try {
Preconditions.checkState(state < -1 , "state %s is error",state);
} catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString( "state " + state + " is error"));
} }
}
/**
* Test checkElementIndex
*/
@Test
public void testCheckElementIndex() throws Exception {
int index = 2;
int size = 30;
//boolean condition is true
Preconditions.checkElementIndex(index, size);
//boolean condition is false
try {
Preconditions.checkElementIndex(-1, 10);
} catch (IndexOutOfBoundsException ex) {
assertThat(ex.getMessage(), containsString("Index: -1, Size: 10"));
}
//boolean condition is false ,expect contains errorMessage
try {
Preconditions.checkElementIndex(100, 50, "index is greater than size");
} catch (IndexOutOfBoundsException ex) {
assertThat(ex.getMessage(), containsString("index is greater than size Index: 100, Size: 50"));
}
}
} }

55
dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/exceptions/RemotingException.java

@ -26,69 +26,34 @@ public class RemotingException extends Exception {
super(); super();
} }
/** Constructs a new runtime exception with the specified detail message. /**
* The cause is not initialized, and may subsequently be initialized by a * Construct a new runtime exception with the detail message
* call to {@link #initCause}.
* *
* @param message the detail message. The detail message is saved for * @param message detail message
* later retrieval by the {@link #getMessage()} method.
*/ */
public RemotingException(String message) { public RemotingException(String message) {
super(message); super(message);
} }
/** /**
* Constructs a new runtime exception with the specified detail message and * Construct a new runtime exception with the detail message and cause
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
* *
* @param message the detail message (which is saved for later retrieval * @param message the detail message
* by the {@link #getMessage()} method). * @param cause the cause
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4 * @since 1.4
*/ */
public RemotingException(String message, Throwable cause) { public RemotingException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
/** Constructs a new runtime exception with the specified cause and a /**
* detail message of <tt>(cause==null ? null : cause.toString())</tt> * Construct a new runtime exception with throwable
* (which typically contains the class and detail message of
* <tt>cause</tt>). This constructor is useful for runtime exceptions
* that are little more than wrappers for other throwables.
* *
* @param cause the cause (which is saved for later retrieval by the * @param cause the cause
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/ */
public RemotingException(Throwable cause) { public RemotingException(Throwable cause) {
super(cause); super(cause);
} }
/**
* Constructs a new runtime exception with the specified detail
* message, cause, suppression enabled or disabled, and writable
* stack trace enabled or disabled.
*
* @param message the detail message.
* @param cause the cause. (A {@code null} value is permitted,
* and indicates that the cause is nonexistent or unknown.)
* @param enableSuppression whether or not suppression is enabled
* or disabled
* @param writableStackTrace whether or not the stack trace should
* be writable
*
* @since 1.7
*/
protected RemotingException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
} }

30
dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/future/ResponseFuture.java

@ -170,21 +170,6 @@ public class ResponseFuture {
} }
} }
@Override
public String toString() {
return "ResponseFuture{" +
"opaque=" + opaque +
", timeoutMillis=" + timeoutMillis +
", invokeCallback=" + invokeCallback +
", releaseSemaphore=" + releaseSemaphore +
", latch=" + latch +
", beginTimestamp=" + beginTimestamp +
", responseCommand=" + responseCommand +
", sendOk=" + sendOk +
", cause=" + cause +
'}';
}
/** /**
* scan future table * scan future table
*/ */
@ -209,4 +194,19 @@ public class ResponseFuture {
} }
} }
} }
@Override
public String toString() {
return "ResponseFuture{" +
"opaque=" + opaque +
", timeoutMillis=" + timeoutMillis +
", invokeCallback=" + invokeCallback +
", releaseSemaphore=" + releaseSemaphore +
", latch=" + latch +
", beginTimestamp=" + beginTimestamp +
", responseCommand=" + responseCommand +
", sendOk=" + sendOk +
", cause=" + cause +
'}';
}
} }

Loading…
Cancel
Save