use of org.apache.cxf.binding.corba.types.CorbaStructHandler in project cxf by apache.
the class CorbaObjectReader method initializeCorbaObjectHandler.
private CorbaObjectHandler initializeCorbaObjectHandler(CorbaObjectHandler template) {
Constructor<?> templateConstructor = template.getClass().getDeclaredConstructors()[0];
Object[] params = new Object[4];
// of a sequence and needs to have the name "item" in order
if (template.isRecursive()) {
// Revisit: Is this always the case?
params[0] = new QName("item");
} else {
params[0] = template.getName();
}
params[1] = template.getIdlType();
params[2] = template.getTypeCode();
params[3] = template.getType();
final CorbaObjectHandler handler;
try {
handler = (CorbaObjectHandler) templateConstructor.newInstance(params);
// the template object.
if (template instanceof CorbaAnyHandler) {
((CorbaAnyHandler) handler).setTypeMap(((CorbaAnyHandler) template).getTypeMap());
}
} catch (java.lang.Exception ex) {
throw new CorbaBindingException("Unable to instantiate sequence element", ex);
}
if (template instanceof CorbaSequenceHandler) {
CorbaSequenceHandler templateSeq = (CorbaSequenceHandler) template;
((CorbaSequenceHandler) handler).setTemplateElement(templateSeq.getTemplateElement());
} else if (template instanceof CorbaStructHandler) {
CorbaStructHandler templateStruct = (CorbaStructHandler) template;
CorbaStructHandler struct = (CorbaStructHandler) handler;
struct.setRecursive(template.isRecursive());
List<CorbaObjectHandler> members = templateStruct.getMembers();
for (int i = 0; i < members.size(); i++) {
CorbaObjectHandler member = initializeCorbaObjectHandler(members.get(i));
struct.addMember(member);
}
} else if (template instanceof CorbaArrayHandler) {
CorbaArrayHandler templateArray = (CorbaArrayHandler) template;
CorbaArrayHandler array = (CorbaArrayHandler) handler;
List<CorbaObjectHandler> elements = templateArray.getElements();
for (int i = 0; i < elements.size(); i++) {
CorbaObjectHandler element = initializeCorbaObjectHandler(elements.get(i));
array.addElement(element);
}
} else if (template instanceof CorbaUnionHandler) {
CorbaUnionHandler templateUnion = (CorbaUnionHandler) template;
CorbaUnionHandler union = (CorbaUnionHandler) handler;
union.setRecursive(template.isRecursive());
union.setDiscriminator(initializeCorbaObjectHandler(templateUnion.getDiscriminator()));
List<CorbaObjectHandler> cases = templateUnion.getCases();
for (int i = 0; i < cases.size(); i++) {
union.addCase(initializeCorbaObjectHandler(cases.get(i)));
}
}
return handler;
}
use of org.apache.cxf.binding.corba.types.CorbaStructHandler in project cxf by apache.
the class CorbaObjectReaderTest method testReadStruct.
@Test
public void testReadStruct() {
OutputStream oStream = orb.create_output_stream();
// create the following struct
// struct TestStruct {
// long member1;
// string member2;
// boolean member3;
// }
int member1 = 12345;
String member2 = "54321";
boolean member3 = true;
oStream.write_long(member1);
oStream.write_string(member2);
oStream.write_boolean(member3);
InputStream iStream = oStream.create_input_stream();
CorbaObjectReader reader = new CorbaObjectReader(iStream);
QName structIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "struct", CorbaConstants.NP_WSDL_CORBA);
QName longIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "long", CorbaConstants.NP_WSDL_CORBA);
QName stringIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "string", CorbaConstants.NP_WSDL_CORBA);
QName boolIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "boolean", CorbaConstants.NP_WSDL_CORBA);
Struct structType = new Struct();
structType.setName("TestStruct");
MemberType m1 = new MemberType();
m1.setIdltype(longIdlType);
m1.setName("member1");
MemberType m2 = new MemberType();
m2.setIdltype(stringIdlType);
m2.setName("member2");
MemberType m3 = new MemberType();
m3.setIdltype(boolIdlType);
m3.setName("member3");
structType.getMember().add(m1);
structType.getMember().add(m2);
structType.getMember().add(m3);
// build the object holder
StructMember[] structMembers = new StructMember[3];
structMembers[0] = new StructMember("member1", orb.get_primitive_tc(TCKind.tk_long), null);
structMembers[1] = new StructMember("member2", orb.get_primitive_tc(TCKind.tk_string), null);
structMembers[2] = new StructMember("member3", orb.get_primitive_tc(TCKind.tk_boolean), null);
TypeCode structTC = orb.create_struct_tc("IDL:org.apache.cxf.TestStruct/1.0", "TestStruct", structMembers);
CorbaStructHandler obj = new CorbaStructHandler(new QName("TestStruct"), structIdlType, structTC, structType);
obj.addMember(new CorbaPrimitiveHandler(new QName("member1"), longIdlType, structMembers[0].type, null));
obj.addMember(new CorbaPrimitiveHandler(new QName("member2"), stringIdlType, structMembers[1].type, null));
obj.addMember(new CorbaPrimitiveHandler(new QName("member3"), boolIdlType, structMembers[2].type, null));
reader.readStruct(obj);
List<CorbaObjectHandler> nestedObjs = obj.getMembers();
assertTrue(Integer.parseInt(((CorbaPrimitiveHandler) nestedObjs.get(0)).getDataFromValue()) == member1);
assertEquals(((CorbaPrimitiveHandler) nestedObjs.get(1)).getDataFromValue(), member2);
assertTrue(Boolean.parseBoolean(((CorbaPrimitiveHandler) nestedObjs.get(2)).getDataFromValue()) == member3);
}
use of org.apache.cxf.binding.corba.types.CorbaStructHandler in project cxf by apache.
the class CorbaObjectWriterTest method testWriteStruct.
@Test
public void testWriteStruct() {
// create the following struct
// struct TestStruct {
// long member1;
// string member2;
// boolean member3;
// }
int member1 = 12345;
String member2 = "54321";
boolean member3 = true;
QName structIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "struct", CorbaConstants.NP_WSDL_CORBA);
QName longIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "long", CorbaConstants.NP_WSDL_CORBA);
QName stringIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "string", CorbaConstants.NP_WSDL_CORBA);
QName boolIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "boolean", CorbaConstants.NP_WSDL_CORBA);
Struct structType = new Struct();
structType.setName("TestStruct");
MemberType m1 = new MemberType();
m1.setIdltype(longIdlType);
m1.setName("member1");
MemberType m2 = new MemberType();
m2.setIdltype(stringIdlType);
m2.setName("member2");
MemberType m3 = new MemberType();
m3.setIdltype(boolIdlType);
m3.setName("member3");
structType.getMember().add(m1);
structType.getMember().add(m2);
structType.getMember().add(m3);
// build the object holder
StructMember[] structMembers = new StructMember[3];
structMembers[0] = new StructMember("member1", orb.get_primitive_tc(TCKind.tk_long), null);
structMembers[1] = new StructMember("member2", orb.get_primitive_tc(TCKind.tk_string), null);
structMembers[2] = new StructMember("member3", orb.get_primitive_tc(TCKind.tk_boolean), null);
TypeCode structTC = orb.create_struct_tc("IDL:org.apache.cxf.TestStruct/1.0", "TestStruct", structMembers);
CorbaStructHandler obj = new CorbaStructHandler(new QName("TestStruct"), structIdlType, structTC, structType);
CorbaPrimitiveHandler memberObj1 = new CorbaPrimitiveHandler(new QName("member1"), longIdlType, structMembers[0].type, null);
CorbaPrimitiveHandler memberObj2 = new CorbaPrimitiveHandler(new QName("member2"), stringIdlType, structMembers[1].type, null);
CorbaPrimitiveHandler memberObj3 = new CorbaPrimitiveHandler(new QName("member3"), boolIdlType, structMembers[2].type, null);
memberObj1.setValueFromData(Integer.toString(member1));
memberObj2.setValueFromData(member2);
memberObj3.setValueFromData(Boolean.toString(member3));
obj.addMember(memberObj1);
obj.addMember(memberObj2);
obj.addMember(memberObj3);
OutputStream oStream = orb.create_output_stream();
CorbaObjectWriter writer = new CorbaObjectWriter(oStream);
writer.writeStruct(obj);
InputStream iStream = oStream.create_input_stream();
int readMember1 = iStream.read_long();
assertTrue(readMember1 == member1);
String readMember2 = iStream.read_string();
assertEquals(readMember2, member2);
boolean readMember3 = iStream.read_boolean();
assertTrue(readMember3 == member3);
}
Aggregations