Skip to content

Commit

Permalink
perf: 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
AnswerAIL committed May 31, 2021
1 parent f2abb4b commit 163d9f2
Show file tree
Hide file tree
Showing 18 changed files with 588 additions and 81 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.answerail</groupId>
<artifactId>dinger-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<version>1.2.0-beta1</version>
<version>1.2.0-beta2</version>

<name>dinger-spring-boot-starter</name>
<description>Dinger-SpringBoot集成钉钉/企业微信群机器人实现消息通知中间件</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright ©2015-2021 Jaemon. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jaemon.dinger.config;

import com.github.jaemon.dinger.core.DingerRobot;
import com.github.jaemon.dinger.core.entity.DingerProperties;
import com.github.jaemon.dinger.core.session.DingerSessionFactory;
import com.github.jaemon.dinger.core.spring.DingerSessionFactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* DingerAutoConfiguration
*
* @author Jaemon
* @since 1.2
*/
@Configuration
@EnableConfigurationProperties(DingerProperties.class)
@AutoConfigureAfter(DingerConfiguration.class)
public class DingerAutoConfiguration implements InitializingBean {
private final DingerProperties properties;
private final DingerRobot dingerRobot;
private final ResourceLoader resourceLoader;

public DingerAutoConfiguration(
DingerProperties dingerProperties,
DingerRobot dingerRobot,
ResourceLoader resourceLoader
) {
this.properties = dingerProperties;
this.dingerRobot = dingerRobot;
this.resourceLoader = resourceLoader;
}

@Bean
@ConditionalOnMissingBean
public DingerSessionFactory dingerSessionFactory() throws Exception {
DingerSessionFactoryBean factory = new DingerSessionFactoryBean();

factory.setConfiguration(
com.github.jaemon.dinger.core.session.Configuration.of(properties, dingerRobot)
);

return factory.getObject();
}

@Override
public void afterPropertiesSet() throws Exception {
checkConfigFileExists();
}

private void checkConfigFileExists() {

if (
StringUtils.hasText(this.properties.getDingerLocations())
) {

Resource resource = this.resourceLoader.getResource(this.properties.getDingerLocations());

Assert.state(resource.exists(), "Cannot find config location: " + resource
+ " (please add config file or check your Dinger configuration)");
}
}
}
38 changes: 21 additions & 17 deletions src/main/java/com/github/jaemon/dinger/core/DingerHandleProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
package com.github.jaemon.dinger.core;

import com.github.jaemon.dinger.core.annatations.DingerClose;
import com.github.jaemon.dinger.core.entity.DingerProperties;
import com.github.jaemon.dinger.core.entity.MsgType;
import com.github.jaemon.dinger.core.entity.enums.DingerResponseCodeEnum;
import com.github.jaemon.dinger.core.entity.enums.DingerType;
import com.github.jaemon.dinger.core.entity.DingerResponse;
import com.github.jaemon.dinger.core.session.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

