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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/server/WSEndpointImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,698 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.server;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.Nullable;
    1.33 +import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
    1.34 +import com.sun.xml.internal.ws.addressing.EPRSDDocumentFilter;
    1.35 +import com.sun.xml.internal.ws.addressing.WSEPRExtension;
    1.36 +import com.sun.xml.internal.ws.api.Component;
    1.37 +import com.sun.xml.internal.ws.api.ComponentFeature;
    1.38 +import com.sun.xml.internal.ws.api.ComponentsFeature;
    1.39 +import com.sun.xml.internal.ws.api.SOAPVersion;
    1.40 +import com.sun.xml.internal.ws.api.WSBinding;
    1.41 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    1.42 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
    1.43 +import com.sun.xml.internal.ws.api.message.Message;
    1.44 +import com.sun.xml.internal.ws.api.message.Packet;
    1.45 +import com.sun.xml.internal.ws.api.model.SEIModel;
    1.46 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    1.47 +import com.sun.xml.internal.ws.api.pipe.*;
    1.48 +import com.sun.xml.internal.ws.api.server.*;
    1.49 +import com.sun.xml.internal.ws.binding.BindingImpl;
    1.50 +import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
    1.51 +import com.sun.xml.internal.ws.model.wsdl.WSDLDirectProperties;
    1.52 +import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties;
    1.53 +import com.sun.xml.internal.ws.model.wsdl.WSDLProperties;
    1.54 +import com.sun.xml.internal.ws.policy.PolicyMap;
    1.55 +import com.sun.xml.internal.ws.resources.HandlerMessages;
    1.56 +import com.sun.xml.internal.ws.util.Pool;
    1.57 +import com.sun.xml.internal.ws.util.Pool.TubePool;
    1.58 +import com.sun.xml.internal.ws.util.ServiceFinder;
    1.59 +import com.sun.xml.internal.ws.wsdl.OperationDispatcher;
    1.60 +import com.sun.org.glassfish.gmbal.ManagedObjectManager;
    1.61 +import org.w3c.dom.Element;
    1.62 +
    1.63 +import javax.annotation.PreDestroy;
    1.64 +import javax.xml.namespace.QName;
    1.65 +import javax.xml.stream.XMLStreamException;
    1.66 +import javax.xml.ws.EndpointReference;
    1.67 +import javax.xml.ws.WebServiceException;
    1.68 +import javax.xml.ws.handler.Handler;
    1.69 +import java.lang.reflect.Method;
    1.70 +import java.util.*;
    1.71 +import java.util.concurrent.CopyOnWriteArraySet;
    1.72 +import java.util.concurrent.Executor;
    1.73 +import java.util.logging.Level;
    1.74 +import java.util.logging.Logger;
    1.75 +import javax.management.ObjectName;
    1.76 +
    1.77 +/**
    1.78 + * {@link WSEndpoint} implementation.
    1.79 + *
    1.80 + * @author Kohsuke Kawaguchi
    1.81 + * @author Jitendra Kotamraju
    1.82 + */
    1.83 +public /*final*/ class WSEndpointImpl<T> extends WSEndpoint<T> implements LazyMOMProvider.WSEndpointScopeChangeListener {
    1.84 +
    1.85 +    private static final Logger logger = Logger.getLogger(com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.endpoint");
    1.86 +
    1.87 +    private final @NotNull QName serviceName;
    1.88 +    private final @NotNull QName portName;
    1.89 +    protected final WSBinding binding;
    1.90 +    private final SEIModel seiModel;
    1.91 +    private final @NotNull Container container;
    1.92 +    private final WSDLPort port;
    1.93 +
    1.94 +    protected final Tube masterTubeline;
    1.95 +    private final ServiceDefinitionImpl serviceDef;
    1.96 +    private final SOAPVersion soapVersion;
    1.97 +    private final Engine engine;
    1.98 +    private final @NotNull Codec masterCodec;
    1.99 +    private final @NotNull PolicyMap endpointPolicy;
   1.100 +    private final Pool<Tube> tubePool;
   1.101 +    private final OperationDispatcher operationDispatcher;
   1.102 +    private @NotNull ManagedObjectManager managedObjectManager;
   1.103 +    private boolean managedObjectManagerClosed = false;
   1.104 +    private final Object managedObjectManagerLock = new Object();
   1.105 +    private LazyMOMProvider.Scope lazyMOMProviderScope = LazyMOMProvider.Scope.STANDALONE;
   1.106 +    private final @NotNull ServerTubeAssemblerContext context;
   1.107 +
   1.108 +    private Map<QName, WSEndpointReference.EPRExtension> endpointReferenceExtensions = new HashMap<QName, WSEndpointReference.EPRExtension>();
   1.109 +    /**
   1.110 +     * Set to true once we start shutting down this endpoint. Used to avoid
   1.111 +     * running the clean up processing twice.
   1.112 +     *
   1.113 +     * @see #dispose()
   1.114 +     */
   1.115 +    private boolean disposed;
   1.116 +    private final Class<T> implementationClass;
   1.117 +    private final @NotNull
   1.118 +    WSDLProperties wsdlProperties;
   1.119 +    private final Set<Component> componentRegistry = new CopyOnWriteArraySet<Component>();
   1.120 +
   1.121 +    protected WSEndpointImpl(@NotNull QName serviceName, @NotNull QName portName, WSBinding binding,
   1.122 +                   Container container, SEIModel seiModel, WSDLPort port,
   1.123 +                   Class<T> implementationClass,
   1.124 +                   @Nullable ServiceDefinitionImpl serviceDef,
   1.125 +                   EndpointAwareTube terminalTube, boolean isSynchronous,
   1.126 +                   PolicyMap endpointPolicy) {
   1.127 +                this.serviceName = serviceName;
   1.128 +                this.portName = portName;
   1.129 +                this.binding = binding;
   1.130 +                this.soapVersion = binding.getSOAPVersion();
   1.131 +                this.container = container;
   1.132 +                this.port = port;
   1.133 +                this.implementationClass = implementationClass;
   1.134 +                this.serviceDef = serviceDef;
   1.135 +                this.seiModel = seiModel;
   1.136 +        this.endpointPolicy = endpointPolicy;
   1.137 +
   1.138 +        LazyMOMProvider.INSTANCE.registerEndpoint(this);
   1.139 +        initManagedObjectManager();
   1.140 +
   1.141 +        if (serviceDef != null) {
   1.142 +            serviceDef.setOwner(this);
   1.143 +        }
   1.144 +
   1.145 +        ComponentFeature cf = binding.getFeature(ComponentFeature.class);
   1.146 +        if (cf != null) {
   1.147 +            switch (cf.getTarget()) {
   1.148 +                case ENDPOINT:
   1.149 +                    componentRegistry.add(cf.getComponent());
   1.150 +                    break;
   1.151 +                case CONTAINER:
   1.152 +                    container.getComponents().add(cf.getComponent());
   1.153 +                    break;
   1.154 +                default:
   1.155 +                    throw new IllegalArgumentException();
   1.156 +            }
   1.157 +        }
   1.158 +        ComponentsFeature csf = binding.getFeature(ComponentsFeature.class);
   1.159 +        if (csf != null) {
   1.160 +            for (ComponentFeature cfi : csf.getComponentFeatures()) {
   1.161 +                switch (cfi.getTarget()) {
   1.162 +                    case ENDPOINT:
   1.163 +                        componentRegistry.add(cfi.getComponent());
   1.164 +                        break;
   1.165 +                    case CONTAINER:
   1.166 +                        container.getComponents().add(cfi.getComponent());
   1.167 +                        break;
   1.168 +                    default:
   1.169 +                        throw new IllegalArgumentException();
   1.170 +                }
   1.171 +            }
   1.172 +        }
   1.173 +
   1.174 +        TubelineAssembler assembler = TubelineAssemblerFactory.create(
   1.175 +                Thread.currentThread().getContextClassLoader(), binding.getBindingId(), container);
   1.176 +        assert assembler != null;
   1.177 +
   1.178 +        this.operationDispatcher = (port == null) ? null : new OperationDispatcher(port, binding, seiModel);
   1.179 +
   1.180 +        context = createServerTubeAssemblerContext(terminalTube, isSynchronous);
   1.181 +        this.masterTubeline = assembler.createServer(context);
   1.182 +
   1.183 +        Codec c = context.getCodec();
   1.184 +        if (c instanceof EndpointAwareCodec) {
   1.185 +            // create a copy to avoid sharing the codec between multiple endpoints
   1.186 +            c = c.copy();
   1.187 +            ((EndpointAwareCodec) c).setEndpoint(this);
   1.188 +        }
   1.189 +        this.masterCodec = c;
   1.190 +
   1.191 +        tubePool = new TubePool(masterTubeline);
   1.192 +        terminalTube.setEndpoint(this);
   1.193 +        engine = new Engine(toString(), container);
   1.194 +        wsdlProperties = (port == null) ? new WSDLDirectProperties(serviceName, portName, seiModel) : new WSDLPortProperties(port, seiModel);
   1.195 +
   1.196 +        Map<QName, WSEndpointReference.EPRExtension> eprExtensions = new HashMap<QName, WSEndpointReference.EPRExtension>();
   1.197 +        try {
   1.198 +            if (port != null) {
   1.199 +                //gather EPR extrensions from WSDL Model
   1.200 +                WSEndpointReference wsdlEpr = port.getEPR();
   1.201 +                if (wsdlEpr != null) {
   1.202 +                    for (WSEndpointReference.EPRExtension extnEl : wsdlEpr.getEPRExtensions()) {
   1.203 +                        eprExtensions.put(extnEl.getQName(), extnEl);
   1.204 +                    }
   1.205 +                }
   1.206 +            }
   1.207 +
   1.208 +            EndpointReferenceExtensionContributor[] eprExtnContributors = ServiceFinder.find(EndpointReferenceExtensionContributor.class).toArray();
   1.209 +            for(EndpointReferenceExtensionContributor eprExtnContributor :eprExtnContributors) {
   1.210 +                WSEndpointReference.EPRExtension wsdlEPRExtn = eprExtensions.remove(eprExtnContributor.getQName());
   1.211 +                    WSEndpointReference.EPRExtension endpointEprExtn = eprExtnContributor.getEPRExtension(this,wsdlEPRExtn);
   1.212 +                    if (endpointEprExtn != null) {
   1.213 +                        eprExtensions.put(endpointEprExtn.getQName(), endpointEprExtn);
   1.214 +                    }
   1.215 +            }
   1.216 +            for (WSEndpointReference.EPRExtension extn : eprExtensions.values()) {
   1.217 +                endpointReferenceExtensions.put(extn.getQName(), new WSEPRExtension(
   1.218 +                        XMLStreamBuffer.createNewBufferFromXMLStreamReader(extn.readAsXMLStreamReader()),extn.getQName()));
   1.219 +            }
   1.220 +        } catch (XMLStreamException ex) {
   1.221 +            throw new WebServiceException(ex);
   1.222 +        }
   1.223 +        if(!eprExtensions.isEmpty()) {
   1.224 +            serviceDef.addFilter(new EPRSDDocumentFilter(this));
   1.225 +        }
   1.226 +  }
   1.227 +
   1.228 +  protected ServerTubeAssemblerContext createServerTubeAssemblerContext(
   1.229 +            EndpointAwareTube terminalTube, boolean isSynchronous) {
   1.230 +    ServerTubeAssemblerContext ctx = new ServerPipeAssemblerContext(
   1.231 +        seiModel, port, this, terminalTube, isSynchronous);
   1.232 +    return ctx;
   1.233 +  }
   1.234 +
   1.235 +        protected WSEndpointImpl(@NotNull QName serviceName, @NotNull QName portName, WSBinding binding, Container container,
   1.236 +                        SEIModel seiModel, WSDLPort port,
   1.237 +                        Tube masterTubeline) {
   1.238 +                this.serviceName = serviceName;
   1.239 +                this.portName = portName;
   1.240 +                this.binding = binding;
   1.241 +                this.soapVersion = binding.getSOAPVersion();
   1.242 +                this.container = container;
   1.243 +                this.endpointPolicy = null;
   1.244 +                this.port = port;
   1.245 +                this.seiModel = seiModel;
   1.246 +                this.serviceDef = null;
   1.247 +                this.implementationClass = null;
   1.248 +                this.masterTubeline = masterTubeline;
   1.249 +                this.masterCodec = ((BindingImpl) this.binding).createCodec();
   1.250 +
   1.251 +        LazyMOMProvider.INSTANCE.registerEndpoint(this);
   1.252 +        initManagedObjectManager();
   1.253 +
   1.254 +        this.operationDispatcher = (port == null) ? null : new OperationDispatcher(port, binding, seiModel);
   1.255 +            this.context = new ServerPipeAssemblerContext(
   1.256 +                seiModel, port, this, null /* not known */, false);
   1.257 +
   1.258 +                tubePool = new TubePool(masterTubeline);
   1.259 +                engine = new Engine(toString(), container);
   1.260 +                wsdlProperties = (port == null) ? new WSDLDirectProperties(serviceName, portName, seiModel) : new WSDLPortProperties(port, seiModel);
   1.261 +  }
   1.262 +
   1.263 +    public Collection<WSEndpointReference.EPRExtension> getEndpointReferenceExtensions() {
   1.264 +        return endpointReferenceExtensions.values();
   1.265 +    }
   1.266 +
   1.267 +    /**
   1.268 +     * Nullable when there is no associated WSDL Model
   1.269 +     * @return
   1.270 +     */
   1.271 +    public @Nullable OperationDispatcher getOperationDispatcher() {
   1.272 +        return operationDispatcher;
   1.273 +    }
   1.274 +
   1.275 +    public PolicyMap getPolicyMap() {
   1.276 +            return endpointPolicy;
   1.277 +    }
   1.278 +
   1.279 +    public @NotNull Class<T> getImplementationClass() {
   1.280 +                return implementationClass;
   1.281 +        }
   1.282 +
   1.283 +    public @NotNull WSBinding getBinding() {
   1.284 +                return binding;
   1.285 +        }
   1.286 +
   1.287 +    public @NotNull Container getContainer() {
   1.288 +                return container;
   1.289 +        }
   1.290 +
   1.291 +        public WSDLPort getPort() {
   1.292 +                return port;
   1.293 +        }
   1.294 +
   1.295 +        @Override
   1.296 +    public @Nullable SEIModel getSEIModel() {
   1.297 +                return seiModel;
   1.298 +        }
   1.299 +
   1.300 +        public void setExecutor(Executor exec) {
   1.301 +                engine.setExecutor(exec);
   1.302 +        }
   1.303 +
   1.304 +        @Override
   1.305 +        public Engine getEngine() {
   1.306 +                return engine;
   1.307 +        }
   1.308 +
   1.309 +    public void schedule(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor) {
   1.310 +        processAsync(request, callback, interceptor, true);
   1.311 +    }
   1.312 +
   1.313 +    private void processAsync(final Packet request,
   1.314 +            final CompletionCallback callback,
   1.315 +            FiberContextSwitchInterceptor interceptor, boolean schedule) {
   1.316 +        Container old = ContainerResolver.getDefault().enterContainer(container);
   1.317 +        try {
   1.318 +            request.endpoint = WSEndpointImpl.this;
   1.319 +            request.addSatellite(wsdlProperties);
   1.320 +
   1.321 +            Fiber fiber = engine.createFiber();
   1.322 +            fiber.setDeliverThrowableInPacket(true);
   1.323 +            if (interceptor != null) {
   1.324 +                fiber.addInterceptor(interceptor);
   1.325 +            }
   1.326 +            final Tube tube = tubePool.take();
   1.327 +            Fiber.CompletionCallback cbak = new Fiber.CompletionCallback() {
   1.328 +                public void onCompletion(@NotNull Packet response) {
   1.329 +                    ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class);
   1.330 +                    if (tc == null) {
   1.331 +                        // Only recycle tubes in non-exception path as some Tubes may be
   1.332 +                        // in invalid state following exception
   1.333 +                        tubePool.recycle(tube);
   1.334 +                    }
   1.335 +
   1.336 +                    if (callback != null) {
   1.337 +                        if (tc != null) {
   1.338 +                            response = createServiceResponseForException(tc,
   1.339 +                                                                         response,
   1.340 +                                                                         soapVersion,
   1.341 +                                                                         request.endpoint.getPort(),
   1.342 +                                                                         null,
   1.343 +                                                                         request.endpoint.getBinding());
   1.344 +                        }
   1.345 +                        callback.onCompletion(response);
   1.346 +                    }
   1.347 +                }
   1.348 +
   1.349 +                public void onCompletion(@NotNull Throwable error) {
   1.350 +                    // will never be called now that we are using
   1.351 +                    // fiber.setDeliverThrowableInPacket(true);
   1.352 +                    throw new IllegalStateException();
   1.353 +                }
   1.354 +            };
   1.355 +
   1.356 +            fiber.start(tube, request, cbak,
   1.357 +                    binding.isFeatureEnabled(SyncStartForAsyncFeature.class)
   1.358 +                            || !schedule);
   1.359 +        } finally {
   1.360 +            ContainerResolver.getDefault().exitContainer(old);
   1.361 +        }
   1.362 +    }
   1.363 +
   1.364 +    @Override
   1.365 +    public Packet createServiceResponseForException(final ThrowableContainerPropertySet tc,
   1.366 +                                                    final Packet      responsePacket,
   1.367 +                                                    final SOAPVersion soapVersion,
   1.368 +                                                    final WSDLPort    wsdlPort,
   1.369 +                                                    final SEIModel    seiModel,
   1.370 +                                                    final WSBinding   binding)
   1.371 +    {
   1.372 +        // This will happen in addressing if it is enabled.
   1.373 +        if (tc.isFaultCreated()) return responsePacket;
   1.374 +
   1.375 +        final Message faultMessage = SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, tc.getThrowable());
   1.376 +        final Packet result = responsePacket.createServerResponse(faultMessage, wsdlPort, seiModel, binding);
   1.377 +        // Pass info to upper layers
   1.378 +        tc.setFaultMessage(faultMessage);
   1.379 +        tc.setResponsePacket(responsePacket);
   1.380 +        tc.setFaultCreated(true);
   1.381 +        return result;
   1.382 +    }
   1.383 +
   1.384 +    @Override
   1.385 +    public void process(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor) {
   1.386 +        processAsync(request, callback, interceptor, false);
   1.387 +    }
   1.388 +
   1.389 +    public @NotNull
   1.390 +    PipeHead createPipeHead() {
   1.391 +        return new PipeHead() {
   1.392 +            private final Tube tube = TubeCloner.clone(masterTubeline);
   1.393 +
   1.394 +            public @NotNull
   1.395 +            Packet process(Packet request, WebServiceContextDelegate wscd,
   1.396 +                    TransportBackChannel tbc) {
   1.397 +                Container old = ContainerResolver.getDefault().enterContainer(container);
   1.398 +                try {
   1.399 +                    request.webServiceContextDelegate = wscd;
   1.400 +                    request.transportBackChannel = tbc;
   1.401 +                    request.endpoint = WSEndpointImpl.this;
   1.402 +                    request.addSatellite(wsdlProperties);
   1.403 +
   1.404 +                    Fiber fiber = engine.createFiber();
   1.405 +                    Packet response;
   1.406 +                    try {
   1.407 +                        response = fiber.runSync(tube, request);
   1.408 +                    } catch (RuntimeException re) {
   1.409 +                        // Catch all runtime exceptions so that transport
   1.410 +                        // doesn't
   1.411 +                        // have to worry about converting to wire message
   1.412 +                        // TODO XML/HTTP binding
   1.413 +                        Message faultMsg = SOAPFaultBuilder
   1.414 +                                .createSOAPFaultMessage(soapVersion, null, re);
   1.415 +                        response = request.createServerResponse(faultMsg,
   1.416 +                                request.endpoint.getPort(), null,
   1.417 +                                request.endpoint.getBinding());
   1.418 +                    }
   1.419 +                    return response;
   1.420 +                } finally {
   1.421 +                    ContainerResolver.getDefault().exitContainer(old);
   1.422 +                }
   1.423 +            }
   1.424 +        };
   1.425 +    }
   1.426 +
   1.427 +        public synchronized void dispose() {
   1.428 +            if (disposed) {
   1.429 +                return;
   1.430 +            }
   1.431 +            disposed = true;
   1.432 +
   1.433 +            masterTubeline.preDestroy();
   1.434 +
   1.435 +            for (Handler handler : binding.getHandlerChain()) {
   1.436 +                for (Method method : handler.getClass().getMethods()) {
   1.437 +                    if (method.getAnnotation(PreDestroy.class) == null) {
   1.438 +                        continue;
   1.439 +                    }
   1.440 +                    try {
   1.441 +                        method.invoke(handler);
   1.442 +                    } catch (Exception e) {
   1.443 +                        logger.log(Level.WARNING, HandlerMessages.HANDLER_PREDESTROY_IGNORE(e.getMessage()), e);
   1.444 +                    }
   1.445 +                    break;
   1.446 +                }
   1.447 +            }
   1.448 +            closeManagedObjectManager();
   1.449 +            LazyMOMProvider.INSTANCE.unregisterEndpoint(this);
   1.450 +        }
   1.451 +
   1.452 +        public ServiceDefinitionImpl getServiceDefinition() {
   1.453 +                return serviceDef;
   1.454 +        }
   1.455 +
   1.456 +        public Set<EndpointComponent> getComponentRegistry() {
   1.457 +                Set<EndpointComponent> sec = new EndpointComponentSet();
   1.458 +                for (Component c : componentRegistry) {
   1.459 +                        sec.add(c instanceof EndpointComponentWrapper ?
   1.460 +                                ((EndpointComponentWrapper) c).component :
   1.461 +                                new ComponentWrapper(c));
   1.462 +                }
   1.463 +                return sec;
   1.464 +        }
   1.465 +
   1.466 +        private class EndpointComponentSet extends HashSet<EndpointComponent> {
   1.467 +
   1.468 +                @Override
   1.469 +                public Iterator<EndpointComponent> iterator() {
   1.470 +                        final Iterator<EndpointComponent> it = super.iterator();
   1.471 +                        return new Iterator<EndpointComponent>() {
   1.472 +                                private EndpointComponent last = null;
   1.473 +
   1.474 +                                public boolean hasNext() {
   1.475 +                                        return it.hasNext();
   1.476 +                                }
   1.477 +
   1.478 +                                public EndpointComponent next() {
   1.479 +                                        last = it.next();
   1.480 +                                        return last;
   1.481 +                                }
   1.482 +
   1.483 +                                public void remove() {
   1.484 +                                        it.remove();
   1.485 +                                        if (last != null) {
   1.486 +                                                componentRegistry.remove(last instanceof ComponentWrapper ?
   1.487 +                                                                ((ComponentWrapper) last).component :
   1.488 +                                                                new EndpointComponentWrapper(last));
   1.489 +                                        }
   1.490 +                                        last = null;
   1.491 +                                }
   1.492 +                        };
   1.493 +                }
   1.494 +
   1.495 +                @Override
   1.496 +                public boolean add(EndpointComponent e) {
   1.497 +                        boolean result = super.add(e);
   1.498 +                        if (result) {
   1.499 +                                componentRegistry.add(new EndpointComponentWrapper(e));
   1.500 +                        }
   1.501 +                        return result;
   1.502 +                }
   1.503 +
   1.504 +                @Override
   1.505 +                public boolean remove(Object o) {
   1.506 +                        boolean result = super.remove(o);
   1.507 +                        if (result) {
   1.508 +                                componentRegistry.remove(o instanceof ComponentWrapper ?
   1.509 +                                                ((ComponentWrapper) o).component :
   1.510 +                                                new EndpointComponentWrapper((EndpointComponent)o));
   1.511 +                        }
   1.512 +                        return result;
   1.513 +                }
   1.514 +
   1.515 +        }
   1.516 +
   1.517 +        private static class ComponentWrapper implements EndpointComponent {
   1.518 +                private final Component component;
   1.519 +
   1.520 +                public ComponentWrapper(Component component) {
   1.521 +                        this.component = component;
   1.522 +                }
   1.523 +
   1.524 +                public <S> S getSPI(Class<S> spiType) {
   1.525 +                        return component.getSPI(spiType);
   1.526 +                }
   1.527 +
   1.528 +                @Override
   1.529 +                public int hashCode() {
   1.530 +                        return component.hashCode();
   1.531 +                }
   1.532 +
   1.533 +                @Override
   1.534 +                public boolean equals(Object obj) {
   1.535 +                    return component.equals(obj);
   1.536 +                }
   1.537 +        }
   1.538 +
   1.539 +        private static class EndpointComponentWrapper implements Component {
   1.540 +                private final EndpointComponent component;
   1.541 +
   1.542 +                public EndpointComponentWrapper(EndpointComponent component) {
   1.543 +                        this.component = component;
   1.544 +                }
   1.545 +
   1.546 +                public <S> S getSPI(Class<S> spiType) {
   1.547 +                        return component.getSPI(spiType);
   1.548 +                }
   1.549 +
   1.550 +                @Override
   1.551 +                public int hashCode() {
   1.552 +                        return component.hashCode();
   1.553 +                }
   1.554 +
   1.555 +                @Override
   1.556 +                public boolean equals(Object obj) {
   1.557 +                        return component.equals(obj);
   1.558 +                }
   1.559 +        }
   1.560 +
   1.561 +        @Override
   1.562 +        public @NotNull Set<Component> getComponents() {
   1.563 +                return componentRegistry;
   1.564 +        }
   1.565 +
   1.566 +    public <T extends EndpointReference> T getEndpointReference(Class<T> clazz, String address, String wsdlAddress, Element... referenceParameters) {
   1.567 +        List<Element> refParams = null;
   1.568 +        if (referenceParameters != null) {
   1.569 +            refParams = Arrays.asList(referenceParameters);
   1.570 +        }
   1.571 +        return getEndpointReference(clazz, address, wsdlAddress, null, refParams);
   1.572 +    }
   1.573 +
   1.574 +    public <T extends EndpointReference> T getEndpointReference(Class<T> clazz,
   1.575 +            String address, String wsdlAddress, List<Element> metadata,
   1.576 +            List<Element> referenceParameters) {
   1.577 +        QName portType = null;
   1.578 +        if (port != null) {
   1.579 +            portType = port.getBinding().getPortTypeName();
   1.580 +        }
   1.581 +
   1.582 +        AddressingVersion av = AddressingVersion.fromSpecClass(clazz);
   1.583 +        return new WSEndpointReference(
   1.584 +                av, address, serviceName, portName, portType, metadata, wsdlAddress, referenceParameters, endpointReferenceExtensions.values(), null).toSpec(clazz);
   1.585 +
   1.586 +    }
   1.587 +
   1.588 +    public @NotNull QName getPortName() {
   1.589 +                return portName;
   1.590 +        }
   1.591 +
   1.592 +
   1.593 +    public @NotNull Codec createCodec() {
   1.594 +                return masterCodec.copy();
   1.595 +        }
   1.596 +
   1.597 +    public @NotNull QName getServiceName() {
   1.598 +                return serviceName;
   1.599 +        }
   1.600 +
   1.601 +    private void initManagedObjectManager() {
   1.602 +        synchronized (managedObjectManagerLock) {
   1.603 +            if (managedObjectManager == null) {
   1.604 +                switch (this.lazyMOMProviderScope) {
   1.605 +                    case GLASSFISH_NO_JMX:
   1.606 +                        managedObjectManager = new WSEndpointMOMProxy(this);
   1.607 +                        break;
   1.608 +                    default:
   1.609 +                        managedObjectManager = obtainManagedObjectManager();
   1.610 +                }
   1.611 +            }
   1.612 +        }
   1.613 +    }
   1.614 +
   1.615 +    public @NotNull ManagedObjectManager getManagedObjectManager() {
   1.616 +        return managedObjectManager;
   1.617 +    }
   1.618 +
   1.619 +    /**
   1.620 +     * Obtains a real instance of {@code ManagedObjectManager} class no matter what lazyMOMProviderScope is this endpoint in (or if the
   1.621 +     * Gmbal API calls should be deferred).
   1.622 +     *
   1.623 +     * @see com.sun.xml.internal.ws.api.server.LazyMOMProvider.Scope
   1.624 +     * @return an instance of {@code ManagedObjectManager}
   1.625 +     */
   1.626 +    @NotNull ManagedObjectManager obtainManagedObjectManager() {
   1.627 +        final MonitorRootService monitorRootService = new MonitorRootService(this);
   1.628 +        final ManagedObjectManager mOM = monitorRootService.createManagedObjectManager(this);
   1.629 +
   1.630 +        // ManagedObjectManager was suspended due to root creation (see MonitorBase#initMOM)
   1.631 +        mOM.resumeJMXRegistration();
   1.632 +
   1.633 +        return mOM;
   1.634 +    }
   1.635 +
   1.636 +    public void scopeChanged(LazyMOMProvider.Scope scope) {
   1.637 +        synchronized (managedObjectManagerLock) {
   1.638 +            if (managedObjectManagerClosed) {
   1.639 +                return;
   1.640 +            }
   1.641 +
   1.642 +            this.lazyMOMProviderScope = scope;
   1.643 +
   1.644 +            // possible lazyMOMProviderScope change can be LazyMOMProvider.Scope.GLASSFISH_NO_JMX or LazyMOMProvider.Scope.GLASSFISH_JMX
   1.645 +            if (managedObjectManager == null) {
   1.646 +                if (scope != LazyMOMProvider.Scope.GLASSFISH_NO_JMX) {
   1.647 +                    managedObjectManager = obtainManagedObjectManager();
   1.648 +                } else {
   1.649 +                    managedObjectManager = new WSEndpointMOMProxy(this);
   1.650 +                }
   1.651 +            } else {
   1.652 +                // if ManagedObjectManager for this endpoint has already been created and is uninitialized proxy then
   1.653 +                // fill it with a real instance
   1.654 +                if (managedObjectManager instanceof WSEndpointMOMProxy
   1.655 +                        && !((WSEndpointMOMProxy)managedObjectManager).isInitialized()) {
   1.656 +                    ((WSEndpointMOMProxy)managedObjectManager).setManagedObjectManager(obtainManagedObjectManager());
   1.657 +                }
   1.658 +            }
   1.659 +        }
   1.660 +    }
   1.661 +
   1.662 +    private static final Logger monitoringLogger = Logger.getLogger(com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".monitoring");
   1.663 +
   1.664 +    // This can be called independently of WSEndpoint.dispose.
   1.665 +    // Example: the WSCM framework calls this before dispose.
   1.666 +    @Override
   1.667 +    public void closeManagedObjectManager() {
   1.668 +        synchronized (managedObjectManagerLock) {
   1.669 +            if (managedObjectManagerClosed == true) {
   1.670 +                return;
   1.671 +            }
   1.672 +            if (managedObjectManager != null) {
   1.673 +                boolean close = true;
   1.674 +
   1.675 +                // ManagedObjectManager doesn't need to be closed because it exists only as a proxy
   1.676 +                if (managedObjectManager instanceof WSEndpointMOMProxy
   1.677 +                        && !((WSEndpointMOMProxy)managedObjectManager).isInitialized()) {
   1.678 +                    close = false;
   1.679 +                }
   1.680 +
   1.681 +                if (close) {
   1.682 +                    try {
   1.683 +                        final ObjectName name = managedObjectManager.getObjectName(managedObjectManager.getRoot());
   1.684 +                        // The name is null when the MOM is a NOOP.
   1.685 +                        if (name != null) {
   1.686 +                            monitoringLogger.log(Level.INFO, "Closing Metro monitoring root: {0}", name);
   1.687 +                        }
   1.688 +                        managedObjectManager.close();
   1.689 +                    } catch (java.io.IOException e) {
   1.690 +                        monitoringLogger.log(Level.WARNING, "Ignoring error when closing Managed Object Manager", e);
   1.691 +                    }
   1.692 +                }
   1.693 +            }
   1.694 +            managedObjectManagerClosed = true;
   1.695 +        }
   1.696 +    }
   1.697 +
   1.698 +    public @NotNull @Override ServerTubeAssemblerContext getAssemblerContext() {
   1.699 +        return context;
   1.700 +    }
   1.701 +}

mercurial