The following examples are provided to illustrate use of the valuetype library methods in a distributed environment.
Example: A client program that uses a remote object to call methods of ::java::util::Vector
//First obtain the stringified ior of an EJB deployed on an AE server using namespace com::ibm::ws; CORBA::Object_var vector_obj; //init the orb CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "DSOM"); //Get the stringified ior from a file, then use it to obtain a valid object reference ifstream in; int fileIndex = 0; char* iorfile = "VectorSession.ior"; // the stringified ior of a session bean that // uses instances of the java serializable // java.util.Vector in.open(iorfile); if (in.fail()) { std::cerr << "Cannot open file " << iorfile << std::endl; return 1; } char iorstr[2048]; //read ior from file in >> iorstr; in.close(); std::cout << "using ior:'" << iorstr << "'" << std::endl; //get stringified ior vector_obj = orb->string_to_object(iorstr); if (CORBA::is_nil(vector_obj)) { std::cerr << "string_to_object failed"<< std::endl; return 1; } // Now, call the createVector method of the stub class ejbPackage::VectorSession to access // an EJB method that returns an instance of the java serializable, java.util.Vector. The // stub method then returns a pointer to a java::util::Vector. ::CORBA::Short initialElement = 999; java::util::Vector *vPrt; try { vPrt = vector_obj ->createVector(initialElement); if (vPrt == 0) { VtlUtil::debug("In testVector: vector_obj ->createVector(arg) returned a null pointer\n"); } } catch (...) { VtlUtil::debug("In testVector: vector_obj ->createVector(arg) has thrown an exception\n"); } //Next use the remote object to a method of ::java::util::Vector. /************************ * Create and populate a * java::lang::Object ************************/ short inValue = 999; ::CORBA::Long incrementValue = 1; java::lang::Object obj; obj <<= inValue; //rvalue is a ptr /************************************************************ * Call the addElement method using the pointer obtaned remotely * via the createVector method. Add "numberToAdd" elements. * Verify that the correct size is returned ***********************************************************/ ::CORBA::Long numberToAdd = 5; for (int i = 0; i < numberToAdd; i ++) { try { vPrt->addElement (obj); } catch (...) { VtlUtil::debug("In testVector: In catch after pwPtr->addElement()\n"); } }
Example: A client program that uses a remote object to call methods of ::java::lang::Boolean
using namespace com::ibm::ws; const char *factoryName = "java::lang::Boolean_init"; // Use the utility method, com::ibm::ws::VtlUtil::getBooleanFactory to get a pointer to the registered // java:lang::Boolean_init factory object. java::lang::Boolean_init* fact = VtlUtil::getBooleanFactory(); if(fact == 0) { VtlUtil::debug("VtlUtil::getFactory returned a null value "); } else { VtlUtil::debug("VtlUtil::getFactory returned a valid value "); } //call create__boolean to create a pointer to a java::lang::Boolean that contains true java::lang::Boolean* booleanPtr = fact->create__boolean(1); if(booleanPtr == 0) { VtlUtil::debug("booleanPtr == 0"); return failed; } else { VtlUtil::debug("create__boolean returned a valid value: test succeeded"); } CORBA::Object_var boolean _obj; ::CORBA::Boolean trueBooleanValue = boolean_obj->callBooleanValue(booleanPtr); int tempTrueBooleanValue = trueBooleanValue; if (tempTrueBooleanValue == 1) { VtlUtil::debug("tempTrueBooleanValue == 1"); }