Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.cxf.binding.corba.runtime;
 
 
 import java.util.List;
 
 
 
 public class CorbaObjectReader {
 
     private static final Logger LOG = LogUtils.getL7dLogger(CorbaObjectReader.class);
 
     private InputStream stream;
 
     public CorbaObjectReader(InputStream inStream) {
          = inStream;
     }
 
     public void read(CorbaObjectHandler objthrows CorbaBindingException {
         switch (obj.getTypeCode().kind().value()) {
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readBoolean());
             break;
         case .:
             Character charValue = this.readChar();
             ((CorbaPrimitiveHandler)obj).setValue(charValue);
             break;
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readWChar());
             break;
         case .:
             Byte octetValue = this.readOctet();
             ((CorbaPrimitiveHandler)obj).setValue(octetValue);
             break;
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readShort());
             break;
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readUShort());
             break;
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readLong());
             break;
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readULong());
             break;
         case .:
             ((CorbaPrimitiveHandler)obj).setValue(this.readLongLong());
            break;
        case .:
            ((CorbaPrimitiveHandler)obj).setValue(this.readULongLong());
            break;
        case .:
            ((CorbaPrimitiveHandler)obj).setValue(this.readFloat());
            break;
        case .:
            ((CorbaPrimitiveHandler)obj).setValue(this.readDouble());
            break;
        case .:
            ((CorbaPrimitiveHandler)obj).setValue(this.readString());
            break;
        case .:
            ((CorbaPrimitiveHandler)obj).setValue(this.readWString());
            break;
        case .:
            ((CorbaAnyHandler)obj).setValue(this.readAny());
            break;
        
        // Now for the complex types
        case .:
            this.readArray((CorbaArrayHandler)obj);
            break;
        case .:
            this.readSequence(obj);
            break;
        case .:
            this.readStruct((CorbaStructHandler)obj);
            break;
        case .:
            this.readEnum(obj);
            break;
        case .:
            this.readException((CorbaExceptionHandler)obj);
            break;
        case .:
            this.readFixed((CorbaFixedHandler)obj);
            break;
        case .:
            this.readUnion((CorbaUnionHandler)obj);
            break;
        case .:
            this.readObjectReference((CorbaObjectReferenceHandler)obj);
            break;            
        default:
        // TODO: Provide Implementation. Do we throw an exception.
        }
    }
    // -- primitive types --
    public Boolean readBoolean() throws CorbaBindingException {
        try {
            return .read_boolean();
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read boolean");
            throw new CorbaBindingException("CorbaObjectReader: readBoolean MARSHAL exception"ex);
        }
    }
    public Character readChar() throws CorbaBindingException {
        try {
            return Character.valueOf(.read_char());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read character");
            throw new CorbaBindingException("CorbaObjectReader: readChar MARSHAL exception"ex);
        }
    }
    public Character readWChar() throws CorbaBindingException {
        try {
            return Character.valueOf(.read_wchar());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read wide character");
            throw new CorbaBindingException("CorbaObjectReader: readWChar MARSHAL exception"ex);
        }
    }
    public Byte readOctet() throws CorbaBindingException {
        try {
            return Byte.valueOf(.read_octet());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read octet");
            throw new CorbaBindingException("CorbaObjectReader: readOctet MARSHAL exception"ex);
        }
    }
    public Short readShort() throws CorbaBindingException {
        try {
            return Short.valueOf(.read_short());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read short");
            throw new CorbaBindingException("CorbaObjectReader: readShort MARSHAL exception"ex);
        }
    }
    public Integer readUShort() throws CorbaBindingException {
        try {
            int result = .read_ushort();
            if (result < 0) {
                result = (result - .) - .;
            }
            return result;
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read unsigned short");
            throw new CorbaBindingException("CorbaObjectReader: readUShort MARSHAL exception"ex);
        }
    }
    public Integer readLong() throws CorbaBindingException {
        Integer result = Integer.valueOf(.read_long());
        try {
            return result;
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read long");
            throw new CorbaBindingException("CorbaObjectReader: readLong MARSHAL exception"ex);
        }
    }
    public long readULong() throws CorbaBindingException {
        try {
            long l = .read_ulong();
            l &= 0xffffffffL;
            return l;
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read unsigned long");
            throw new CorbaBindingException("CorbaObjectReader: readULong MARSHAL exception"ex);
        }
    }
    public Long readLongLong() throws CorbaBindingException {
        try {
            return Long.valueOf(.read_longlong());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read long long");
            throw new CorbaBindingException("CorbaObjectReader: readLongLong MARSHAL exception"ex);
        }
    }
    private BigInteger convertLongToULong(long l) {
        if (l < 0) {
            long l2 = l & 0x7FFFFFFFFFFFFFL;
            BigInteger i = BigInteger.valueOf(l2);
            BigInteger i2 = BigInteger.valueOf(0);
            i2 = i2.setBit(63);
            i = i.or(i2);
            return i;
        }
        return BigInteger.valueOf(l);
    }
    public BigInteger readULongLong() throws CorbaBindingException {
        try {
            return convertLongToULong(.read_ulonglong());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read unsigned long long");
            throw new CorbaBindingException("CorbaObjectReader: readULongLong MARSHAL exception"ex);
        }
    }
    public Float readFloat() throws CorbaBindingException {
        try {
            return Float.valueOf(.read_float());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read float");
            throw new CorbaBindingException("CorbaObjectReader: readFloat MARSHAL exception"ex);
        }
    }
    public Double readDouble() throws CorbaBindingException {
        try {
            return Double.valueOf(.read_double());
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read double");
            throw new CorbaBindingException("CorbaObjectReader: readDouble MARSHAL exception"ex);
        }
    }
    public String readString() throws CorbaBindingException {
        try {
            return .read_string();
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read string");
            throw new CorbaBindingException("CorbaObjectReader: readString MARSHAL exception"ex);
        }
    }
    public String readWString() throws CorbaBindingException {
        try {
            return .read_wstring();
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read wide string");
            throw new CorbaBindingException("CorbaObjectReader: readWString MARSHAL exception"ex);
        }
    }
    public Any readAny() throws CorbaBindingException {
        try {
            return .read_any();
        } catch (org.omg.CORBA.MARSHAL ex) {
            .log(."CorbaObjectReader: could not read any");
            throw new CorbaBindingException("CorbaObjectReader: readAny MARSHAL exception"ex);
        }
    }
    // -- complex types --
    public void readEnum(CorbaObjectHandler objthrows CorbaBindingException {
        int enumIndex = .read_long();
        Enum enumType = (Enumobj.getType();
        List<Enumeratorenumerators = enumType.getEnumerator();
        CorbaEnumHandler enumObj = (CorbaEnumHandler)obj;
        enumObj.setValue(enumerators.get(enumIndex).getValue());
    }
    public void readStruct(CorbaStructHandler structObjthrows CorbaBindingException {
        List<CorbaObjectHandlerstructMembers = structObj.getMembers();
        for (int i = 0; i < structMembers.size(); ++i) {
            this.read(structMembers.get(i));
        }
    }
    public void readException(CorbaExceptionHandler exceptObjthrows CorbaBindingException {
        List<CorbaObjectHandlerexceptElements = exceptObj.getMembers();
        String exceptId = .read_string();
        exceptObj.setId(exceptId);
        for (int i = 0; i < exceptElements.size(); ++i) {
            this.read(exceptElements.get(i));
        }
    }
    public void readFixed(CorbaFixedHandler fixedHandlerthrows CorbaBindingException {
        long scale = fixedHandler.getScale();
        try {
            java.math.BigDecimal fixedValue = .read_fixed().movePointLeft((int)scale);
            fixedHandler.setValue(fixedValue);
        } catch (NO_IMPLEMENT ex) {
            //the read_fixed method is a "late addition" and not all orbs implement it.
            //Some of them have a "read_fixed(TypeCode)" method, we'll try that
            Method m = null;
            try {
                m = .getClass().getMethod("read_fixed"new Class[] {TypeCode.class});
                BigDecimal fixedValue =
                    (BigDecimal)m.invoke(new Object[] {fixedHandler.getTypeCode()});
                fixedHandler.setValue(fixedValue);
            } catch (Throwable e1) {
                throw ex;
            }
        }
    }
    public void readEnumDiscriminator(CorbaUnionHandler unionHandlerCorbaEnumHandler disc)
        throws CorbaBindingException {
        int enumIndex = .read_long();
        Enum enumType = (Enumdisc.getType();
        List<Enumeratorenumerators = enumType.getEnumerator();
        if (enumIndex == .) {
            enumIndex = unionHandler.getDefaultIndex();
        }
        disc.setValue(enumerators.get(enumIndex).getValue());
    }
    public void readUnion(CorbaUnionHandler unionHandlerthrows CorbaBindingException {
        Union unionType = (Union)unionHandler.getType();
        List<Unionbranchbranches = unionType.getUnionbranch();
        CorbaObjectHandler discriminator = unionHandler.getDiscriminator();
        if (branches.size() > 0) {
            String discLabel = null;
            if (discriminator.getTypeCodeKind().value() == .) {
                CorbaEnumHandler disc = (CorbaEnumHandlerdiscriminator;
                readEnumDiscriminator(unionHandlerdisc);
                discLabel = disc.getValue();
            } else {
                read(discriminator);
                discLabel = ((CorbaPrimitiveHandler)discriminator).getDataFromValue(); 
            }
            // Now find the label in the union to get the right case
            Unionbranch defaultBranch = null;
            boolean caseFound = false;
            for (Iterator<UnionbranchbranchIter = branches.iterator(); branchIter.hasNext();) {
                Unionbranch branch = branchIter.next();
                if (branch.isSetDefault() && branch.isDefault()) {
                    defaultBranch = branch;
                }
                List<CaseTypecases = branch.getCase();
                for (Iterator<CaseTypecaseIter = cases.iterator(); caseIter.hasNext();) {
                    CaseType c = caseIter.next();
                    if (c.getLabel().equalsIgnoreCase(discLabel)) {
                        CorbaObjectHandler branchObj = unionHandler.getBranchByName(branch.getName());
                        this.read(branchObj);
                        unionHandler.setValue(branch.getName(), branchObj);
                        caseFound = true;
                        break;
                    }
                }
                if (caseFound) {
                    break;
                }
            }
            
            // If we never find a case that matches the value of the discriminiator, then we must have
            // found the default case.
            if (!caseFound && defaultBranch != null) {
                CorbaObjectHandler branchObj = unionHandler.getBranchByName(defaultBranch.getName());
                this.read(branchObj);
                unionHandler.setValue(defaultBranch.getName(), branchObj);
            }
        }
    }
    //CHECKSTYLE:OFF  -  processing the typecodes in a switch makes this method fairly long/complex
    public void readArray(CorbaArrayHandler arrayObjthrows CorbaBindingException {
        List<CorbaObjectHandlerarrayElements = arrayObj.getElements();
        Object val = null;
        int arraySize = arrayElements.size();
        if (arraySize > 0) {
            switch(arrayElements.get(0).getTypeCodeKind().value()) {
            case .: {
                boolean[] values = new boolean[arraySize];
                .read_boolean_array(values, 0, arraySize); 
                val = values;
                break;
            }
            case .: {
                char[] values = new char[arraySize];
                .read_char_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                char[] values = new char[arraySize];
                .read_wchar_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                byte[] values = new byte[arraySize];
                .read_octet_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                short[] values = new short[arraySize];
                .read_short_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                short[] values = new short[arraySize];
                .read_ushort_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                int[] values = new int[arraySize];
                .read_long_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                int[] values = new int[arraySize];
                .read_ulong_array(values, 0, arraySize);
                long[] v2 = new long[arraySize];
                for (int x = 0; x < arraySizex++) {
                    v2[x] = values[x];
                    v2[x] &= 0xFFFFFFFFL;
                }
                val = v2;
                break;
            }
            case .: {
                long[] values = new long[arraySize];
                .read_longlong_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                long[] values = new long[arraySize];
                .read_ulonglong_array(values, 0, arraySize);
                BigInteger[] v2 = new BigInteger[arraySize];
                for (int x = 0; x < arraySizex++) {
                    v2[x] = convertLongToULong(values[x]);
                }
                val = v2;
                break;
            }
            case .: {
                float[] values = new float[arraySize];
                .read_float_array(values, 0, arraySize);
                val = values;
                break;
            }
            case .: {
                double[] values = new double[arraySize];
                .read_double_array(values, 0, arraySize);
                val = values;
                break;
            }
            default:
                for (int i = 0; i < arrayElements.size(); ++i) {
                    this.read(arrayElements.get(i));
                }
            }
            if (val != null) {
                int sz = Array.getLength(val);
                for (int i = 0; i < szi++) {
                    ((CorbaPrimitiveHandler)arrayElements.get(i)).setValue(Array.get(vali));
                }
            }
        }
    }
    //CHECKSTYLE:ON
    public void readSequence(CorbaObjectHandler objthrows CorbaBindingException {
        if (obj instanceof CorbaOctetSequenceHandler) {
            int length = .read_ulong();
            byte[] value = new byte[length];
            .read_octet_array(value, 0, length);
            ((CorbaOctetSequenceHandlerobj).setValue(value);
        } else {
            CorbaSequenceHandler sequenceObj = (CorbaSequenceHandler)obj;
            List<CorbaObjectHandlerseqElements = sequenceObj.getElements();
            int length = .read_ulong();
            List<CorbaObjectHandlerelements = new ArrayList<CorbaObjectHandler>(length);
            
            // Simply checking the bound won't handle our recursive types.  We need to check for the
            // existance of template, which will be present for all unbounded sequences and for bound
            // sequences with recursive type elements.  Use the template element to construct each
            // object that is in the input stream.
            if (sequenceObj.getTemplateElement() != null) {
                sequenceObj.clear();
                CorbaObjectHandler template = sequenceObj.getTemplateElement();
                for (int i = 0; i < length; ++i) {
                    CorbaObjectHandler seqElement;
                    if (i < seqElements.size()) {
                        seqElement = seqElements.get(i);
                    } else {
                        seqElement = initializeCorbaObjectHandler(template);
                    }
                    read(seqElement);
                    elements.add(seqElement);
                }
                sequenceObj.setElements(elements);
            } else {
                // We have a bounded sequence and the object should already be pre-built
                for (int i = 0; i < length; ++i) {
                    read(seqElements.get(i));
                }
                sequenceObj.setElements(seqElements.subList(0, length));
            }
        }
    }
    public void readObjectReference(CorbaObjectReferenceHandler objRefObjthrows CorbaBindingException {
        org.omg.CORBA.Object objRef = .read_Object();
        objRefObj.setReference(objRef);
    }
        Constructor<?> templateConstructor = template.getClass().getDeclaredConstructors()[0];
        Object[] params = new Object[4];
        
        // Check to see if the template type is a recursive type.  If so, it means that it is part
        // 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();
        
        CorbaObjectHandler handler = null;
        try {
            handler = (CorbaObjectHandlertemplateConstructor.newInstance(params);
            // To construct an any, we also need to set a typemap.  This should be available through
            // 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 = (CorbaSequenceHandlertemplate;
            ((CorbaSequenceHandler)handler).
                setTemplateElement(templateSeq.getTemplateElement());
        } else if (template instanceof CorbaStructHandler) {
            CorbaStructHandler templateStruct = (CorbaStructHandlertemplate;
            CorbaStructHandler struct = (CorbaStructHandlerhandler;
            struct.setRecursive(template.isRecursive());
            List<CorbaObjectHandlermembers = 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 = (CorbaArrayHandlertemplate;
            CorbaArrayHandler array = (CorbaArrayHandlerhandler;
            List<CorbaObjectHandlerelements = 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 = (CorbaUnionHandlertemplate;
            CorbaUnionHandler union = (CorbaUnionHandlerhandler;
            union.setRecursive(template.isRecursive());
            union.setDiscriminator(initializeCorbaObjectHandler(templateUnion.getDiscriminator()));
            List<CorbaObjectHandlercases = templateUnion.getCases();
            for (int i = 0; i < cases.size(); i++) {
                union.addCase(initializeCorbaObjectHandler(cases.get(i)));
            }
        }
        return handler;
    }
New to GrepCode? Check out our FAQ X