All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.aspectran.core.context.rule.JoinpointRule Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2024 The Aspectran Project
*
* 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.aspectran.core.context.rule;
import com.aspectran.core.context.rule.params.JoinpointParameters;
import com.aspectran.core.context.rule.params.PointcutParameters;
import com.aspectran.core.context.rule.params.PointcutQualifierParameters;
import com.aspectran.core.context.rule.type.JoinpointTargetType;
import com.aspectran.core.context.rule.type.MethodType;
import com.aspectran.utils.StringUtils;
import com.aspectran.utils.ToStringBuilder;
import com.aspectran.utils.annotation.jsr305.NonNull;
import com.aspectran.utils.annotation.jsr305.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* A join point is the specific point in the application such as method execution,
* exception handling, injecting settings values etc. In Aspectran AOP a join points
* is always the execution of a method.
*
*
* <aspect id="sampleAspect" order="0" isolated="true">
* <joinpoint>
* methods: [
* "GET"
* "POST"
* "PATCH"
* "PUT"
* "DELETE"
* ]
* headers: [
* "Origin"
* ]
* pointcut: {
* type: "wildcard"
* +: "/a/[email protected] ^method1"
* +: "/x/[email protected] ^method1"
* -: "/a/b/[email protected] ^method1"
* -: "/x/y/[email protected] ^method1"
* }
* pointcut: {
* type: "regexp"
* include: {
* translet: "/a/b"
* bean: "sample.bean1"
* method: "method1"
* }
* exclude: {
* translet: "/a/b/c"
* bean: "sample.bean3"
* method: "method1"
* }
* }
* </joinpoint>
* <settings>
* </settings>
* <advice>
* </advice>
* <exception>
* </exception>
* <aspect>
*
*/
public class JoinpointRule {
private JoinpointTargetType joinpointTargetType;
private MethodType[] methods;
private String[] headers;
private PointcutRule pointcutRule;
private JoinpointParameters joinpointParameters;
public JoinpointTargetType getJoinpointTargetType() {
return joinpointTargetType;
}
public void setJoinpointTargetType(JoinpointTargetType joinpointTargetType) {
this.joinpointTargetType = joinpointTargetType;
if (joinpointParameters != null) {
if (joinpointTargetType != null) {
joinpointParameters.putValue(JoinpointParameters.target, joinpointTargetType.toString());
} else {
joinpointParameters.putValue(JoinpointParameters.target, null);
}
}
}
public MethodType[] getMethods() {
return methods;
}
public void setMethods(MethodType[] methods) {
this.methods = methods;
}
public String[] getHeaders() {
return headers;
}
public void setHeaders(String[] headers) {
this.headers = headers;
}
public PointcutRule getPointcutRule() {
return pointcutRule;
}
public void setPointcutRule(PointcutRule pointcutRule) {
this.pointcutRule = pointcutRule;
}
public JoinpointParameters getJoinpointParameters() {
return joinpointParameters;
}
private void setJoinpointParameters(JoinpointParameters joinpointParameters) {
this.joinpointParameters = joinpointParameters;
}
@Override
public String toString() {
ToStringBuilder tsb = new ToStringBuilder();
tsb.append("target", joinpointTargetType);
tsb.append("methods", methods);
tsb.append("headers", headers);
tsb.append("pointcut", pointcutRule);
return tsb.toString();
}
@NonNull
public static JoinpointRule newInstance() {
return new JoinpointRule();
}
public static void updateJoinpoint(JoinpointRule joinpointRule, String apon)
throws IllegalRuleException {
if (StringUtils.hasText(apon)) {
JoinpointParameters joinpointParameters;
try {
joinpointParameters = new JoinpointParameters(apon);
} catch (IOException e) {
throw new IllegalRuleException("Joinpoint parameter can not be parsed", e);
}
updateJoinpoint(joinpointRule, joinpointParameters);
}
}
public static void updateJoinpoint(@NonNull JoinpointRule joinpointRule, JoinpointParameters joinpointParameters)
throws IllegalRuleException {
if (joinpointRule.getJoinpointTargetType() == null) {
String target = joinpointParameters.getString(JoinpointParameters.target);
updateJoinpointTargetType(joinpointRule, target);
}
updateMethods(joinpointRule, joinpointParameters.getStringArray(JoinpointParameters.methods));
updateHeaders(joinpointRule, joinpointParameters.getStringArray(JoinpointParameters.headers));
updatePointcutRule(joinpointRule, joinpointParameters.getParameters(JoinpointParameters.pointcut));
joinpointRule.setJoinpointParameters(joinpointParameters);
}
public static void updateJoinpointTargetType(@NonNull JoinpointRule joinpointRule, String target) {
if (target != null) {
JoinpointTargetType joinpointTargetType = JoinpointTargetType.resolve(target);
if (joinpointTargetType == null) {
throw new IllegalArgumentException("No joinpoint target type for '" + target + "'");
}
joinpointRule.setJoinpointTargetType(joinpointTargetType);
}
}
public static void updateMethods(@NonNull JoinpointRule joinpointRule, String[] methods) {
if (methods != null && methods.length > 0) {
List methodTypes = new ArrayList<>(methods.length);
for (String method : methods) {
MethodType methodType = MethodType.resolve(method);
if (methodType == null) {
throw new IllegalArgumentException("No request method type for '" + method + "'");
}
methodTypes.add(methodType);
}
joinpointRule.setMethods(methodTypes.toArray(new MethodType[0]));
}
}
public static void updateHeaders(@NonNull JoinpointRule joinpointRule, String[] headers) {
if (headers != null && headers.length > 0) {
List headerList = new ArrayList<>(headers.length);
for (String header : headers) {
if (StringUtils.hasText(header)) {
headerList.add(header);
}
}
if (!headerList.isEmpty()) {
joinpointRule.setHeaders(headerList.toArray(new String[0]));
}
}
}
public static void updatePointcutRule(@NonNull JoinpointRule joinpointRule, PointcutParameters pointcutParameters)
throws IllegalRuleException {
if (pointcutParameters == null) {
return;
}
List plusPatternStringList = pointcutParameters.getStringList(PointcutParameters.plus);
List minusPatternStringList = pointcutParameters.getStringList(PointcutParameters.minus);
List includeQualifierParametersList = pointcutParameters.getParametersList(PointcutParameters.include);
List excludeQualifierParametersList = pointcutParameters.getParametersList(PointcutParameters.exclude);
List pointcutPatternRuleList = mergePointcutPatternRules(plusPatternStringList, includeQualifierParametersList);
List excludePointcutPatternRuleList = mergePointcutPatternRules(minusPatternStringList, excludeQualifierParametersList);
if (pointcutPatternRuleList == null && excludePointcutPatternRuleList == null) {
return;
}
PointcutRule pointcutRule = PointcutRule.newInstance(pointcutParameters.getString(PointcutParameters.type));
if (pointcutPatternRuleList != null) {
for (PointcutPatternRule pointcutPatternRule : pointcutPatternRuleList) {
pointcutPatternRule.setExcludePointcutPatternRuleList(excludePointcutPatternRuleList);
pointcutRule.addPointcutPatternRule(pointcutPatternRule);
}
} else {
PointcutPatternRule pointcutPatternRule = new PointcutPatternRule();
pointcutPatternRule.setExcludePointcutPatternRuleList(excludePointcutPatternRuleList);
pointcutRule.addPointcutPatternRule(pointcutPatternRule);
}
joinpointRule.setPointcutRule(pointcutRule);
}
@Nullable
private static List mergePointcutPatternRules(List patternStringList,
List qualifierParametersList) {
int patternStringSize = (patternStringList != null ? patternStringList.size() : 0);
int qualifierParametersSize = (qualifierParametersList != null ? qualifierParametersList.size() : 0);
if (patternStringSize == 0 && qualifierParametersSize == 0) {
return null;
}
List pointcutPatternRuleList = new ArrayList<>(patternStringSize + qualifierParametersSize);
if (patternStringSize > 0) {
for (String patternString : patternStringList) {
PointcutPatternRule pointcutPatternRule = PointcutPatternRule.newInstance(patternString);
pointcutPatternRuleList.add(pointcutPatternRule);
}
}
if (qualifierParametersSize > 0) {
for (PointcutQualifierParameters qualifierParameters : qualifierParametersList) {
PointcutPatternRule pointcutPatternRule = createPointcutPatternRule(qualifierParameters);
pointcutPatternRuleList.add(pointcutPatternRule);
}
}
return pointcutPatternRuleList;
}
private static PointcutPatternRule createPointcutPatternRule(@NonNull PointcutQualifierParameters qualifierParameters) {
PointcutPatternRule pointcutPatternRule = null;
String translet = qualifierParameters.getString(PointcutQualifierParameters.translet);
String bean = qualifierParameters.getString(PointcutQualifierParameters.bean);
String method = qualifierParameters.getString(PointcutQualifierParameters.method);
if (StringUtils.hasLength(translet) || StringUtils.hasLength(bean) || StringUtils.hasLength(method)) {
pointcutPatternRule = PointcutPatternRule.newInstance(translet, bean, method);
}
return pointcutPatternRule;
}
}