コード例 #1
0
    def attach(self,
               pjvm: Optional[object] = None):  # -> Tuple['_JVM', jni.JNIEnv]:

        if_bind = pjvm is not None

        try:
            if if_bind and not pjvm:
                raise JVMException(EStatusCode.EINVAL,
                                   "First paramter must be a JNI jvm handle")

            penv = jni.obj(jni.POINTER(jni.JNIEnv))
            if if_bind:
                self._jvm.jnijvm = jni.cast(pjvm, jni.POINTER(jni.JavaVM))[0]
                self._jvm.jnijvm.AttachCurrentThread(penv)
            else:
                self._jvm.jnijvm.GetEnv(penv, JVM.JNI_VERSION)
            jenv = jni.JEnv(penv)
            self._jvm._initialize(jenv)
            return self._jvm, jenv
        except Exception as exc:
            try:
                self.handleException(exc)
            finally:
                if if_bind:
                    self._jvm.jnijvm = None
コード例 #2
0
 def start(self, *jvmoptions, **jvmargs):
     ignoreUnrecognized = jvmargs.get("ignoreUnrecognized", True)
     try:
         pjvm = jni.obj(jni.POINTER(jni.JavaVM))
         penv = jni.obj(jni.POINTER(jni.JNIEnv))
         jvm_args = jni.obj(jni.JavaVMInitArgs)
         jvm_args.version = JVM.JNI_VERSION
         jvm_args.nOptions = len(jvmoptions)
         jvm_args.options = joptions = jni.new_array(
             jni.JavaVMOption, jvm_args.nOptions)
         _keep = []
         for i, option in enumerate(jvmoptions):
             optionString = jni.new_cstr(option if isinstance(
                 option, bytes) else str(option).encode("utf-8"))
             _keep.append(optionString)
             jvm_args.options[i].optionString = optionString
             jvm_args.options[i].extraInfo = jni.NULL
         jvm_args.ignoreUnrecognized = jni.JNI_TRUE if ignoreUnrecognized else jni.JNI_FALSE
         err = self._JNI.CreateJavaVM(pjvm, penv, jvm_args)
         del _keep, joptions, jvm_args
         if err != jni.JNI_OK or jni.isNULL(pjvm):
             raise jni.JNIException(
                 err if err != jni.JNI_OK else jni.JNI_ERR,
                 info="JNI_CreateJavaVM")
         self._jnijvm = jni.JVM(pjvm)
         return self._jnijvm, jni.JEnv(penv)
     except Exception as exc:
         try:
             self.handleException(exc)
         finally:
             self._jnijvm = None
コード例 #3
0
 def attachThread(self, daemon: bool = False):
     try:
         penv = jni.obj(jni.POINTER(jni.JNIEnv))
         if not daemon:
             self._jvm.jnijvm.AttachCurrentThread(penv)
         else:
             self._jvm.jnijvm.AttachCurrentThreadAsDaemon(penv)
         return self._jvm, jni.JEnv(penv)
     except Exception as exc:
         self.handleException(exc)
コード例 #4
0
 def __enter__(self):
     if self._jvm is None:
         raise JVMException(
             EStatusCode.EDETACHED,
             "Unable to use JVM: thread detached from the VM")
     if self._jvm.jnijvm:
         penv = jni.obj(jni.POINTER(jni.JNIEnv))
         self._jvm.jnijvm.AttachCurrentThread(penv)
         return self._jvm, jni.JEnv(penv)
     else:
         return self._jvm, None
コード例 #5
0
 def shutdown(self):
     if self._jvm.jnijvm is None: return
     try:
         penv = jni.obj(jni.POINTER(jni.JNIEnv))
         self._jvm.jnijvm.AttachCurrentThread(penv)
         jenv = jni.JEnv(penv)
         self._jvm._dispose(jenv)
         self._jvm.jnijvm.DestroyJavaVM()
     except Exception as exc:
         try:
             self.handleException(exc)
         finally:
             self._jvm.jnijvm = None
コード例 #6
0
    def start(self, *jvmoptions, **jvmargs):  # -> Tuple['_JVM', jni.JNIEnv]:

        jvmoptions = tuple([
            "-Djava.class.path=" + os.pathsep.join([
                item.partition("=")[2] for item in jvmoptions
                if item.lstrip().startswith("-Djava.class.path=")
            ] + [str(path) for path in INTERNAL_CLASSPATHS])
        ] + [
            item for item in jvmoptions
            if not item.lstrip().startswith("-Djava.class.path=")
        ])
        ignoreUnrecognized = jvmargs.get("ignoreUnrecognized", True)

        try:
            pjvm = jni.obj(jni.POINTER(jni.JavaVM))
            penv = jni.obj(jni.POINTER(jni.JNIEnv))
            jvm_args = jni.obj(jni.JavaVMInitArgs)
            jvm_args.version = JVM.JNI_VERSION
            jvm_args.nOptions = len(jvmoptions)
            jvm_args.options = joptions = jni.new_array(
                jni.JavaVMOption, jvm_args.nOptions)
            _keep = []
            for i, option in enumerate(jvmoptions):
                optionString = jni.new_cstr(option if isinstance(
                    option, bytes) else str(option).encode("utf-8"))
                _keep.append(optionString)
                jvm_args.options[i].optionString = optionString
                jvm_args.options[i].extraInfo = jni.NULL
            jvm_args.ignoreUnrecognized = jni.JNI_TRUE if ignoreUnrecognized else jni.JNI_FALSE
            err = self._jvm.JNI.CreateJavaVM(pjvm, penv, jvm_args)
            del _keep, joptions, jvm_args
            if err != jni.JNI_OK or jni.isNULL(pjvm):
                raise jni.JNIException(
                    err if err != jni.JNI_OK else jni.JNI_ERR,
                    info="JNI_CreateJavaVM")
            self._jvm.jnijvm = jni.JVM(pjvm)
            jenv = jni.JEnv(penv)
            try:
                self._jvm._initialize(jenv)
            except Exception as exc:
                try:
                    self._jvm.jnijvm.DestroyJavaVM()
                except Exception:
                    pass
                raise exc
            return self._jvm, jenv
        except Exception as exc:
            try:
                self.handleException(exc)
            finally:
                self._jvm.jnijvm = None