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

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 368
0989ad8c0860
child 408
b0610cd08440
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

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

mercurial