From 617b7629156ea7d56c78f17035ecd581d84885b8 Mon Sep 17 00:00:00 2001 From: Mathieu Lirzin Date: Sat, 2 Mar 2019 19:40:48 +0000 Subject: [PATCH] Improved: Disallow extension of the script helper bindings (OFBIZ-10825) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ‘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 --- .../ofbiz/base/util/ScriptHelperFactory.java | 31 ---------------- .../apache/ofbiz/base/util/ScriptUtil.java | 30 +++------------- ...apache.ofbiz.base.util.ScriptHelperFactory | 18 ---------- .../scripting/ScriptHelperFactoryImpl.java | 35 ------------------- 4 files changed, 5 insertions(+), 109 deletions(-) delete mode 100644 framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptHelperFactory.java delete mode 100644 framework/common/src/main/java/META-INF/services/org.apache.ofbiz.base.util.ScriptHelperFactory delete mode 100644 framework/common/src/main/java/org/apache/ofbiz/common/scripting/ScriptHelperFactoryImpl.java diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptHelperFactory.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptHelperFactory.java deleted file mode 100644 index 56e7cda718..0000000000 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptHelperFactory.java +++ /dev/null @@ -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 ScriptHelper factory. - */ -public interface ScriptHelperFactory extends Factory { - - ScriptHelper getInstance(ScriptContext context); -} diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java index 44ba579d3b..5d3b347b63 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java @@ -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 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 SCRIPT_NAMES; @@ -104,15 +103,6 @@ public final class ScriptUtil { } } SCRIPT_NAMES = Collections.unmodifiableSet(writableScriptNames); - Iterator 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 String and returns the result. * * @param language diff --git a/framework/common/src/main/java/META-INF/services/org.apache.ofbiz.base.util.ScriptHelperFactory b/framework/common/src/main/java/META-INF/services/org.apache.ofbiz.base.util.ScriptHelperFactory deleted file mode 100644 index 73994bd032..0000000000 --- a/framework/common/src/main/java/META-INF/services/org.apache.ofbiz.base.util.ScriptHelperFactory +++ /dev/null @@ -1,18 +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. - -org.apache.ofbiz.common.scripting.ScriptHelperFactoryImpl diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/scripting/ScriptHelperFactoryImpl.java b/framework/common/src/main/java/org/apache/ofbiz/common/scripting/ScriptHelperFactoryImpl.java deleted file mode 100644 index 044eff8652..0000000000 --- a/framework/common/src/main/java/org/apache/ofbiz/common/scripting/ScriptHelperFactoryImpl.java +++ /dev/null @@ -1,35 +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.common.scripting; - -import javax.script.ScriptContext; - -import org.apache.ofbiz.base.util.ScriptHelper; -import org.apache.ofbiz.base.util.ScriptHelperFactory; - -/** - * An implementation of the ScriptHelperFactory interface. - */ -public final class ScriptHelperFactoryImpl implements ScriptHelperFactory { - - @Override - public ScriptHelper getInstance(ScriptContext context) { - return new ScriptHelperImpl(context); - } -}