diff --git a/pom.xml b/pom.xml index 97dddb5d3712fb5583d7b60f6b432cf091e30c84..923d1dfab593bb105e4d6b2fedfc1fb0e0b896bc 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,7 @@ rest-api-service rest-api-service-dev rest-api-spi-dev + rest-api-utils diff --git a/rest-api-utils/pom.xml b/rest-api-utils/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..c71659d13d480df205a6db59c149879c514829b3 --- /dev/null +++ b/rest-api-utils/pom.xml @@ -0,0 +1,33 @@ + + + + rest-api-model + com.inspur.edp + 0.2.0 + + 4.0.0 + + rest-api-utils + + + + io.iec.edp + caf-boot-starter-session + 1.0.0-rc.1 + + + io.iec.edp + caf-boot-starter-tenancy + 1.0.0-rc.1 + + + io.iec.edp + caf-boot-starter-context + 1.0.0-rc.1 + + + + + \ No newline at end of file diff --git a/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/bsession/BackendSessionUtils.java b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/bsession/BackendSessionUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..d87a2d6dfb16c666106772308847aadc58e75ee7 --- /dev/null +++ b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/bsession/BackendSessionUtils.java @@ -0,0 +1,82 @@ +/* + * Copyright © OpenAtom Foundation. + * 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 com.inspur.edp.sgf.utils.bsession; + +import io.iec.edp.caf.boot.context.CAFContext; +import io.iec.edp.caf.commons.utils.SpringBeanUtils; +import io.iec.edp.caf.core.session.CafSession; +import io.iec.edp.caf.core.session.ICafSessionService; +import io.iec.edp.caf.core.session.SessionType; +import io.iec.edp.caf.core.session.core.CAFSessionThreadHolder; +import io.iec.edp.caf.tenancy.api.ITenantService; +import io.iec.edp.caf.tenancy.api.entity.Tenant; +import lombok.extern.slf4j.Slf4j; + +import java.util.HashMap; +import java.util.List; +import java.util.function.Consumer; + +/** + * 功能描述: + * + * @ClassName: BackendSessionUtils + * @Author: Fynn Qi + * @Date: 2021/6/23 12:43 + * @Version: V1.0 + */ +@Slf4j +public class BackendSessionUtils { + private static String BACKEND_SESSION_USER_ID = "abc"; + + public static void wrapFirstTenantBackendSession(Consumer consumer) { + ICafSessionService service = SpringBeanUtils.getBean(ICafSessionService.class); + ITenantService tenantService = SpringBeanUtils.getBean(ITenantService.class); + + CafSession oldSession = CAFSessionThreadHolder.getCurrentSession(); + + try { + List tenants = tenantService.getAllTenants("zh-CHS"); + if (tenants != null && tenants.size() > 0) { + Tenant tenant = tenants.get(0); + if (oldSession != null) { + //如果当前存在session,则先清空 + CAFSessionThreadHolder.purge(); + } + if (CAFContext.current.getCurrentSession() == null) { + //如果session已经没了,就创建一个bsesion + CafSession cafSession = service.create(tenant.getId(), BACKEND_SESSION_USER_ID, "zh-CHS", new HashMap<>(), SessionType.backend); + CAFSessionThreadHolder.setCurrentSession(cafSession); + log.info("create bsession with tenant=" + tenant.getId() + ",userId=" + BACKEND_SESSION_USER_ID + ", lang=zh-CHS"); + } else { + //如果还存在,就用当前的。这个分支应该不会走到。 + CafSession curSession = CAFContext.current.getCurrentSession(); + log.warn("exist session with tenant={}, userId={}, lang={}, sessionType={}", curSession.getTenantId(), curSession.getUserId(), curSession.getLanguage(), curSession.getSessionType()); + } + + consumer.accept(tenant); + } else { + log.warn("not fount tenants"); + } + } finally { + if (oldSession == null) { + //如果原来不存在Session,清空当前Session + CAFSessionThreadHolder.purge(); + } else { + //如果原来存在Session,则设回之前的Session + CAFSessionThreadHolder.setCurrentSession(oldSession); + } + } + } +} diff --git a/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/cache/ConfigCacheUtil.java b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/cache/ConfigCacheUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..5aa3b4b7065de59707b53f66543294481bb39956 --- /dev/null +++ b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/cache/ConfigCacheUtil.java @@ -0,0 +1,30 @@ +/* + * Copyright © OpenAtom Foundation. + * 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 com.inspur.edp.sgf.utils.cache; + +/** + * 功能描述: + * + * @ClassName: ConfigCacheUtil + * @Author: Fynn Qi + * @Date: 2020/12/19 14:45 + * @Version: V1.0 + */ +public class ConfigCacheUtil { + + public static String getCacheKey(String httpMethod,String uri){ + return String.format("%s####%s",httpMethod.toUpperCase(),uri); + } +} diff --git a/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/collections/CollectionUtil.java b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/collections/CollectionUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..74415de13138af5c2c7650d5a19f3b20895b4297 --- /dev/null +++ b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/collections/CollectionUtil.java @@ -0,0 +1,64 @@ +/* + * Copyright © OpenAtom Foundation. + * 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 com.inspur.edp.sgf.utils.collections; + +import java.util.List; + +/** + * 功能描述: + * + * @ClassName: CollectionUtil + * @Author: Fynn Qi + * @Date: 2021/2/5 15:15 + * @Version: V1.0 + */ +public class CollectionUtil { + + public static boolean isExist(String element, List elements) { + if (elements == null || elements.size() <= 0) { + return false; + } + return elements.stream().anyMatch(x -> x.equals(element)); + } + + public static void tryAdd(String element, List elements) { + boolean isExist = elements.stream().anyMatch(x -> x.equals(element)); + if (!isExist) { + elements.add(element); + } + } + + public static void tryAddAll(List sourceElements, List targeElements) { + if (sourceElements == null || sourceElements.size() <= 0 || targeElements == null) { + return; + } + for (String sourceKey : sourceElements) { + boolean isExist = targeElements.stream().anyMatch(x -> x.equals(sourceKey)); + if (!isExist) { + targeElements.add(sourceKey); + } + } + } + + /** + * 获取List大小 + * + * @param list List,可null + * @return 0 or List.size() + */ + public static int getListSize(List list) { + return list == null ? 0 : list.size(); + } +} diff --git a/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/thread/ThreadPoolManager.java b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/thread/ThreadPoolManager.java new file mode 100644 index 0000000000000000000000000000000000000000..12c0c0d4a0a675c40f2852002b94234545223cb0 --- /dev/null +++ b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/thread/ThreadPoolManager.java @@ -0,0 +1,91 @@ +/* + * Copyright © OpenAtom Foundation. + * 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 com.inspur.edp.sgf.utils.thread; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @Description + * @Author Kaixuan Shi + * @Date 2021/12/8 + * @Version 1.0 + */ +public class ThreadPoolManager { + + private static final Logger logger = LoggerFactory.getLogger(ThreadPoolManager.class); + private final ThreadPoolExecutor threadPool; + private final String THREAD_NAME_PREFIX = "eapi-thread-"; + + public ThreadPoolManager() { + int maximumPoolSize = 8; + try { + int availableProcessors = Runtime.getRuntime().availableProcessors(); + maximumPoolSize = 2 * availableProcessors + 1; + } catch (Exception e) { + logger.info("get the number of processors available to the Java virtual machine error,maximumPoolSize:8"); + } + //保证corePoolSize>=maximumPoolSize,否则抛异常 + maximumPoolSize = Math.max(maximumPoolSize, 8); + + threadPool = new ThreadPoolExecutor(4, maximumPoolSize, 10, TimeUnit.SECONDS, + new LinkedBlockingQueue(4), + new ThreadFactory() { + private final AtomicInteger id = new AtomicInteger(0); + + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setName(THREAD_NAME_PREFIX + id.addAndGet(1)); + return thread; + } + }, new ThreadPoolExecutor.CallerRunsPolicy() + ); + } + + public void submit(Runnable task) { + threadPool.submit(task); + } + + public void shutdown() { + threadPool.shutdown(); + } + + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { + return threadPool.awaitTermination(timeout, unit); + } +} + + + + + + + + + + + + + + + + diff --git a/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/valid/JavaIdentifierValid.java b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/valid/JavaIdentifierValid.java new file mode 100644 index 0000000000000000000000000000000000000000..ed86f6f4f2a62ed8132f836c3bcccfe907e60d98 --- /dev/null +++ b/rest-api-utils/src/main/java/com/inspur/edp/sgf/utils/valid/JavaIdentifierValid.java @@ -0,0 +1,83 @@ +/* + * Copyright © OpenAtom Foundation. + * 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 com.inspur.edp.sgf.utils.valid; + +/** 功能描述: @ClassName: JavaIdentifierValid @Author: Fynn Qi @Date: 2020/11/12 11:11 @Version: V1.0 */ +public class JavaIdentifierValid { + + private final static String JAVA_IDENTIFIER_EMPTY_STRING =""; + + /** + * 对单独的name进行校验 + * + * @param name + * @return + */ + public static boolean isValidJavaIdentifier(String name) { + // 确定是否允许将指定字符作为 Java 标识符中的首字符。 + if (name.length() == 0 || !Character.isJavaIdentifierStart(name.charAt(0))) { + return false; + } + + String nameExceptFirst = name.substring(1); + // 确定指定字符是否可以是 Java 标识符中首字符以外的部分。 + for (int i = 0; i < nameExceptFirst.length(); i++) { + if (!Character.isJavaIdentifierPart(nameExceptFirst.charAt(i))) { + return false; + } + } + return true; + } + + /** + * 对 package name 和 class name 进行校验 + * + * @param javaName + * @return + */ + public static boolean isValidJavaIdentifierName(String javaName) { + if (JAVA_IDENTIFIER_EMPTY_STRING.equals(javaName)) { + return false; + } + boolean flag = true; + try { + if (!javaName.endsWith(".")) { + int index = javaName.indexOf("."); + if (index != -1) { + String[] splitNames = javaName.split("\\."); + for (String splitName : splitNames) { + if (JAVA_IDENTIFIER_EMPTY_STRING.equals(splitName)) { + flag = false; + break; + } else if (!isValidJavaIdentifier(splitName)) { + flag = false; + break; + } + } + } else if (!isValidJavaIdentifier(javaName)) { + flag = false; + } + + } else { + flag = false; + } + + } catch (Exception ex) { + flag = false; + ex.printStackTrace(); + } + return flag; + } +}