/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.webservices.service;

import javax.management.MBeanServer;

import org.jboss.as.webservices.util.WSServices;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceBuilder.DependencyType;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceListener;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.ServiceTarget;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jboss.ws.common.management.DefaultEndpointRegistry;
import org.jboss.ws.common.management.ManagedEndpointRegistry;
import org.jboss.wsf.spi.management.EndpointRegistry;

/**
 * The service for the endpoint registry
 *
 * @author alessio.soldano@jboss.com
 * @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
 */
public final class EndpointRegistryService implements Service<EndpointRegistry> {

    private static final ServiceName MBEAN_SERVER_NAME = ServiceName.JBOSS.append("mbean", "server");
    private static final EndpointRegistryService INSTANCE = new EndpointRegistryService();

    private volatile EndpointRegistry registry;
    private final InjectedValue<MBeanServer> injectedMBeanServer = new InjectedValue<MBeanServer>();

    private EndpointRegistryService() {
        // forbidden inheritance
    }

    @Override
    public EndpointRegistry getValue() {
        return registry;
    }

    @Override
    public void start(final StartContext context) throws StartException {
        if (injectedMBeanServer.getValue() != null) {
            final ManagedEndpointRegistry managedEndpointRegistry = new ManagedEndpointRegistry();
            managedEndpointRegistry.setMbeanServer(injectedMBeanServer.getValue());
            registry = managedEndpointRegistry;
        } else {
            registry = new DefaultEndpointRegistry();
        }
    }

    @Override
    public void stop(final StopContext context) {
    }

    private InjectedValue<MBeanServer> getMBeanServerInjector() {
        return injectedMBeanServer;
    }

    public static ServiceController<?> install(final ServiceTarget serviceTarget, final ServiceListener<Object> listener) {
        final ServiceBuilder<EndpointRegistry> builder = serviceTarget.addService(WSServices.REGISTRY_SERVICE, INSTANCE);
        builder.addDependency(DependencyType.REQUIRED, MBEAN_SERVER_NAME, MBeanServer.class, INSTANCE.getMBeanServerInjector());
        builder.addListener(listener);
        builder.setInitialMode(Mode.ACTIVE);
        return builder.install();
    }

}

@@ -1732,23 +1732,23 @@
 agement.
-Default
+Managed
 Endpoint
@@ -1768,39 +1768,121 @@
 ort org.jboss.ws
-.common
+f.spi.SPIProvider;%0Aimport org.jboss.wsf.spi.SPIProviderResolver;%0Aimport org.jboss.wsf.spi
 .management.Mana
@@ -1869,39 +1869,32 @@
 .spi.management.
-Managed
 EndpointRegistry
@@ -1947,16 +1947,23 @@
 Registry
+Factory
 ;%0A%0A/**%0A 
@@ -2391,57 +2391,8 @@
 e();
-%0A%0A    private volatile EndpointRegistry registry;
 %0A   
@@ -2488,16 +2488,64 @@
 rver%3E();
+%0A    private volatile EndpointRegistry registry;
 %0A%0A    pr
@@ -2582,41 +2582,8 @@
 ) %7B%0A
-        // forbidden inheritance%0A
     
@@ -2772,50 +2772,278 @@
     
-if (injectedMBeanServer.getValue() != null
+final SPIProvider spiProvider = SPIProviderResolver.getInstance().getProvider();%0A        registry = spiProvider.getSPI(EndpointRegistryFactory.class).getEndpointRegistry();%0A        if (injectedMBeanServer.getValue() != null && registry instanceof ManagedEndpointRegistry
 ) %7B%0A
@@ -3110,20 +3110,17 @@
 istry = 
-new 
+(
 ManagedE
@@ -3126,34 +3126,41 @@
 EndpointRegistry
-(
 )
+registry
 ;%0A            ma
@@ -3233,127 +3233,8 @@
 ));%0A
-            registry = managedEndpointRegistry;%0A        %7D else %7B%0A            registry = new DefaultEndpointRegistry();%0A
     

fix WS endpoint registry integration code