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;
 
 import java.util.List;
 import java.util.Map;
 
 
 
 public class CorbaConduit implements Conduit {
     private static final Logger LOG = LogUtils.getL7dLogger(CorbaConduit.class);
 
 
     private EndpointInfo endpointInfo;
     private EndpointReferenceType target;
     private MessageObserver incomingObserver;
     private ORB orb;
     private OrbConfig orbConfig;
     private CorbaTypeMap typeMap;
 
     public CorbaConduit(EndpointInfo eiEndpointReferenceType refOrbConfig config) {
          = ei;
          = getTargetReference(ref);
          = config;
          = TypeMapCache.get(ei.getService());
     }
 
     public OrbConfig getOrbConfig() {
         return ;
     }
     
     public synchronized void prepareOrb() {
         if ( == null) {
              = CorbaBindingHelper.getDefaultORB();
         }
     }
     public void prepare(Message messagethrows IOException {    
         try {
             prepareOrb();
             
             String address = null;
            if ( != null) {
                address = .getAddress().getValue();
            }
            if (address == null) {
                AddressType ad = .getExtensor(AddressType.class);
                if (ad != null) {
                    address = ad.getLocation();
                }
            }
            String ref = (String)message.get(.);
            if (ref != null) {
                // A non-null endpoint address from the message means that we want to invoke on a particular
                // object reference specified by the endpoint reference type.  If the reference is null, then
                // we want to invoke on the default location as specified in the WSDL.
                address = ref;
            }
            if (address == null) {
                .log(."Unable to locate a valid CORBA address");
                throw new CorbaBindingException("Unable to locate a valid CORBA address");
            }
            org.omg.CORBA.Object targetObject;
            targetObject = CorbaUtils.importObjectReference(address);
            message.put(.);
            message.put(.targetObject);
            message.setContent(OutputStream.class,
                               new CorbaOutputStream(message));
            
            if (message instanceof CorbaMessage) {
                ((CorbaMessage)message).setCorbaTypeMap();
            }
           
        } catch (java.lang.Exception ex) {
            .log(."Could not resolve target object");
            throw new CorbaBindingException(ex);
        }
    }
    public void close(Message messagethrows IOException {
        if (message.get(.) != null) {
            BindingOperationInfo boi = message.getExchange().get(BindingOperationInfo.class);
            OperationType opType = boi.getExtensor(OperationType.class);
            try {
                if (message instanceof CorbaMessage) {
                    buildRequest((CorbaMessage)messageopType);            
                }
                message.getContent(OutputStream.class).close();
            } catch (Exception ex) {
                .log(."Could not build the corba request");
                throw new CorbaBindingException(ex);
            }
        }
    }
    public EndpointReferenceType getTarget() {
        return ;
    }
    public void close() {
        
    }
    public void setMessageObserver(MessageObserver observer) {
         = observer;
    }
        EndpointReferenceType ref = null;
        if (null == t) {
            ref = new EndpointReferenceType();
            AttributedURIType address = new AttributedURIType();
            address.setValue(getAddress());
            ref.setAddress(address);
        } else {
            ref = t;
        }
        return ref;
    }
    public final String getAddress() {
        return .getAddress();
    }
        
    public void buildRequest(CorbaMessage messageOperationType opTypethrows Exception {        
        ServiceInfo service = message.getExchange().get(ServiceInfo.class);
        NVList nvlist = getArguments(message);
        NamedValue ret = getReturn(message);
        Map<TypeCodeRaisesTypeexceptions = getOperationExceptions(opType);
        ExceptionList exList = getExceptionList(exceptionsmessageopType);
        Request request = getRequest(messageopType.getName(), nvlistretexList);
        if (request == null) {
            throw new CorbaBindingException("Couldn't build the corba request");
        }
        try {
            request.invoke();
        } catch (SystemException ex) {
            message.setContent(Exception.classnew Fault(ex));
            message.setSystemException(ex);
            return;
        }
        Exception ex = request.env().exception();
        if (ex != null) {
            if (ex instanceof UnknownUserException) {
                UnknownUserException userEx = (UnknownUserExceptionex;
                Any except = userEx.except;
                RaisesType raises = exceptions.get(except.type());
                if (raises == null) {
                    throw new CorbaBindingException("Couldn't find the exception type code to unmarshall");
                }
                QName elName = new QName(""raises.getException().getLocalPart());
                CorbaObjectHandler handler =
                    CorbaHandlerUtils.initializeObjectHandler(,
                                                              elName,
                                                              raises.getException(),
                                                              ,
                                                              service);
                
                CorbaStreamable exStreamable = message.createStreamableObject(handlerelName);
                exStreamable._read(except.create_input_stream());
                message.setStreamableException(exStreamable);
                message.setContent(Exception.classnew Fault(userEx));
            } else {
                message.setContent(Exception.classnew Fault(ex));
            }
        }
    }
       
    public NVList getArguments(CorbaMessage message) {
        if ( == null) {
            prepareOrb();
        }
        // Build the list of DII arguments, returns, and exceptions
        NVList list = null;
        if (message.getStreamableArguments() != null) {
            CorbaStreamable[] arguments = message.getStreamableArguments();
            list = .create_list(arguments.length);
            for (CorbaStreamable argument : arguments) {
                Any value = CorbaAnyHelper.createAny();
                argument.getObject().setIntoAny(valueargumenttrue);
                list.add_value(argument.getName(), valueargument.getMode());
            }
        } else {
            list = .create_list(0);
        }
        return list;        
    }
    
    public NamedValue getReturn(CorbaMessage message) {
        if ( == null) {
            prepareOrb();
        }
        CorbaStreamable retVal = message.getStreamableReturn();
        NamedValue ret = null;
        if (retVal != null) {
            Any returnAny = CorbaAnyHelper.createAny();
            retVal.getObject().setIntoAny(returnAnyretValfalse);
            ret = .create_named_value(retVal.getName(), returnAny....);
        } else {
            // TODO: REVISIT: for some reason,some ORBs do not like to
            // have a null NamedValue return value. Create this 'empty' 
            // one if a void return type is used.
            ret = .create_named_value("return".create_any(), ....);
        }
        return ret;        
    }
    
    public ExceptionList getExceptionList(Map<TypeCodeRaisesTypeexceptions,
                                             CorbaMessage message
                                             OperationType opType) {
        if ( == null) {
            prepareOrb();
        }
        // Get the typecodes for the exceptions this operation can throw.
        // These are defined in the operation definition from WSDL.
        ExceptionList exList = .create_exception_list();
               
        if (exceptions != null) {
            Object[] tcs = null;
            tcs = exceptions.keySet().toArray();
        
            for (int i = 0; i < exceptions.size(); ++i) {
                exList.add((TypeCode)tcs[i]);
            }
        }
        return exList;
    }
            
    public Request getRequest(CorbaMessage message,
                                 String opName,
                                 org.omg.CORBA.NVList nvlist
                                 org.omg.CORBA.NamedValue ret
                                 org.omg.CORBA.ExceptionList exList
        throws Exception {
        Request request = null;
        if ( == null) {
            prepareOrb();
        }
        ContextList ctxList = .create_context_list();
        Context ctx = null;
        try {
            ctx = .get_default_context();            
        } catch (Exception ex) {
            //ignore?
        }
        org.omg.CORBA.Object targetObj =
            (org.omg.CORBA.Object)message.get(.);
        if (targetObj != null) {
            request = targetObj._create_request(ctxopNamenvlistretexListctxList);
        }
        return request;
    }
        
                                         OperationType operation
                                         CorbaTypeMap map) {
        if ( == null) {
            prepareOrb();
        }
        Map<TypeCodeRaisesTypeexceptions = new HashMap<TypeCodeRaisesType>();
        List<RaisesTypeexList = operation.getRaises(); 
        if (exList != null) {
            for (int i = 0; i < exList.size(); ++i) {
                RaisesType ex = exList.get(i);
                TypeCode tc = CorbaUtils.getTypeCode(ex.getException(), map);
                exceptions.put(tcex);
            }
        }
        return exceptions;
    }
    
    private class CorbaOutputStream extends CachedOutputStream {
       
        private Message message;
        private boolean isOneWay;
        CorbaOutputStream(Message m) {
             = m;        
        }

        
Perform any actions required on stream flush (freeze headers, reset output stream ... etc.)
        public void doFlush() throws IOException {
            // do nothing here
        }

        
Perform any actions required on stream closure (handle response etc.)
        public void doClose() throws IOException {            
            if (ContextUtils.isRequestor() && ContextUtils.isOutbound()) {
                try {
                     = .getExchange().isOneWay();
                    
                    if (!) {                
                        handleResponse();
                    }
                } catch (Exception ex) {
                    .log(."Connection failed with Exception : "ex);
                    throw new IOException(ex.toString());
                }            
            }
        }
        public void onWrite() throws IOException {
        }
        public void handleResponse() throws IOException {
            .log(."incoming observer is " + );
            Exchange exchange = .getExchange();
            CorbaMessage corbaMsg = (CorbaMessage;
            MessageImpl inMessage = new MessageImpl();
            CorbaDestination destination = new CorbaDestination();
            inMessage.setDestination(destination);
            exchange.put(ORB.class);
            inMessage.setExchange(exchange);
            CorbaMessage inCorbaMsg = new CorbaMessage(inMessage);       
            inCorbaMsg.setCorbaTypeMap();
            if (corbaMsg.getStreamableException() != null) {
                exchange.setInFaultMessage(corbaMsg);
                inCorbaMsg.setStreamableException(corbaMsg.getStreamableException());
            } else if (corbaMsg.getSystemException() != null) {
                exchange.setInFaultMessage(corbaMsg);
                inCorbaMsg.setSystemException(corbaMsg.getSystemException());
            }
            .log(."incoming observer is " + );
            .onMessage(inCorbaMsg);
            .setContent(Exception.classinCorbaMsg.getContent(Exception.class));
        }
    }
        return this.;
    }
New to GrepCode? Check out our FAQ X