Skip to content

Commit

Permalink
Make it easier to configure the EL Function provider
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Oct 2, 2023
1 parent 25cbeec commit 8f7342d
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public class DefaultExpressionManager implements ExpressionManager {

protected ExpressionFactory expressionFactory;
protected List<FlowableFunctionDelegate> functionDelegates;
protected BiFunction<String, String, FlowableFunctionDelegate> functionResolver;
protected FlowableFunctionResolver functionResolver;
protected FlowableFunctionResolverFactory functionResolverFactory = FunctionDelegatesFlowableFunctionResolver::new;
protected List<FlowableAstFunctionCreator> astFunctionCreators;

protected ELContext parsingElContext;
Expand Down Expand Up @@ -196,18 +197,7 @@ public void setFunctionDelegates(List<FlowableFunctionDelegate> functionDelegate

protected void updateFunctionResolver() {
if (this.functionDelegates != null) {
Map<String, FlowableFunctionDelegate> functionDelegateMap = new LinkedHashMap<>();
for (FlowableFunctionDelegate functionDelegate : functionDelegates) {
for (String prefix : functionDelegate.prefixes()) {
for (String localName : functionDelegate.localNames()) {
functionDelegateMap.put(prefix + ":" + localName, functionDelegate);
}

}

}

this.functionResolver = (prefix, localName) -> functionDelegateMap.get(prefix + ":" + localName);
this.functionResolver = this.functionResolverFactory.create(this.functionDelegates);

} else {
this.functionResolver = null;
Expand All @@ -228,6 +218,17 @@ public void setAstFunctionCreators(List<FlowableAstFunctionCreator> astFunctionC
}
}

@Override
public FlowableFunctionResolverFactory getFunctionResolverFactory() {
return functionResolverFactory;
}

@Override
public void setFunctionResolverFactory(FlowableFunctionResolverFactory functionResolverFactory) {
this.functionResolverFactory = functionResolverFactory;
updateFunctionResolver();
}

public DeploymentCache<Expression> getExpressionCache() {
return expressionCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ public interface ExpressionManager {
*/
void setAstFunctionCreators(List<FlowableAstFunctionCreator> astFunctionCreators);

FlowableFunctionResolverFactory getFunctionResolverFactory();

void setFunctionResolverFactory(FlowableFunctionResolverFactory functionResolverFactory);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
package org.flowable.common.engine.impl.el;

import java.util.function.BiFunction;

import org.flowable.common.engine.api.delegate.FlowableFunctionDelegate;
import org.flowable.common.engine.impl.javax.el.ELContext;
import org.flowable.common.engine.impl.javax.el.ELResolver;
import org.flowable.common.engine.impl.javax.el.FunctionMapper;
Expand All @@ -27,9 +24,9 @@
public class FlowableElContext extends ELContext {

protected ELResolver elResolver;
protected BiFunction<String, String, FlowableFunctionDelegate> functionResolver;
protected FlowableFunctionResolver functionResolver;

public FlowableElContext(ELResolver elResolver, BiFunction<String, String, FlowableFunctionDelegate> functionResolver) {
public FlowableElContext(ELResolver elResolver, FlowableFunctionResolver functionResolver) {
this.elResolver = elResolver;
this.functionResolver = functionResolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
package org.flowable.common.engine.impl.el;

import java.lang.reflect.Method;
import java.util.function.BiFunction;

import org.flowable.common.engine.api.delegate.FlowableFunctionDelegate;
import org.flowable.common.engine.impl.javax.el.FunctionMapper;

/**
Expand All @@ -27,22 +25,21 @@
*/
public class FlowableFunctionMapper extends FunctionMapper {

protected BiFunction<String, String, FlowableFunctionDelegate> functionResolver; // first parameter = prefix, second parameter = localName
protected FlowableFunctionResolver functionResolver;

public FlowableFunctionMapper(BiFunction<String, String, FlowableFunctionDelegate> functionResolver) {
public FlowableFunctionMapper(FlowableFunctionResolver functionResolver) {
setFunctionResolver(functionResolver);

}

public void setFunctionResolver(BiFunction<String, String, FlowableFunctionDelegate> functionResolver) {
public void setFunctionResolver(FlowableFunctionResolver functionResolver) {
this.functionResolver = functionResolver;
}

@Override
public Method resolveFunction(String prefix, String localName) {
if (functionResolver != null) {
FlowableFunctionDelegate functionDelegate = functionResolver.apply(prefix, localName);
return functionDelegate != null ? functionDelegate.functionMethod() : null;
return functionResolver.resolveFunction(prefix, localName);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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 org.flowable.common.engine.impl.el;

import java.lang.reflect.Method;

/**
* @author Filip Hrisafov
*/
@FunctionalInterface
public interface FlowableFunctionResolver {

Method resolveFunction(String prefix, String localName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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 org.flowable.common.engine.impl.el;

import java.util.Collection;

import org.flowable.common.engine.api.delegate.FlowableFunctionDelegate;

/**
* @author Filip Hrisafov
*/
@FunctionalInterface
public interface FlowableFunctionResolverFactory {

FlowableFunctionResolver create(Collection<FlowableFunctionDelegate> functionDelegates);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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 org.flowable.common.engine.impl.el;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.flowable.common.engine.api.delegate.FlowableFunctionDelegate;

/**
* @author Filip Hrisafov
*/
public class FunctionDelegatesFlowableFunctionResolver implements FlowableFunctionResolver {

protected final Map<String, FlowableFunctionDelegate> functionDelegateMap;

public FunctionDelegatesFlowableFunctionResolver(Collection<FlowableFunctionDelegate> functionDelegates) {
functionDelegateMap = new LinkedHashMap<>();
for (FlowableFunctionDelegate functionDelegate : functionDelegates) {
for (String prefix : functionDelegate.prefixes()) {
for (String localName : functionDelegate.localNames()) {
functionDelegateMap.put(prefix + ":" + localName, functionDelegate);
}

}

}
}

@Override
public Method resolveFunction(String prefix, String localName) {
return resolveFunction(functionDelegateMap.get(prefix + ":" + localName));
}

protected Method resolveFunction(FlowableFunctionDelegate functionDelegate) {
return functionDelegate != null ? functionDelegate.functionMethod() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
package org.flowable.common.engine.impl.el;

import java.util.function.BiFunction;

import org.flowable.common.engine.api.delegate.FlowableFunctionDelegate;
import org.flowable.common.engine.impl.javax.el.ELContext;
import org.flowable.common.engine.impl.javax.el.ELResolver;
import org.flowable.common.engine.impl.javax.el.ExpressionFactory;
Expand All @@ -32,9 +29,9 @@
*/
public class ParsingElContext extends ELContext {

protected BiFunction<String, String, FlowableFunctionDelegate> functionResolver;
protected FlowableFunctionResolver functionResolver;

public ParsingElContext(BiFunction<String, String, FlowableFunctionDelegate> functionResolver) {
public ParsingElContext(FlowableFunctionResolver functionResolver) {
this.functionResolver = functionResolver;
}

Expand Down

0 comments on commit 8f7342d

Please sign in to comment.