Step 2: Implement the creator class

PowerBuilder Native Interface

Step 2: Implement the creator class

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