Expand All @@ -36,43 +35,48 @@
* @author Jaemon
* @since 1.0
*/
public class DingerHandleProxy extends DingerMessageHandler implements InvocationHandler {
public class DingerHandleProxy extends DingerInvocationHandler {
private static final Logger log = LoggerFactory.getLogger(DingerHandleProxy.class);
private static final String DEFAULT_STRING_METHOD = "java.lang.Object.toString";

public DingerHandleProxy(DingerRobot dingerRobot, DingerProperties dingerProperties) {
this.dingerRobot = dingerRobot;
this.dingerProperties = dingerProperties;
public DingerHandleProxy(Configuration configuration) {
this.dingerRobot = configuration.getDingerRobot();
this.dingerProperties = configuration.getDingerProperties();
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Class<?> dingerClass = method.getDeclaringClass();
boolean clzClose = dingerClass.isAnnotationPresent(DingerClose.class);
if (clzClose) {

final String methodName = method.getName();

if (
ignoreMethodMap.containsKey(methodName)
) {
return ignoreMethodMap.get(methodName).execute(this, args);
}

if (
dingerClass.isAnnotationPresent(DingerClose.class)
) {
return null;
}

boolean methodClose = method.isAnnotationPresent(DingerClose.class);
if (methodClose) {
if (
method.isAnnotationPresent(DingerClose.class)
) {
return null;
}

final String dingerClassName = dingerClass.getName();
final String methodName = method.getName();
String keyName = dingerClassName + SPOT_SEPERATOR + methodName;

if (DEFAULT_STRING_METHOD.equals(keyName)) {
return this.toString();
}

try {
DingerType useDinger = dingerType(method);
DingerDefinition dingerDefinition = dingerDefinition(
useDinger, dingerClassName, keyName
);

DingerResponse dingerResponse;

if (dingerDefinition == null) {
dingerResponse = DingerResponse.failed(
DingerResponseCodeEnum.MESSAGE_TYPE_UNSUPPORTED,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright ©2015-2021 Jaemon. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jaemon.dinger.core;

import java.lang.reflect.InvocationHandler;

/**
* Dinger Invocation Handler
*
* @author Jaemon
* @since 1.2
*/
public abstract class DingerInvocationHandler
extends DingerMessageHandler
implements InvocationHandler {

@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright ©2015-2021 Jaemon. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jaemon.dinger.core.entity.enums;

import com.github.jaemon.dinger.core.DingerInvocationHandler;

/**
* Ignore Method
*
* @author Jaemon
* @since 1.2
*/
public enum IgnoreMethod {

EQUALS("equals") {
@Override
public Boolean execute(DingerInvocationHandler invocationHandler, Object[] args) {
return invocationHandler.equals(args[0]);
}
},
CLONE("clone") {
@Override
public Object execute(DingerInvocationHandler invocationHandler, Object[] args) throws CloneNotSupportedException {
return invocationHandler.clone();
}
},
HASH_CODE("hashCode") {
@Override
public Integer execute(DingerInvocationHandler invocationHandler, Object[] args) throws Exception {
return invocationHandler.hashCode();
}
},
TO_STRING("toString") {
@Override
public String execute(DingerInvocationHandler invocationHandler, Object[] args) throws Exception {
return invocationHandler.toString();
}
},

;

private String methodName;

IgnoreMethod(String methodName) {
this.methodName = methodName;
}


public abstract Object execute(DingerInvocationHandler invocationHandler, Object[] args) throws Exception;

public String getMethodName() {
return methodName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright ©2015-2021 Jaemon. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jaemon.dinger.core.session;

import com.github.jaemon.dinger.core.DingerRobot;
import com.github.jaemon.dinger.core.entity.DingerProperties;

/**
* Configuration
*
* @author Jaemon
* @version 1.2
*/
public class Configuration {
protected DingerProperties dingerProperties;
protected DingerRobot dingerRobot;

private Configuration(DingerProperties dingerProperties, DingerRobot dingerRobot) {
this.dingerProperties = dingerProperties;
this.dingerRobot = dingerRobot;
}

public static Configuration of(DingerProperties dingerProperties, DingerRobot dingerRobot) {
return new Configuration(dingerProperties, dingerRobot);
}

public DingerProperties getDingerProperties() {
return dingerProperties;
}

public DingerRobot getDingerRobot() {
return dingerRobot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright ©2015-2021 Jaemon. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jaemon.dinger.core.session;


/**
* DingerSession
*
* @author Jaemon
* @since 1.0
*/
public interface DingerSession {

/**
* Retrieves a dinger.
*
* @param type
* dinger interface class
* @param <T>
* the dinger type
* @return
* a dinger bound to this DingerSession
*/
<T> T getDinger(Class<T> type);

/**
* dinger configuration
*
* @return
* {@link Configuration}
*/
Configuration configuration();

}
Loading

0 comments on commit 163d9f2

Please sign in to comment.