com.photon.phresco.plugins.DrupalPackage.java Source code

Java tutorial

Introduction

Here is the source code for com.photon.phresco.plugins.DrupalPackage.java

Source

/*
 * ###
 * drupal-maven-plugin Maven Mojo
 * 
 * Copyright (C) 1999 - 2012 Photon Infotech Inc.
 * 
 * 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.
 * ###
 */
package com.photon.phresco.plugins;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import com.photon.phresco.plugin.commons.PluginConstants;
import com.photon.phresco.plugin.commons.PluginUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.photon.phresco.commons.BuildInfo;
import com.photon.phresco.exception.PhrescoException;
import com.photon.phresco.util.ArchiveUtil;
import com.photon.phresco.util.ArchiveUtil.ArchiveType;

/**
 * Goal which builds the Drupal project
 * 
 * @goal package
 * 
 * @execute goal="clean"
 */

public class DrupalPackage extends AbstractMojo implements PluginConstants {

    /**
     * The Maven project.
     * 
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * @parameter expression="${project.basedir}" required="true"
     * @readonly
     */
    protected File baseDir;

    /**
     * @parameter expression="${environmentName}" required="true"
     */
    protected String environmentName;

    /**
     * @parameter expression="${buildName}" required="true"
     */
    protected String buildName;

    /**
     * @parameter expression="${buildNumber}" required="true"
     */
    protected String buildNumber;

    protected int buildNo;

    private File targetDir;
    private File srcDir;
    private File buildDir;
    private File buildInfoFile;
    private List<BuildInfo> buildInfoList;
    private int nextBuildNo;
    private String zipName;
    private Date currentDate;

    public void execute() throws MojoExecutionException, MojoFailureException {
        init();
        boolean buildStatus = build();
        writeBuildInfo(buildStatus);
    }

    private void init() throws MojoExecutionException {
        try {
            buildInfoList = new ArrayList<BuildInfo>(); // initialization
            srcDir = new File(baseDir.getPath() + File.separator + DRUPAL_SOURCE_DIR);
            buildDir = new File(baseDir.getPath() + BUILD_DIRECTORY);
            if (!buildDir.exists()) {
                buildDir.mkdir();
                getLog().info("Build directory created..." + buildDir.getPath());
            }
            buildInfoFile = new File(buildDir.getPath() + BUILD_INFO_FILE);
            targetDir = new File(baseDir + File.separator + TARGET_DIRECTORY);
            nextBuildNo = generateNextBuildNo();
            currentDate = Calendar.getInstance().getTime();
        } catch (Exception e) {
            getLog().error(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    private boolean build() throws MojoExecutionException {
        boolean isBuildSuccess = true;
        try {
            configure();
            FileUtils.copyDirectory(srcDir, targetDir);
            createPackage();
        } catch (Exception e) {
            isBuildSuccess = false;
            getLog().error(e);
        }
        return isBuildSuccess;
    }

    private void configure() throws MojoExecutionException {
        try {
            getLog().info("Configuring the project....");
            File srcConfigFile = new File(baseDir + DRUPAL_SOURCE_CONFIG_FILE);
            String basedir = baseDir.getName();
            PluginUtils pu = new PluginUtils();
            pu.executeUtil(environmentName, basedir, srcConfigFile);
            pu.encryptConfigFile(srcConfigFile.getPath());
        } catch (PhrescoException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    private void createPackage() throws MojoExecutionException {
        try {
            if (buildName != null) {
                zipName = buildName + ".zip";
            } else {
                if (buildNumber != null) {
                    zipName = PROJECT_CODE + buildNumber + STR_UNDERSCORE + getTimeStampForBuildName(currentDate)
                            + ".zip";
                } else {
                    zipName = PROJECT_CODE + nextBuildNo + STR_UNDERSCORE + getTimeStampForBuildName(currentDate)
                            + ".zip";
                }
            }
            String zipFilePath = buildDir.getPath() + File.separator + zipName;
            ArchiveUtil.createArchive(targetDir.getPath(), zipFilePath, ArchiveType.ZIP);
        } catch (PhrescoException e) {
            throw new MojoExecutionException(e.getErrorMessage(), e);
        }
    }

    private void writeBuildInfo(boolean isBuildSuccess) throws MojoExecutionException {

        try {
            if (buildNumber != null) {
                buildNo = Integer.parseInt(buildNumber);
            }

            PluginUtils pu = new PluginUtils();
            BuildInfo buildInfo = new BuildInfo();
            List<String> envList = pu.csvToList(environmentName);
            if (buildNo > 0) {
                buildInfo.setBuildNo(buildNo);
            } else {
                buildInfo.setBuildNo(nextBuildNo);
            }
            buildInfo.setTimeStamp(getTimeStampForDisplay(currentDate));
            if (isBuildSuccess) {
                buildInfo.setBuildStatus(SUCCESS);
            } else {
                buildInfo.setBuildStatus(FAILURE);
            }
            buildInfo.setBuildName(zipName);
            buildInfo.setEnvironments(envList);
            buildInfoList.add(buildInfo);
            Gson gson = new Gson();
            FileWriter writer = new FileWriter(buildInfoFile);
            gson.toJson(buildInfoList, writer);
            writer.close();
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    private String getTimeStampForDisplay(Date currentDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
        String timeStamp = formatter.format(currentDate.getTime());
        return timeStamp;
    }

    private String getTimeStampForBuildName(Date currentDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy-HH-mm-ss");
        String timeStamp = formatter.format(currentDate.getTime());
        return timeStamp;
    }

    private int generateNextBuildNo() throws IOException {
        int nextBuildNo = 1;
        if (!buildInfoFile.exists()) {
            return nextBuildNo;
        }

        BufferedReader read = new BufferedReader(new FileReader(buildInfoFile));
        String content = read.readLine();
        Gson gson = new Gson();
        Type listType = new TypeToken<List<BuildInfo>>() {
        }.getType();
        buildInfoList = (List<BuildInfo>) gson.fromJson(content, listType);
        if (buildInfoList == null || buildInfoList.size() == 0) {
            return nextBuildNo;
        }
        int buildArray[] = new int[buildInfoList.size()];
        int count = 0;
        for (BuildInfo buildInfo : buildInfoList) {
            buildArray[count] = buildInfo.getBuildNo();
            count++;
        }

        Arrays.sort(buildArray); // sort to the array to find the max build no

        nextBuildNo = buildArray[buildArray.length - 1] + 1; // increment 1 to the max in the build list
        return nextBuildNo;
    }
}