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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,649 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, 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.SOAPVersion;
    1.39 +import com.sun.xml.internal.ws.api.WSBinding;
    1.40 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    1.41 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
    1.42 +import com.sun.xml.internal.ws.api.message.Message;
    1.43 +import com.sun.xml.internal.ws.api.message.Packet;
    1.44 +import com.sun.xml.internal.ws.api.model.SEIModel;
    1.45 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    1.46 +import com.sun.xml.internal.ws.api.pipe.Codec;
    1.47 +import com.sun.xml.internal.ws.api.pipe.Engine;
    1.48 +import com.sun.xml.internal.ws.api.pipe.Fiber;
    1.49 +import com.sun.xml.internal.ws.api.pipe.FiberContextSwitchInterceptor;
    1.50 +import com.sun.xml.internal.ws.api.pipe.ServerPipeAssemblerContext;
    1.51 +import com.sun.xml.internal.ws.api.pipe.ServerTubeAssemblerContext;
    1.52 +import com.sun.xml.internal.ws.api.pipe.SyncStartForAsyncFeature;
    1.53 +import com.sun.xml.internal.ws.api.pipe.Tube;
    1.54 +import com.sun.xml.internal.ws.api.pipe.TubeCloner;
    1.55 +import com.sun.xml.internal.ws.api.pipe.TubelineAssembler;
    1.56 +import com.sun.xml.internal.ws.api.pipe.TubelineAssemblerFactory;
    1.57 +import com.sun.xml.internal.ws.api.server.Container;
    1.58 +import com.sun.xml.internal.ws.api.server.EndpointAwareCodec;
    1.59 +import com.sun.xml.internal.ws.api.server.EndpointComponent;
    1.60 +import com.sun.xml.internal.ws.api.server.EndpointReferenceExtensionContributor;
    1.61 +import com.sun.xml.internal.ws.api.server.LazyMOMProvider;
    1.62 +import com.sun.xml.internal.ws.api.server.TransportBackChannel;
    1.63 +import com.sun.xml.internal.ws.api.server.WSEndpoint;
    1.64 +import com.sun.xml.internal.ws.api.server.WebServiceContextDelegate;
    1.65 +import com.sun.xml.internal.ws.binding.BindingImpl;
    1.66 +import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
    1.67 +import com.sun.xml.internal.ws.model.wsdl.WSDLDirectProperties;
    1.68 +import com.sun.xml.internal.ws.model.wsdl.WSDLPortImpl;
    1.69 +import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties;
    1.70 +import com.sun.xml.internal.ws.model.wsdl.WSDLProperties;
    1.71 +import com.sun.xml.internal.ws.policy.PolicyMap;
    1.72 +import com.sun.xml.internal.ws.resources.HandlerMessages;
    1.73 +import com.sun.xml.internal.ws.util.Pool;
    1.74 +import com.sun.xml.internal.ws.util.Pool.TubePool;
    1.75 +import com.sun.xml.internal.ws.util.ServiceFinder;
    1.76 +import com.sun.xml.internal.ws.wsdl.OperationDispatcher;
    1.77 +import com.sun.org.glassfish.gmbal.ManagedObjectManager;
    1.78 +import org.w3c.dom.Element;
    1.79 +
    1.80 +import javax.annotation.PreDestroy;
    1.81 +import javax.xml.namespace.QName;
    1.82 +import javax.xml.stream.XMLStreamException;
    1.83 +import javax.xml.ws.EndpointReference;
    1.84 +import javax.xml.ws.WebServiceException;
    1.85 +import javax.xml.ws.handler.Handler;
    1.86 +import java.lang.reflect.Method;
    1.87 +import java.util.Arrays;
    1.88 +import java.util.Collection;
    1.89 +import java.util.HashMap;
    1.90 +import java.util.HashSet;
    1.91 +import java.util.Iterator;
    1.92 +import java.util.List;
    1.93 +import java.util.Map;
    1.94 +import java.util.Set;
    1.95 +import java.util.concurrent.CopyOnWriteArraySet;
    1.96 +import java.util.concurrent.Executor;
    1.97 +import java.util.logging.Level;
    1.98 +import java.util.logging.Logger;
    1.99 +
   1.100 +/**
   1.101 + * {@link WSEndpoint} implementation.
   1.102 + *
   1.103 + * @author Kohsuke Kawaguchi
   1.104 + * @author Jitendra Kotamraju
   1.105 + */
   1.106 +public /*final*/ class WSEndpointImpl<T> extends WSEndpoint<T> implements LazyMOMProvider.WSEndpointScopeChangeListener {
   1.107 +        private static final Logger LOGGER = Logger.getLogger(WSEndpointImpl.class.getName());
   1.108 +
   1.109 +    private final @NotNull QName serviceName;
   1.110 +    private final @NotNull QName portName;
   1.111 +        protected final WSBinding binding;
   1.112 +        private final SEIModel seiModel;
   1.113 +    private final @NotNull Container container;
   1.114 +        private final WSDLPort port;
   1.115 +
   1.116 +        protected final Tube masterTubeline;
   1.117 +        private final ServiceDefinitionImpl serviceDef;
   1.118 +        private final SOAPVersion soapVersion;
   1.119 +        private final Engine engine;
   1.120 +    private final @NotNull Codec masterCodec;
   1.121 +    private final @NotNull PolicyMap endpointPolicy;
   1.122 +        private final Pool<Tube> tubePool;
   1.123 +    private final OperationDispatcher operationDispatcher;
   1.124 +    private       @NotNull ManagedObjectManager managedObjectManager;
   1.125 +    private       boolean managedObjectManagerClosed = false;
   1.126 +    private       Object managedObjectManagerLock = new Object();
   1.127 +    private       LazyMOMProvider.Scope lazyMOMProviderScope = LazyMOMProvider.Scope.STANDALONE;
   1.128 +    private final @NotNull ServerTubeAssemblerContext context;
   1.129 +
   1.130 +    private Map<QName, WSEndpointReference.EPRExtension> endpointReferenceExtensions = new HashMap<QName, WSEndpointReference.EPRExtension>();
   1.131 +        /**
   1.132 +     * Set to true once we start shutting down this endpoint.
   1.133 +     * Used to avoid running the clean up processing twice.
   1.134 +         *
   1.135 +         * @see #dispose()
   1.136 +         */
   1.137 +        private boolean disposed;
   1.138 +
   1.139 +        private final Class<T> implementationClass;
   1.140 +    private final @NotNull WSDLProperties wsdlProperties;
   1.141 +        private final Set<Component> componentRegistry = new CopyOnWriteArraySet<Component>();
   1.142 +
   1.143 +    protected WSEndpointImpl(@NotNull QName serviceName, @NotNull QName portName, WSBinding binding,
   1.144 +                   Container container, SEIModel seiModel, WSDLPort port,
   1.145 +                   Class<T> implementationClass,
   1.146 +                   @Nullable ServiceDefinitionImpl serviceDef,
   1.147 +                   EndpointAwareTube terminalTube, boolean isSynchronous,
   1.148 +                   PolicyMap endpointPolicy) {
   1.149 +                this.serviceName = serviceName;
   1.150 +                this.portName = portName;
   1.151 +                this.binding = binding;
   1.152 +                this.soapVersion = binding.getSOAPVersion();
   1.153 +                this.container = container;
   1.154 +                this.port = port;
   1.155 +                this.implementationClass = implementationClass;
   1.156 +                this.serviceDef = serviceDef;
   1.157 +                this.seiModel = seiModel;
   1.158 +        this.endpointPolicy = endpointPolicy;
   1.159 +
   1.160 +        LazyMOMProvider.INSTANCE.registerEndpoint(this);
   1.161 +        initManagedObjectManager();
   1.162 +
   1.163 +                if (serviceDef != null) {
   1.164 +                        serviceDef.setOwner(this);
   1.165 +                }
   1.166 +
   1.167 +                ComponentFeature cf = binding.getFeature(ComponentFeature.class);
   1.168 +                if (cf != null) {
   1.169 +                    switch(cf.getTarget()) {
   1.170 +                        case ENDPOINT:
   1.171 +                            componentRegistry.add(cf.getComponent());
   1.172 +                            break;
   1.173 +                        case CONTAINER:
   1.174 +                            container.getComponents().add(cf.getComponent());
   1.175 +                        default:
   1.176 +                            throw new IllegalArgumentException();
   1.177 +                    }
   1.178 +                }
   1.179 +
   1.180 +        TubelineAssembler assembler = TubelineAssemblerFactory.create(
   1.181 +                Thread.currentThread().getContextClassLoader(), binding.getBindingId(), container);
   1.182 +                assert assembler != null;
   1.183 +
   1.184 +        this.operationDispatcher = (port == null) ? null : new OperationDispatcher(port, binding, seiModel);
   1.185 +
   1.186 +        context = createServerTubeAssemblerContext(terminalTube, isSynchronous);
   1.187 +    this.masterTubeline = assembler.createServer(context);
   1.188 +
   1.189 +                Codec c = context.getCodec();
   1.190 +                if (c instanceof EndpointAwareCodec) {
   1.191 +            // create a copy to avoid sharing the codec between multiple endpoints
   1.192 +                        c = c.copy();
   1.193 +                        ((EndpointAwareCodec) c).setEndpoint(this);
   1.194 +                }
   1.195 +                this.masterCodec = c;
   1.196 +
   1.197 +                tubePool = new TubePool(masterTubeline);
   1.198 +                terminalTube.setEndpoint(this);
   1.199 +                engine = new Engine(toString());
   1.200 +                wsdlProperties = (port == null) ? new WSDLDirectProperties(serviceName, portName, seiModel) : new WSDLPortProperties(port, seiModel);
   1.201 +
   1.202 +        Map<QName, WSEndpointReference.EPRExtension> eprExtensions = new HashMap<QName, WSEndpointReference.EPRExtension>();
   1.203 +        try {
   1.204 +            if (port != null) {
   1.205 +                //gather EPR extrensions from WSDL Model
   1.206 +                WSEndpointReference wsdlEpr = ((WSDLPortImpl) port).getEPR();
   1.207 +                if (wsdlEpr != null) {
   1.208 +                    for (WSEndpointReference.EPRExtension extnEl : wsdlEpr.getEPRExtensions()) {
   1.209 +                        eprExtensions.put(extnEl.getQName(), extnEl);
   1.210 +                    }
   1.211 +                }
   1.212 +            }
   1.213 +
   1.214 +            EndpointReferenceExtensionContributor[] eprExtnContributors = ServiceFinder.find(EndpointReferenceExtensionContributor.class).toArray();
   1.215 +            for(EndpointReferenceExtensionContributor eprExtnContributor :eprExtnContributors) {
   1.216 +                WSEndpointReference.EPRExtension wsdlEPRExtn = eprExtensions.remove(eprExtnContributor.getQName());
   1.217 +                    WSEndpointReference.EPRExtension endpointEprExtn = eprExtnContributor.getEPRExtension(this,wsdlEPRExtn);
   1.218 +                    if (endpointEprExtn != null) {
   1.219 +                        eprExtensions.put(endpointEprExtn.getQName(), endpointEprExtn);
   1.220 +                    }
   1.221 +            }
   1.222 +            for (WSEndpointReference.EPRExtension extn : eprExtensions.values()) {
   1.223 +                endpointReferenceExtensions.put(extn.getQName(), new WSEPRExtension(
   1.224 +                        XMLStreamBuffer.createNewBufferFromXMLStreamReader(extn.readAsXMLStreamReader()),extn.getQName()));
   1.225 +            }
   1.226 +        } catch (XMLStreamException ex) {
   1.227 +            throw new WebServiceException(ex);
   1.228 +        }
   1.229 +        if(!eprExtensions.isEmpty()) {
   1.230 +            serviceDef.addFilter(new EPRSDDocumentFilter(this));
   1.231 +        }
   1.232 +  }
   1.233 +
   1.234 +  protected ServerTubeAssemblerContext createServerTubeAssemblerContext(
   1.235 +            EndpointAwareTube terminalTube, boolean isSynchronous) {
   1.236 +    ServerTubeAssemblerContext context = new ServerPipeAssemblerContext(
   1.237 +        seiModel, port, this, terminalTube, isSynchronous);
   1.238 +    return context;
   1.239 +  }
   1.240 +
   1.241 +        protected WSEndpointImpl(@NotNull QName serviceName, @NotNull QName portName, WSBinding binding, Container container,
   1.242 +                        SEIModel seiModel, WSDLPort port,
   1.243 +                        Tube masterTubeline) {
   1.244 +                this.serviceName = serviceName;
   1.245 +                this.portName = portName;
   1.246 +                this.binding = binding;
   1.247 +                this.soapVersion = binding.getSOAPVersion();
   1.248 +                this.container = container;
   1.249 +                this.endpointPolicy = null;
   1.250 +                this.port = port;
   1.251 +                this.seiModel = seiModel;
   1.252 +                this.serviceDef = null;
   1.253 +                this.implementationClass = null;
   1.254 +                this.masterTubeline = masterTubeline;
   1.255 +                this.masterCodec = ((BindingImpl) this.binding).createCodec();
   1.256 +
   1.257 +        LazyMOMProvider.INSTANCE.registerEndpoint(this);
   1.258 +        initManagedObjectManager();
   1.259 +
   1.260 +        this.operationDispatcher = (port == null) ? null : new OperationDispatcher(port, binding, seiModel);
   1.261 +            this.context = new ServerPipeAssemblerContext(
   1.262 +                seiModel, port, this, null /* not known */, false);
   1.263 +
   1.264 +                tubePool = new TubePool(masterTubeline);
   1.265 +                engine = new Engine(toString());
   1.266 +                wsdlProperties = (port == null) ? new WSDLDirectProperties(serviceName, portName, seiModel) : new WSDLPortProperties(port, seiModel);
   1.267 +  }
   1.268 +
   1.269 +    public Collection<WSEndpointReference.EPRExtension> getEndpointReferenceExtensions() {
   1.270 +        return endpointReferenceExtensions.values();
   1.271 +    }
   1.272 +
   1.273 +    /**
   1.274 +     * Nullable when there is no associated WSDL Model
   1.275 +     * @return
   1.276 +     */
   1.277 +    public @Nullable OperationDispatcher getOperationDispatcher() {
   1.278 +        return operationDispatcher;
   1.279 +    }
   1.280 +
   1.281 +    public PolicyMap getPolicyMap() {
   1.282 +            return endpointPolicy;
   1.283 +    }
   1.284 +
   1.285 +    public @NotNull Class<T> getImplementationClass() {
   1.286 +                return implementationClass;
   1.287 +        }
   1.288 +
   1.289 +    public @NotNull WSBinding getBinding() {
   1.290 +                return binding;
   1.291 +        }
   1.292 +
   1.293 +    public @NotNull Container getContainer() {
   1.294 +                return container;
   1.295 +        }
   1.296 +
   1.297 +        public WSDLPort getPort() {
   1.298 +                return port;
   1.299 +        }
   1.300 +
   1.301 +        @Override
   1.302 +    public @Nullable SEIModel getSEIModel() {
   1.303 +                return seiModel;
   1.304 +        }
   1.305 +
   1.306 +        public void setExecutor(Executor exec) {
   1.307 +                engine.setExecutor(exec);
   1.308 +        }
   1.309 +
   1.310 +        @Override
   1.311 +        public Engine getEngine() {
   1.312 +                return engine;
   1.313 +        }
   1.314 +
   1.315 +    public void schedule(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor) {
   1.316 +        processAsync(request, callback, interceptor, true);
   1.317 +    }
   1.318 +
   1.319 +    private void processAsync(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor, boolean schedule) {
   1.320 +                request.endpoint = WSEndpointImpl.this;
   1.321 +                request.addSatellite(wsdlProperties);
   1.322 +
   1.323 +        Fiber fiber = engine.createFiber();
   1.324 +                if (interceptor != null) {
   1.325 +                        fiber.addInterceptor(interceptor);
   1.326 +                }
   1.327 +                final Tube tube = tubePool.take();
   1.328 +                Fiber.CompletionCallback cbak = new Fiber.CompletionCallback() {
   1.329 +            public void onCompletion(@NotNull Packet response) {
   1.330 +                                tubePool.recycle(tube);
   1.331 +                                if (callback != null) {
   1.332 +                                        callback.onCompletion(response);
   1.333 +                                }
   1.334 +                        }
   1.335 +
   1.336 +            public void onCompletion(@NotNull Throwable error) {
   1.337 +                // let's not reuse tubes as they might be in a wrong state, so not
   1.338 +                                // calling tubePool.recycle()
   1.339 +                // Convert all runtime exceptions to Packet so that transport doesn't
   1.340 +                                // have to worry about converting to wire message
   1.341 +                                // TODO XML/HTTP binding
   1.342 +                                Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(
   1.343 +                                                soapVersion, null, error);
   1.344 +                Packet response = request.createServerResponse(faultMsg, request.endpoint.getPort(), null,
   1.345 +                        request.endpoint.getBinding());
   1.346 +                                if (callback != null) {
   1.347 +                                        callback.onCompletion(response);
   1.348 +                                }
   1.349 +                        }
   1.350 +                };
   1.351 +
   1.352 +                fiber.start(tube, request, cbak,
   1.353 +                                binding.isFeatureEnabled(SyncStartForAsyncFeature.class) || !schedule);
   1.354 +        }
   1.355 +
   1.356 +    @Override
   1.357 +    public void process(final Packet request, final CompletionCallback callback, FiberContextSwitchInterceptor interceptor) {
   1.358 +        processAsync(request, callback, interceptor, false);
   1.359 +    }
   1.360 +
   1.361 +    public @NotNull PipeHead createPipeHead() {
   1.362 +                return new PipeHead() {
   1.363 +                        private final Tube tube = TubeCloner.clone(masterTubeline);
   1.364 +
   1.365 +            public @NotNull Packet process(Packet request, WebServiceContextDelegate wscd, TransportBackChannel tbc) {
   1.366 +                                request.webServiceContextDelegate = wscd;
   1.367 +                                request.transportBackChannel = tbc;
   1.368 +                                request.endpoint = WSEndpointImpl.this;
   1.369 +                                request.addSatellite(wsdlProperties);
   1.370 +
   1.371 +                                Fiber fiber = engine.createFiber();
   1.372 +                                Packet response;
   1.373 +                                try {
   1.374 +                                        response = fiber.runSync(tube, request);
   1.375 +                                } catch (RuntimeException re) {
   1.376 +                                        // Catch all runtime exceptions so that transport doesn't
   1.377 +                                        // have to worry about converting to wire message
   1.378 +                                        // TODO XML/HTTP binding
   1.379 +                                        Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(
   1.380 +                                                        soapVersion, null, re);
   1.381 +                    response = request.createServerResponse(faultMsg, request.endpoint.getPort(), null, request.endpoint.getBinding());
   1.382 +                                }
   1.383 +                                return response;
   1.384 +                        }
   1.385 +                };
   1.386 +        }
   1.387 +
   1.388 +        public synchronized void dispose() {
   1.389 +                if (disposed)
   1.390 +                        return;
   1.391 +                disposed = true;
   1.392 +
   1.393 +    masterTubeline.preDestroy();
   1.394 +
   1.395 +                for (Handler handler : binding.getHandlerChain()) {
   1.396 +                        for (Method method : handler.getClass().getMethods()) {
   1.397 +                                if (method.getAnnotation(PreDestroy.class) == null) {
   1.398 +                                        continue;
   1.399 +                                }
   1.400 +                                try {
   1.401 +                                        method.invoke(handler);
   1.402 +                                } catch (Exception e) {
   1.403 +                    logger.log(Level.WARNING, HandlerMessages.HANDLER_PREDESTROY_IGNORE(e.getMessage()), e);
   1.404 +                                }
   1.405 +                                break;
   1.406 +                        }
   1.407 +                }
   1.408 +        closeManagedObjectManager();
   1.409 +        }
   1.410 +
   1.411 +        public ServiceDefinitionImpl getServiceDefinition() {
   1.412 +                return serviceDef;
   1.413 +        }
   1.414 +
   1.415 +        public Set<EndpointComponent> getComponentRegistry() {
   1.416 +                Set<EndpointComponent> sec = new EndpointComponentSet();
   1.417 +                for (Component c : componentRegistry) {
   1.418 +                        sec.add(c instanceof EndpointComponentWrapper ?
   1.419 +                                ((EndpointComponentWrapper) c).component :
   1.420 +                                new ComponentWrapper(c));
   1.421 +                }
   1.422 +                return sec;
   1.423 +        }
   1.424 +
   1.425 +        private class EndpointComponentSet extends HashSet<EndpointComponent> {
   1.426 +
   1.427 +                @Override
   1.428 +                public Iterator<EndpointComponent> iterator() {
   1.429 +                        final Iterator<EndpointComponent> it = super.iterator();
   1.430 +                        return new Iterator<EndpointComponent>() {
   1.431 +                                private EndpointComponent last = null;
   1.432 +
   1.433 +                                public boolean hasNext() {
   1.434 +                                        return it.hasNext();
   1.435 +                                }
   1.436 +
   1.437 +                                public EndpointComponent next() {
   1.438 +                                        last = it.next();
   1.439 +                                        return last;
   1.440 +                                }
   1.441 +
   1.442 +                                public void remove() {
   1.443 +                                        it.remove();
   1.444 +                                        if (last != null) {
   1.445 +                                                componentRegistry.remove(last instanceof ComponentWrapper ?
   1.446 +                                                                ((ComponentWrapper) last).component :
   1.447 +                                                                new EndpointComponentWrapper(last));
   1.448 +                                        }
   1.449 +                                        last = null;
   1.450 +                                }
   1.451 +                        };
   1.452 +                }
   1.453 +
   1.454 +                @Override
   1.455 +                public boolean add(EndpointComponent e) {
   1.456 +                        boolean result = super.add(e);
   1.457 +                        if (result) {
   1.458 +                                componentRegistry.add(new EndpointComponentWrapper(e));
   1.459 +                        }
   1.460 +                        return result;
   1.461 +                }
   1.462 +
   1.463 +                @Override
   1.464 +                public boolean remove(Object o) {
   1.465 +                        boolean result = super.remove(o);
   1.466 +                        if (result) {
   1.467 +                                componentRegistry.remove(o instanceof ComponentWrapper ?
   1.468 +                                                ((ComponentWrapper) o).component :
   1.469 +                                                new EndpointComponentWrapper((EndpointComponent)o));
   1.470 +                        }
   1.471 +                        return result;
   1.472 +                }
   1.473 +
   1.474 +        }
   1.475 +
   1.476 +        private static class ComponentWrapper implements EndpointComponent {
   1.477 +                private final Component component;
   1.478 +
   1.479 +                public ComponentWrapper(Component component) {
   1.480 +                        this.component = component;
   1.481 +                }
   1.482 +
   1.483 +                public <S> S getSPI(Class<S> spiType) {
   1.484 +                        return component.getSPI(spiType);
   1.485 +                }
   1.486 +
   1.487 +                @Override
   1.488 +                public int hashCode() {
   1.489 +                        return component.hashCode();
   1.490 +                }
   1.491 +
   1.492 +                @Override
   1.493 +                public boolean equals(Object obj) {
   1.494 +                        return component.equals(obj);
   1.495 +                }
   1.496 +        }
   1.497 +
   1.498 +        private static class EndpointComponentWrapper implements Component {
   1.499 +                private final EndpointComponent component;
   1.500 +
   1.501 +                public EndpointComponentWrapper(EndpointComponent component) {
   1.502 +                        this.component = component;
   1.503 +                }
   1.504 +
   1.505 +                public <S> S getSPI(Class<S> spiType) {
   1.506 +                        return component.getSPI(spiType);
   1.507 +                }
   1.508 +
   1.509 +                @Override
   1.510 +                public int hashCode() {
   1.511 +                        return component.hashCode();
   1.512 +                }
   1.513 +
   1.514 +                @Override
   1.515 +                public boolean equals(Object obj) {
   1.516 +                        return component.equals(obj);
   1.517 +                }
   1.518 +        }
   1.519 +
   1.520 +        public @NotNull Set<Component> getComponents() {
   1.521 +                return componentRegistry;
   1.522 +        }
   1.523 +
   1.524 +    private static final Logger logger = Logger.getLogger(
   1.525 +        com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.endpoint");
   1.526 +
   1.527 +    public <T extends EndpointReference> T getEndpointReference(Class<T>
   1.528 +            clazz, String address, String wsdlAddress, Element... referenceParameters) {
   1.529 +        List<Element> refParams = null;
   1.530 +        if (referenceParameters != null) {
   1.531 +            refParams = Arrays.asList(referenceParameters);
   1.532 +        }
   1.533 +        return getEndpointReference(clazz, address, wsdlAddress, null, refParams);
   1.534 +    }
   1.535 +    public <T extends EndpointReference> T getEndpointReference(Class<T> clazz,
   1.536 +                        String address, String wsdlAddress, List<Element> metadata,
   1.537 +                        List<Element> referenceParameters) {
   1.538 +                QName portType = null;
   1.539 +                if (port != null) {
   1.540 +                        portType = port.getBinding().getPortTypeName();
   1.541 +                }
   1.542 +
   1.543 +        AddressingVersion av = AddressingVersion.fromSpecClass(clazz);
   1.544 +        return new WSEndpointReference(
   1.545 +                    av, address, serviceName, portName, portType, metadata, wsdlAddress, referenceParameters,endpointReferenceExtensions.values(), null).toSpec(clazz);
   1.546 +
   1.547 +        }
   1.548 +
   1.549 +    public @NotNull QName getPortName() {
   1.550 +                return portName;
   1.551 +        }
   1.552 +
   1.553 +
   1.554 +    public @NotNull Codec createCodec() {
   1.555 +                return masterCodec.copy();
   1.556 +        }
   1.557 +
   1.558 +    public @NotNull QName getServiceName() {
   1.559 +                return serviceName;
   1.560 +        }
   1.561 +
   1.562 +    private void initManagedObjectManager() {
   1.563 +        synchronized (managedObjectManagerLock) {
   1.564 +            if (managedObjectManager == null) {
   1.565 +                switch (this.lazyMOMProviderScope) {
   1.566 +                    case GLASSFISH_NO_JMX:
   1.567 +                        managedObjectManager = new WSEndpointMOMProxy(this);
   1.568 +                        break;
   1.569 +                    default:
   1.570 +                        managedObjectManager = obtainManagedObjectManager();
   1.571 +                }
   1.572 +            }
   1.573 +        }
   1.574 +    }
   1.575 +
   1.576 +    public @NotNull ManagedObjectManager getManagedObjectManager() {
   1.577 +        return managedObjectManager;
   1.578 +    }
   1.579 +
   1.580 +    /**
   1.581 +     * Obtains a real instance of {@code ManagedObjectManager} class no matter what lazyMOMProviderScope is this endpoint in (or if the
   1.582 +     * Gmbal API calls should be deferred).
   1.583 +     *
   1.584 +     * @see com.sun.xml.internal.ws.api.server.LazyMOMProvider.Scope
   1.585 +     * @return an instance of {@code ManagedObjectManager}
   1.586 +     */
   1.587 +    @NotNull ManagedObjectManager obtainManagedObjectManager() {
   1.588 +        final MonitorRootService monitorRootService = new MonitorRootService(this);
   1.589 +        final ManagedObjectManager managedObjectManager = monitorRootService.createManagedObjectManager(this);
   1.590 +
   1.591 +        // ManagedObjectManager was suspended due to root creation (see MonitorBase#initMOM)
   1.592 +        managedObjectManager.resumeJMXRegistration();
   1.593 +
   1.594 +        return managedObjectManager;
   1.595 +    }
   1.596 +
   1.597 +    public void scopeChanged(LazyMOMProvider.Scope scope) {
   1.598 +        synchronized (managedObjectManagerLock) {
   1.599 +            if (managedObjectManagerClosed) {
   1.600 +                return;
   1.601 +            }
   1.602 +
   1.603 +            this.lazyMOMProviderScope = scope;
   1.604 +
   1.605 +            // possible lazyMOMProviderScope change can be LazyMOMProvider.Scope.GLASSFISH_NO_JMX or LazyMOMProvider.Scope.GLASSFISH_JMX
   1.606 +            if (managedObjectManager == null) {
   1.607 +                if (scope != LazyMOMProvider.Scope.GLASSFISH_NO_JMX) {
   1.608 +                    managedObjectManager = obtainManagedObjectManager();
   1.609 +                } else {
   1.610 +                    managedObjectManager = new WSEndpointMOMProxy(this);
   1.611 +                }
   1.612 +            } else {
   1.613 +                // if ManagedObjectManager for this endpoint has already been created and is uninitialized proxy then
   1.614 +                // fill it with a real instance
   1.615 +                if (managedObjectManager instanceof WSEndpointMOMProxy
   1.616 +                        && !((WSEndpointMOMProxy)managedObjectManager).isInitialized()) {
   1.617 +                    ((WSEndpointMOMProxy)managedObjectManager).setManagedObjectManager(obtainManagedObjectManager());
   1.618 +                }
   1.619 +            }
   1.620 +        }
   1.621 +    }
   1.622 +
   1.623 +    // This can be called independently of WSEndpoint.dispose.
   1.624 +    // Example: the WSCM framework calls this before dispose.
   1.625 +    public void closeManagedObjectManager() {
   1.626 +        synchronized (managedObjectManagerLock) {
   1.627 +            if (managedObjectManagerClosed == true) {
   1.628 +                return;
   1.629 +            }
   1.630 +            if (managedObjectManager != null) {
   1.631 +                boolean close = true;
   1.632 +
   1.633 +                // ManagedObjectManager doesn't need to be closed because it exists only as a proxy
   1.634 +                if (managedObjectManager instanceof WSEndpointMOMProxy
   1.635 +                        && !((WSEndpointMOMProxy)managedObjectManager).isInitialized()) {
   1.636 +                    close = false;
   1.637 +                }
   1.638 +
   1.639 +                if (close) {
   1.640 +                    // no further notification on scope change
   1.641 +                    LazyMOMProvider.INSTANCE.unregisterEndpoint(this);
   1.642 +                    MonitorBase.closeMOM(managedObjectManager);
   1.643 +                }
   1.644 +            }
   1.645 +            managedObjectManagerClosed = true;
   1.646 +        }
   1.647 +    }
   1.648 +
   1.649 +    public @NotNull ServerTubeAssemblerContext getAssemblerContext() {
   1.650 +        return context;
   1.651 +    }
   1.652 +}

mercurial