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
437 changed files
with
16,027 additions
and
8,380 deletions.
There are no files selected for viewing
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
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"); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/SHAUtils.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,69 @@ | ||
/* | ||
* 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.conf.CommonVars; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class SHAUtils { | ||
|
||
public static String DOCTOR_NONCE = "12345"; | ||
public static final CommonVars<String> DOCTOR_TOKEN = | ||
CommonVars.apply("linkis.doctor.signature.token", ""); | ||
|
||
/** | ||
* 对字符串加密,默认使用SHA-256 | ||
* | ||
* @param strSrc 要加密的字符串 | ||
* @param encName 加密类型 | ||
* @return | ||
* @throws UnsupportedEncodingException | ||
*/ | ||
public static String Encrypt(String strSrc, String encName) throws UnsupportedEncodingException { | ||
MessageDigest md = null; | ||
String strDes = null; | ||
byte[] bt = strSrc.getBytes("utf-8"); | ||
try { | ||
if (encName == null || encName.equals("")) { | ||
encName = "SHA-256"; | ||
} | ||
md = MessageDigest.getInstance(encName); | ||
md.update(bt); | ||
strDes = bytes2Hex(md.digest()); // to HexString | ||
} catch (NoSuchAlgorithmException e) { | ||
return null; | ||
} | ||
return strDes; | ||
} | ||
|
||
public static String bytes2Hex(byte[] bts) { | ||
String des = ""; | ||
String tmp = null; | ||
for (int i = 0; i < bts.length; i++) { | ||
tmp = (Integer.toHexString(bts[i] & 0xFF)); | ||
if (tmp.length() == 1) { | ||
des += "0"; | ||
} | ||
des += tmp; | ||
} | ||
return des; | ||
} | ||
} |
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
Oops, something went wrong.