com.blade.loader.BladeLoader Java Examples

The following examples show how to use com.blade.loader.BladeLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NettyServer.java    From blade with Apache License 2.0 4 votes vote down vote up
private void parseAndCreate(Class<?> clazz) {
    if (null != clazz.getAnnotation(Bean.class) || null != clazz.getAnnotation(Value.class)) {
        blade.register(clazz);
    }
    if (null != clazz.getAnnotation(Path.class)) {
        if (null == blade.getBean(clazz)) {
            blade.register(clazz);
        }
        Object controller = blade.getBean(clazz);
        routeBuilder.addRouter(clazz, controller);
    }
    if (null != clazz.getAnnotation(Configuration.class) && clazz.getMethods().length > 0) {
        Object config = ReflectKit.newInstance(clazz);
        Arrays.stream(clazz.getMethods())
                .filter(m -> m.getAnnotation(Bean.class) != null)
                .forEach(n -> {
                    try {
                        blade.register(n.invoke(config));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
    }
    if (ReflectKit.hasInterface(clazz, WebHook.class) && null != clazz.getAnnotation(Bean.class)) {
        URLPattern URLPattern = clazz.getAnnotation(URLPattern.class);
        if (null == URLPattern) {
            routeBuilder.addWebHook(clazz, "/.*");
        } else {
            Stream.of(URLPattern.values())
                    .forEach(pattern -> routeBuilder.addWebHook(clazz, pattern));
        }
    }

    if (ReflectKit.hasInterface(clazz, BladeLoader.class) && null != clazz.getAnnotation(Bean.class)) {
        this.loaders.add((BladeLoader) blade.getBean(clazz));
    }
    if (ReflectKit.hasInterface(clazz, BeanProcessor.class) && null != clazz.getAnnotation(Bean.class)) {
        this.processors.add((BeanProcessor) blade.getBean(clazz));
    }
    if (isExceptionHandler(clazz)) {
        ExceptionHandler exceptionHandler = (ExceptionHandler) blade.getBean(clazz);
        blade.exceptionHandler(exceptionHandler);
    }
    WebSocket webSocket;
    if (null != (webSocket = clazz.getAnnotation(WebSocket.class))) {
        if (null == blade.getBean(clazz)) {
            blade.register(clazz);
        }
        if (ReflectKit.hasInterface(clazz, WebSocketHandler.class)) {
            blade.webSocket(webSocket.value(), (WebSocketHandler) blade.getBean(clazz));
        } else {
            WebSocketHandlerWrapper wrapper = blade.getBean(WebSocketHandlerWrapper.class);
            if (wrapper == null) {
                wrapper = WebSocketHandlerWrapper.init(blade);
                blade.register(wrapper);
            }
            blade.webSocket(webSocket.value(), wrapper);
            wrapper.wrapHandler(webSocket.value(), clazz);
        }

    }
}
 
Example #2
Source File: Blade.java    From blade with Apache License 2.0 4 votes vote down vote up
public List<BladeLoader> loaders() {
    return this.loaders;
}
 
Example #3
Source File: Blade.java    From blade with Apache License 2.0 2 votes vote down vote up
/**
 * Add blade loader
 *
 * @param loader
 * @return
 */
public Blade addLoader(@NonNull BladeLoader loader) {
    this.loaders.add(loader);
    return this;
}