diff --git a/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionException.java b/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionException.java new file mode 100644 index 000000000..9fa55efd1 --- /dev/null +++ b/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionException.java @@ -0,0 +1,43 @@ +package com.alibaba.cola.extension; + +/** + * 扩展点初始化或者查找失败时,使用次异常 + *

+ * 扩展点初始化或者查找失败时,使用次异常 + *

+ * + * @author ***flying@126.com + * @version 1.0.0 + * @since 1.0.0 2022/9/26 + */ +public class ExtensionException extends RuntimeException { + + private String errCode; + + public ExtensionException(String errMessage) { + super(errMessage); + } + + public ExtensionException(String errCode, String errMessage) { + super(errMessage); + this.errCode = errCode; + } + + public ExtensionException(String errMessage, Throwable e) { + super(errMessage, e); + } + + public ExtensionException(String errCode, String errMessage, Throwable e) { + super(errMessage, e); + this.errCode = errCode; + } + + public String getErrCode() { + return errCode; + } + + public void setErrCode(String errCode) { + this.errCode = errCode; + } + +} diff --git a/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionExecutor.java b/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionExecutor.java index 680b5761d..17ad7f442 100644 --- a/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionExecutor.java +++ b/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/ExtensionExecutor.java @@ -15,12 +15,15 @@ import javax.annotation.Resource; /** - * ExtensionExecutor + * ExtensionExecutor + * * @author fulan.zjf 2017-11-05 */ @Component public class ExtensionExecutor extends AbstractComponentExecutor { + private static final String EXTENSION_NOT_FOUND = "extension_not_found"; + private Logger logger = LoggerFactory.getLogger(ExtensionExecutor.class); @Resource @@ -35,12 +38,13 @@ protected C locateComponent(Class targetClz, BizScenario bizScenario) { /** * if the bizScenarioUniqueIdentity is "ali.tmall.supermarket" - * + *

* the search path is as below: * 1、first try to get extension by "ali.tmall.supermarket", if get, return it. * 2、loop try to get extension by "ali.tmall", if get, return it. * 3、loop try to get extension by "ali", if get, return it. * 4、if not found, try the default extension + * * @param targetClz */ protected Ext locateExtension(Class targetClz, BizScenario bizScenario) { @@ -68,35 +72,37 @@ protected Ext locateExtension(Class targetClz, BizScenario bizScenari return extension; } - throw new RuntimeException("Can not find extension with ExtensionPoint: "+targetClz+" BizScenario:"+bizScenario.getUniqueIdentity()); + String errMessage = "Can not find extension with ExtensionPoint: " + + targetClz + " BizScenario:" + bizScenario.getUniqueIdentity(); + throw new ExtensionException(EXTENSION_NOT_FOUND, errMessage); } /** * first try with full namespace - * + *

* example: biz1.useCase1.scenario1 */ - private Ext firstTry(Class targetClz, BizScenario bizScenario) { + private Ext firstTry(Class targetClz, BizScenario bizScenario) { logger.debug("First trying with " + bizScenario.getUniqueIdentity()); return locate(targetClz.getName(), bizScenario.getUniqueIdentity()); } /** * second try with default scenario - * + *

* example: biz1.useCase1.#defaultScenario# */ - private Ext secondTry(Class targetClz, BizScenario bizScenario){ + private Ext secondTry(Class targetClz, BizScenario bizScenario) { logger.debug("Second trying with " + bizScenario.getIdentityWithDefaultScenario()); return locate(targetClz.getName(), bizScenario.getIdentityWithDefaultScenario()); } /** * third try with default use case + default scenario - * + *

* example: biz1.#defaultUseCase#.#defaultScenario# */ - private Ext defaultUseCaseTry(Class targetClz, BizScenario bizScenario){ + private Ext defaultUseCaseTry(Class targetClz, BizScenario bizScenario) { logger.debug("Third trying with " + bizScenario.getIdentityWithDefaultUseCase()); return locate(targetClz.getName(), bizScenario.getIdentityWithDefaultUseCase()); } @@ -107,8 +113,8 @@ private Ext locate(String name, String uniqueIdentity) { return ext; } - private void checkNull(BizScenario bizScenario){ - if(bizScenario == null){ + private void checkNull(BizScenario bizScenario) { + if (bizScenario == null) { throw new IllegalArgumentException("BizScenario can not be null for extension"); } } diff --git a/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/register/ExtensionRegister.java b/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/register/ExtensionRegister.java index f8075484e..07d105b82 100644 --- a/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/register/ExtensionRegister.java +++ b/cola-components/cola-component-extension-starter/src/main/java/com/alibaba/cola/extension/register/ExtensionRegister.java @@ -8,7 +8,6 @@ package com.alibaba.cola.extension.register; import com.alibaba.cola.extension.*; - import org.springframework.aop.support.AopUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.stereotype.Component; @@ -17,11 +16,25 @@ import javax.annotation.Resource; /** - * ExtensionRegister + * ExtensionRegister + * * @author fulan.zjf 2017-11-05 */ @Component -public class ExtensionRegister{ +public class ExtensionRegister { + + /** + * 扩展点接口名称不合法 + */ + private static final String EXTENSION_INTERFACE_NAME_ILLEGAL = "extension_interface_name_illegal"; + /** + * 扩展点不合法 + */ + private static final String EXTENSION_ILLEGAL = "extension_illegal"; + /** + * 扩展点定义重复 + */ + private static final String EXTENSION_DEFINE_DUPLICATE = "extension_define_duplicate"; @Resource private ExtensionRepository extensionRepository; @@ -29,8 +42,8 @@ public class ExtensionRegister{ public final static String EXTENSION_EXTPT_NAMING = "ExtPt"; - public void doRegistration(ExtensionPointI extensionObject){ - Class extensionClz = extensionObject.getClass(); + public void doRegistration(ExtensionPointI extensionObject) { + Class extensionClz = extensionObject.getClass(); if (AopUtils.isAopProxy(extensionObject)) { extensionClz = ClassUtils.getUserClass(extensionObject); } @@ -39,7 +52,8 @@ public void doRegistration(ExtensionPointI extensionObject){ ExtensionCoordinate extensionCoordinate = new ExtensionCoordinate(calculateExtensionPoint(extensionClz), bizScenario.getUniqueIdentity()); ExtensionPointI preVal = extensionRepository.getExtensionRepo().put(extensionCoordinate, extensionObject); if (preVal != null) { - throw new RuntimeException("Duplicate registration is not allowed for :" + extensionCoordinate); + String errMessage = "Duplicate registration is not allowed for :" + extensionCoordinate; + throw new ExtensionException(EXTENSION_DEFINE_DUPLICATE, errMessage); } } @@ -49,14 +63,18 @@ public void doRegistration(ExtensionPointI extensionObject){ */ private String calculateExtensionPoint(Class targetClz) { Class[] interfaces = ClassUtils.getAllInterfacesForClass(targetClz); - if (interfaces == null || interfaces.length == 0) - throw new RuntimeException("Please assign a extension point interface for "+targetClz); + if (interfaces == null || interfaces.length == 0) { + throw new ExtensionException(EXTENSION_ILLEGAL, "Please assign a extension point interface for " + targetClz); + } for (Class intf : interfaces) { String extensionPoint = intf.getSimpleName(); - if (extensionPoint.contains(EXTENSION_EXTPT_NAMING)) + if (extensionPoint.contains(EXTENSION_EXTPT_NAMING)) { return intf.getName(); + } } - throw new RuntimeException("Your name of ExtensionPoint for "+targetClz+" is not valid, must be end of "+ EXTENSION_EXTPT_NAMING); + String errMessage = "Your name of ExtensionPoint for " + targetClz + + " is not valid, must be end of " + EXTENSION_EXTPT_NAMING; + throw new ExtensionException(EXTENSION_INTERFACE_NAME_ILLEGAL, errMessage); } }