src/share/jaxws_classes/com/sun/xml/internal/ws/api/server/Adapter.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial