1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * 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 and
16 * limitations under the License.
17 */
18 package org.apache.giraph.jython;
19
20 import org.apache.giraph.graph.Language;
21 import org.apache.giraph.jython.factories.JythonComputationFactory;
22 import org.apache.hadoop.conf.Configuration;
23 import org.python.core.Py;
24 import org.python.core.PyObject;
25 import org.python.util.PythonInterpreter;
26
27 import static org.apache.giraph.conf.GiraphConstants.COMPUTATION_FACTORY_CLASS;
28 import static org.apache.giraph.conf.GiraphConstants.COMPUTATION_LANGUAGE;
29
30 /**
31 * Helpers for running jobs with Jython.
32 */
33 public class JythonUtils {
34 /**
35 * The Jython interpreter. Cached here for fast access. We use a singleton
36 * for this so that we can parse all of the Jython scripts once at startup
37 * and then have their data loaded for the rest of the job.
38 */
39 private static final PythonInterpreter INTERPRETER =
40 new PythonInterpreter();
41
42 /** Don't construct */
43 private JythonUtils() { }
44
45 /**
46 * Get Jython interpreter
47 *
48 * @return interpreter
49 */
50 public static PythonInterpreter getInterpreter() {
51 return INTERPRETER;
52 }
53
54 /**
55 * Sets up the Configuration for using Jython
56 *
57 * @param conf Configuration to se
58 * @param klassName Class name of Jython Computation
59 */
60 public static void 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 }
65
66 /**
67 * Instantiate new instance of the Jython class
68 *
69 * @param className Jython class name
70 * @return new instance of class
71 */
72 public static PyObject newInstance(String className) {
73 PyObject pyClass = JythonUtils.getInterpreter().get(className);
74 PyObject pyObject = pyClass.__call__();
75 return pyObject;
76 }
77
78 /**
79 * Instantiate new instance of the Jython class
80 *
81 * @param <T> Jython type
82 * @param className Jython class name
83 * @param klass Java interface Class
84 * @return new instance of class
85 */
86 public static <T> T newInstance(String className, Class<? extends T> klass) {
87 PyObject pyObject = newInstance(className);
88 Object object = pyObject.__tojava__(klass);
89 if (Py.NoConversion.equals(object)) {
90 throw new IllegalArgumentException("Cannot coerce Jython class " +
91 className + " to Java type " + klass.getSimpleName());
92 } else {
93 return (T) object;
94 }
95 }
96 }