org.ow2.petals.cloud.tools.generator.soap.SOAPArtifactGenerator.java Source code

Java tutorial

Introduction

Here is the source code for org.ow2.petals.cloud.tools.generator.soap.SOAPArtifactGenerator.java

Source

/****************************************************************************
 *
 * Copyright (c) 2013, Linagora
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/
package org.ow2.petals.cloud.tools.generator.soap;

import com.google.common.collect.Range;
import org.apache.commons.io.FileUtils;
import org.ow2.petals.cloud.controllers.api.ControllerException;
import org.ow2.petals.cloud.controllers.api.artifact.Artifact;
import org.ow2.petals.cloud.controllers.api.artifact.ArtifactGenerator;
import org.ow2.petals.cloud.controllers.api.artifact.ArtifactType;
import org.ow2.petals.tools.generator.jbi.ws2jbi.Constants;
import org.ow2.petals.tools.generator.jbi.ws2jbi.WS2Jbi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Generate SOAP artifacts
 *
 * @author Christophe Hamerling - chamerling@linagora.com
 */
public class SOAPArtifactGenerator implements ArtifactGenerator {

    private static final Logger LOG = LoggerFactory.getLogger(SOAPArtifactGenerator.class);

    /**
     * Where to generate files
     */
    File folder;

    /**
     *
     * @param folder
     */
    public SOAPArtifactGenerator(File folder) {
        this.folder = folder;
    }

    public String getName() {
        return SOAPArtifactGenerator.class.getCanonicalName();
    }

    public String getVersion() {
        return "1.0";
    }

    public Range getSupportedRange() {
        return Range.open("0.0", "1.0");
    }

    public String getMode() {
        return "provides";
    }

    public String getProtocol() {
        return "soap";
    }

    /**
     *
     * @param properties requires a 'wsdl' property with the WSDL URL (local/remote).
     *
     * @return
     * @throws ControllerException
     */
    public Artifact generate(Properties properties) throws ControllerException {
        if (properties.getProperty("wsdl") == null) {
            throw new ControllerException("Can not generate artifact from null WSDL");
        }

        Properties props = new Properties();
        Date date = new Date();
        props.setProperty("info", "generated by the SOAPGenerator bundle on " + date);
        Artifact artifact = new Artifact();
        artifact.setDate(date);
        artifact.setType(new ArtifactType("soap", "4.0"));
        artifact.setProperties(props);
        try {
            artifact.setUrl(generateFile(properties.getProperty("wsdl")).toURI().toURL());
        } catch (Exception e) {
            throw new ControllerException(e);
        }
        return artifact;
    }

    protected File generateFile(String wsdl) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put(org.ow2.petals.tools.generator.commons.Constants.COMPONENT_VERSION, "4.0");
        map.put(Constants.OUTPUT_DIR, this.folder.getAbsolutePath());
        URI wsdlURI = new URI(wsdl);
        return new WS2Jbi(wsdlURI, map).generate();
    }

    /**
     * Clean the generator folder
     */
    public void clean() {
        LOG.info("Cleaning the generator folder...");
        if (folder != null) {
            try {
                FileUtils.deleteDirectory(folder);
            } catch (IOException e) {
                LOG.warn("Can not delete folder", e);
            }
        }
    }
}