org.eclipse.swordfish.internal.resolver.backend.remote.SwordfishRegistryProvider.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swordfish.internal.resolver.backend.remote.SwordfishRegistryProvider.java

Source

/*******************************************************************************
 * Copyright (c) 2008, 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.swordfish.internal.resolver.backend.remote;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swordfish.core.SwordfishException;
import org.eclipse.swordfish.core.resolver.backend.ClientRequest;
import org.eclipse.swordfish.core.resolver.backend.ClientResponse;
import org.eclipse.swordfish.core.resolver.backend.RegistryProxy;
import org.eclipse.swordfish.core.resolver.backend.ProxyConstants.Status;
import org.eclipse.swordfish.core.resolver.registry.ServiceDescription;
import org.eclipse.swordfish.internal.resolver.backend.base.AbstractDocumentProvider;
import org.eclipse.swordfish.internal.resolver.backend.base.RegistryProxyFactory;
import org.eclipse.swordfish.internal.resolver.backend.base.wsdl.ServiceDescriptionReader;
import org.eclipse.swordfish.internal.resolver.backend.base.wsdl.WSDLList;
import org.springframework.util.Assert;

/**
 *
 */
public class SwordfishRegistryProvider extends AbstractDocumentProvider {

    private static final Log LOG = LogFactory.getLog(SwordfishRegistryProvider.class);

    private static final String REGISTRY_URL_PROPERTY = "registryURL";

    public static final String REGISTRY_URL_SYSPROPERTY = "org.eclipse.swordfish.registry.url";

    public static final String DESCRIPTION_CONTEXT = "wsdl";

    private URL registryURL;

    private RegistryProxy proxy;

    private ServiceDescriptionReader<?> wsdlReader;

    public List<ServiceDescription<?>> getServiceProviderDescriptions(QName interfaceName) {
        List<ServiceDescription<?>> descriptions = new ArrayList<ServiceDescription<?>>();

        try {
            checkProperties();

            URL descripionBaseUrl = getResourceUrl(getRegistryURL().toString(), DESCRIPTION_CONTEXT);

            Map<String, String> properties = new HashMap<String, String>();
            properties.put("type", "portType");
            properties.put("targetNamespace", interfaceName.getNamespaceURI());
            properties.put("name", interfaceName.getLocalPart());

            ClientRequest request = RegistryProxyFactory.getInstance().createRequest();
            request.setURI(descripionBaseUrl.toURI());
            request.setProperties(properties);
            request.setEntityType(WSDLList.class);

            ClientResponse response = getProxy().get(request);
            if (!response.getStatus().equals(Status.SUCCESS)) {
                handleFailure(response);
            }

            WSDLList descriptionUrls = WSDLList.class.cast(response.getEntity());
            for (String url : descriptionUrls.getUrl()) {
                URL nextDescriptionUrl = getResourceUrl(descripionBaseUrl.toString(), url);
                ServiceDescription<?> description = getWsdlReader().readDescription(nextDescriptionUrl);
                descriptions.add(description);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Successfully retrieved service description: " + description.getServiceName()
                            + " for portType: " + interfaceName);
                }
            }
        } catch (SwordfishException se) {
            throw se;
        } catch (Exception e) {
            LOG.error("Error resolving endpoint - couldn't retrieve " + "service description for port type "
                    + interfaceName, e);
            throw new SwordfishException("Error resolving endpoint - couldn't retrieve "
                    + "service description for port type " + interfaceName, e);
        }
        return descriptions;
    }

    @Override
    public void onReceiveConfiguration(Map<String, Object> configuration) {
        super.onReceiveConfiguration(configuration);

        if (System.getProperty(REGISTRY_URL_SYSPROPERTY) != null) {
            try {
                URL registryUrlSystemProperty = new URL(System.getProperty(REGISTRY_URL_SYSPROPERTY));
                setRegistryURL(registryUrlSystemProperty);
            } catch (MalformedURLException e) {
                LOG.error("Couldn't initialize Swordfish "
                        + "registry proxy: malformed registry URL specified in the system property", e);
                throw new IllegalArgumentException("Couldn't initialize Swordfish "
                        + "registry proxy: malformed registry URL specified in the system property", e);
            }
        } else {
            if (configuration != null && configuration.containsKey(REGISTRY_URL_PROPERTY)) {
                try {
                    setRegistryURL(new URL((String) configuration.get(REGISTRY_URL_PROPERTY)));
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Service registry URL has been set to: " + getRegistryURL());
                    }
                } catch (MalformedURLException e) {
                    LOG.error("Couldn't initialize Swordfish " + "registry proxy: malformed registry URL specified",
                            e);
                    throw new IllegalArgumentException(
                            "Couldn't initialize Swordfish " + "registry proxy: malformed registry URL specified",
                            e);
                }
            }
        }
    }

    private void handleFailure(ClientResponse response) {
        RuntimeException exception = null;
        Throwable remoteException = null;

        if (response.getEntity() != null && Throwable.class.isAssignableFrom(response.getEntity().getClass())) {
            remoteException = (Throwable) response.getEntity();
        }

        if (response.getStatus().equals(Status.ERROR)) {
            exception = new SwordfishException("Service registry is not accessible on URL " + getRegistryURL(),
                    remoteException);
        } else if (response.getStatus().equals(Status.NOT_FOUND)) {
            exception = new SwordfishException("Service description has not been found in " + "service registry",
                    remoteException);
        } else if (response.getStatus().equals(Status.MALFORMED_QUERY)) {
            exception = new SwordfishException(
                    "Request has not been recognized by " + "remote registry - wrong query specified",
                    remoteException);
        } else {
            exception = new SwordfishException(
                    "Error resolving endpoint - cannot retrieve " + "service description", remoteException);
        }
        throw exception;
    }

    private void checkProperties() {
        Object[] props = { getRegistryURL(), getProxy(), getWsdlReader() };
        Assert.noNullElements(props, "Service registry properties are not initialized.");
    }

    private URL getResourceUrl(String registryBaseURL, String relativeURL) throws MalformedURLException {
        StringBuilder descripionBaseUrl = new StringBuilder(registryBaseURL);

        if (!registryBaseURL.endsWith("/")) {
            descripionBaseUrl.append("/");
        }

        if (relativeURL.startsWith(".")) {
            relativeURL = relativeURL.substring(1);
        }
        if (relativeURL.startsWith("/")) {
            relativeURL = relativeURL.substring(1);
        }
        descripionBaseUrl.append(relativeURL);

        return new URL(descripionBaseUrl.toString());
    }

    public URL getRegistryURL() {
        return registryURL;
    }

    public void setRegistryURL(URL registryURL) {
        this.registryURL = registryURL;
    }

    public RegistryProxy getProxy() {
        return proxy;
    }

    public void setProxy(RegistryProxy proxy) {
        this.proxy = proxy;
    }

    public ServiceDescriptionReader<?> getWsdlReader() {
        return wsdlReader;
    }

    public void setWsdlReader(ServiceDescriptionReader<?> wsdlReader) {
        this.wsdlReader = wsdlReader;
    }

}