Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.server; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.addressing.EPRSDDocumentFilter; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.addressing.WSEPRExtension; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.Component; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.api.ComponentFeature; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.ComponentsFeature; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.api.WSBinding; |
aoqi@0 | 38 | import com.sun.xml.internal.ws.api.addressing.AddressingVersion; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; |
aoqi@0 | 40 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 41 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.api.model.SEIModel; |
aoqi@0 | 43 | import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; |
aoqi@0 | 44 | import com.sun.xml.internal.ws.api.pipe.*; |
aoqi@0 | 45 | import com.sun.xml.internal.ws.api.server.*; |
aoqi@0 | 46 | import com.sun.xml.internal.ws.binding.BindingImpl; |
aoqi@0 | 47 | import com.sun.xml.internal.ws.fault.SOAPFaultBuilder; |
aoqi@0 | 48 | import com.sun.xml.internal.ws.model.wsdl.WSDLDirectProperties; |
aoqi@0 | 49 | import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties; |
aoqi@0 | 50 | import com.sun.xml.internal.ws.model.wsdl.WSDLProperties; |
aoqi@0 | 51 | import com.sun.xml.internal.ws.policy.PolicyMap; |
aoqi@0 | 52 | import com.sun.xml.internal.ws.resources.HandlerMessages; |
aoqi@0 | 53 | import com.sun.xml.internal.ws.util.Pool; |
aoqi@0 | 54 | import com.sun.xml.internal.ws.util.Pool.TubePool; |
aoqi@0 | 55 | import com.sun.xml.internal.ws.util.ServiceFinder; |
aoqi@0 | 56 | import com.sun.xml.internal.ws.wsdl.OperationDispatcher; |
aoqi@0 | 57 | import com.sun.org.glassfish.gmbal.ManagedObjectManager; |
aoqi@0 | 58 | import org.w3c.dom.Element; |
aoqi@0 | 59 | |
aoqi@0 | 60 | import javax.annotation.PreDestroy; |
aoqi@0 | 61 | import javax.xml.namespace.QName; |
aoqi@0 | 62 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 63 | import javax.xml.ws.EndpointReference; |
aoqi@0 | 64 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 65 | import javax.xml.ws.handler.Handler; |
aoqi@0 | 66 | import java.lang.reflect.Method; |
aoqi@0 | 67 | import java.util.*; |
aoqi@0 | 68 | import java.util.concurrent.CopyOnWriteArraySet; |
aoqi@0 | 69 | import java.util.concurrent.Executor; |
aoqi@0 | 70 | import java.util.logging.Level; |
aoqi@0 | 71 | import java.util.logging.Logger; |
aoqi@0 | 72 | import javax.management.ObjectName; |
aoqi@0 | 73 | |
aoqi@0 | 74 | /** |
aoqi@0 | 75 | * {@link WSEndpoint} implementation. |
aoqi@0 | 76 | * |
aoqi@0 | 77 | * @author Kohsuke Kawaguchi |
aoqi@0 | 78 | * @author Jitendra Kotamraju |
aoqi@0 | 79 | */ |
aoqi@0 | 80 | public /*final*/ class WSEndpointImpl<T> extends WSEndpoint<T> implements LazyMOMProvider.WSEndpointScopeChangeListener { |
aoqi@0 | 81 | |
aoqi@0 | 82 | private static final Logger logger = Logger.getLogger(com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.endpoint"); |
aoqi@0 | 83 | |
aoqi@0 | 84 | private final @NotNull QName serviceName; |
aoqi@0 | 85 | private final @NotNull QName portName; |
aoqi@0 | 86 | protected final WSBinding binding; |
aoqi@0 | 87 | private final SEIModel seiModel; |
aoqi@0 | 88 | private final @NotNull Container container; |
aoqi@0 | 89 | private final WSDLPort port; |
aoqi@0 | 90 | |
aoqi@0 | 91 | protected final Tube masterTubeline; |
aoqi@0 | 92 | private final ServiceDefinitionImpl serviceDef; |
aoqi@0 | 93 | private final SOAPVersion soapVersion; |
aoqi@0 | 94 | private final Engine engine; |
aoqi@0 | 95 | private final @NotNull Codec masterCodec; |
aoqi@0 | 96 | private final @NotNull PolicyMap endpointPolicy; |
aoqi@0 | 97 | private final Pool<Tube> tubePool; |
aoqi@0 | 98 | private final OperationDispatcher operationDispatcher; |
aoqi@0 | 99 | private @NotNull ManagedObjectManager managedObjectManager; |
aoqi@0 | 100 | private boolean managedObjectManagerClosed = false; |
aoqi@0 | 101 | private final Object managedObjectManagerLock = new Object(); |
aoqi@0 | 102 | private LazyMOMProvider.Scope lazyMOMProviderScope = LazyMOMProvider.Scope.STANDALONE; |
aoqi@0 | 103 | private final @NotNull ServerTubeAssemblerContext context; |
aoqi@0 | 104 | |
aoqi@0 | 105 | private Map<QName, WSEndpointReference.EPRExtension> endpointReferenceExtensions = new HashMap<QName, WSEndpointReference.EPRExtension>(); |
aoqi@0 | 106 | /** |
aoqi@0 | 107 | * Set to true once we start shutting down this endpoint. Used to avoid |
aoqi@0 | 108 | * running the clean up processing twice. |
aoqi@0 | 109 | * |
aoqi@0 | 110 | * @see #dispose() |
aoqi@0 | 111 | */ |
aoqi@0 | 112 | private boolean disposed; |
aoqi@0 | 113 | private final Class<T> implementationClass; |
aoqi@0 | 114 | private final @NotNull |
aoqi@0 | 115 | WSDLProperties wsdlProperties; |
aoqi@0 | 116 | private final Set<Component> componentRegistry = new CopyOnWriteArraySet<Component>(); |
aoqi@0 | 117 | |
aoqi@0 | 118 | protected WSEndpointImpl(@NotNull QName serviceName, @NotNull QName portName, WSBinding binding, |
aoqi@0 | 119 | Container container, SEIModel seiModel, WSDLPort port, |
aoqi@0 | 120 | Class<T> implementationClass, |
aoqi@0 | 121 | @Nullable ServiceDefinitionImpl serviceDef, |
aoqi@0 | 122 | EndpointAwareTube terminalTube, boolean isSynchronous, |
aoqi@0 | 123 | PolicyMap endpointPolicy) { |
aoqi@0 | 124 | this.serviceName = serviceName; |
aoqi@0 | 125 | this.portName = portName; |
aoqi@0 | 126 | this.binding = binding; |
aoqi@0 | 127 | this.soapVersion = binding.getSOAPVersion(); |
aoqi@0 | 128 | this.container = container; |
aoqi@0 | 129 | this.port = port; |
aoqi@0 | 130 | this.implementationClass = implementationClass; |
aoqi@0 | 131 | this.serviceDef = serviceDef; |
aoqi@0 | 132 | this.seiModel = seiModel; |
aoqi@0 | 133 | this.endpointPolicy = endpointPolicy; |
aoqi@0 | 134 | |
aoqi@0 | 135 | LazyMOMProvider.INSTANCE.registerEndpoint(this); |
aoqi@0 | 136 | initManagedObjectManager(); |
aoqi@0 | 137 | |
aoqi@0 | 138 | if (serviceDef != null) { |
aoqi@0 | 139 | serviceDef.setOwner(this); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | ComponentFeature cf = binding.getFeature(ComponentFeature.class); |
aoqi@0 | 143 | if (cf != null) { |
aoqi@0 | 144 | switch (cf.getTarget()) { |
aoqi@0 | 145 | case ENDPOINT: |
aoqi@0 | 146 | componentRegistry.add(cf.getComponent()); |
aoqi@0 | 147 | break; |
aoqi@0 | 148 | case CONTAINER: |
aoqi@0 | 149 | container.getComponents().add(cf.getComponent()); |
aoqi@0 | 150 | break; |
aoqi@0 | 151 | default: |
aoqi@0 | 152 | throw new IllegalArgumentException(); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | } |
aoqi@0 | 155 | ComponentsFeature csf = binding.getFeature(ComponentsFeature.class); |
aoqi@0 | 156 | if (csf != null) { |
aoqi@0 | 157 | for (ComponentFeature cfi : csf.getComponentFeatures()) { |
aoqi@0 | 158 | switch (cfi.getTarget()) { |
aoqi@0 | 159 | case ENDPOINT: |
aoqi@0 | 160 | componentRegistry.add(cfi.getComponent()); |
aoqi@0 | 161 | break; |
aoqi@0 | 162 | case CONTAINER: |
aoqi@0 | 163 | container.getComponents().add(cfi.getComponent()); |
aoqi@0 | 164 | break; |
aoqi@0 | 165 | default: |
aoqi@0 | 166 | throw new IllegalArgumentException(); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | } |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | TubelineAssembler assembler = TubelineAssemblerFactory.create( |
aoqi@0 | 172 | Thread.currentThread().getContextClassLoader(), binding.getBindingId(), container); |
aoqi@0 | 173 | assert assembler != null; |
aoqi@0 | 174 | |
aoqi@0 | 175 | this.operationDispatcher = (port == null) ? null : new OperationDispatcher(port, binding, seiModel); |
aoqi@0 | 176 | |
aoqi@0 | 177 | context = createServerTubeAssemblerContext(terminalTube, isSynchronous); |
aoqi@0 | 178 | this.masterTubeline = assembler.createServer(context); |
aoqi@0 | 179 | |
aoqi@0 | 180 | Codec c = context.getCodec(); |
aoqi@0 | 181 | if (c instanceof EndpointAwareCodec) { |
aoqi@0 | 182 | // create a copy to avoid sharing the codec between multiple endpoints |
aoqi@0 | 183 | c = c.copy(); |
aoqi@0 | 184 | ((EndpointAwareCodec) c).setEndpoint(this); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | this.masterCodec = c; |
aoqi@0 | 187 | |
aoqi@0 | 188 | tubePool = new TubePool(masterTubeline); |
aoqi@0 | 189 | terminalTube.setEndpoint(this); |
aoqi@0 | 190 | engine = new Engine(toString(), container); |
aoqi@0 | 191 | wsdlProperties = (port == null) ? new WSDLDirectProperties(serviceName, portName, seiModel) : new WSDLPortProperties(port, seiModel); |
aoqi@0 | 192 | |
aoqi@0 | 193 | Map<QName, WSEndpointReference.EPRExtension> eprExtensions = new HashMap<QName, WSEndpointReference.EPRExtension>(); |
aoqi@0 | 194 | try { |
aoqi@0 | 195 | if (port != null) { |
aoqi@0 | 196 | //gather EPR extrensions from WSDL Model |
aoqi@0 | 197 | WSEndpointReference wsdlEpr = port.getEPR(); |
aoqi@0 | 198 | if (wsdlEpr != null) { |
aoqi@0 | 199 | for (WSEndpointReference.EPRExtension extnEl : wsdlEpr.getEPRExtensions()) { |
aoqi@0 | 200 | eprExtensions.put(extnEl.getQName(), extnEl); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | } |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | EndpointReferenceExtensionContributor[] eprExtnContributors = ServiceFinder.find(EndpointReferenceExtensionContributor.class).toArray(); |
aoqi@0 | 206 | for(EndpointReferenceExtensionContributor eprExtnContributor :eprExtnContributors) { |
aoqi@0 | 207 | WSEndpointReference.EPRExtension wsdlEPRExtn = eprExtensions.remove(eprExtnContributor.getQName()); |
aoqi@0 | 208 | WSEndpointReference.EPRExtension endpointEprExtn = eprExtnContributor.getEPRExtension(this,wsdlEPRExtn); |
aoqi@0 | 209 | if (endpointEprExtn != null) { |
aoqi@0 | 210 | eprExtensions.put(endpointEprExtn.getQName(), endpointEprExtn); |
aoqi@0 | 211 | } |
aoqi@0 | 212 | } |
aoqi@0 | 213 | for (WSEndpointReference.EPRExtension extn : eprExtensions.values()) { |
aoqi@0 | 214 | endpointReferenceExtensions.put(extn.getQName(), new WSEPRExtension( |
aoqi@0 | 215 | XMLStreamBuffer.createNewBufferFromXMLStreamReader(extn.readAsXMLStreamReader()),extn.getQName())); |
aoqi@0 | 216 | } |
aoqi@0 | 217 | } catch (XMLStreamException ex) { |
aoqi@0 | 218 | throw new WebServiceException(ex); |
aoqi@0 | 219 | } |
aoqi@0 | 220 | if(!eprExtensions.isEmpty()) { |
aoqi@0 | 221 | serviceDef.addFilter(new EPRSDDocumentFilter(this)); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | protected ServerTubeAssemblerContext createServerTubeAssemblerContext( |
aoqi@0 | 226 | EndpointAwareTube terminalTube, boolean isSynchronous) { |
aoqi@0 | 227 | ServerTubeAssemblerContext ctx = new ServerPipeAssemblerContext( |
aoqi@0 | 228 | seiModel, port, this, terminalTube, isSynchronous); |
aoqi@0 | 229 | return ctx; |
aoqi@0 | 230 | } |
aoqi@0 | 231 | |
aoqi@0 | 232 | protected WSEndpointImpl(@NotNull QName serviceName, @NotNull QName portName, WSBinding binding, Container container, |
aoqi@0 | 233 | SEIModel seiModel, WSDLPort port, |
aoqi@0 | 234 | Tube masterTubeline) { |
aoqi@0 | 235 | this.serviceName = serviceName; |
aoqi@0 | 236 | this.portName = portName; |
aoqi@0 | 237 | this.binding = binding; |
aoqi@0 | 238 | this.soapVersion = binding.getSOAPVersion(); |
aoqi@0 | 239 | this.container = container; |
aoqi@0 | 240 | this.endpointPolicy = null; |
aoqi@0 | 241 | this.port = port; |
aoqi@0 | 242 | this.seiModel = seiModel; |
aoqi@0 | 243 | this.serviceDef = null; |
aoqi@0 | 244 | this.implementationClass = null; |
aoqi@0 | 245 | this.masterTubeline = masterTubeline; |
aoqi@0 | 246 | this.masterCodec = ((BindingImpl) this.binding).createCodec(); |
aoqi@0 | 247 | |
aoqi@0 | 248 | LazyMOMProvider.INSTANCE.registerEndpoint(this); |
aoqi@0 | 249 | initManagedObjectManager(); |
aoqi@0 | 250 | |
aoqi@0 | 251 | this.operationDispatcher = (port == null) ? null : new OperationDispatcher(port, binding, seiModel); |
aoqi@0 | 252 | this.context = new ServerPipeAssemblerContext( |
aoqi@0 | 253 | seiModel, port, this, null /* not known */, false); |
aoqi@0 | 254 | |
aoqi@0 | 255 | tubePool = new TubePool(masterTubeline); |
aoqi@0 | 256 | engine = new Engine(toString(), container); |
aoqi@0 | 257 | wsdlProperties = (port == null) ? new WSDLDirectProperties(serviceName, portName, seiModel) : new WSDLPortProperties(port, seiModel); |
aoqi@0 | 258 | } |
aoqi@0 | 259 | |
aoqi@0 | 260 | public Collection<WSEndpointReference.EPRExtension> getEndpointReferenceExtensions() { |
aoqi@0 | 261 | return endpointReferenceExtensions.values(); |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | /** |
aoqi@0 | 265 | * Nullable when there is no associated WSDL Model |
aoqi@0 | 266 | * @return |
aoqi@0 | 267 | */ |
aoqi@0 | 268 | public @Nullable OperationDispatcher getOperationDispatcher() { |
aoqi@0 | 269 | return operationDispatcher; |
aoqi@0 | 270 | } |
aoqi@0 | 271 | |
aoqi@0 | 272 | public PolicyMap getPolicyMap() { |
aoqi@0 | 273 | return endpointPolicy; |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | public @NotNull Class<T> getImplementationClass() { |
aoqi@0 | 277 | return implementationClass; |
aoqi@0 | 278 | } |
aoqi@0 | 279 | |
aoqi@0 | 280 | public @NotNull WSBinding getBinding() { |
aoqi@0 | 281 | return binding; |
aoqi@0 | 282 | } |
aoqi@0 | 283 | |
aoqi@0 | 284 | public @NotNull Container getContainer() { |
aoqi@0 | 285 | return container; |
aoqi@0 | 286 | } |
aoqi@0 | 287 | |
aoqi@0 | 288 | public WSDLPort getPort() { |
aoqi@0 | 289 | return port; |
aoqi@0 | 290 | } |
aoqi@0 | 291 | |
aoqi@0 | 292 | @Override |
aoqi@0 | 293 | public @Nullable SEIModel getSEIModel() { |
aoqi@0 | 294 | return seiModel; |
aoqi@0 | 295 | } |
aoqi@0 | 296 | |
aoqi@0 | 297 | public void setExecutor(Executor exec) { |
aoqi@0 | 298 | engine.setExecutor(exec); |
aoqi@0 | 299 | } |
aoqi@0 | 300 | |
aoqi@0 | 301 | @Override |
aoqi@0 | 302 | public Engine getEngine() { |
aoqi@0 | 303 | return engine; |
aoqi@0 | 304 | } |
aoqi@0 | 305 | |
aoqi@0 | 306 | public void schedule(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor) { |
aoqi@0 | 307 | processAsync(request, callback, interceptor, true); |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | private void processAsync(final Packet request, |
aoqi@0 | 311 | final CompletionCallback callback, |
aoqi@0 | 312 | FiberContextSwitchInterceptor interceptor, boolean schedule) { |
aoqi@0 | 313 | Container old = ContainerResolver.getDefault().enterContainer(container); |
aoqi@0 | 314 | try { |
aoqi@0 | 315 | request.endpoint = WSEndpointImpl.this; |
aoqi@0 | 316 | request.addSatellite(wsdlProperties); |
aoqi@0 | 317 | |
aoqi@0 | 318 | Fiber fiber = engine.createFiber(); |
aoqi@0 | 319 | fiber.setDeliverThrowableInPacket(true); |
aoqi@0 | 320 | if (interceptor != null) { |
aoqi@0 | 321 | fiber.addInterceptor(interceptor); |
aoqi@0 | 322 | } |
aoqi@0 | 323 | final Tube tube = tubePool.take(); |
aoqi@0 | 324 | Fiber.CompletionCallback cbak = new Fiber.CompletionCallback() { |
aoqi@0 | 325 | public void onCompletion(@NotNull Packet response) { |
aoqi@0 | 326 | ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class); |
aoqi@0 | 327 | if (tc == null) { |
aoqi@0 | 328 | // Only recycle tubes in non-exception path as some Tubes may be |
aoqi@0 | 329 | // in invalid state following exception |
aoqi@0 | 330 | tubePool.recycle(tube); |
aoqi@0 | 331 | } |
aoqi@0 | 332 | |
aoqi@0 | 333 | if (callback != null) { |
aoqi@0 | 334 | if (tc != null) { |
aoqi@0 | 335 | response = createServiceResponseForException(tc, |
aoqi@0 | 336 | response, |
aoqi@0 | 337 | soapVersion, |
aoqi@0 | 338 | request.endpoint.getPort(), |
aoqi@0 | 339 | null, |
aoqi@0 | 340 | request.endpoint.getBinding()); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | callback.onCompletion(response); |
aoqi@0 | 343 | } |
aoqi@0 | 344 | } |
aoqi@0 | 345 | |
aoqi@0 | 346 | public void onCompletion(@NotNull Throwable error) { |
aoqi@0 | 347 | // will never be called now that we are using |
aoqi@0 | 348 | // fiber.setDeliverThrowableInPacket(true); |
aoqi@0 | 349 | throw new IllegalStateException(); |
aoqi@0 | 350 | } |
aoqi@0 | 351 | }; |
aoqi@0 | 352 | |
aoqi@0 | 353 | fiber.start(tube, request, cbak, |
aoqi@0 | 354 | binding.isFeatureEnabled(SyncStartForAsyncFeature.class) |
aoqi@0 | 355 | || !schedule); |
aoqi@0 | 356 | } finally { |
aoqi@0 | 357 | ContainerResolver.getDefault().exitContainer(old); |
aoqi@0 | 358 | } |
aoqi@0 | 359 | } |
aoqi@0 | 360 | |
aoqi@0 | 361 | @Override |
aoqi@0 | 362 | public Packet createServiceResponseForException(final ThrowableContainerPropertySet tc, |
aoqi@0 | 363 | final Packet responsePacket, |
aoqi@0 | 364 | final SOAPVersion soapVersion, |
aoqi@0 | 365 | final WSDLPort wsdlPort, |
aoqi@0 | 366 | final SEIModel seiModel, |
aoqi@0 | 367 | final WSBinding binding) |
aoqi@0 | 368 | { |
aoqi@0 | 369 | // This will happen in addressing if it is enabled. |
aoqi@0 | 370 | if (tc.isFaultCreated()) return responsePacket; |
aoqi@0 | 371 | |
aoqi@0 | 372 | final Message faultMessage = SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, tc.getThrowable()); |
aoqi@0 | 373 | final Packet result = responsePacket.createServerResponse(faultMessage, wsdlPort, seiModel, binding); |
aoqi@0 | 374 | // Pass info to upper layers |
aoqi@0 | 375 | tc.setFaultMessage(faultMessage); |
aoqi@0 | 376 | tc.setResponsePacket(responsePacket); |
aoqi@0 | 377 | tc.setFaultCreated(true); |
aoqi@0 | 378 | return result; |
aoqi@0 | 379 | } |
aoqi@0 | 380 | |
aoqi@0 | 381 | @Override |
aoqi@0 | 382 | public void process(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor) { |
aoqi@0 | 383 | processAsync(request, callback, interceptor, false); |
aoqi@0 | 384 | } |
aoqi@0 | 385 | |
aoqi@0 | 386 | public @NotNull |
aoqi@0 | 387 | PipeHead createPipeHead() { |
aoqi@0 | 388 | return new PipeHead() { |
aoqi@0 | 389 | private final Tube tube = TubeCloner.clone(masterTubeline); |
aoqi@0 | 390 | |
aoqi@0 | 391 | public @NotNull |
aoqi@0 | 392 | Packet process(Packet request, WebServiceContextDelegate wscd, |
aoqi@0 | 393 | TransportBackChannel tbc) { |
aoqi@0 | 394 | Container old = ContainerResolver.getDefault().enterContainer(container); |
aoqi@0 | 395 | try { |
aoqi@0 | 396 | request.webServiceContextDelegate = wscd; |
aoqi@0 | 397 | request.transportBackChannel = tbc; |
aoqi@0 | 398 | request.endpoint = WSEndpointImpl.this; |
aoqi@0 | 399 | request.addSatellite(wsdlProperties); |
aoqi@0 | 400 | |
aoqi@0 | 401 | Fiber fiber = engine.createFiber(); |
aoqi@0 | 402 | Packet response; |
aoqi@0 | 403 | try { |
aoqi@0 | 404 | response = fiber.runSync(tube, request); |
aoqi@0 | 405 | } catch (RuntimeException re) { |
aoqi@0 | 406 | // Catch all runtime exceptions so that transport |
aoqi@0 | 407 | // doesn't |
aoqi@0 | 408 | // have to worry about converting to wire message |
aoqi@0 | 409 | // TODO XML/HTTP binding |
aoqi@0 | 410 | Message faultMsg = SOAPFaultBuilder |
aoqi@0 | 411 | .createSOAPFaultMessage(soapVersion, null, re); |
aoqi@0 | 412 | response = request.createServerResponse(faultMsg, |
aoqi@0 | 413 | request.endpoint.getPort(), null, |
aoqi@0 | 414 | request.endpoint.getBinding()); |
aoqi@0 | 415 | } |
aoqi@0 | 416 | return response; |
aoqi@0 | 417 | } finally { |
aoqi@0 | 418 | ContainerResolver.getDefault().exitContainer(old); |
aoqi@0 | 419 | } |
aoqi@0 | 420 | } |
aoqi@0 | 421 | }; |
aoqi@0 | 422 | } |
aoqi@0 | 423 | |
aoqi@0 | 424 | public synchronized void dispose() { |
aoqi@0 | 425 | if (disposed) { |
aoqi@0 | 426 | return; |
aoqi@0 | 427 | } |
aoqi@0 | 428 | disposed = true; |
aoqi@0 | 429 | |
aoqi@0 | 430 | masterTubeline.preDestroy(); |
aoqi@0 | 431 | |
aoqi@0 | 432 | for (Handler handler : binding.getHandlerChain()) { |
aoqi@0 | 433 | for (Method method : handler.getClass().getMethods()) { |
aoqi@0 | 434 | if (method.getAnnotation(PreDestroy.class) == null) { |
aoqi@0 | 435 | continue; |
aoqi@0 | 436 | } |
aoqi@0 | 437 | try { |
aoqi@0 | 438 | method.invoke(handler); |
aoqi@0 | 439 | } catch (Exception e) { |
aoqi@0 | 440 | logger.log(Level.WARNING, HandlerMessages.HANDLER_PREDESTROY_IGNORE(e.getMessage()), e); |
aoqi@0 | 441 | } |
aoqi@0 | 442 | break; |
aoqi@0 | 443 | } |
aoqi@0 | 444 | } |
aoqi@0 | 445 | closeManagedObjectManager(); |
aoqi@0 | 446 | LazyMOMProvider.INSTANCE.unregisterEndpoint(this); |
aoqi@0 | 447 | } |
aoqi@0 | 448 | |
aoqi@0 | 449 | public ServiceDefinitionImpl getServiceDefinition() { |
aoqi@0 | 450 | return serviceDef; |
aoqi@0 | 451 | } |
aoqi@0 | 452 | |
aoqi@0 | 453 | public Set<EndpointComponent> getComponentRegistry() { |
aoqi@0 | 454 | Set<EndpointComponent> sec = new EndpointComponentSet(); |
aoqi@0 | 455 | for (Component c : componentRegistry) { |
aoqi@0 | 456 | sec.add(c instanceof EndpointComponentWrapper ? |
aoqi@0 | 457 | ((EndpointComponentWrapper) c).component : |
aoqi@0 | 458 | new ComponentWrapper(c)); |
aoqi@0 | 459 | } |
aoqi@0 | 460 | return sec; |
aoqi@0 | 461 | } |
aoqi@0 | 462 | |
aoqi@0 | 463 | private class EndpointComponentSet extends HashSet<EndpointComponent> { |
aoqi@0 | 464 | |
aoqi@0 | 465 | @Override |
aoqi@0 | 466 | public Iterator<EndpointComponent> iterator() { |
aoqi@0 | 467 | final Iterator<EndpointComponent> it = super.iterator(); |
aoqi@0 | 468 | return new Iterator<EndpointComponent>() { |
aoqi@0 | 469 | private EndpointComponent last = null; |
aoqi@0 | 470 | |
aoqi@0 | 471 | public boolean hasNext() { |
aoqi@0 | 472 | return it.hasNext(); |
aoqi@0 | 473 | } |
aoqi@0 | 474 | |
aoqi@0 | 475 | public EndpointComponent next() { |
aoqi@0 | 476 | last = it.next(); |
aoqi@0 | 477 | return last; |
aoqi@0 | 478 | } |
aoqi@0 | 479 | |
aoqi@0 | 480 | public void remove() { |
aoqi@0 | 481 | it.remove(); |
aoqi@0 | 482 | if (last != null) { |
aoqi@0 | 483 | componentRegistry.remove(last instanceof ComponentWrapper ? |
aoqi@0 | 484 | ((ComponentWrapper) last).component : |
aoqi@0 | 485 | new EndpointComponentWrapper(last)); |
aoqi@0 | 486 | } |
aoqi@0 | 487 | last = null; |
aoqi@0 | 488 | } |
aoqi@0 | 489 | }; |
aoqi@0 | 490 | } |
aoqi@0 | 491 | |
aoqi@0 | 492 | @Override |
aoqi@0 | 493 | public boolean add(EndpointComponent e) { |
aoqi@0 | 494 | boolean result = super.add(e); |
aoqi@0 | 495 | if (result) { |
aoqi@0 | 496 | componentRegistry.add(new EndpointComponentWrapper(e)); |
aoqi@0 | 497 | } |
aoqi@0 | 498 | return result; |
aoqi@0 | 499 | } |
aoqi@0 | 500 | |
aoqi@0 | 501 | @Override |
aoqi@0 | 502 | public boolean remove(Object o) { |
aoqi@0 | 503 | boolean result = super.remove(o); |
aoqi@0 | 504 | if (result) { |
aoqi@0 | 505 | componentRegistry.remove(o instanceof ComponentWrapper ? |
aoqi@0 | 506 | ((ComponentWrapper) o).component : |
aoqi@0 | 507 | new EndpointComponentWrapper((EndpointComponent)o)); |
aoqi@0 | 508 | } |
aoqi@0 | 509 | return result; |
aoqi@0 | 510 | } |
aoqi@0 | 511 | |
aoqi@0 | 512 | } |
aoqi@0 | 513 | |
aoqi@0 | 514 | private static class ComponentWrapper implements EndpointComponent { |
aoqi@0 | 515 | private final Component component; |
aoqi@0 | 516 | |
aoqi@0 | 517 | public ComponentWrapper(Component component) { |
aoqi@0 | 518 | this.component = component; |
aoqi@0 | 519 | } |
aoqi@0 | 520 | |
aoqi@0 | 521 | public <S> S getSPI(Class<S> spiType) { |
aoqi@0 | 522 | return component.getSPI(spiType); |
aoqi@0 | 523 | } |
aoqi@0 | 524 | |
aoqi@0 | 525 | @Override |
aoqi@0 | 526 | public int hashCode() { |
aoqi@0 | 527 | return component.hashCode(); |
aoqi@0 | 528 | } |
aoqi@0 | 529 | |
aoqi@0 | 530 | @Override |
aoqi@0 | 531 | public boolean equals(Object obj) { |
aoqi@0 | 532 | return component.equals(obj); |
aoqi@0 | 533 | } |
aoqi@0 | 534 | } |
aoqi@0 | 535 | |
aoqi@0 | 536 | private static class EndpointComponentWrapper implements Component { |
aoqi@0 | 537 | private final EndpointComponent component; |
aoqi@0 | 538 | |
aoqi@0 | 539 | public EndpointComponentWrapper(EndpointComponent component) { |
aoqi@0 | 540 | this.component = component; |
aoqi@0 | 541 | } |
aoqi@0 | 542 | |
aoqi@0 | 543 | public <S> S getSPI(Class<S> spiType) { |
aoqi@0 | 544 | return component.getSPI(spiType); |
aoqi@0 | 545 | } |
aoqi@0 | 546 | |
aoqi@0 | 547 | @Override |
aoqi@0 | 548 | public int hashCode() { |
aoqi@0 | 549 | return component.hashCode(); |
aoqi@0 | 550 | } |
aoqi@0 | 551 | |
aoqi@0 | 552 | @Override |
aoqi@0 | 553 | public boolean equals(Object obj) { |
aoqi@0 | 554 | return component.equals(obj); |
aoqi@0 | 555 | } |
aoqi@0 | 556 | } |
aoqi@0 | 557 | |
aoqi@0 | 558 | @Override |
aoqi@0 | 559 | public @NotNull Set<Component> getComponents() { |
aoqi@0 | 560 | return componentRegistry; |
aoqi@0 | 561 | } |
aoqi@0 | 562 | |
aoqi@0 | 563 | public <T extends EndpointReference> T getEndpointReference(Class<T> clazz, String address, String wsdlAddress, Element... referenceParameters) { |
aoqi@0 | 564 | List<Element> refParams = null; |
aoqi@0 | 565 | if (referenceParameters != null) { |
aoqi@0 | 566 | refParams = Arrays.asList(referenceParameters); |
aoqi@0 | 567 | } |
aoqi@0 | 568 | return getEndpointReference(clazz, address, wsdlAddress, null, refParams); |
aoqi@0 | 569 | } |
aoqi@0 | 570 | |
aoqi@0 | 571 | public <T extends EndpointReference> T getEndpointReference(Class<T> clazz, |
aoqi@0 | 572 | String address, String wsdlAddress, List<Element> metadata, |
aoqi@0 | 573 | List<Element> referenceParameters) { |
aoqi@0 | 574 | QName portType = null; |
aoqi@0 | 575 | if (port != null) { |
aoqi@0 | 576 | portType = port.getBinding().getPortTypeName(); |
aoqi@0 | 577 | } |
aoqi@0 | 578 | |
aoqi@0 | 579 | AddressingVersion av = AddressingVersion.fromSpecClass(clazz); |
aoqi@0 | 580 | return new WSEndpointReference( |
aoqi@0 | 581 | av, address, serviceName, portName, portType, metadata, wsdlAddress, referenceParameters, endpointReferenceExtensions.values(), null).toSpec(clazz); |
aoqi@0 | 582 | |
aoqi@0 | 583 | } |
aoqi@0 | 584 | |
aoqi@0 | 585 | public @NotNull QName getPortName() { |
aoqi@0 | 586 | return portName; |
aoqi@0 | 587 | } |
aoqi@0 | 588 | |
aoqi@0 | 589 | |
aoqi@0 | 590 | public @NotNull Codec createCodec() { |
aoqi@0 | 591 | return masterCodec.copy(); |
aoqi@0 | 592 | } |
aoqi@0 | 593 | |
aoqi@0 | 594 | public @NotNull QName getServiceName() { |
aoqi@0 | 595 | return serviceName; |
aoqi@0 | 596 | } |
aoqi@0 | 597 | |
aoqi@0 | 598 | private void initManagedObjectManager() { |
aoqi@0 | 599 | synchronized (managedObjectManagerLock) { |
aoqi@0 | 600 | if (managedObjectManager == null) { |
aoqi@0 | 601 | switch (this.lazyMOMProviderScope) { |
aoqi@0 | 602 | case GLASSFISH_NO_JMX: |
aoqi@0 | 603 | managedObjectManager = new WSEndpointMOMProxy(this); |
aoqi@0 | 604 | break; |
aoqi@0 | 605 | default: |
aoqi@0 | 606 | managedObjectManager = obtainManagedObjectManager(); |
aoqi@0 | 607 | } |
aoqi@0 | 608 | } |
aoqi@0 | 609 | } |
aoqi@0 | 610 | } |
aoqi@0 | 611 | |
aoqi@0 | 612 | public @NotNull ManagedObjectManager getManagedObjectManager() { |
aoqi@0 | 613 | return managedObjectManager; |
aoqi@0 | 614 | } |
aoqi@0 | 615 | |
aoqi@0 | 616 | /** |
aoqi@0 | 617 | * Obtains a real instance of {@code ManagedObjectManager} class no matter what lazyMOMProviderScope is this endpoint in (or if the |
aoqi@0 | 618 | * Gmbal API calls should be deferred). |
aoqi@0 | 619 | * |
aoqi@0 | 620 | * @see com.sun.xml.internal.ws.api.server.LazyMOMProvider.Scope |
aoqi@0 | 621 | * @return an instance of {@code ManagedObjectManager} |
aoqi@0 | 622 | */ |
aoqi@0 | 623 | @NotNull ManagedObjectManager obtainManagedObjectManager() { |
aoqi@0 | 624 | final MonitorRootService monitorRootService = new MonitorRootService(this); |
aoqi@0 | 625 | final ManagedObjectManager mOM = monitorRootService.createManagedObjectManager(this); |
aoqi@0 | 626 | |
aoqi@0 | 627 | // ManagedObjectManager was suspended due to root creation (see MonitorBase#initMOM) |
aoqi@0 | 628 | mOM.resumeJMXRegistration(); |
aoqi@0 | 629 | |
aoqi@0 | 630 | return mOM; |
aoqi@0 | 631 | } |
aoqi@0 | 632 | |
aoqi@0 | 633 | public void scopeChanged(LazyMOMProvider.Scope scope) { |
aoqi@0 | 634 | synchronized (managedObjectManagerLock) { |
aoqi@0 | 635 | if (managedObjectManagerClosed) { |
aoqi@0 | 636 | return; |
aoqi@0 | 637 | } |
aoqi@0 | 638 | |
aoqi@0 | 639 | this.lazyMOMProviderScope = scope; |
aoqi@0 | 640 | |
aoqi@0 | 641 | // possible lazyMOMProviderScope change can be LazyMOMProvider.Scope.GLASSFISH_NO_JMX or LazyMOMProvider.Scope.GLASSFISH_JMX |
aoqi@0 | 642 | if (managedObjectManager == null) { |
aoqi@0 | 643 | if (scope != LazyMOMProvider.Scope.GLASSFISH_NO_JMX) { |
aoqi@0 | 644 | managedObjectManager = obtainManagedObjectManager(); |
aoqi@0 | 645 | } else { |
aoqi@0 | 646 | managedObjectManager = new WSEndpointMOMProxy(this); |
aoqi@0 | 647 | } |
aoqi@0 | 648 | } else { |
aoqi@0 | 649 | // if ManagedObjectManager for this endpoint has already been created and is uninitialized proxy then |
aoqi@0 | 650 | // fill it with a real instance |
aoqi@0 | 651 | if (managedObjectManager instanceof WSEndpointMOMProxy |
aoqi@0 | 652 | && !((WSEndpointMOMProxy)managedObjectManager).isInitialized()) { |
aoqi@0 | 653 | ((WSEndpointMOMProxy)managedObjectManager).setManagedObjectManager(obtainManagedObjectManager()); |
aoqi@0 | 654 | } |
aoqi@0 | 655 | } |
aoqi@0 | 656 | } |
aoqi@0 | 657 | } |
aoqi@0 | 658 | |
aoqi@0 | 659 | private static final Logger monitoringLogger = Logger.getLogger(com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".monitoring"); |
aoqi@0 | 660 | |
aoqi@0 | 661 | // This can be called independently of WSEndpoint.dispose. |
aoqi@0 | 662 | // Example: the WSCM framework calls this before dispose. |
aoqi@0 | 663 | @Override |
aoqi@0 | 664 | public void closeManagedObjectManager() { |
aoqi@0 | 665 | synchronized (managedObjectManagerLock) { |
aoqi@0 | 666 | if (managedObjectManagerClosed == true) { |
aoqi@0 | 667 | return; |
aoqi@0 | 668 | } |
aoqi@0 | 669 | if (managedObjectManager != null) { |
aoqi@0 | 670 | boolean close = true; |
aoqi@0 | 671 | |
aoqi@0 | 672 | // ManagedObjectManager doesn't need to be closed because it exists only as a proxy |
aoqi@0 | 673 | if (managedObjectManager instanceof WSEndpointMOMProxy |
aoqi@0 | 674 | && !((WSEndpointMOMProxy)managedObjectManager).isInitialized()) { |
aoqi@0 | 675 | close = false; |
aoqi@0 | 676 | } |
aoqi@0 | 677 | |
aoqi@0 | 678 | if (close) { |
aoqi@0 | 679 | try { |
aoqi@0 | 680 | final ObjectName name = managedObjectManager.getObjectName(managedObjectManager.getRoot()); |
aoqi@0 | 681 | // The name is null when the MOM is a NOOP. |
aoqi@0 | 682 | if (name != null) { |
aoqi@0 | 683 | monitoringLogger.log(Level.INFO, "Closing Metro monitoring root: {0}", name); |
aoqi@0 | 684 | } |
aoqi@0 | 685 | managedObjectManager.close(); |
aoqi@0 | 686 | } catch (java.io.IOException e) { |
aoqi@0 | 687 | monitoringLogger.log(Level.WARNING, "Ignoring error when closing Managed Object Manager", e); |
aoqi@0 | 688 | } |
aoqi@0 | 689 | } |
aoqi@0 | 690 | } |
aoqi@0 | 691 | managedObjectManagerClosed = true; |
aoqi@0 | 692 | } |
aoqi@0 | 693 | } |
aoqi@0 | 694 | |
aoqi@0 | 695 | public @NotNull @Override ServerTubeAssemblerContext getAssemblerContext() { |
aoqi@0 | 696 | return context; |
aoqi@0 | 697 | } |
aoqi@0 | 698 | } |