-
Notifications
You must be signed in to change notification settings - Fork 749
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
feature:支持扩展,目前先支持DtpExecutor和ScheduledDtpExecutor的扩展 #288
Changes from all commits
21096e3
6def4fd
9599ad2
13c196a
bdc8182
bc1ccdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.dromara.dynamictp.common.entity; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class DtpExtensionProps { | ||
|
||
private String extensionPath; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.dromara.dynamictp.core.plugin; | ||
|
||
import org.dromara.dynamictp.core.thread.DtpExecutor; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.RejectedExecutionHandler; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ConstructorUtil { | ||
|
||
public static Object[] buildDtpExecutorConstructorArgs(DtpExecutor dtpExecutor) { | ||
|
||
return new Object[] { | ||
dtpExecutor.getCorePoolSize(), | ||
dtpExecutor.getMaximumPoolSize(), | ||
dtpExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS), | ||
TimeUnit.MILLISECONDS, | ||
dtpExecutor.getQueue(), | ||
dtpExecutor.getThreadFactory(), | ||
dtpExecutor.getRejectedExecutionHandler() | ||
}; | ||
} | ||
|
||
public static Class[] buildDtpExecutorConstructorArgTypes() { | ||
|
||
return new Class[] { | ||
int.class, | ||
int.class, | ||
long.class, | ||
TimeUnit.class, | ||
BlockingQueue.class, | ||
ThreadFactory.class, | ||
RejectedExecutionHandler.class | ||
}; | ||
} | ||
|
||
|
||
public static Class[] buildConstructorArgs() { | ||
|
||
return null; | ||
} | ||
|
||
public static Class[] buildConstructorArgTypes(Object obj) { | ||
|
||
Class clazz = obj.getClass(); | ||
Constructor[] constructors = clazz.getConstructors(); | ||
if (constructors != null && constructors.length > 0) { | ||
return constructors[0].getParameterTypes(); | ||
} | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.dromara.dynamictp.core.plugin; | ||
|
||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
/** | ||
* Extension interface | ||
* @author windsearcher.lq | ||
* @since 22:22 | ||
*/ | ||
public interface DtpExtension { | ||
|
||
Object intercept(DtpInvocation invocation) throws InvocationTargetException, IllegalAccessException; | ||
|
||
default Object plugin(Object target) { | ||
return DtpExtensionProxyFactory.wrap(target, this); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an overloaded method. default Object plugin(Object target, Class[] argumentTypes, Object[] arguments) { } |
||
|
||
default Object plugin(Object target, Class[] argumentTypes, Object[] arguments) { | ||
return DtpExtensionProxyFactory.wrap(target, argumentTypes, arguments, this); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.dromara.dynamictp.core.plugin; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author windsearcher.lq | ||
* @since 2023/6/9 20:47 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface DtpExtensionPoint { | ||
|
||
DtpSignature[] value(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.dromara.dynamictp.core.plugin; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add Apache Licenses to the class header |
||
|
||
import net.sf.cglib.proxy.MethodInterceptor; | ||
import net.sf.cglib.proxy.MethodProxy; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Proxy packaging class | ||
* @author windsearcher.lq | ||
* @since 2023/6/9 21:02 | ||
*/ | ||
public class DtpExtensionProxy implements MethodInterceptor { | ||
|
||
private final Object target; | ||
|
||
private final DtpExtension interceptor; | ||
|
||
private final Map<Class<?>, Set<Method>> signatureMap; | ||
|
||
public DtpExtensionProxy(Object target, DtpExtension interceptor, Map<Class<?>, Set<Method>> signatureMap) { | ||
this.target = target; | ||
this.interceptor = interceptor; | ||
this.signatureMap = signatureMap; | ||
} | ||
|
||
@Override | ||
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { | ||
Set<Method> methods = signatureMap.get(method.getDeclaringClass()); | ||
if (CollectionUtils.isNotEmpty(methods) && methods.contains(method)) { | ||
return interceptor.intercept(new DtpInvocation(target, method, args)); | ||
WindSearcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return method.invoke(target, args); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use StringUtils.isBlack()