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.net.URI;
 import java.util.List;
 import java.util.Map;
 
 
 public class CorbaDestination implements MultiplexDestination {
     
     private static final String IOR_SHARED_KEY = "ior:shared-key";
     private static final Logger LOG = LogUtils.getL7dLogger(CorbaDestination.class);
     
     private AddressType address;
     private EndpointReferenceType reference;
     private ORB orb;
     private BindingInfo binding;
     private EndpointInfo endpointInfo;
     private OrbConfig orbConfig;
     private MessageObserver incomingObserver;
     private CorbaTypeMap typeMap;
     private byte[] objectId;
     private POA bindingPOA;
     private String poaName;
     private String serviceId;
     private boolean isPersistent;
     private org.omg.CORBA.Object obj;
 
     public CorbaDestination(EndpointInfo eiOrbConfig config) {
         this(eiconfignull);    
     }
 
     public CorbaDestination(EndpointInfo eiOrbConfig configCorbaTypeMap tm) {
          = ei.getExtensor(AddressType.class);
          = ei.getBinding();
          = new EndpointReferenceType();
         AttributedURIType addr = new AttributedURIType();
         addr.setValue(.getLocation());
         .setAddress(addr);
          = ei;
          = config;
         if (tm != null) {
              = tm;
         } else {
              = TypeMapCache.get(.getService());
         }
         PolicyType policy = ei.getExtensor(PolicyType.class);
         if (policy != null) {
             = policy.getPoaname();
             = policy.isPersistent();
             = policy.getServiceid();            
        }
    }
    public OrbConfig getOrbConfig() {
        return ;
    }
    
    public EndpointReferenceType getAddress() {
        return ;
    }    
    public Conduit getBackChannel(Message inMessage)
        throws IOException {
        return new CorbaServerConduit(,
                                      );
    }
    public BindingInfo getBindingInfo() {
        return ;
    }
    
    public EndpointInfo getEndPointInfo() {
        return ;
    }
    public CorbaTypeMap getCorbaTypeMap() {
        return ;
    }
    public void shutdown() {
        deactivate();
        if ( != null) {
            try {
                // Ask for the ORB to be destroyed.  If another destination is using it, we'll
                // simply decrement a use count, but not destroy the ORB so that we don't break the
                // other CorbaDestination.
                if (CorbaUtils.isIOR(getDestinationAddress())) {
                    CorbaBindingHelper.destroyORB();
                } else {
                    CorbaBindingHelper.destroyORB(getDestinationAddress(), );
                }
            } catch (Exception ex) {
                throw new CorbaBindingException(ex);
            }
             = null;
        }
    }
    
    public ORB getORB(List<StringorbArgs
                      String location
                      java.util.Properties props) {
        // See if an ORB has already been created for the given address. If so,
        // we'll simply use it
        // so that we don't try re-create another ORB on the same host and port.
        if (CorbaUtils.isIOR(location)) {
            location = ;
        }
         = CorbaBindingHelper.getAddressSpecificORB(locationpropsorbArgs);
        // Get the binding helper to remember that we need this ORB kept alive, even if another
        // destination tries to destroy it.
        CorbaBindingHelper.keepORBAlive(location);
        
        return ;
    }
    
    protected ORB getOrb() {
        return ;
    }
    
    protected AddressType getAddressType() {
        return ;
    }    
    public synchronized void setMessageObserver(MessageObserver observer) {
        if (observer != ) {
            MessageObserver old = ;
             = observer;
            if (observer != null) { 
                if (old == null) {
                    activate();
                }
            } else {
                if (old != null) {
                    deactivate();
                }
            }
        }
    }
    public void activate() {
        java.util.Properties props = new java.util.Properties();
        Properties configSpecifiedOrbProperties = .getOrbProperties();
        props.putAll(configSpecifiedOrbProperties);
        if (.getOrbClass() != null) {
            props.put("org.omg.CORBA.ORBClass".getOrbClass());
        }
        if (.getOrbSingletonClass() != null) {
            props.put("org.omg.CORBA.ORBSingletonClass"
                .getOrbSingletonClass());
        }
        
        String location = getDestinationAddress();
        
        if (!CorbaUtils.isValidURL(location)) {
            throw new CorbaBindingException(
                    "Invalid addressing specified for CORBA port location");
        }
        
        .info("Service address retrieved: " + location);
        
        
        URI addressURI = null;
        try {
            addressURI = new URI(location);
        } catch (java.net.URISyntaxException ex) {
            throw new CorbaBindingException(
                    "Unable to create ORB with address " + );
        }
        
        
        List<StringorbArgs = new ArrayList<String>(.getOrbArgs());
        String scheme = addressURI.getScheme();
        // A corbaloc address gives us host and port information to use when
        // setting up the
        // endpoint for the ORB. Other types of references will just create ORBs
        // on the
        // host and port used when no preference has been specified.
        if ( != null) {
             = .replace('.''_');
        }
        if ("corbaloc".equals(scheme)) {
            if ( == null) {
                 = getEndPointInfo().getName().getLocalPart().replace('.''_');                
            }
            setCorbaLocArgs(addressURIorbArgs);
        } else if ("corbaname".equals(scheme)) {
            int idx = location.indexOf("#");
            if (idx != -1) {
                 = location.substring(idx + 1);
            }
        }
        if () {
            if ( == null) {
                throw new CorbaBindingException(
                        "POA name missing for corba port "
                                + "with a persistent policy");
            }
        } else {
             = CorbaUtils.getUniquePOAName(getEndPointInfo()
                    .getService().getName(), getEndPointInfo().getName()
                    .getLocalPart(), ).replace('.''_');
        }
         = getORB(orbArgslocationprops);        
        
        try {
            POA rootPOA = POAHelper.narrow(.resolve_initial_references("RootPOA"));
            POAManager poaManager = rootPOA.the_POAManager();
            try {
                 = rootPOA.find_POA(false);
            } catch (org.omg.PortableServer.POAPackage.AdapterNonExistent ex) {
                // do nothing
            }
            // When using object references, we can run into a situation where
            // we are implementing
            // multiple instances of the same port type such that we would end
            // up using the same
            // poaname for each when persistance is used. Handle this case by
            // not throwing an
            // exception at this point during the activation, we should see an
            // exception if we try
            // an activate two objects with the same servant ID instead.
            if ( != null && ! &&  == null) {
                throw new CorbaBindingException(
                        "Corba Port activation failed because the poa "
                                +  + " already exists");
            } else if ( == null) {
                 = createPOA(rootPOApoaManager);
            }
                        
            if ( == null) {
                throw new CorbaBindingException("Unable to create CXF CORBA Binding POA");
            }
            CorbaDSIServant servant = new CorbaDSIServant();
            servant.init(this);
            if ( != null) {
                 = .getBytes();
                try {
                    .activate_object_with_id(servant);
                } catch (org.omg.PortableServer.POAPackage.ObjectAlreadyActive ex) {
                    if (!) {
                        throw new CorbaBindingException("Object "
                                                        + 
                                                        + " already active for non-persistent poa");
                    }
                }
            } else {                
                 = .activate_object(servant);
            }
            .set_servant(servant);
             = .id_to_reference();
            .exportObjectReference(location);
            
            populateEpr(.object_to_string());
            .info("Object Reference: " + .object_to_string());
            // TODO: Provide other export mechanisms? 
            poaManager.activate();
        } catch (Exception ex) {
            throw new CorbaBindingException("Unable to activate CORBA servant"ex);
        }
    }
    private void populateEpr(String ior) {
        AttributedURIType addr = new AttributedURIType();
        addr.setValue(ior);
        .setAddress(addr);
    }
    public String getDestinationAddress() {
        // We should check the endpoint first for an address.  This allows object references
        // to use the address that is associated with their endpoint instead of the single 
        // address for a particular port type that is listed in the wsdl.  Otherwise, for all 
        // object references we want to create, we would need to add the address to the wsdl 
        // file before running the application.
        String location = null;
        if ( != null) {
            location = .getAddress();
        }
        if (location == null) {
            location = .getLocation();
        }
        return location;
    }
    
        return ;
    }
    public void deactivate() {
        if ( != null) {
            if ( == null) {
                throw new CorbaBindingException("Corba Port deactivation failed because the poa is null");
            }
            try {
                .deactivate_object();
            } catch (ObjectNotActive ona) {
                .info("Caught ObjectNotActive exception: " + ona
                         + " during deactivate_object() call on POA: " + );
            } catch (Exception ex) {
                throw new CorbaBindingException("Unable to deactivate CORBA servant"ex);
            }
        }
    }
    
    private void setCorbaLocArgs(URI addressURIList<StringorbArgs) {
        String schemeSpecificPart = addressURI.getSchemeSpecificPart();
        int keyIndex = schemeSpecificPart.indexOf('/');
        String corbaAddr = schemeSpecificPart.substring(0, keyIndex);
        String key = schemeSpecificPart.substring(keyIndex + 1);
        int index = corbaAddr.indexOf(':');
        String protocol = "iiop";
        if (index != 0) {
            protocol = corbaAddr.substring(0, index);
        }
        int oldIndex = index;
        index = corbaAddr.indexOf(':'oldIndex + 1);
        String host = corbaAddr.substring(oldIndex + 1, index);
        String port = corbaAddr.substring(index + 1);
        orbArgs.add("-ORB" + key + ":" + protocol + ":host");
        orbArgs.add(host);
        orbArgs.add("-ORB" + key + ":" + protocol + ":port");
        orbArgs.add(port);
        orbArgs.add("-ORBpoa:" +  + ":direct_persistent");
        orbArgs.add("true");
        orbArgs.add("-ORBpoa:" +  + ":well_known_address");
        orbArgs.add(key);
         = true;
         = key;
    }
    protected POA createPOA(String namePOA parentPOAPOAManager poaManager) {
        List<Policypolicies = new ArrayList<Policy>();
        policies.add(parentPOA
                .create_thread_policy(.));
        if () {
            policies.add(parentPOA
                    .create_lifespan_policy(.));
        } else {
            policies.add(parentPOA
                    .create_lifespan_policy(.));
        }
        if ( != null) {
            policies.add(parentPOA
                         .create_id_assignment_policy(.));
            
        }
        policies.add(parentPOA.create_request_processing_policy(value));        
        
        .addPOAPolicies(nameparentPOApoaManagerpolicies);
        
        Policy[] policyList = policies.toArray(new Policy[policies.size()]);
        try {
            return parentPOA.create_POA(namepoaManagerpolicyList);
        } catch (Exception ex) {
            throw new CorbaBindingException(
                    "Could not create POA during activation"ex);
        }
    }
        EndpointReferenceType ref = null;
        if ( == null) {
            throw new CorbaBindingException(
                 "getAddressWithId failed because the poa is null");
        }
        try {
            Servant servant = .id_to_servant();
            org.omg.CORBA.Object objRef 
                = .create_reference_with_id(id.getBytes(),
                                               servant._all_interfaces()[0]);
            AddressType addr = new AddressType();
            .exportObjectReference(objRef,
                                            .getLocation(),
                                            addr);
            ref = EndpointReferenceUtils.getEndpointReference(addr.getLocation());
            EndpointInfo ei = getEndPointInfo();
            if (ei.getService() != null) {
                EndpointReferenceUtils.setServiceAndPortName(refei.getService().getName(), 
                                                             ei.getName().getLocalPart());
            }
        } catch (Exception e) {
            throw new CorbaBindingException("Failed to getAddressWithId, reason:" + e.toString(), e);
        }
        return ref;
    }
    public String getId(Map<StringObjectcontextMap) {
        String id = null;
        try {
            Current currentPoa = (Current
                .resolve_initial_references("POACurrent");
            byte[] idBytes = currentPoa.get_object_id();
            id = new String(idBytes); //NOPMD
        } catch (Exception e) {
            throw new CorbaBindingException("Unable to getId, current is unavailable, reason: "
                                             + ee);
        }
        return id;
    }
New to GrepCode? Check out our FAQ X