Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.api.server; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.api.config.management.Reconfigurable; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.api.Component; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.pipe.Codec; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.server.WSEndpoint.PipeHead; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.util.Pool; |
aoqi@0 | 34 | |
aoqi@0 | 35 | /** |
aoqi@0 | 36 | * Receives incoming messages from a transport (such as HTTP, JMS, etc) |
aoqi@0 | 37 | * in a transport specific way, and delivers it to {@link WSEndpoint.PipeHead#process}. |
aoqi@0 | 38 | * |
aoqi@0 | 39 | * <p> |
aoqi@0 | 40 | * Since this class mostly concerns itself with converting a |
aoqi@0 | 41 | * transport-specific message representation to a {@link Packet}, |
aoqi@0 | 42 | * the name is the "adapter". |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * <p> |
aoqi@0 | 45 | * The purpose of this class is twofolds: |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * <ol> |
aoqi@0 | 48 | * <li> |
aoqi@0 | 49 | * To hide the logic of converting a transport-specific connection |
aoqi@0 | 50 | * to a {@link Packet} and do the other way around. |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * <li> |
aoqi@0 | 53 | * To manage thread-unsafe resources, such as {@link WSEndpoint.PipeHead}, |
aoqi@0 | 54 | * and {@link Codec}. |
aoqi@0 | 55 | * </ol> |
aoqi@0 | 56 | * |
aoqi@0 | 57 | * <p> |
aoqi@0 | 58 | * {@link Adapter}s are extended to work with each kind of transport, |
aoqi@0 | 59 | * and therefore {@link Adapter} class itself is not all that |
aoqi@0 | 60 | * useful by itself --- it merely provides a design template |
aoqi@0 | 61 | * that can be followed. |
aoqi@0 | 62 | * |
aoqi@0 | 63 | * <p> |
aoqi@0 | 64 | * For managing resources, an adapter uses an object called {@link Toolkit} |
aoqi@0 | 65 | * (think of it as a tray full of tools that a dentist uses --- |
aoqi@0 | 66 | * trays are identical, but each patient has to get one. You have |
aoqi@0 | 67 | * a pool of them and you assign it to a patient.) |
aoqi@0 | 68 | * |
aoqi@0 | 69 | * {@link Adapter.Toolkit} can be extended by derived classes. |
aoqi@0 | 70 | * That actual type is the {@code TK} type parameter this class takes. |
aoqi@0 | 71 | * |
aoqi@0 | 72 | * @author Kohsuke Kawaguchi |
aoqi@0 | 73 | */ |
aoqi@0 | 74 | public abstract class Adapter<TK extends Adapter.Toolkit> |
aoqi@0 | 75 | implements Reconfigurable, Component { |
aoqi@0 | 76 | |
aoqi@0 | 77 | protected final WSEndpoint<?> endpoint; |
aoqi@0 | 78 | |
aoqi@0 | 79 | /** |
aoqi@0 | 80 | * Object that groups all thread-unsafe resources. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public class Toolkit { |
aoqi@0 | 83 | /** |
aoqi@0 | 84 | * For encoding/decoding infoset to/from the byte stream. |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | public final Codec codec; |
aoqi@0 | 87 | /** |
aoqi@0 | 88 | * This object from {@link WSEndpoint} serves the request. |
aoqi@0 | 89 | */ |
aoqi@0 | 90 | public final PipeHead head; |
aoqi@0 | 91 | |
aoqi@0 | 92 | public Toolkit() { |
aoqi@0 | 93 | this.codec = endpoint.createCodec(); |
aoqi@0 | 94 | this.head = endpoint.createPipeHead(); |
aoqi@0 | 95 | } |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** |
aoqi@0 | 99 | * Pool of {@link Toolkit}s. |
aoqi@0 | 100 | * |
aoqi@0 | 101 | * Instances of this pool may be replaced at runtime. Therefore, when you take |
aoqi@0 | 102 | * an object out of the pool, you must make sure that it is recycled by the |
aoqi@0 | 103 | * same instance of the pool. |
aoqi@0 | 104 | */ |
aoqi@0 | 105 | protected volatile Pool<TK> pool = new Pool<TK>() { |
aoqi@0 | 106 | protected TK create() { |
aoqi@0 | 107 | return createToolkit(); |
aoqi@0 | 108 | } |
aoqi@0 | 109 | }; |
aoqi@0 | 110 | |
aoqi@0 | 111 | /** |
aoqi@0 | 112 | * Creates an {@link Adapter} that delivers |
aoqi@0 | 113 | * messages to the given endpoint. |
aoqi@0 | 114 | */ |
aoqi@0 | 115 | protected Adapter(WSEndpoint endpoint) { |
aoqi@0 | 116 | assert endpoint!=null; |
aoqi@0 | 117 | this.endpoint = endpoint; |
aoqi@0 | 118 | // Enables other components to reconfigure this adapter |
aoqi@0 | 119 | endpoint.getComponents().add(getEndpointComponent()); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | protected Component getEndpointComponent() { |
aoqi@0 | 123 | return new Component() { |
aoqi@0 | 124 | public <S> S getSPI(Class<S> spiType) { |
aoqi@0 | 125 | if (spiType.isAssignableFrom(Reconfigurable.class)) { |
aoqi@0 | 126 | return spiType.cast(Adapter.this); |
aoqi@0 | 127 | } |
aoqi@0 | 128 | return null; |
aoqi@0 | 129 | } |
aoqi@0 | 130 | }; |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | /** |
aoqi@0 | 134 | * The pool instance needs to be recreated to prevent reuse of old Toolkit instances. |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | public void reconfigure() { |
aoqi@0 | 137 | this.pool = new Pool<TK>() { |
aoqi@0 | 138 | protected TK create() { |
aoqi@0 | 139 | return createToolkit(); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | }; |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | public <S> S getSPI(Class<S> spiType) { |
aoqi@0 | 145 | if (spiType.isAssignableFrom(Reconfigurable.class)) { |
aoqi@0 | 146 | return spiType.cast(this); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | if (endpoint != null) { |
aoqi@0 | 149 | return endpoint.getSPI(spiType); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | return null; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | /** |
aoqi@0 | 155 | * Gets the endpoint that this {@link Adapter} is serving. |
aoqi@0 | 156 | * |
aoqi@0 | 157 | * @return |
aoqi@0 | 158 | * always non-null. |
aoqi@0 | 159 | */ |
aoqi@0 | 160 | public WSEndpoint<?> getEndpoint() { |
aoqi@0 | 161 | return endpoint; |
aoqi@0 | 162 | } |
aoqi@0 | 163 | |
aoqi@0 | 164 | /** |
aoqi@0 | 165 | * Returns a reference to the pool of Toolkits for this adapter. |
aoqi@0 | 166 | * |
aoqi@0 | 167 | * The pool may be recreated during runtime reconfiguration and this method |
aoqi@0 | 168 | * will then return a reference to a new instance. When you recycle a toolkit, |
aoqi@0 | 169 | * you must make sure that you return it to the same pool instance that you |
aoqi@0 | 170 | * took it from. |
aoqi@0 | 171 | * |
aoqi@0 | 172 | * @return |
aoqi@0 | 173 | */ |
aoqi@0 | 174 | protected Pool<TK> getPool() { |
aoqi@0 | 175 | return pool; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | /** |
aoqi@0 | 179 | * Creates a {@link Toolkit} instance. |
aoqi@0 | 180 | * |
aoqi@0 | 181 | * <p> |
aoqi@0 | 182 | * If the derived class doesn't have to add any per-thread state |
aoqi@0 | 183 | * to {@link Toolkit}, simply implement this as {@code new Toolkit()}. |
aoqi@0 | 184 | */ |
aoqi@0 | 185 | protected abstract TK createToolkit(); |
aoqi@0 | 186 | } |