/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.axis2.corba.receivers; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; import org.apache.axis2.context.MessageContext; import org.apache.axis2.corba.deployer.CorbaConstants; import org.apache.axis2.corba.exceptions.CorbaException; import org.apache.axis2.corba.exceptions.CorbaInvocationException; import org.apache.axis2.corba.idl.IDLProcessor; import org.apache.axis2.corba.idl.PreProcessorInputStream; import org.apache.axis2.corba.idl.types.AbstractCollectionType; import org.apache.axis2.corba.idl.types.ArrayType; import org.apache.axis2.corba.idl.types.CompositeDataType; import org.apache.axis2.corba.idl.types.DataType; import org.apache.axis2.corba.idl.types.EnumType; import org.apache.axis2.corba.idl.types.ExceptionType; import org.apache.axis2.corba.idl.types.IDL; import org.apache.axis2.corba.idl.types.Member; import org.apache.axis2.corba.idl.types.PrimitiveDataType; import org.apache.axis2.corba.idl.types.SequenceType; import org.apache.axis2.corba.idl.types.Struct; import org.apache.axis2.corba.idl.types.Typedef; import org.apache.axis2.corba.idl.types.UnionMember; import org.apache.axis2.corba.idl.types.UnionType; import org.apache.axis2.corba.idl.types.ValueType; import org.apache.axis2.corba.idl.values.AbstractCollectionValue; import org.apache.axis2.corba.idl.values.AbstractValue; import org.apache.axis2.corba.idl.values.AliasValue; import org.apache.axis2.corba.idl.values.ArrayValue; import org.apache.axis2.corba.idl.values.EnumValue; import org.apache.axis2.corba.idl.values.ExceptionValue; import org.apache.axis2.corba.idl.values.ObjectByValue; import org.apache.axis2.corba.idl.values.SequenceValue; import org.apache.axis2.corba.idl.values.StreamableValueFactory; import org.apache.axis2.corba.idl.values.StructValue; import org.apache.axis2.corba.idl.values.UnionValue; import org.apache.axis2.description.AxisService; import org.apache.axis2.description.Parameter; import org.apache.axis2.description.java2wsdl.TypeTable; import org.apache.axis2.namespace.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.omg.CORBA.Any; import org.omg.CORBA.TCKind; import org.omg.CORBA.TypeCode; import org.omg.CORBA_2_3.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import org.omg.CosNaming.NamingContextPackage.CannotProceed; import org.omg.CosNaming.NamingContextPackage.InvalidName; import org.omg.CosNaming.NamingContextPackage.NotFound; import javax.xml.namespace.QName; import java.io.File; //import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; //import java.util.zip.ZipEntry; //import java.util.zip.ZipInputStream; public class CorbaUtil implements CorbaConstants { private static Map IDL_CACHE = new HashMap(); private static final Log log = LogFactory.getLog(CorbaUtil.class); public static org.omg.CORBA_2_3.ORB getORB(AxisService service) { Parameter orbClass = service.getParameter(ORB_CLASS); Parameter orbSingletonClass = service.getParameter(ORB_SINGLETON_CLASS); Properties props = System.getProperties(); if (orbClass!=null) props.put(ORG_OMG_CORBA_ORBCLASS, ((String) orbClass.getValue()).trim()); else props.put(ORG_OMG_CORBA_ORBCLASS, DEFAULR_ORB_CLASS); if (orbSingletonClass!=null) props.put(ORG_OMG_CORBA_ORBSINGLETON_CLASS, ((String) orbSingletonClass.getValue()).trim()); else props.put(ORG_OMG_CORBA_ORBSINGLETON_CLASS, DEFAULT_ORBSINGLETON_CLASS); return (ORB) ORB.init(new String[]{}, props); } public static org.omg.CORBA.Object resolveObject(AxisService service, org.omg.CORBA_2_3.ORB orb) throws CorbaInvocationException { org.omg.CORBA.Object obj; try { Parameter namingServiceUrl = service.getParameter(NAMING_SERVICE_URL); Parameter objectName = service.getParameter(OBJECT_NAME); Parameter iorFilePath = service.getParameter(IOR_FILE_PATH); Parameter iorString = service.getParameter(IOR_STRING); if (namingServiceUrl!=null && objectName!=null) { obj = orb.string_to_object(((String) namingServiceUrl.getValue()).trim()); NamingContextExt nc = NamingContextExtHelper.narrow(obj); obj = nc.resolve(nc.to_name(((String) objectName.getValue()).trim())); } else if (iorFilePath!=null) { FileReader fileReader = new FileReader(((String) iorFilePath.getValue()).trim()); char[] buf = new char[1000]; fileReader.read(buf); obj = orb.string_to_object((new String(buf)).trim()); } else if (iorString!=null) { obj = orb.string_to_object(((String) iorString.getValue()).trim()); } else { throw new CorbaInvocationException("cannot resolve object"); } } catch (NotFound notFound) { throw new CorbaInvocationException("cannot resolve object", notFound); } catch (CannotProceed cannotProceed) { throw new CorbaInvocationException("cannot resolve object", cannotProceed); } catch (InvalidName invalidName) { throw new CorbaInvocationException("cannot resolve object", invalidName); } catch (IOException e) { throw new CorbaInvocationException("cannot resolve object", e); } return obj; } public static IDL getIDL(AxisService service, ORB orb, String dirName) throws CorbaException { Parameter idlFile = service.getParameter(IDL_FILE); if (idlFile == null) { throw new CorbaInvocationException("Please specify the IDL file"); } String idlFileName = ((String) idlFile.getValue()).trim(); String cacheKey = dirName + File.separator + idlFileName; IDL idl = (IDL) IDL_CACHE.get(cacheKey); if (idl==null) { try { /*File file = new File(dirName); InputStream stream; if (file.isDirectory()) { stream = new FileInputStream(cacheKey); } else { ZipInputStream zin = new ZipInputStream(new FileInputStream(file)); ZipEntry entry; boolean found = false; while ((entry = zin.getNextEntry()) != null) { if (entry.getName().equalsIgnoreCase(idlFileName)) { found = true; break; } } if (!found) new CorbaInvocationException("cannot find " + idlFileName + " in " + file.getPath()); stream = zin; }*/ InputStream stream = new PreProcessorInputStream(dirName, idlFileName); //TODO: Set pre-processor system and user input paths IDLProcessor idlProcessor = new IDLProcessor(stream); idl = idlProcessor.process(); stream.close(); IDL_CACHE.put(cacheKey, idl); } catch (IOException e) { throw new CorbaInvocationException("cannot process idl file", e); } } Map types = idl.getCompositeDataTypes(); if (types!=null) { Iterator iter = types.values().iterator(); while (iter.hasNext()) { DataType type = (DataType) iter.next(); if (type instanceof ValueType) { StreamableValueFactory.register(orb, (ValueType) type); } } } return idl; } public static Invoker getInvoker(AxisService service, org.omg.CORBA.Object obj, IDL idl, String methodName) throws CorbaInvocationException { InvokerFactory invokerFactory = new CorbaInvokerFactory(idl); Parameter interfaceName = service.getParameter(INTERFACE_NAME); if (interfaceName==null) throw new CorbaInvocationException("interfaceName cannot be null"); return invokerFactory.newInvoker(((String) interfaceName.getValue()).trim(), methodName, obj); } public static Object[] extractParameters(OMElement methodElement, Member[] parameterMembers) throws CorbaInvocationException { if (parameterMembers==null) return new Object[0]; Object[] retObjs = new Object[parameterMembers.length]; if (methodElement==null) return retObjs; Iterator paramsIter = methodElement.getChildElements(); Map paramsMap = new HashMap(); String localName; while(paramsIter!=null && paramsIter.hasNext()) { OMElement param = (OMElement) paramsIter.next(); localName = param.getLocalName(); if (paramsMap.containsKey(localName)) { Object value = paramsMap.get(localName); if (value instanceof List) { ((List) value).add(param); } else { List valueList = new ArrayList(); valueList.add(value); valueList.add(param); paramsMap.put(localName, valueList); } } else { paramsMap.put(localName, param); } } String paramName; for (int i = 0; i < parameterMembers.length; i++) { paramName = parameterMembers[i].getName(); retObjs[i] = extractValue(parameterMembers[i].getDataType(), paramsMap.get(paramName)); } return retObjs; } private static Object extractValue(DataType dataType, Object param) throws CorbaInvocationException { if (dataType instanceof Typedef) { Typedef typedef = (Typedef) dataType; AliasValue aliasValue = new AliasValue(typedef); OMElement paramElement; if (param instanceof OMElement) paramElement = (OMElement) param; else return null; DataType aliasType = typedef.getDataType(); if (!(aliasType instanceof AbstractCollectionType)) { paramElement = paramElement.getFirstElement(); if (paramElement == null || !ARRAY_ITEM.equals(paramElement.getLocalName())) return null; } aliasValue.setValue(extractValue(aliasType, paramElement)); return aliasValue; } else if (dataType instanceof PrimitiveDataType) { if (param!=null) return parseValue(dataType, ((OMElement) param).getText()); } else if (dataType instanceof AbstractCollectionType) { AbstractCollectionType collectionType = (AbstractCollectionType) dataType; OMElement paramElement; if (param instanceof OMElement) paramElement = (OMElement) param; else return null; Iterator paramsIter = paramElement.getChildElements(); List children = new ArrayList(); while (paramsIter.hasNext()) { children.add(extractValue(collectionType.getDataType(), paramsIter.next())); } AbstractCollectionValue collectionValue; if (collectionType.isArray()) { collectionValue = new ArrayValue((ArrayType) collectionType); } else if (collectionType.isSequence()) { collectionValue = new SequenceValue((SequenceType) collectionType); } else { return null; } collectionValue.setValues(children.toArray()); return collectionValue; } else if (dataType instanceof EnumType) { EnumType enumType = (EnumType) dataType; String enumText = ((OMElement) param).getText(); int index = enumType.getEnumMembers().indexOf(enumText); if (index >= 0) { EnumValue enumValue = new EnumValue(enumType); enumValue.setValue(index); return enumValue; } } else if (dataType instanceof UnionType) { UnionType unionType = (UnionType) dataType; OMElement unElement = ((OMElement) param).getFirstElement(); String unionMemberName = unElement.getLocalName(); UnionValue unionValue = new UnionValue(unionType); unionValue.setMemberName(unionMemberName); Member[] members = unionType.getMembers(); UnionMember member = null; for (int i = 0; i < members.length; i++) { member = (UnionMember) members[i]; if (member.getName().equals(unionMemberName)) { break; } } if (member != null) { unionValue.setMemberValue(extractValue(member.getDataType(), unElement)); } return unionValue; } else if (dataType instanceof CompositeDataType) { CompositeDataType compositeType = (CompositeDataType) dataType; Member[] compositeMembers = compositeType.getMembers(); Object[] compositeValues = extractParameters(((OMElement) param), compositeMembers); AbstractValue value; if (compositeType instanceof ValueType) value = new ObjectByValue((ValueType) compositeType); else if (compositeType instanceof Struct) value = new StructValue((Struct) compositeType); else throw new CorbaInvocationException("Parameter type not supported"); value.setMemberValues(compositeValues); return value; } return null; } public static void processResponse(Object resObject, Member[] params, Object[] outParamValues, DataType dataType, AxisService service, String methodName, SOAPFactory fac, String messageNameSpace, MessageContext outMessage) throws AxisFault { boolean qualified = service.isElementFormDefault(); OMNamespace ns = fac.createOMNamespace(messageNameSpace, service.getSchemaTargetNamespacePrefix()); OMElement bodyContent = fac.createOMElement(methodName + RESPONSE, ns); OMElement child; if (qualified) { child = fac.createOMElement(RETURN_WRAPPER, ns); } else { child = fac.createOMElement(RETURN_WRAPPER, null); } bodyContent.addChild(child); if (dataType!=null && !getQualifiedName(dataType).equals(VOID) && resObject!=null) { processResponse(child, bodyContent, resObject, dataType, fac, ns, qualified, service); } else { child.addAttribute("nil", "true", fac.createOMNamespace(Constants.URI_2001_SCHEMA_XSI, Constants.NS_PREFIX_SCHEMA_XSI)); } Member param; List outParamList = Arrays.asList(outParamValues); Iterator paramsIter = outParamList.iterator(); for (int i = 0; i < params.length; i++) { param = params[i]; if (Member.MODE_INOUT.equals(param.getMode()) || Member.MODE_OUT.equals(param.getMode())) { if (qualified) { child = fac.createOMElement(param.getName(), ns); } else { child = fac.createOMElement(param.getName(), null); } bodyContent.addChild(child); processResponse(child, bodyContent, paramsIter.next(), param.getDataType(), fac, ns, qualified, service); } } SOAPEnvelope envelope = fac.getDefaultEnvelope(); envelope.getBody().addChild(bodyContent); outMessage.setEnvelope(envelope); } private static void processResponse(OMElement child, OMElement bodyContent, Object resObject, DataType dataType, SOAPFactory fac, OMNamespace defaultNS, boolean qualified, AxisService service) { if (dataType instanceof PrimitiveDataType) { child.addChild(fac.createOMText(child, resObject.toString())); } else if (dataType instanceof Typedef) { Typedef typedef = (Typedef) dataType; AliasValue aliasValue = (AliasValue) resObject; OMNamespace ns = getNameSpaceForType(fac, service, typedef); OMElement item = fac.createOMElement(ARRAY_ITEM, ns, child); processResponse(item, child, aliasValue.getValue(), typedef.getDataType(), fac, ns, qualified, service); } else if (dataType instanceof AbstractCollectionType) { AbstractCollectionType collectionType = (AbstractCollectionType) dataType; AbstractCollectionValue collectionValue = (AbstractCollectionValue) resObject; Object[] values = collectionValue.getValues(); int length = values.length; for (int i=0; i