Skip to content

Commit

Permalink
扩展点初始化,加载、调用查找失败时抛出自定义异常。
Browse files Browse the repository at this point in the history
  • Loading branch information
pasion committed Sep 26, 2022
1 parent 7ed540d commit d30b55c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.alibaba.cola.extension;

/**
* 扩展点初始化或者查找失败时,使用次异常
* <p>
* 扩展点初始化或者查找失败时,使用次异常
* <p>
*
* @author ***[email protected]
* @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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,12 +38,13 @@ protected <C> C locateComponent(Class<C> targetClz, BizScenario bizScenario) {

/**
* if the bizScenarioUniqueIdentity is "ali.tmall.supermarket"
*
* <p>
* 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> Ext locateExtension(Class<Ext> targetClz, BizScenario bizScenario) {
Expand Down Expand Up @@ -68,35 +72,37 @@ protected <Ext> Ext locateExtension(Class<Ext> 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
*
* <p>
* example: biz1.useCase1.scenario1
*/
private <Ext> Ext firstTry(Class<Ext> targetClz, BizScenario bizScenario) {
private <Ext> Ext firstTry(Class<Ext> targetClz, BizScenario bizScenario) {
logger.debug("First trying with " + bizScenario.getUniqueIdentity());
return locate(targetClz.getName(), bizScenario.getUniqueIdentity());
}

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

/**
* third try with default use case + default scenario
*
* <p>
* example: biz1.#defaultUseCase#.#defaultScenario#
*/
private <Ext> Ext defaultUseCaseTry(Class<Ext> targetClz, BizScenario bizScenario){
private <Ext> Ext defaultUseCaseTry(Class<Ext> targetClz, BizScenario bizScenario) {
logger.debug("Third trying with " + bizScenario.getIdentityWithDefaultUseCase());
return locate(targetClz.getName(), bizScenario.getIdentityWithDefaultUseCase());
}
Expand All @@ -107,8 +113,8 @@ private <Ext> 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");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,20 +16,34 @@
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;

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);
}
Expand All @@ -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);
}
}

Expand All @@ -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);
}

}

0 comments on commit d30b55c

Please sign in to comment.