增加Http路由器相关封装

This commit is contained in:
aoshiguchen
2022-07-16 20:50:01 +08:00
parent e2b53c7936
commit 40e93a29bd
17 changed files with 512 additions and 5 deletions
@@ -19,7 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.bean;
package fun.asgc.neutrino.core.base;
/**
* Identity 用于标识一个身份
@@ -48,7 +48,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry,
/**
* 父工厂
*/
private AbstractBeanFactory parent;
protected AbstractBeanFactory parent;
/**
* bean缓存
*/
@@ -21,6 +21,7 @@
*/
package fun.asgc.neutrino.core.bean;
import fun.asgc.neutrino.core.base.Identity;
import fun.asgc.neutrino.core.util.Assert;
/**
@@ -87,9 +87,11 @@ public class BeanWrapper implements LifeCycle {
@Override
public void destroy() {
if (!hasInstance()) {
if (BeanStatus.DESTROY == status) {
return;
}
this.setStatus(BeanStatus.DESTROY);
Set<Method> methods = ReflectUtil.getMethods(type);
if (CollectionUtil.notEmpty(methods)) {
for (Method method : methods) {
@@ -356,4 +356,16 @@ public class SimpleBeanFactory extends AbstractBeanFactory {
}
return null;
}
/**
* BeanWrapper集合
* @return
*/
public List<BeanWrapper> beanWrapperList() {
return FunctionUtil.mergeList(
() -> null == parent ? null :
parent instanceof SimpleBeanFactory ? ((SimpleBeanFactory)parent).beanWrapperList() : null,
() -> beanCache.values().stream().collect(Collectors.toList())
);
}
}
@@ -24,6 +24,8 @@ package fun.asgc.neutrino.core.util;
import fun.asgc.neutrino.core.aop.AopCallback;
import fun.asgc.neutrino.core.base.CodeBlock;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
/**
@@ -81,4 +83,26 @@ public class FunctionUtil {
return null;
}
/**
* 合并list
* @param suppliers
* @param <T>
* @return
*/
public static <T> List<T> mergeList(Supplier<List<T>> ...suppliers) {
List<T> list = new ArrayList<>();
if (null != suppliers && suppliers.length > 0) {
for (Supplier<List<T>> supplier : suppliers) {
if (null == supplier) {
continue;
}
List<T> tmpList = supplier.get();
if (CollectionUtil.isEmpty(tmpList)) {
continue;
}
list.addAll(tmpList);
}
}
return list;
}
}
@@ -25,6 +25,7 @@ import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.context.ApplicationConfig;
import fun.asgc.neutrino.core.web.router.DefaultHttpRouter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
@@ -38,6 +39,8 @@ import io.netty.handler.codec.http.FullHttpRequest;
public class HttpRequestHandler {
@Autowired
private ApplicationConfig applicationConfig;
@Autowired
private DefaultHttpRouter defaultHttpRouter;
public void handle(ChannelHandlerContext context, FullHttpRequest request) {
// TODO
@@ -21,10 +21,12 @@
*/
package fun.asgc.neutrino.core.web;
import com.google.common.collect.Sets;
import fun.asgc.neutrino.core.annotation.*;
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
import fun.asgc.neutrino.core.context.ApplicationContext;
import fun.asgc.neutrino.core.context.ApplicationRunner;
import fun.asgc.neutrino.core.web.router.DefaultHttpRouter;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
@@ -63,8 +65,15 @@ public class WebApplicationContext implements ApplicationRunner {
@Override
public void run(String[] args) throws Exception {
this.webApplicationBeanFactory.registerBean(HttpRequestHandler.class);
this.webApplicationBeanFactory.registerBean(WebApplicationServer.class);
this.webApplicationBeanFactory.register(
Sets.newHashSet(
DefaultHttpRouter.class,
HttpRequestHandler.class,
WebApplicationServer.class
)
);
// this.webApplicationBeanFactory.registerBean(HttpRequestHandler.class);
// this.webApplicationBeanFactory.registerBean(WebApplicationServer.class);
}
}
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.annotation;
import fun.asgc.neutrino.core.web.HttpMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author: aoshiguchen
* @date: 2022/7/16
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface RequestMapping {
String[] value() default {};
HttpMethod[] method() default {};
}
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
import fun.asgc.neutrino.core.annotation.*;
import fun.asgc.neutrino.core.bean.BeanWrapper;
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 默认的http路由器
* @author: aoshiguchen
* @date: 2022/7/16
*/
@Slf4j
@NonIntercept
@Component
public class DefaultHttpRouter implements HttpRouter {
/**
* 路由缓存
*/
private Map<HttpRouteIdentity, HttpRouteInfo> routeCache = new ConcurrentHashMap<>(256);
@Autowired
private SimpleBeanFactory webApplicationBeanFactory;
@Init
public void init() {
log.debug("Http路由器初始化...");
List<BeanWrapper> beanWrapperList = webApplicationBeanFactory.beanWrapperList();
// TODO 路由初始化
}
@Override
public HttpRouteResult route(HttpRouteParam httpRouteParam) {
HttpRouteIdentity identity = new HttpRouteIdentity(httpRouteParam.getMethod(), httpRouteParam.getUrl());
HttpRouteInfo httpRouteInfo = routeCache.get(identity);
if (null == httpRouteInfo) {
return null;
}
if (HttpRouterType.PAGE == httpRouteInfo.getType()) {
return new HttpRouteResult()
.setType(httpRouteInfo.getType())
.setPageLocation(httpRouteInfo.getPageLocation());
}
return new HttpRouteResult()
.setType(httpRouteInfo.getType())
.setMethod(httpRouteInfo.getMethod())
.setInstance(webApplicationBeanFactory.getBean(httpRouteInfo.getBeanIdentity()));
}
@Destroy
public void destroy() {
log.debug("Http路由器销毁...");
}
}
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
import fun.asgc.neutrino.core.base.Identity;
import fun.asgc.neutrino.core.util.Assert;
import fun.asgc.neutrino.core.web.HttpMethod;
/**
* http路由唯一标识
* @author: aoshiguchen
* @date: 2022/7/16
*/
public class HttpRouteIdentity implements Identity {
/**
* http方法
*/
private HttpMethod method;
/**
* url
*/
private String url;
/**
* hashCode
*/
private int identityHashCode;
@Override
public boolean isOnly() {
return true;
}
public HttpRouteIdentity(HttpMethod method, String url) {
Assert.notNull(method, "http方法不能为空!");
Assert.notEmpty(url, "url不能为空!");
this.method = method;
this.url = url;
this.identityHashCode = System.identityHashCode(url);
}
public HttpMethod getMethod() {
return method;
}
public String getUrl() {
return url;
}
@Override
public boolean equals(Object obj) {
if (null == obj) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof HttpRouteIdentity)) {
return false;
}
HttpRouteIdentity httpRouteIdentity = (HttpRouteIdentity)obj;
return this.method.equals(httpRouteIdentity.getMethod()) && this.url.equals(httpRouteIdentity.getUrl());
}
@Override
public int hashCode() {
return identityHashCode;
}
}
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
import fun.asgc.neutrino.core.bean.BeanIdentity;
import lombok.Data;
import java.lang.reflect.Method;
/**
* http路由信息
* @author: aoshiguchen
* @date: 2022/7/16
*/
@Data
public class HttpRouteInfo {
/**
* 路由类型
*/
private HttpRouterType type;
/**
* bean唯一标识
*/
private BeanIdentity beanIdentity;
/**
* 方法
*/
private Method method;
/**
* 页面位置
*/
private String pageLocation;
}
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
import fun.asgc.neutrino.core.web.HttpMethod;
import lombok.Data;
/**
* http路由参数
* @author: aoshiguchen
* @date: 2022/7/16
*/
@Data
public class HttpRouteParam {
private HttpMethod method;
private String url;
}
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
import lombok.Data;
import lombok.experimental.Accessors;
import java.lang.reflect.Method;
/**
* http路由结果
* @author: aoshiguchen
* @date: 2022/7/16
*/
@Accessors(chain = true)
@Data
public class HttpRouteResult {
/**
* 路由类型
*/
private HttpRouterType type;
/**
* 实例
*/
private Object instance;
/**
* 方法
*/
private Method method;
/**
* 页面位置
*/
private String pageLocation;
}
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
/**
* http路由器
* @author: aoshiguchen
* @date: 2022/7/16
*/
public interface HttpRouter extends Router<HttpRouteParam, HttpRouteResult> {
}
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* http路由类型
* @author: aoshiguchen
* @date: 2022/7/16
*/
@Getter
@AllArgsConstructor
public enum HttpRouterType {
METHOD(1, "方法"),
PAGE(2, "页面");
private Integer type;
private String desc;
}
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.router;
/**
* 路由器
* @author: aoshiguchen
* @date: 2022/7/16
*/
public interface Router<P,R> {
/**
* 路由
* @param p
* @return
*/
R route(P p);
}