com.zaubersoftware.mule.module.jenkins.JenkinsCloudConnector.java Source code

Java tutorial

Introduction

Here is the source code for com.zaubersoftware.mule.module.jenkins.JenkinsCloudConnector.java

Source

/**
 * Copyright (c) 2011 Zauber S.A. <http://www.zaubersoftware.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * This file was automatically generated by the Mule Cloud Connector Development Kit
 */
package com.zaubersoftware.mule.module.jenkins;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import org.apache.commons.lang.Validate;
import org.mule.api.lifecycle.Initialisable;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.tools.cloudconnect.annotations.Connector;
import org.mule.tools.cloudconnect.annotations.Operation;
import org.mule.tools.cloudconnect.annotations.Parameter;
import org.mule.tools.cloudconnect.annotations.Property;
import org.springframework.beans.PropertyEditorRegistrySupport;
import org.springframework.beans.support.ResourceEditorRegistrar;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor;
import org.springframework.core.io.ResourceLoader;

import com.zaubersoftware.mule.module.jenkins.api.JenkinsService;
import com.zaubersoftware.mule.module.jenkins.api.impl.HttpJenkinsService;
import com.zaubersoftware.mule.module.jenkins.template.velocity.VelocityTemplate;

@Connector(namespacePrefix = "jenkins")
public class JenkinsCloudConnector implements Initialisable {
    /** URL of the jenkins server: Ej: https://ci.example.org/jenkins/ */
    @Property
    private URI jenkinsBaseURL;
    /** username used to login in as */
    @Property(optional = true)
    private String jenkinsUsername;
    /** password used to login in as */
    @Property(optional = true)
    private String jenkinsPassword;
    /** Service facade. Used to override on custom environmets (like unit testing) */
    @Property(name = "service-ref", optional = true)
    private JenkinsService service;
    private ResourceLoader loader;

    /** Creates the JenkinsCloudConnector. */
    public JenkinsCloudConnector() {
        loader = new DefaultResourceLoader();
        new ResourceEditorRegistrar(loader).registerCustomEditors(new PropertyEditorRegistrySupport());
    }

    /** Creates the JenkinsCloudConnector. */
    public JenkinsCloudConnector(final URI uri) throws InitialisationException {
        this();
        setJenkinsBaseURL(uri);
        initialise();
    }

    /** Creates the JenkinsCloudConnector. */
    public JenkinsCloudConnector(final URI uri, final String username, final String password)
            throws InitialisationException {
        this();
        setJenkinsBaseURL(uri);
        setJenkinsUsername(username);
        setJenkinsPassword(password);
        initialise();
    }

    /**
     * Checks if the project exists in the jenkins instance
     * 
     * {@code <jenkins:exists projectName="#[payload]" />}
     * 
     * @param projectName name of the project to check
     */
    @Operation
    public boolean exists(@Parameter final String projectName) {
        return service.exists(projectName);
    }

    /**
     * Creates a Hudson project
     * 
     * {@code <jenkins:create projectName="#[map-payload:name]" template="#[map-payload:content]"/>}
     * 
     * @param projectName Name of the project to create
     * @param template template for the project creation. Could be an {@link InputStream}, {@link String}
     *         {@link ByteArrayInputStream}, etc
     */
    @Operation
    public void create(@Parameter final String projectName, @Parameter final InputStream template) {
        service.create(projectName, template);
    }

    /**
     * Build a project.
     *
     * {@code <jenkins:build projectName="#[payload]" />}
     *
     * @param projectName project to build
     */
    @Operation
    public void build(@Parameter final String projectName) {
        service.build(projectName);
    }

    /**
     * Utility operation: Renders a template. Used in combination with the create operation 
     * 
     * {@code 
     * <jenkins:render-template resourcePath="classpath:config.xml" >
     *       <jenkins:models>
     *           <jenkins:model key="groupId" value="foo-bar"/>
     *       </jenkins:models>
     * </jenkins:render-template>
     * <jenkins:create projectName="foo" template="#[payload]"/>
     * }
     *
     * @param resourcePath template's resource path (Spring's {@link Resource}...Ex: classpath:com/.... http:..., etc.)
     * @param models model to replace in the template
     * @param charset template's charset
     * @param templateType  template format
     * @return an string with the template rendered
     */
    @Operation
    public String renderTemplate(final String resourcePath, final Map<String, String> models,
            @Parameter(optional = true, defaultValue = "utf-8") final String charset,
            @Parameter(optional = true, defaultValue = "VELOCITY") TemplateType templateType) {
        final ResourceEditor editor = new ResourceEditor(loader);
        editor.setAsText(resourcePath);
        final Resource resource = (Resource) editor.getValue();
        final String ret;
        if (TemplateType.VELOCITY.equals(templateType)) {
            ret = new VelocityTemplate(resource, charset).render(models);
        } else {
            throw new IllegalArgumentException("Don't know how to render" + templateType);
        }
        return ret;
    }

    @Override
    public void initialise() throws InitialisationException {
        if (service == null) {
            Validate.notNull(jenkinsBaseURL, "base url is null");
            if ((jenkinsUsername == null && jenkinsPassword != null)
                    || (jenkinsUsername != null && jenkinsPassword == null)) {
                throw new IllegalArgumentException("Missing username or password");
            }
            try {
                service = new HttpJenkinsService(jenkinsBaseURL, jenkinsUsername, jenkinsPassword);
            } catch (final KeyManagementException e) {
                throw new InitialisationException(e, this);
            } catch (final NoSuchAlgorithmException e) {
                throw new InitialisationException(e, this);
            }
        }
    }

    /** @return the facade */
    public JenkinsService getService() {
        return service;
    }

    public void setService(final JenkinsService service) {
        this.service = service;
    }

    public final URI getJenkinsBaseURL() {
        return jenkinsBaseURL;
    }

    public final void setJenkinsBaseURL(URI jenkinsBaseURL) {
        this.jenkinsBaseURL = jenkinsBaseURL;
    }

    public String getJenkinsUsername() {
        return jenkinsUsername;
    }

    public void setJenkinsUsername(final String jenkinsUsername) {
        this.jenkinsUsername = jenkinsUsername;
    }

    public String getJenkinsPassword() {
        return jenkinsPassword;
    }

    public void setJenkinsPassword(final String jenkinsPassword) {
        this.jenkinsPassword = jenkinsPassword;
    }

    public enum TemplateType {
        VELOCITY
    }
}