1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.server;
  27 
  28 import com.sun.istack.internal.NotNull;
  29 import com.sun.xml.internal.ws.api.server.SDDocument;
  30 import com.sun.xml.internal.ws.api.server.SDDocumentFilter;
  31 import com.sun.xml.internal.ws.api.server.ServiceDefinition;
  32 import com.sun.xml.internal.ws.wsdl.SDDocumentResolver;
  33 
  34 import java.util.ArrayList;
  35 import java.util.HashMap;
  36 import java.util.Iterator;
  37 import java.util.List;
  38 import java.util.Map;
  39 
  40 /**
  41  * {@link ServiceDefinition} implementation.
  42  *
  43  * <p>
  44  * You construct a {@link ServiceDefinitionImpl} by first constructing
  45  * a list of {@link SDDocumentImpl}s.
  46  *
  47  * @author Kohsuke Kawaguchi
  48  */
  49 public final class ServiceDefinitionImpl implements ServiceDefinition, SDDocumentResolver {
  50     private final List<SDDocumentImpl> docs;
  51 
  52     private final Map<String,SDDocumentImpl> bySystemId;
  53     private final @NotNull SDDocumentImpl primaryWsdl;
  54 
  55     /**
  56      * Set when {@link WSEndpointImpl} is created.
  57      */
  58     /*package*/ WSEndpointImpl<?> owner;
  59 
  60     /*package*/ final List<SDDocumentFilter> filters = new ArrayList<SDDocumentFilter>();
  61 
  62     /**
  63      * @param docs
  64      *      List of {@link SDDocumentImpl}s to form the description.
  65      *      There must be at least one entry.
  66      *      The first document is considered {@link #getPrimary() primary}.
  67      */
  68     public ServiceDefinitionImpl(List<SDDocumentImpl> docs, @NotNull SDDocumentImpl primaryWsdl) {
  69         assert docs.contains(primaryWsdl);
  70         this.docs = docs;
  71         this.primaryWsdl = primaryWsdl;
  72 
  73         this.bySystemId = new HashMap<String, SDDocumentImpl>(docs.size());
  74         for (SDDocumentImpl doc : docs) {
  75             bySystemId.put(doc.getURL().toExternalForm(),doc);
  76             doc.setFilters(filters);
  77             doc.setResolver(this);
  78         }
  79     }
  80 
  81     /**
  82      * The owner is set when {@link WSEndpointImpl} is created.
  83      */
  84     /*package*/ void setOwner(WSEndpointImpl<?> owner) {
  85         assert owner!=null && this.owner==null;
  86         this.owner = owner;
  87     }
  88 
  89     public @NotNull SDDocument getPrimary() {
  90         return primaryWsdl;
  91     }
  92 
  93     public void addFilter(SDDocumentFilter filter) {
  94         filters.add(filter);
  95     }
  96 
  97     public Iterator<SDDocument> iterator() {
  98         return (Iterator)docs.iterator();
  99     }
 100 
 101     /**
 102      * Gets the {@link SDDocumentImpl} whose {@link SDDocumentImpl#getURL()}
 103      * returns the specified value.
 104      *
 105      * @return
 106      *      null if none is found.
 107      */
 108     public SDDocument resolve(String systemId) {
 109         return bySystemId.get(systemId);
 110     }
 111 }