forked from apache/linkis
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
13 deletions.
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/LinkisUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.linkis.common.utils; | ||
|
||
import org.apache.linkis.common.exception.ErrorException; | ||
import org.apache.linkis.common.exception.FatalException; | ||
import org.apache.linkis.common.exception.WarnException; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Function; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class LinkisUtils { | ||
private static final Logger logger = LoggerFactory.getLogger(LinkisUtils.class); | ||
|
||
public static <T> T tryCatch(Callable<T> tryOp, Function<Throwable, T> catchOp) { | ||
T result = null; | ||
try { | ||
result = tryOp.call(); | ||
} catch (Throwable t) { | ||
if (t instanceof FatalException) { | ||
logger.error("Fatal error, system exit...", t); | ||
System.exit(((FatalException) t).getErrCode()); | ||
} else if (t instanceof VirtualMachineError) { | ||
logger.error("Fatal error, system exit...", t); | ||
System.exit(-1); | ||
} else if (null != t.getCause() | ||
&& (t.getCause() instanceof FatalException | ||
|| t.getCause() instanceof VirtualMachineError)) { | ||
logger.error("Caused by fatal error, system exit...", t); | ||
System.exit(-1); | ||
} else if (t instanceof Error) { | ||
logger.error("Throw error", t); | ||
throw (Error) t; | ||
} else { | ||
result = catchOp.apply(t); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static void tryFinally(Runnable tryOp, Runnable finallyOp) { | ||
try { | ||
tryOp.run(); | ||
} finally { | ||
finallyOp.run(); | ||
} | ||
} | ||
|
||
public static <T> T tryAndWarn(Callable<T> tryOp, Logger log) { | ||
return tryCatch( | ||
tryOp, | ||
t -> { | ||
if (t instanceof ErrorException) { | ||
ErrorException error = (ErrorException) t; | ||
log.error( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", | ||
error.getErrCode(), | ||
error.getDesc(), | ||
error); | ||
|
||
} else if (t instanceof WarnException) { | ||
WarnException warn = (WarnException) t; | ||
log.warn( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", | ||
warn.getErrCode(), | ||
warn.getDesc(), | ||
warn); | ||
|
||
} else { | ||
log.warn("", t); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
public static void tryAndErrorMsg(Runnable tryOp, String message, Logger log) { | ||
try { | ||
tryOp.run(); | ||
} catch (WarnException t) { | ||
WarnException warn = (WarnException) t; | ||
log.warn( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", warn.getErrCode(), warn.getDesc()); | ||
log.warn(message, warn); | ||
} catch (Exception t) { | ||
log.warn(message, t); | ||
} | ||
} | ||
|
||
public static <T> void tryAndWarn(Runnable tryOp, Logger log) { | ||
try { | ||
tryOp.run(); | ||
} catch (Throwable error) { | ||
if (error instanceof WarnException) { | ||
WarnException warn = (WarnException) error; | ||
log.warn( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", | ||
warn.getErrCode(), | ||
warn.getDesc(), | ||
error); | ||
} else { | ||
log.warn("", error); | ||
} | ||
} | ||
} | ||
|
||
public static void tryAndWarnMsg(Runnable tryOp, String message, Logger log) { | ||
try { | ||
tryOp.run(); | ||
} catch (WarnException t) { | ||
WarnException warn = (WarnException) t; | ||
log.warn( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", warn.getErrCode(), warn.getDesc()); | ||
log.warn(message, warn); | ||
} catch (Exception t) { | ||
log.warn(message, t); | ||
} | ||
} | ||
|
||
public static <T> T tryAndWarnMsg(Callable<T> tryOp, String message, Logger log) { | ||
return tryCatch( | ||
tryOp, | ||
t -> { | ||
if (t instanceof ErrorException) { | ||
ErrorException error = (ErrorException) t; | ||
log.warn( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", | ||
error.getErrCode(), | ||
error.getDesc()); | ||
log.warn(message, error); | ||
} else if (t instanceof WarnException) { | ||
WarnException warn = (WarnException) t; | ||
log.warn( | ||
"Warning code(警告码): {}, Warning message(警告信息): {}.", | ||
warn.getErrCode(), | ||
warn.getDesc()); | ||
log.warn(message, warn); | ||
} else { | ||
log.warn(message, t); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
public static String getJvmUser() { | ||
return System.getProperty("user.name"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/MD5Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.linkis.common.utils; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class MD5Utils { | ||
|
||
/** | ||
* @param plaintext | ||
* @return | ||
* @throws NoSuchAlgorithmException | ||
*/ | ||
public static String encrypt(String plaintext) throws NoSuchAlgorithmException { | ||
// 使用 MD5 算法创建 MessageDigest 对象 | ||
MessageDigest md = MessageDigest.getInstance("MD5"); | ||
// 更新 MessageDigest 对象中的字节数据 | ||
md.update(plaintext.getBytes()); | ||
// 对更新后的数据计算哈希值,存储在 byte 数组中 | ||
byte[] digest = md.digest(); | ||
// 将 byte 数组转换为十六进制字符串 | ||
StringBuilder sb = new StringBuilder(); | ||
for (byte b : digest) { | ||
sb.append(String.format("%02x", b & 0xff)); | ||
} | ||
// 返回十六进制字符串 | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters