/jcc/java/org/apache/jcc/PythonVM.java

https://github.com/fnp/pylucene · Java · 112 lines · 32 code · 13 blank · 67 comment · 2 complexity · 7881632657e8140594dcead5dbe6a649 MD5 · raw file

  1. /* ====================================================================
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. * ====================================================================
  14. */
  15. package org.apache.jcc;
  16. public class PythonVM {
  17. static protected PythonVM vm;
  18. static {
  19. System.loadLibrary("jcc");
  20. }
  21. /**
  22. * Start the embedded Python interpreter. The specified
  23. * program name and args are set into the Python variable sys.argv.
  24. * This returns an instance of the Python VM; it may be called
  25. * multiple times, and will return the same VM instance each time.
  26. *
  27. * @param programName the name of the Python program, typically
  28. * /usr/bin/python. This is informational; the program is not
  29. * actually executed.
  30. * @param args additional arguments to be put into sys.argv.
  31. * @return a singleton instance of PythonVM
  32. */
  33. static public PythonVM start(String programName, String[] args)
  34. {
  35. if (vm == null)
  36. {
  37. vm = new PythonVM();
  38. vm.init(programName, args);
  39. }
  40. return vm;
  41. }
  42. /**
  43. * Start the embedded Python interpreter. The specified
  44. * program name is set into the Python variable sys.argv[0].
  45. * This returns an instance of the Python VM; it may be called
  46. * multiple times, and will return the same VM instance each time.
  47. *
  48. * @param programName the name of the Python program, typically
  49. * /usr/bin/python. This is informational; the program is not
  50. * actually executed.
  51. * @return a singleton instance of PythonVM
  52. */
  53. static public PythonVM start(String programName)
  54. {
  55. return start(programName, null);
  56. }
  57. /**
  58. * Obtain the PythonVM instance, or null if the Python VM
  59. * has not yet been started.
  60. *
  61. * @return a singleton instance of PythonVM, or null
  62. */
  63. static public PythonVM get()
  64. {
  65. return vm;
  66. }
  67. protected PythonVM()
  68. {
  69. }
  70. protected native void init(String programName, String[] args);
  71. /**
  72. * Instantiate the specified Python class, and return the instance.
  73. *
  74. * @param moduleName the Python module the class is defined in
  75. * @param className the Python class to instantiate.
  76. * @return a handle on the Python instance.
  77. */
  78. public native Object instantiate(String moduleName, String className)
  79. throws PythonException;
  80. /**
  81. * Bump the Python thread state counter. Every thread should
  82. * do this before calling into Python, to prevent the Python
  83. * thread state from being inadvertently collected (and causing loss
  84. * of thread-local variables)
  85. *
  86. * @return the Python thread state counter. A return value less
  87. * than zero signals an error.
  88. */
  89. public native int acquireThreadState();
  90. /**
  91. * Release the Python thread state counter. Every thread that has
  92. * called acquireThreadState() should call this before
  93. * terminating.
  94. *
  95. * @return the Python thread state counter. A return value less
  96. * than zero signals an error.
  97. */
  98. public native int releaseThreadState();
  99. }