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.api.server;
  27 
  28 import java.util.Collection;
  29 import java.util.Collections;
  30 import java.util.Set;
  31 import java.util.concurrent.CopyOnWriteArraySet;
  32 
  33 import com.sun.istack.internal.NotNull;
  34 import com.sun.xml.internal.ws.api.Component;
  35 import com.sun.xml.internal.ws.api.ComponentEx;
  36 import com.sun.xml.internal.ws.api.ComponentRegistry;
  37 
  38 /**
  39  * Root of the SPI implemented by the container
  40  * (such as application server.)
  41  *
  42  * <p>
  43  * Often technologies that are built on top of JAX-WS
  44  * (such as Tango) needs to negotiate private contracts between
  45  * them and the container. This interface allows such technologies
  46  * to query the negotiated SPI by using the {@link #getSPI(Class)}.
  47  *
  48  * <p>
  49  * For example, if a security pipe needs to get some information
  50  * from a container, they can do the following:
  51  * <ol>
  52  *  <li>Negotiate an interface with the container and define it.
  53  *      (let's call it {@code ContainerSecuritySPI}.)
  54  *  <li>The container will implement {@code ContainerSecuritySPI}.
  55  *  <li>At the runtime, a security pipe gets
  56  *      {@link WSEndpoint} and then to {@link Container}.
  57  *  <li>It calls {@code container.getSPI(ContainerSecuritySPI.class)}
  58  *  <li>The container returns an instance of {@code ContainerSecuritySPI}.
  59  *  <li>The security pipe talks to the container through this SPI.
  60  * </ol>
  61  *
  62  * <p>
  63  * This protects JAX-WS from worrying about the details of such contracts,
  64  * while still providing the necessary service of hooking up those parties.
  65  *
  66  * <p>
  67  * Technologies that run inside JAX-WS server runtime can access this object through
  68  * {@link WSEndpoint#getContainer()}. In the client runtime, it can be accessed from
  69  * {@link ContainerResolver#getContainer()}
  70  *
  71  * @author Kohsuke Kawaguchi
  72  * @see WSEndpoint
  73  */
  74 public abstract class Container implements ComponentRegistry, ComponentEx {
  75         private final Set<Component> components = new CopyOnWriteArraySet<Component>();
  76 
  77     /**
  78      * For derived classes.
  79      */
  80     protected Container() {
  81     }
  82 
  83     /**
  84      * Constant that represents a "no {@link Container}",
  85      * which always returns null from {@link #getSPI(Class)}.
  86      */
  87     public static final Container NONE = new NoneContainer();
  88 
  89     private static final class NoneContainer extends Container {
  90     }
  91 
  92     public <S> S getSPI(Class<S> spiType) {
  93         if (components == null) return null;
  94         for (Component c : components) {
  95                 S s = c.getSPI(spiType);
  96                 if (s != null)
  97                         return s;
  98         }
  99         return null;
 100     }
 101 
 102         public Set<Component> getComponents() {
 103                 return components;
 104         }
 105 
 106         public @NotNull <E> Iterable<E> getIterableSPI(Class<E> spiType) {
 107         E item = getSPI(spiType);
 108         if (item != null) {
 109                 Collection<E> c = Collections.singletonList(item);
 110                 return c;
 111         }
 112         return Collections.emptySet();
 113     }
 114 }