Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » webservices » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.core.webservices;
   19   
   20   import org.apache.openejb.OpenEJBException;
   21   
   22   import javax.xml.namespace.QName;
   23   import java.util.Collections;
   24   import java.util.HashMap;
   25   import java.util.HashSet;
   26   import java.util.Map;
   27   import java.util.Set;
   28   import java.util.TreeMap;
   29   
   30   public class PortAddressRegistryImpl implements PortAddressRegistry {
   31       private Map<String, PortAddress> portsById = new TreeMap<String, PortAddress>();
   32       private Map<String, Map<String, PortAddress>> portsByInterface = new TreeMap<String, Map<String, PortAddress>>();
   33       private Map<String, Map<String, PortAddress>> portsByServiceId = new TreeMap<String, Map<String, PortAddress>>();
   34       private Map<QName, Map<String, PortAddress>> portsByServiceQName = new HashMap<QName, Map<String, PortAddress>>();
   35   
   36       public synchronized void addPort(String serviceId, QName serviceQName, String portId, QName portQName, String portInterface, String address) throws OpenEJBException {
   37           if (serviceId == null) throw new NullPointerException("serviceId is null");
   38           if (serviceQName == null) throw new NullPointerException("serviceQName is null");
   39           if (portId == null) throw new NullPointerException("portId is null");
   40           if (portQName == null) throw new NullPointerException("portQName is null");
   41           if (address == null) throw new NullPointerException("address is null");
   42   
   43           // create portAddress
   44           PortAddress portAddress = portsById.get(portId);
   45           if (portAddress != null) {
   46               throw new OpenEJBException("A webservice port with qname " + portAddress.getPortQName() + " is already registered to the portId " + portId);
   47           }
   48           portAddress = new PortAddress(portId, serviceQName, portQName, address, portInterface);
   49           portsById.put(portId, portAddress);
   50   
   51           // portsByInterface
   52           Map<String, PortAddress> ports = portsByInterface.get(portInterface);
   53           if (ports == null) {
   54               ports = new TreeMap<String, PortAddress>();
   55               portsByInterface.put(portInterface, ports);
   56           }
   57           ports.put(portId, portAddress);
   58   
   59   
   60           // portsByServiceId
   61           ports = portsByServiceId.get(serviceId);
   62           if (ports == null) {
   63               ports = new TreeMap<String, PortAddress>();
   64               portsByServiceId.put(serviceId, ports);
   65           }
   66           ports.put(portId, portAddress);
   67   
   68           // portsByServiceQName
   69           ports = portsByServiceQName.get(serviceQName);
   70           if (ports == null) {
   71               ports = new TreeMap<String, PortAddress>();
   72               portsByServiceQName.put(serviceQName, ports);
   73           }
   74           ports.put(portId, portAddress);
   75       }
   76   
   77       public synchronized void removePort(String serviceId, QName serviceQName, String portId) {
   78           if (serviceId == null) throw new NullPointerException("serviceId is null");
   79           if (serviceQName == null) throw new NullPointerException("serviceQName is null");
   80           if (portId == null) throw new NullPointerException("portId is null");
   81   
   82           // remove from portById
   83           PortAddress portAddress = portsById.remove(portId);
   84           if (portAddress != null) {
   85               // port was not registered
   86               return;
   87           }
   88   
   89           // remove from portsByInterface
   90           Map<String, PortAddress> ports = portsByInterface.get(serviceId);
   91           if (ports != null) {
   92               ports.remove(portId);
   93               if (ports.isEmpty()) {
   94                   portsByInterface.remove(serviceId);
   95               }
   96           }
   97   
   98           // remove from portsByServiceId
   99           ports = portsByServiceId.get(serviceId);
  100           if (ports != null) {
  101               ports.remove(portId);
  102               if (ports.isEmpty()) {
  103                   portsByServiceId.remove(serviceId);
  104               }
  105           }
  106   
  107           // remove from portsByServiceQName
  108           ports = portsByServiceQName.get(serviceQName);
  109           if (ports != null) {
  110               ports.remove(portId);
  111               if (ports.isEmpty()) {
  112                   portsByServiceId.remove(serviceId);
  113               }
  114           }
  115       }
  116   
  117       public synchronized Set<PortAddress> getPorts(String id, QName serviceQName, String referenceClassName) {
  118           if (serviceQName == null) throw new NullPointerException("serviceQName is null");
  119   
  120           // check if there is a port with the id
  121           if (id != null) {
  122               PortAddress portAddress = portsById.get(id);
  123               if (portAddress != null) {
  124                   return Collections.singleton(portAddress);
  125               }
  126           }
  127   
  128           // check if there is a unique port with the specifiec interface
  129           if (referenceClassName != null) {
  130               Map<String, PortAddress> interfacePorts = portsByInterface.get(referenceClassName);
  131               if (interfacePorts != null && interfacePorts.size() == 1) {
  132                   PortAddress portAddress = interfacePorts.values().iterator().next();
  133                   return Collections.singleton(portAddress);
  134               }
  135           }
  136   
  137           // find matching ports by id
  138           Map<String, PortAddress> ports = new TreeMap<String, PortAddress>();
  139           if (id != null) {
  140               Map<String, PortAddress> idPorts = portsByServiceId.get(id);
  141               if (idPorts != null) ports.putAll(idPorts);
  142           }
  143   
  144           // find matching ports  by serviceQName
  145           if (ports.isEmpty()) {
  146               Map<String, PortAddress> qnamePorts = portsByServiceQName.get(serviceQName);
  147               if (qnamePorts != null) ports.putAll(qnamePorts);
  148           }
  149   
  150           Set<PortAddress> portAddresses = new HashSet<PortAddress>(ports.values());
  151           return portAddresses;
  152       }
  153   
  154   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » webservices » [javadoc | source]