Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #40 & add global handle exception interface #41

Merged
merged 4 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cicada-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>cicada</artifactId>
<groupId>top.crossoverjie.opensource</groupId>
<version>2.0.1</version>
<version>2.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cicada-base</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<packaging>jar</packaging>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public interface CicadaBeanFactory {
*/
Object getBean(String name) throws Exception;

/**
* get bean by class type
* @param clazz
* @param <T>
* @return bean
* @throws Exception
*/
<T> T getBean(Class<T> clazz) throws Exception;

/**
* release all beans
*/
Expand Down
4 changes: 2 additions & 2 deletions cicada-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>cicada</artifactId>
<groupId>top.crossoverjie.opensource</groupId>
<version>2.0.1</version>
<version>2.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cicada-core</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
<packaging>jar</packaging>

<name>cicada-core</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static CicadaHttpRequest init(DefaultHttpRequest httpRequest){
//build headers
buildHeaders(httpRequest, request);

//init cookies
//initBean cookies
initCookies(request);

return request ;
Expand All @@ -53,7 +53,7 @@ private static void buildHeaders(DefaultHttpRequest httpRequest, CicadaHttpReque
}

/**
* init cookies
* initBean cookies
* @param request request
*/
private static void initCookies(CicadaHttpRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package top.crossoverjie.cicada.server.annotation;


import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CicadaBean {

String value() default "" ;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package top.crossoverjie.cicada.server.bean;

import org.slf4j.Logger;
import top.crossoverjie.cicada.base.bean.CicadaBeanFactory;
import top.crossoverjie.cicada.base.log.LoggerBuilder;
import top.crossoverjie.cicada.server.exception.GlobalHandelException;
import top.crossoverjie.cicada.server.reflect.ClassScanner;
import top.crossoverjie.cicada.server.route.RouteProcess;

import java.util.Map;

Expand All @@ -14,7 +16,7 @@
* @since JDK 1.8
*/
public final class CicadaBeanManager {

private final static Logger LOGGER = LoggerBuilder.getLogger(CicadaBeanManager.class);

private CicadaBeanManager(){
}
Expand All @@ -23,9 +25,11 @@ private CicadaBeanManager(){

private static CicadaBeanFactory cicadaBeanFactory ;

private GlobalHandelException handelException ;

public static CicadaBeanManager getInstance() {
if (cicadaBeanManager == null) {
synchronized (RouteProcess.class) {
synchronized (CicadaBeanManager.class) {
if (cicadaBeanManager == null) {
cicadaBeanManager = new CicadaBeanManager();
}
Expand All @@ -35,19 +39,25 @@ public static CicadaBeanManager getInstance() {
}

/**
* init route bean factory
* initBean route bean factory
* @param packageName
* @throws Exception
*/
public void init(String packageName) throws Exception {
Map<String, Class<?>> cicadaAction = ClassScanner.getCicadaAction(packageName);
public void initBean(String packageName) throws Exception {
Map<String, Class<?>> cicadaBean = ClassScanner.getCicadaBean(packageName);

Class<?> bean = ClassScanner.getCustomRouteBean();
cicadaBeanFactory = (CicadaBeanFactory) bean.newInstance() ;

for (Map.Entry<String, Class<?>> classEntry : cicadaAction.entrySet()) {
for (Map.Entry<String, Class<?>> classEntry : cicadaBean.entrySet()) {
Object instance = classEntry.getValue().newInstance();
cicadaBeanFactory.register(instance) ;

//set exception handle
if (ClassScanner.isInterface(classEntry.getValue(), GlobalHandelException.class)){
GlobalHandelException exception = (GlobalHandelException) instance;
CicadaBeanManager.getInstance().exceptionHandle(exception);
}
}

}
Expand All @@ -59,15 +69,43 @@ public void init(String packageName) throws Exception {
* @return
* @throws Exception
*/
public Object getBean(String name) throws Exception {
return cicadaBeanFactory.getBean(name) ;
public Object getBean(String name) {
try {
return cicadaBeanFactory.getBean(name) ;
} catch (Exception e) {
LOGGER.error("get bean error",e);
}
return null ;
}

/**
*
* @param clazz
* @param <T>
* @return
* @throws Exception
*/
public <T> T getBean(Class<T> clazz) {
try {
return cicadaBeanFactory.getBean(clazz) ;
} catch (Exception e) {
LOGGER.error("get bean error",e);
}
return null ;
}

/**
* release all beans
*/
public void releaseBean(){
cicadaBeanFactory.releaseBean();
}

public void exceptionHandle(GlobalHandelException ex){
handelException = ex ;
}

public GlobalHandelException exceptionHandle(){
return handelException ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public Object getBean(String name) throws Exception {
return aClass.newInstance();
}

@Override
public <T> T getBean(Class<T> clazz) throws Exception {
return clazz.newInstance();
}

@Override
public void releaseBean() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static void setting(Class<?> clazz, String rootPath) throws Exception {
//Set application configuration
setAppConfig(rootPath);

//init route bean factory
CicadaBeanManager.getInstance().init(rootPath);
//initBean route bean factory
CicadaBeanManager.getInstance().initBean(rootPath);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package top.crossoverjie.cicada.server.exception;

import top.crossoverjie.cicada.server.context.CicadaContext;

/**
* Function: global exception handle
*
* @author crossoverJie
* Date: 2019-07-10 17:12
* @since JDK 1.8
*/
public interface GlobalHandelException {

/**
* exception handle
* @param context
* @param e
*/
void resolveException(CicadaContext context,Exception e) ;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package top.crossoverjie.cicada.server.handle;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import top.crossoverjie.cicada.base.log.LoggerBuilder;
import top.crossoverjie.cicada.server.action.param.Param;
Expand All @@ -17,11 +15,12 @@
import top.crossoverjie.cicada.server.action.req.CicadaRequest;
import top.crossoverjie.cicada.server.action.res.CicadaHttpResponse;
import top.crossoverjie.cicada.server.action.res.CicadaResponse;
import top.crossoverjie.cicada.server.action.res.WorkRes;
import top.crossoverjie.cicada.server.bean.CicadaBeanManager;
import top.crossoverjie.cicada.server.config.AppConfig;
import top.crossoverjie.cicada.server.constant.CicadaConstant;
import top.crossoverjie.cicada.server.context.CicadaContext;
import top.crossoverjie.cicada.server.exception.CicadaException;
import top.crossoverjie.cicada.server.exception.GlobalHandelException;
import top.crossoverjie.cicada.server.intercept.InterceptProcess;
import top.crossoverjie.cicada.server.route.RouteProcess;
import top.crossoverjie.cicada.server.route.RouterScanner;
Expand All @@ -48,6 +47,9 @@ public final class HttpDispatcher extends SimpleChannelInboundHandler<DefaultHtt
private final InterceptProcess interceptProcess = InterceptProcess.getInstance();
private final RouterScanner routerScanner = RouterScanner.getInstance();
private final RouteProcess routeProcess = RouteProcess.getInstance() ;
private final CicadaBeanManager cicadaBeanManager = CicadaBeanManager.getInstance() ;
private final GlobalHandelException exceptionHandle = cicadaBeanManager.exceptionHandle() ;
private Exception exception ;

@Override
public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest httpRequest) {
Expand Down Expand Up @@ -113,7 +115,6 @@ public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest httpReque
* @param ctx
*/
private void responseContent(ChannelHandlerContext ctx) {

CicadaResponse cicadaResponse = CicadaContext.getResponse();
String context = cicadaResponse.getHttpContent() ;

Expand Down Expand Up @@ -145,20 +146,14 @@ private Param buildParamMap(QueryStringDecoder queryStringDecoder) {

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

exception = (Exception) cause;
if (CicadaException.isResetByPeer(cause.getMessage())){
return;
}

LOGGER.error(cause.getMessage(), cause);

WorkRes workRes = new WorkRes();
workRes.setCode(String.valueOf(HttpResponseStatus.NOT_FOUND.code()));
workRes.setMessage(cause.getMessage());

DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.copiedBuffer(JSON.toJSONString(workRes), CharsetUtil.UTF_8));
buildHeader(response);
ctx.writeAndFlush(response);
if (exceptionHandle != null){
exceptionHandle.resolveException(CicadaContext.getContext(),exception);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import top.crossoverjie.cicada.base.bean.CicadaBeanFactory;
import top.crossoverjie.cicada.base.log.LoggerBuilder;
import top.crossoverjie.cicada.server.annotation.CicadaAction;
import top.crossoverjie.cicada.server.annotation.CicadaBean;
import top.crossoverjie.cicada.server.annotation.Interceptor;
import top.crossoverjie.cicada.server.bean.CicadaDefaultBean;
import top.crossoverjie.cicada.server.configuration.AbstractCicadaConfiguration;
Expand All @@ -28,7 +29,7 @@
* Date: 2018/9/1 11:36
* @since JDK 1.8
*/
public class ClassScanner {
public final class ClassScanner {

private final static Logger LOGGER = LoggerBuilder.getLogger(ClassScanner.class);

Expand Down Expand Up @@ -74,13 +75,13 @@ public static List<Class<?>> getConfiguration(String packageName) throws Excepti
return configurationList;
}
/**
* get @CicadaAction
* get @CicadaAction & @CicadaBean
*
* @param packageName
* @return
* @throws Exception
*/
public static Map<String, Class<?>> getCicadaAction(String packageName) throws Exception {
public static Map<String, Class<?>> getCicadaBean(String packageName) throws Exception {

if (actionMap == null) {
Set<Class<?>> clsList = getClasses(packageName);
Expand All @@ -92,19 +93,40 @@ public static Map<String, Class<?>> getCicadaAction(String packageName) throws E
actionMap = new HashMap<>(16);
for (Class<?> cls : clsList) {

Annotation annotation = cls.getAnnotation(CicadaAction.class);
if (annotation == null) {
CicadaAction action = cls.getAnnotation(CicadaAction.class);
CicadaBean bean = cls.getAnnotation(CicadaBean.class);
if (action == null && bean == null) {
continue;
}

CicadaAction cicadaAction = (CicadaAction) annotation;
actionMap.put(cicadaAction.value() == null ? cls.getName() : cicadaAction.value(), cls);
if (action != null){
actionMap.put(action.value() == null ? cls.getName() : action.value(), cls);
}

if (bean != null){
actionMap.put(bean.value() == null ? cls.getName() : bean.value(), cls);
}

}
}
return actionMap;
}

/**
* whether is the target class
* @param clazz
* @param target
* @return
*/
public static boolean isInterface(Class<?> clazz,Class<?> target){
for (Class<?> aClass : clazz.getInterfaces()) {
if (aClass.getName().equals(target.getName())){
return true ;
}
}
return false ;
}

/**
* get @Interceptor
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void getClasses() throws Exception {

@Test
public void getActionAction() throws Exception{
Map<String, Class<?>> cicadaAction = ClassScanner.getCicadaAction("top.crossoverjie.cicada.server");
Map<String, Class<?>> cicadaAction = ClassScanner.getCicadaBean("top.crossoverjie.cicada.server");
LOGGER.info("classes=[{}]", JSON.toJSONString(cicadaAction));
}

Expand Down
Loading