From cc6523be1f156b6c3fb9a5716264b0d0b13ddc20 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Wed, 11 Apr 2012 11:22:59 +0000 Subject: [PATCH] Fix: SOB-38 by using Jython interpreters with isolated PySystemState() objects. SVN: 24864 --- .../cisd/common/evaluator/Evaluator.java | 4 +- .../cisd/common/utilities/PythonUtils.java | 39 +++++++++++++++++++ .../JythonTopLevelDataSetHandler.java | 3 +- .../v2/JythonTopLevelDataSetHandlerV2.java | 3 +- .../v1/validation/ValidationScriptRunner.java | 3 +- .../MasterDataRegistrationScriptRunner.java | 3 +- .../etl/jython/JythonPlateDataSetHandler.java | 3 +- 7 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/PythonUtils.java diff --git a/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java b/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java index df230def7a4..499e168601b 100644 --- a/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java +++ b/common/source/java/ch/systemsx/cisd/common/evaluator/Evaluator.java @@ -39,6 +39,8 @@ import org.python.core.PyStringMap; import org.python.core.PySystemState; import org.python.util.PythonInterpreter; +import ch.systemsx.cisd.common.utilities.PythonUtils; + /** * A class for evaluating expressions, based on Jython. * <p> @@ -122,7 +124,7 @@ public final class Evaluator { throw new EvaluatorException("Expression '" + expression + "' contains line breaks"); } - this.interpreter = new PythonInterpreter(); + this.interpreter = PythonUtils.createIsolatedPythonInterpreter(); // Security: do not allow file access. try diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/PythonUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/PythonUtils.java new file mode 100644 index 00000000000..99a0d61dd35 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/PythonUtils.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.utilities; + +import org.python.core.PySystemState; +import org.python.util.PythonInterpreter; + +/** + * Utility functions for Jython. + * + * @author Bernd Rinn + */ +public class PythonUtils +{ + + /** + * Creates a new Jython interpreter with a fully isolated system state (i.e. interpreters in + * different threads don't influence each other. + */ + public static PythonInterpreter createIsolatedPythonInterpreter() + { + return new PythonInterpreter(null, new PySystemState()); + } + +} diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/JythonTopLevelDataSetHandler.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/JythonTopLevelDataSetHandler.java index a5971fc0d90..99d9930c7c0 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/JythonTopLevelDataSetHandler.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/JythonTopLevelDataSetHandler.java @@ -30,6 +30,7 @@ import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.filesystem.FileUtilities; import ch.systemsx.cisd.common.utilities.IDelegatedActionWithResult; import ch.systemsx.cisd.common.utilities.PropertyUtils; +import ch.systemsx.cisd.common.utilities.PythonUtils; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; import ch.systemsx.cisd.etlserver.TopLevelDataSetRegistratorGlobalState; import ch.systemsx.cisd.etlserver.registrator.api.v1.SecondaryTransactionFailure; @@ -198,7 +199,7 @@ public class JythonTopLevelDataSetHandler<T extends DataSetInformation> extends { return createJythonDataSetRegistrationService(incomingDataSetFile, callerDataSetInformationOrNull, cleanAfterwardsAction, delegate, - new PythonInterpreter(), getGlobalState()); + PythonUtils.createIsolatedPythonInterpreter(), getGlobalState()); } /** diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java index 024d94d4a53..7278192638c 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java @@ -21,6 +21,7 @@ import java.io.File; import org.python.util.PythonInterpreter; import ch.systemsx.cisd.common.utilities.IDelegatedActionWithResult; +import ch.systemsx.cisd.common.utilities.PythonUtils; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; import ch.systemsx.cisd.etlserver.TopLevelDataSetRegistratorGlobalState; import ch.systemsx.cisd.etlserver.registrator.DataSetFile; @@ -60,7 +61,7 @@ public class JythonTopLevelDataSetHandlerV2<T extends DataSetInformation> extend { return createJythonDataSetRegistrationServiceV2(incomingDataSetFile, callerDataSetInformationOrNull, cleanAfterwardsAction, delegate, - new PythonInterpreter(), getGlobalState()); + PythonUtils.createIsolatedPythonInterpreter(), getGlobalState()); } /** diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java index 80503292525..92503c0467a 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java @@ -32,6 +32,7 @@ import org.python.core.PyObject; import org.python.util.PythonInterpreter; import ch.systemsx.cisd.common.utilities.JythonUtils; +import ch.systemsx.cisd.common.utilities.PythonUtils; /** * @author Chandrasekhar Ramakrishnan @@ -76,7 +77,7 @@ public class ValidationScriptRunner private ValidationScriptRunner(String scriptString) { - this.interpreter = new PythonInterpreter(); + this.interpreter = PythonUtils.createIsolatedPythonInterpreter(); // Load the script this.scriptString = scriptString; diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/jython/api/v1/impl/MasterDataRegistrationScriptRunner.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/jython/api/v1/impl/MasterDataRegistrationScriptRunner.java index 8c6f1b1fa9a..7fac147a409 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/jython/api/v1/impl/MasterDataRegistrationScriptRunner.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/jython/api/v1/impl/MasterDataRegistrationScriptRunner.java @@ -21,6 +21,7 @@ import java.io.File; import org.python.util.PythonInterpreter; import ch.systemsx.cisd.common.filesystem.FileUtilities; +import ch.systemsx.cisd.common.utilities.PythonUtils; /** * A class for running python scripts that register master data. @@ -51,7 +52,7 @@ public class MasterDataRegistrationScriptRunner implements IMasterDataScriptRegi MasterDataRegistrationService service = new MasterDataRegistrationService(commonServer); // Configure the evaluator - PythonInterpreter interpreter = new PythonInterpreter(); + PythonInterpreter interpreter = PythonUtils.createIsolatedPythonInterpreter(); interpreter.set(SERVICE_VARIABLE_NAME, service); // Invoke the evaluator diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/jython/JythonPlateDataSetHandler.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/jython/JythonPlateDataSetHandler.java index 51b0910ee50..c210e3d49e1 100644 --- a/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/jython/JythonPlateDataSetHandler.java +++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/etl/jython/JythonPlateDataSetHandler.java @@ -21,6 +21,7 @@ import ch.systemsx.cisd.common.filesystem.FileUtilities; import ch.systemsx.cisd.common.io.FileBasedContentNode; import ch.systemsx.cisd.common.utilities.IDelegatedActionWithResult; import ch.systemsx.cisd.common.utilities.PropertyUtils; +import ch.systemsx.cisd.common.utilities.PythonUtils; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; import ch.systemsx.cisd.etlserver.TopLevelDataSetRegistratorGlobalState; import ch.systemsx.cisd.etlserver.registrator.AbstractDataSetRegistrationDetailsFactory; @@ -346,7 +347,7 @@ public class JythonPlateDataSetHandler extends JythonTopLevelDataSetHandler<Data { return new JythonDataSetRegistrationService<DataSetInformation>(this, incomingDataSetFile, callerDataSetInformationOrNull, cleanAfterwardsAction, delegate, - new PythonInterpreter(), getGlobalState()) + PythonUtils.createIsolatedPythonInterpreter(), getGlobalState()) { @SuppressWarnings("unchecked") @Override -- GitLab