001package org.nuxeo.ecm.platform.ws;
002
003import java.io.IOException;
004import java.util.Collection;
005import java.util.HashMap;
006import java.util.Map;
007
008import javax.xml.ws.Endpoint;
009
010import org.apache.commons.logging.Log;
011import org.apache.commons.logging.LogFactory;
012import org.nuxeo.ecm.platform.api.ws.WSEndpointDescriptor;
013import org.nuxeo.runtime.api.Framework;
014import org.nuxeo.runtime.model.ComponentContext;
015import org.nuxeo.runtime.model.ComponentInstance;
016import org.nuxeo.runtime.model.DefaultComponent;
017
018/**
019 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
020 * @since 5.7.3
021 */
022public class WSEndpointManagerImpl extends DefaultComponent implements WSEndpointManager {
023
024    public static final String ENDPOINT_EP = "endpoint";
025
026    private static final Log log = LogFactory.getLog(WSEndpointManagerImpl.class);
027
028    private static final int APPLICATION_STARTED_ORDER = 1500;
029
030    protected WSEndpointRegistry regitry = new WSEndpointRegistry();
031
032    protected Map<String, Endpoint> endpoints = new HashMap<>();
033
034    @Override
035    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
036        if (ENDPOINT_EP.equals(extensionPoint)) {
037            regitry.addContribution((WSEndpointDescriptor) contribution);
038        } else {
039            log.info("Unknown extension point: " + extensionPoint);
040        }
041    }
042
043    @Override
044    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
045        if (ENDPOINT_EP.equals(extensionPoint)) {
046            WSEndpointDescriptor descriptor = (WSEndpointDescriptor) contribution;
047            stopIfExists(descriptor.name);
048            regitry.removeContribution(descriptor);
049        }
050    }
051
052    @Override
053    public void deactivate(ComponentContext context) {
054        super.deactivate(context);
055
056        for (Endpoint ep : endpoints.values()) {
057            ep.stop();
058        }
059        endpoints.clear();
060    }
061
062    @Override
063    public void applicationStarted(ComponentContext context) {
064        if (!Framework.isTestModeSet()) {
065            publishEndpoints();
066        }
067    }
068
069    @Override
070    public void publishEndpoints() {
071        for (WSEndpointDescriptor desc : regitry.getContributions()) {
072            try {
073                stopIfExists(desc.name);
074
075                Endpoint ep = desc.toEndpoint();
076
077                ep.publish(desc.address);
078                desc.configurePostPublishing(ep);
079
080                if (ep.isPublished()) {
081                    endpoints.put(desc.name, ep);
082                } else {
083                    log.warn("Endpoint publishing is failing: " + desc.name);
084                }
085            } catch (IOException | IllegalAccessException | InstantiationException e) {
086                log.warn("Unable to register endpoint: " + desc.name, e);
087            }
088        }
089    }
090
091    @Override
092    public Collection<WSEndpointDescriptor> getDescriptors() {
093        return regitry.getContributions();
094    }
095
096    protected void stopIfExists(String name) {
097        // Stop endpoint publishing
098        Endpoint ep = endpoints.get(name);
099        if (ep != null) {
100            ep.stop();
101            endpoints.remove(name);
102        }
103    }
104
105    @Override
106    public int getApplicationStartedOrder() {
107        return APPLICATION_STARTED_ORDER;
108    }
109}