All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.core.context.rule.JoinpointRule Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
/*
 * Copyright (c) 2008-2023 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.core.util.StringUtils;
import com.aspectran.core.util.ToStringBuilder;

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(); } 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(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(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(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); } if (!methodTypes.isEmpty()) { joinpointRule.setMethods(methodTypes.toArray(new MethodType[0])); } } } public static void updateHeaders(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(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); } 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(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; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy