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.utils;
 
 import java.util.Map;
 
 
 
 public final class CorbaAnyHelper {
    
     private static final Map<QNameQNameSCHEMA_TO_IDL_TYPES = new HashMap<QNameQName>();
     private static final Map<QNameQNameIDL_TO_SCHEMA_TYPES = new HashMap<QNameQName>();
     
     private static Constructor<?> fixedAnyConstructor;
     
     private CorbaAnyHelper() {
         //utility class
     }
     
     public static Any createAny(ORB orb) {
         Any value = orb.create_any();
         if ("com.sun.corba.se.impl.corba.AnyImpl".equals(value.getClass().getName())) {
             value = createFixedAny(orbvalue);
         }
         
         return value;
     }
 
     public static boolean isPrimitiveSchemaType(QName schemaType) {
         return .get(schemaType) != null;
     }
 
     public static boolean isPrimitiveIdlType(QName idlType) {
         return .get(idlType) != null;
     }
     
     public static QName convertPrimitiveSchemaToIdlType(QName schemaType) {
         return .get(schemaType);
     }
 
     public static QName convertPrimitiveIdlToSchemaType(QName idlType) {
         return .get(idlType);
     }
 
     public static QName getPrimitiveIdlTypeFromTypeCode(TypeCode tc) {
         TCKind type = tc.kind();
         QName result = null;
 
         switch(type.value()) {
         case .:
             result = .;
             break;
         case .:
             result = .;
             break;
         case .:
             result = .;
             break;
         case .:
             result = .;
             break;
         case .:
             result = .;
             break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        case .:
            result = .;
            break;
        default:
            result = null;
        }
        return result;
    }
    public static void insertPrimitiveIntoAny(Any aCorbaPrimitiveHandler primitive)
        throws CorbaBindingException {
        assert primitive != null;
        switch (primitive.getTypeCode().kind().value()) {
        case .:
            a.insert_boolean((Boolean)primitive.getValue());
            break;
        case .:
            a.insert_char((Character)primitive.getValue());
            break;
        case .:
            a.insert_wchar((Character)primitive.getValue());
            break;
        case .:
            a.insert_octet((Byte)primitive.getValue());
            break;
        case .:
            a.insert_short((Short)primitive.getValue());
            break;
        case .:
            a.insert_ushort(((Integer)primitive.getValue()).shortValue());
            break;
        case .:
            a.insert_long((Integer)primitive.getValue());
            break;
        case .:
            a.insert_ulong(((BigInteger)primitive.getValue()).intValue());
            break;
        case .:
            a.insert_longlong((Long)primitive.getValue());
            break;
        case .:
            a.insert_ulonglong(((BigInteger)primitive.getValue()).intValue());
            break;
        case .:
            a.insert_float((Float)primitive.getValue());
            break;
        case .:
            a.insert_double((Double)primitive.getValue());
            break;
        case .:
            a.insert_string((String)primitive.getValue());
            break;
        case .:
            a.insert_wstring((String)primitive.getValue());
            break;
        default:
            throw new CorbaBindingException("Unable to insert type into any.  Kind = " 
                                            + primitive.getTypeCode().kind().value());
        }
    }
    public static void extractPrimitiveFromAny(Any aCorbaPrimitiveHandler primitive) {
        assert primitive != null;
        switch (primitive.getTypeCode().kind().value()) {
        case .:
            primitive.setValue(Boolean.valueOf(a.extract_boolean()));
            break;
        case .:
            primitive.setValue(Character.valueOf(a.extract_char()));
            break;
        case .:
            primitive.setValue(Character.valueOf(a.extract_wchar()));
            break;
        case .:
            primitive.setValue(Byte.valueOf(a.extract_octet()));
            break;
        case .:
            primitive.setValue(Short.valueOf(a.extract_short()));
            break;
        case .:
            primitive.setValue(Integer.valueOf(a.extract_ushort()));
            break;
        case .:
            primitive.setValue(Integer.valueOf(a.extract_long()));
            break;
        case .:
            primitive.setValue(BigInteger.valueOf(a.extract_ulong()));
            break;
        case .:
            primitive.setValue(Long.valueOf(a.extract_longlong()));
            break;
        case .:
            primitive.setValue(BigInteger.valueOf(a.extract_ulonglong()));
            break;
        case .:
            primitive.setValue(Float.valueOf(a.extract_float()));
            break;
        case .:
            primitive.setValue(Double.valueOf(a.extract_double()));
            break;
        case .:
            primitive.setValue(a.extract_string());
            break;
        case .:
            primitive.setValue(a.extract_wstring());
            break;
        default:
            throw new CorbaBindingException("Unable to extract type from any.  Kind = " 
                                            + primitive.getTypeCode().kind().value());
        }
    }
    // NOTE: We have an issue when we get a schema type of String.  We don't know whether this means
    // that we have an IDL type of wchar, string, or wstring.  To be safe, we'll simply use a CORBA
    // string for the mapping.
    static {
        //SCHEMA_TO_IDL_TYPES.put(W3CConstants.NT_SCHEMA_STRING, CorbaConstants.NT_CORBA_WCHAR);
        //SCHEMA_TO_IDL_TYPES.put(W3CConstants.NT_SCHEMA_STRING, CorbaConstants.NT_CORBA_WSTRING);
    }
    static {
    }
    
    private static Any createFixedAny(ORB orbAny any) {
        createFixedAnyConstructor();
        try {
            return (Any).newInstance(orbany);
        } catch (Exception e) {
            return any;
        }
    }
    private static synchronized void createFixedAnyConstructor() {
        if ( != null) {
            return;
        }
        
        ASMHelper helper = new ASMHelper();
        ClassWriter cw = helper.createClassWriter();
        FieldVisitor fv;
                 "org/apache/cxf/binding/corba/utils/FixedAnyImpl"
                 null"com/sun/corba/se/impl/corba/AnyImpl"null);
        cw.visitSource("FixedAnyImpl.java"null);
        fv = cw.visitField(0, "obj""Lorg/omg/CORBA/portable/Streamable;"nullnull);
        fv.visitEnd();
        addFixedAnyConstructor(helpercw);
        addInsertOverride(helpercw);
        addExtractOverride(helpercw);
        addReadOverride(helpercw);
        addWriteOverride(helpercw);
        
        cw.visitEnd();
        byte[] b = cw.toByteArray();
        Class<?> c = helper.loadClass("org.apache.cxf.binding.corba.utils.FixedAnyImpl"
                                      CorbaAnyHelper.classb);
        try {
             = c.getConstructor(ORB.classAny.class);
        } catch (Exception e) {
            //shouldn't happen since we generated that constructor
        }
    }
    private static void addReadOverride(ASMHelper helperClassWriter cw) {
        MethodVisitor mv = cw.visitMethod(."read_value"
                            "(Lorg/omg/CORBA/portable/InputStream;Lorg/omg/CORBA/TypeCode;)V"
                            nullnull);
        mv.visitCode();
        Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(54, l0);
        mv.visitVarInsn(., 0);
        mv.visitFieldInsn(."org/apache/cxf/binding/corba/utils/FixedAnyImpl",
                          "obj""Lorg/omg/CORBA/portable/Streamable;");
        Label l1 = helper.createLabel();
        mv.visitJumpInsn(.l1);
        Label l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLineNumber(55, l2);
        mv.visitVarInsn(., 0);
        mv.visitFieldInsn(."org/apache/cxf/binding/corba/utils/FixedAnyImpl"
                          "obj""Lorg/omg/CORBA/portable/Streamable;");
        mv.visitVarInsn(., 1);
        mv.visitMethodInsn(."org/omg/CORBA/portable/Streamable"
                           "_read""(Lorg/omg/CORBA/portable/InputStream;)V");
        Label l3 = helper.createLabel();
        mv.visitJumpInsn(.l3);
        mv.visitLabel(l1);
        mv.visitLineNumber(57, l1);
        mv.visitVarInsn(., 0);
        mv.visitVarInsn(., 1);
        mv.visitVarInsn(., 2);
        mv.visitMethodInsn(."com/sun/corba/se/impl/corba/AnyImpl"
                           "read_value"
                           "(Lorg/omg/CORBA/portable/InputStream;Lorg/omg/CORBA/TypeCode;)V");
        mv.visitLabel(l3);
        mv.visitLineNumber(59, l3);
        mv.visitInsn(.);
        Label l4 = helper.createLabel();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this""Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;",
                              nulll0l4, 0);
        mv.visitLocalVariable("is""Lorg/omg/CORBA/portable/InputStream;"nulll0l4, 1);
        mv.visitLocalVariable("t""Lorg/omg/CORBA/TypeCode;"nulll0l4, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
        
    private static void addWriteOverride(ASMHelper helperClassWriter cw) {
        MethodVisitor mv = cw.visitMethod(."write_value"
                            "(Lorg/omg/CORBA/portable/OutputStream;)V"nullnull);
        mv.visitCode();
        Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(61, l0);
        mv.visitVarInsn(., 0);
        mv.visitFieldInsn(."org/apache/cxf/binding/corba/utils/FixedAnyImpl",
                          "obj""Lorg/omg/CORBA/portable/Streamable;");
        Label l1 = helper.createLabel();
        mv.visitJumpInsn(.l1);
        Label l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLineNumber(62, l2);
        mv.visitVarInsn(., 0);
        mv.visitFieldInsn(."org/apache/cxf/binding/corba/utils/FixedAnyImpl",
                          "obj""Lorg/omg/CORBA/portable/Streamable;");
        mv.visitVarInsn(., 1);
        mv.visitMethodInsn(."org/omg/CORBA/portable/Streamable",
                           "_write""(Lorg/omg/CORBA/portable/OutputStream;)V");
        Label l3 = helper.createLabel();
        mv.visitJumpInsn(.l3);
        mv.visitLabel(l1);
        mv.visitLineNumber(64, l1);
        mv.visitVarInsn(., 0);
        mv.visitVarInsn(., 1);
        mv.visitMethodInsn(."com/sun/corba/se/impl/corba/AnyImpl",
                           "write_value""(Lorg/omg/CORBA/portable/OutputStream;)V");
        mv.visitLabel(l3);
        mv.visitLineNumber(66, l3);
        mv.visitInsn(.);
        Label l4 = helper.createLabel();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this""Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;",
                              nulll0l4, 0);
        mv.visitLocalVariable("os""Lorg/omg/CORBA/portable/OutputStream;"nulll0l4, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
        
    }
    private static void addExtractOverride(ASMHelper helperClassWriter cw) {
        // TODO Auto-generated method stub
        MethodVisitor mv = cw.visitMethod(."extract_Streamable",
                            "()Lorg/omg/CORBA/portable/Streamable;"nullnull);
        mv.visitCode();
        Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(47, l0);
        mv.visitVarInsn(., 0);
        mv.visitFieldInsn(."org/apache/cxf/binding/corba/utils/FixedAnyImpl"
                          "obj""Lorg/omg/CORBA/portable/Streamable;");
        Label l1 = helper.createLabel();
        mv.visitJumpInsn(.l1);
        Label l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLineNumber(48, l2);
        mv.visitVarInsn(., 0);
        mv.visitFieldInsn(."org/apache/cxf/binding/corba/utils/FixedAnyImpl",
                          "obj""Lorg/omg/CORBA/portable/Streamable;");
        mv.visitInsn(.);
        mv.visitLabel(l1);
        mv.visitLineNumber(50, l1);
        mv.visitVarInsn(., 0);
        mv.visitMethodInsn(."com/sun/corba/se/impl/corba/AnyImpl",
                           "extract_Streamable""()Lorg/omg/CORBA/portable/Streamable;");
        mv.visitInsn(.);
        Label l3 = helper.createLabel();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this""Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;"nulll0l3, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
        
    }
    private static void addInsertOverride(ASMHelper helperClassWriter cw) {
        MethodVisitor mv = cw.visitMethod(.,
                            "insert_Streamable"
                            "(Lorg/omg/CORBA/portable/Streamable;)V"nullnull);
        mv.visitCode();
        Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(43, l0);
        mv.visitVarInsn(., 0);
        mv.visitVarInsn(., 1);
                           "com/sun/corba/se/impl/corba/AnyImpl"
                           "insert_Streamable"
                           "(Lorg/omg/CORBA/portable/Streamable;)V");
        Label l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(44, l1);
        mv.visitVarInsn(., 0);
        mv.visitVarInsn(., 1);
        mv.visitFieldInsn(.
                          "org/apache/cxf/binding/corba/utils/FixedAnyImpl""obj"
                          "Lorg/omg/CORBA/portable/Streamable;");
        Label l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLineNumber(45, l2);
        mv.visitInsn(.);
        Label l3 = helper.createLabel();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this""Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;",
                              nulll0l3, 0);
        mv.visitLocalVariable("s""Lorg/omg/CORBA/portable/Streamable;"nulll0l3, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    private static void addFixedAnyConstructor(ASMHelper helperClassWriter cw) {
        MethodVisitor mv = cw.visitMethod(."<init>""(Lorg/omg/CORBA/ORB;)V"nullnull);
        mv.visitCode();
        Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(36, l0);
        mv.visitVarInsn(., 0);
        mv.visitVarInsn(., 1);
        mv.visitTypeInsn(."com/sun/corba/se/spi/orb/ORB");
                           "com/sun/corba/se/impl/corba/AnyImpl",
                           "<init>""(Lcom/sun/corba/se/spi/orb/ORB;)V");
        Label l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(37, l1);
        mv.visitInsn(.);
        Label l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this",
                              "Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;"
                              nulll0l2, 0);
        mv.visitLocalVariable("orb""Lorg/omg/CORBA/ORB;"nulll0l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
        
        mv = cw.visitMethod(."<init>",
                            "(Lorg/omg/CORBA/ORB;Lorg/omg/CORBA/Any;)V",
                            nullnull);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(39, l0);
        mv.visitVarInsn(., 0);
        mv.visitVarInsn(., 1);
        mv.visitTypeInsn(."com/sun/corba/se/spi/orb/ORB");
        mv.visitVarInsn(., 2);
                           "com/sun/corba/se/impl/corba/AnyImpl",
                           "<init>",
                           "(Lcom/sun/corba/se/spi/orb/ORB;Lorg/omg/CORBA/Any;)V");
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(40, l1);
        mv.visitInsn(.);
        l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this""Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;",
                              nulll0l2, 0);
        mv.visitLocalVariable("orb""Lorg/omg/CORBA/ORB;"nulll0l2, 1);
        mv.visitLocalVariable("any""Lorg/omg/CORBA/Any;"nulll0l2, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
        
    }
New to GrepCode? Check out our FAQ X