001/*
002 * (C) Copyright 2013 Nuxeo SA (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 deactivate(ComponentContext context) {
072        super.deactivate(context);
073
074        for (Endpoint ep : endpoints.values()) {
075            ep.stop();
076        }
077        endpoints.clear();
078    }
079
080    @Override
081    public void applicationStarted(ComponentContext context) {
082        if (!Framework.isTestModeSet()) {
083            publishEndpoints();
084        }
085    }
086
087    @Override
088    public void publishEndpoints() {
089        for (WSEndpointDescriptor desc : regitry.getContributions()) {
090            try {
091                stopIfExists(desc.name);
092
093                Endpoint ep = desc.toEndpoint();
094
095                ep.publish(desc.address);
096                desc.configurePostPublishing(ep);
097
098                if (ep.isPublished()) {
099                    endpoints.put(desc.name, ep);
100                } else {
101                    log.warn("Endpoint publishing is failing: " + desc.name);
102                }
103            } catch (IOException | IllegalAccessException | InstantiationException e) {
104                log.warn("Unable to register endpoint: " + desc.name, e);
105            }
106        }
107    }
108
109    @Override
110    public Collection<WSEndpointDescriptor> getDescriptors() {
111        return regitry.getContributions();
112    }
113
114    protected void stopIfExists(String name) {
115        // Stop endpoint publishing
116        Endpoint ep = endpoints.get(name);
117        if (ep != null) {
118            ep.stop();
119            endpoints.remove(name);
120        }
121    }
122
123    @Override
124    public int getApplicationStartedOrder() {
125        return APPLICATION_STARTED_ORDER;
126    }
127}