001/*
002 * (C) Copyright 2013-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.platform.ws;
020
021import java.io.IOException;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.xml.ws.Endpoint;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.platform.api.ws.WSEndpointDescriptor;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.ComponentContext;
033import org.nuxeo.runtime.model.ComponentInstance;
034import org.nuxeo.runtime.model.DefaultComponent;
035
036/**
037 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
038 * @since 5.7.3
039 */
040public class WSEndpointManagerImpl extends DefaultComponent implements WSEndpointManager {
041
042    public static final String ENDPOINT_EP = "endpoint";
043
044    private static final Log log = LogFactory.getLog(WSEndpointManagerImpl.class);
045
046    private static final int APPLICATION_STARTED_ORDER = 1500;
047
048    protected WSEndpointRegistry regitry = new WSEndpointRegistry();
049
050    protected Map<String, Endpoint> endpoints = new HashMap<>();
051
052    @Override
053    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
054        if (ENDPOINT_EP.equals(extensionPoint)) {
055            regitry.addContribution((WSEndpointDescriptor) contribution);
056        } else {
057            log.info("Unknown extension point: " + extensionPoint);
058        }
059    }
060
061    @Override
062    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
063        if (ENDPOINT_EP.equals(extensionPoint)) {
064            WSEndpointDescriptor descriptor = (WSEndpointDescriptor) contribution;
065            stopIfExists(descriptor.name);
066            regitry.removeContribution(descriptor);
067        }
068    }
069
070    @Override
071    public void start(ComponentContext context) {
072        if (Framework.isTestModeSet()) {
073            return;
074        }
075        publishEndpoints();
076    }
077
078    @Override
079    public void stop(ComponentContext context) {
080        for (Endpoint ep : endpoints.values()) {
081            ep.stop();
082        }
083        endpoints.clear();
084    }
085
086    @Override
087    public void publishEndpoints() {
088        for (WSEndpointDescriptor desc : regitry.getContributions()) {
089            try {
090                stopIfExists(desc.name);
091
092                Endpoint ep = desc.toEndpoint();
093
094                ep.publish(desc.address);
095                desc.configurePostPublishing(ep);
096
097                if (ep.isPublished()) {
098                    endpoints.put(desc.name, ep);
099                } else {
100                    log.warn("Endpoint publishing is failing: " + desc.name);
101                }
102            } catch (IOException | IllegalAccessException | InstantiationException e) {
103                log.warn("Unable to register endpoint: " + desc.name, e);
104            }
105        }
106    }
107
108    @Override
109    public Collection<WSEndpointDescriptor> getDescriptors() {
110        return regitry.getContributions();
111    }
112
113    protected void stopIfExists(String name) {
114        // Stop endpoint publishing
115        Endpoint ep = endpoints.get(name);
116        if (ep != null) {
117            ep.stop();
118            endpoints.remove(name);
119        }
120    }
121
122    @Override
123    public int getApplicationStartedOrder() {
124        return APPLICATION_STARTED_ORDER;
125    }
126}