Improved: Disallow extension of the script helper bindings (OFBIZ-10825)

‘ScriptHelper’ is a mechanism used to add some useful bindings to
scripts.  This feature has been introduced in revision 1299924 by
Adrian Crum as part of an general effort of supporting the Generic
Scripting Engine specified by JSR-223.

Previously this mechanism was extensible, however it appears to be
undesirable (nor used in practice) to extend the bindings available to
services since the internal service implementations are highly coupled
to those bindings.  As a consequence the usage of the Abstract Factory
pattern which was used for that purpose is now removed.

The side effect of this change is that we have now simpler code.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1854668 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mathieu Lirzin
2019-03-02 19:40:48 +00:00
parent 4fbb189ac9
commit 617b762915
4 changed files with 5 additions and 109 deletions
@@ -1,31 +0,0 @@
/*******************************************************************************
* 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.apache.ofbiz.base.util;
import javax.script.ScriptContext;
import org.apache.ofbiz.base.lang.Factory;
/**
* A <code>ScriptHelper</code> factory.
*/
public interface ScriptHelperFactory extends Factory<ScriptHelper, ScriptContext> {
ScriptHelper getInstance(ScriptContext context);
}
@@ -27,10 +27,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import javax.script.Bindings;
@@ -47,6 +45,8 @@ import javax.script.SimpleScriptContext;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.util.cache.UtilCache;
import org.apache.ofbiz.base.util.ScriptHelper;
import org.apache.ofbiz.common.scripting.ScriptHelperImpl;
import org.codehaus.groovy.runtime.InvokerHelper;
/**
@@ -70,7 +70,6 @@ public final class ScriptUtil {
public static final String SCRIPT_HELPER_KEY = "ofbiz";
private static final UtilCache<String, CompiledScript> parsedScripts = UtilCache.createUtilCache("script.ParsedScripts", 0, 0, false);
private static final Object[] EMPTY_ARGS = {};
private static ScriptHelperFactory helperFactory = null;
/** A set of script names - derived from the JSR-223 scripting engines. */
public static final Set<String> SCRIPT_NAMES;
@@ -104,15 +103,6 @@ public final class ScriptUtil {
}
}
SCRIPT_NAMES = Collections.unmodifiableSet(writableScriptNames);
Iterator<ScriptHelperFactory> iter = ServiceLoader.load(ScriptHelperFactory.class).iterator();
if (iter.hasNext()) {
helperFactory = iter.next();
if (Debug.verboseOn()) {
Debug.logVerbose("ScriptHelper factory set to " + helperFactory.getClass().getName(), module);
}
} else {
Debug.logWarning("ScriptHelper factory not found", module);
}
}
/**
@@ -206,7 +196,7 @@ public final class ScriptUtil {
localContext.put(WIDGET_CONTEXT_KEY, context);
localContext.put("context", context);
ScriptContext scriptContext = new SimpleScriptContext();
ScriptHelper helper = createScriptHelper(scriptContext);
ScriptHelper helper = new ScriptHelperImpl(scriptContext);
if (helper != null) {
localContext.put(SCRIPT_HELPER_KEY, helper);
}
@@ -233,21 +223,11 @@ public final class ScriptUtil {
ScriptContext scriptContext = new SimpleScriptContext();
Bindings bindings = new ProtectedBindings(localContext, Collections.unmodifiableSet(protectedKeys));
scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
ScriptHelper helper = createScriptHelper(scriptContext);
if (helper != null) {
localContext.put(SCRIPT_HELPER_KEY, helper);
}
localContext.put(SCRIPT_HELPER_KEY, new ScriptHelperImpl(scriptContext));
return scriptContext;
}
public static ScriptHelper createScriptHelper(ScriptContext context) {
if (helperFactory != null) {
return helperFactory.getInstance(context);
}
return null;
}
/**
/**
* Executes a script <code>String</code> and returns the result.
*
* @param language