Step 2:
Implement the creator class
Like any nonvisual native class, the CJavaVM class must implement
the Invoke and Destroy functions in addition to the class functions
CreateJavaObject and CreateJavaVM.
The CreateJavaVm function of CjavaVM gets the classpath and
properties from the PBCallInfo structure. Then it loads the Java VM by
calling the loadJavaVM function of a wrapper class called JavaVMWrapper.
The JavaVMWrapper class encapsulates the JavaVM interface provided by
JNI.
The CreateJavaObject function creates an instance of a Java class
based on the given class name, creates a PowerBuilder proxy object for
the Java object, creates a JavaMarshaler object, and associates the
marshaler object with the proxy object.
The following is the CreateJavaObject function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
PBXRESULT CJavaVM::CreateJavaObject ( IPB_Session *session, pbobject obj, PBCallInfo *ci ) { enum { kSuccessful = 0, kInvalidJavaClassName = -1, kFailedToCreateJavaClass = -2, kInvalidProxyName = -3, kFailToCreateProxy = -4 }; // Get java class name. string jclassName; { pbstring jcn = ci->pArgs->GetAt(1)->GetPBString(); if (jcn == NULL) { ci->returnValue->SetLong(kInvalidJavaClassName); return PBX_OK; } else { jclassName = session->GetString(jcn); } } // Create java object JavaVMWrapper* jvm = JavaVMWrapper::instance(); JNIEnv* env = jvm->getEnv(); jclass jcls = env->FindClass(jclassName.c_str()); jobject jobj = NULL; if (jcls != NULL) { JLocalRef lrClz(env, jcls); jmethodID mid = env->GetMethodID(jcls, "<init>", "()V"); if (mid != NULL) { jobj = env->NewObject(jcls, mid); } } // Get PowerBuilder proxy name string proxyName; { pbstring pn = ci->pArgs->GetAt(2)->GetPBString(); if (pn == NULL) { ci->returnValue->SetLong(kInvalidProxyName); return PBX_OK; } else { proxyName = session->GetString(pn); } } // Find proxy class pbgroup group = session->FindGroup(proxyName.c_str(), pbgroup_proxy); if (group == NULL) { ci->returnValue->SetLong(kInvalidProxyName); return PBX_OK; } pbclass cls = session->FindClass(group, proxyName.c_str()); if (cls == NULL) { ci->returnValue->SetLong(kInvalidProxyName); return PBX_OK; } // Create PowerBuilder proxy object. pbproxyObject proxy = session->NewProxyObject(cls); if (proxy == NULL) { ci->returnValue->SetLong(kFailToCreateProxy); return PBX_OK; } // Create JavaMarshaler JavaMarshaler* marshaler = new JavaMarshaler(env, proxy, jobj); // Associate the JavaMarshaler with the proxy session->SetMarshaler(proxy, marshaler); ci->pArgs->GetAt(0)->SetObject(proxy); ci->returnValue->SetLong(kSuccessful); return PBX_OK; } |