Skip to content

Dev #20

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

Merged
merged 21 commits into from
Feb 14, 2023
Merged

Dev #20

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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-fast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>1.5.2</version>
<version>1.5.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.codingapi.springboot.fast.executor.JpaExecutor;
import com.codingapi.springboot.fast.mapping.MvcEndpointMapping;
import com.codingapi.springboot.fast.registrar.MvcMappingRegistrar;
import org.springframework.aop.Advisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
Expand All @@ -11,6 +12,7 @@
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.persistence.EntityManager;
import java.util.List;

@Configuration
@ConditionalOnClass(WebMvcConfigurer.class)
Expand All @@ -25,8 +27,10 @@ public MvcEndpointMapping mvcEndpointMapping(RequestMappingHandlerMapping handle

@Bean(initMethod = "registerMvcMapping")
@ConditionalOnMissingBean
public MvcMappingRegistrar mappingRegistrar(MvcEndpointMapping mvcEndpointMapping, JpaExecutor jpaExecutor) {
return new MvcMappingRegistrar(mvcEndpointMapping, jpaExecutor);
public MvcMappingRegistrar mappingRegistrar(MvcEndpointMapping mvcEndpointMapping,
JpaExecutor jpaExecutor,
List<Advisor> advisors) {
return new MvcMappingRegistrar(mvcEndpointMapping, jpaExecutor,advisors);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@

import com.codingapi.springboot.fast.annotation.FastMapping;
import lombok.AllArgsConstructor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

@AllArgsConstructor
public class MvcMethodProxy implements InvocationHandler {
public class MvcMethodInterceptor implements MethodInterceptor {

private final JpaExecutor jpaExecutor;

@Override
public Object invoke(Object proxy, Method method, Object[] args)
public Object invoke(MethodInvocation invocation)
throws Throwable {
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();

if (method.equals(Object.class.getMethod("equals", Object.class))) {
return false;
}

if (method.equals(Object.class.getMethod("hashCode"))) {
return hashCode();
}

FastMapping fastMapping = method.getAnnotation(FastMapping.class);
if (fastMapping != null) {
Class<?> returnType = method.getReturnType();
return jpaExecutor.execute(fastMapping.value(), fastMapping.countQuery(), args, returnType);
}

// mvc mapping proxy can't execute return null.
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@
import com.codingapi.springboot.fast.annotation.FastMapping;
import com.codingapi.springboot.fast.exception.FastMappingErrorException;
import com.codingapi.springboot.fast.executor.JpaExecutor;
import com.codingapi.springboot.fast.executor.MvcMethodProxy;
import com.codingapi.springboot.fast.executor.MvcMethodInterceptor;
import com.codingapi.springboot.fast.mapping.MvcEndpointMapping;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.framework.AopProxyFactory;
import org.springframework.aop.framework.DefaultAopProxyFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@AllArgsConstructor
public class MvcMappingRegistrar {
protected final static Set<Class<?>> classSet = new HashSet<>();
private final MvcEndpointMapping mvcEndpointMapping;
private final JpaExecutor jpaExecutor;

private final AopProxyFactory proxyFactory;

private final List<Advisor> advisors;

private final MvcMethodInterceptor interceptor;

public MvcMappingRegistrar(MvcEndpointMapping mvcEndpointMapping,
JpaExecutor jpaExecutor,
List<Advisor> advisors) {
this.mvcEndpointMapping = mvcEndpointMapping;
this.advisors = advisors;
this.interceptor = new MvcMethodInterceptor(jpaExecutor);
this.proxyFactory = new DefaultAopProxyFactory();
}

@SneakyThrows
public void registerMvcMapping() {
Expand All @@ -30,36 +47,47 @@ public void registerMvcMapping() {
for (Method method : methods) {
FastMapping fastMapping = method.getAnnotation(FastMapping.class);
if (verify(fastMapping, method)) {
MvcMethodProxy handler = new MvcMethodProxy(jpaExecutor);
Object methodProxy = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, handler);
mvcEndpointMapping.addMapping(fastMapping.mapping(), fastMapping.method(), methodProxy, method);
AdvisedSupport advisedSupport = createAdvisedSupport(clazz);
AopProxy proxy = proxyFactory.createAopProxy(advisedSupport);
mvcEndpointMapping.addMapping(fastMapping.mapping(), fastMapping.method(),
proxy.getProxy(), method);
}
}
}
}

private AdvisedSupport createAdvisedSupport(Class<?> clazz) {
AdvisedSupport advisedSupport = new AdvisedSupport(clazz);
advisedSupport.setTarget(interceptor);
advisedSupport.addAdvisors(advisors);
advisedSupport.addAdvice(interceptor);
return advisedSupport;
}

private boolean verify(FastMapping fastMapping, Method method) throws FastMappingErrorException {
if (fastMapping == null) {
return false;
}

if (!StringUtils.hasText(fastMapping.mapping())) {
throw new FastMappingErrorException(String.format("fast method %s missing mapping .", method.getName()));
throw new FastMappingErrorException(String.format("fast method %s missing mapping .",
method.getName()));
}

if (!StringUtils.hasText(fastMapping.value())) {
throw new FastMappingErrorException(String.format("fast mapping %s missing value .", fastMapping.mapping()));
throw new FastMappingErrorException(String.format("fast mapping %s missing value .",
fastMapping.mapping()));
}

Class<?>[] parameterTypes = method.getParameterTypes();
for (Class<?> parameter : parameterTypes) {
if (Pageable.class.isAssignableFrom(parameter)) {
if (!StringUtils.hasText(fastMapping.countQuery())) {
throw new FastMappingErrorException(String.format("fast mapping %s missing countQuery .", fastMapping.mapping()));
throw new FastMappingErrorException(String.format("fast mapping %s missing countQuery .",
fastMapping.mapping()));
}
}
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-id-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>1.5.2</version>
<version>1.5.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-security-jwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>1.5.2</version>
<version>1.5.3</version>
</parent>

<artifactId>springboot-starter-security-jwt</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
</parent>
<artifactId>springboot-starter</artifactId>

Expand Down