1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.jython;
1920import org.apache.giraph.graph.Language;
21import org.apache.giraph.jython.factories.JythonComputationFactory;
22import org.apache.hadoop.conf.Configuration;
23import org.python.core.Py;
24import org.python.core.PyObject;
25import org.python.util.PythonInterpreter;
2627importstatic org.apache.giraph.conf.GiraphConstants.COMPUTATION_FACTORY_CLASS;
28importstatic org.apache.giraph.conf.GiraphConstants.COMPUTATION_LANGUAGE;
2930/**31 * Helpers for running jobs with Jython.32 */33publicclassJythonUtils {
34/**35 * The Jython interpreter. Cached here for fast access. We use a singleton36 * for this so that we can parse all of the Jython scripts once at startup37 * and then have their data loaded for the rest of the job.38 */39privatestaticfinal PythonInterpreter INTERPRETER =
40new PythonInterpreter();
4142/** Don't construct */43privateJythonUtils() { }
4445/**46 * Get Jython interpreter47 *48 * @return interpreter49 */50publicstatic PythonInterpreter getInterpreter() {
51return INTERPRETER;
52 }
5354/**55 * Sets up the Configuration for using Jython56 *57 * @param conf Configuration to se58 * @param klassName Class name of Jython Computation59 */60publicstaticvoid init(Configuration conf, String klassName) {
61 COMPUTATION_LANGUAGE.set(conf, Language.JYTHON);
62 COMPUTATION_FACTORY_CLASS.set(conf, JythonComputationFactory.class);
63 JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.set(conf, klassName);
64 }
6566/**67 * Instantiate new instance of the Jython class68 *69 * @param className Jython class name70 * @return new instance of class71 */72publicstatic PyObject newInstance(String className) {
73 PyObject pyClass = JythonUtils.getInterpreter().get(className);
74 PyObject pyObject = pyClass.__call__();
75return pyObject;
76 }
7778/**79 * Instantiate new instance of the Jython class80 *81 * @param <T> Jython type82 * @param className Jython class name83 * @param klass Java interface Class84 * @return new instance of class85 */86publicstatic <T> T newInstance(String className, Class<? extends T> klass) {
87 PyObject pyObject = newInstance(className);
88 Object object = pyObject.__tojava__(klass);
89if (Py.NoConversion.equals(object)) {
90thrownew IllegalArgumentException("Cannot coerce Jython class " +
91 className + " to Java type " + klass.getSimpleName());
92 } else {
93return (T) object;
94 }
95 }
96 }