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:
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;
}