All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.maven.components.migration.manager.phases.AbstractMoveSourcesPhase Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you 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.marvelution.maven.components.migration.manager.phases;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.marvelution.maven.components.migration.manager.MigrationResult;
import com.marvelution.maven.components.migration.manager.configuration.MigrationDescriptor;
import com.marvelution.maven.components.migration.manager.environment.MigrationEnvironment;
import com.marvelution.maven.components.migration.manager.exception.MigrationExecutionException;
import com.marvelution.maven.components.migration.manager.exception.MigrationFailureException;
import com.marvelution.utils.io.DirectoryUtils;
import com.marvelution.utils.io.FileUtils;

/**
 * Abstract {@link MigrationPhase} for moving files from a source directory to a destination directory
 * 
 * @author Mark Rekveld
 */
public abstract class AbstractMoveSourcesPhase extends AbstractMigrationPhase {

	/**
	 * Maven default root source directory
	 */
	public static final String DEFAULT_SOURCE_ROOT_DIRECTORY = "src";

	/**
	 * Maven default root source directory
	 */
	public static final String DEFAULT_SOURCE_MAIN_DIRECTORY = "src/main";

	/**
	 * Maven default root source directory
	 */
	public static final String DEFAULT_SOURCE_TEST_DIRECTORY = "src/test";

	/**
	 * Maven default source directory
	 */
	public static final String DEFAULT_SOURCE_DIRECTORY = "src/main/java";

	/**
	 * Maven default resource directory
	 */
	public static final String DEFAULT_RESOURCE_DIRECTORY = "src/main/resources";

	/**
	 * Maven default WebApp directory
	 */
	public static final String DEFAULT_WEBAPP_DIRECTORY = "src/main/webapp";

	/**
	 * Maven default EAR App directory
	 */
	public static final String DEFAULT_EARAPP_DIRECTORY = "src/main/application";

	/**
	 * Maven default test source directory
	 */
	public static final String DEFAULT_TEST_SOURCE_DIRECTORY = "src/test/java";

	/**
	 * Maven default test resource directory
	 */
	public static final String DEFAULT_TEST_RESOURCE_DIRECTORY = "src/test/resources";

	/**
	 * Maven default site directory
	 */
	public static final String DEFAULT_SITE_DIRECTORY = "src/site";

	/**
	 * Site resources for default Maven site
	 */
	public static final String[] SITE_RESOURCES =
		new String[] {"apt/index.apt", "apt/format.apt", "fml/faq.fml", "xdoc/xdoc.xml", "site.xml"};

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult execute(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
					throws MigrationExecutionException, MigrationFailureException {
		final MigrationResult result = new MigrationResult();
		if (!descriptor.isSkipDirectoryMigration()) {
			final Map mappedSources = mapSourceAndDestinationDirectories(descriptor);
			final Map files = getFilesFromMappedSources(descriptor.getWorkingDirectory(), mappedSources);
			for (final Iterator iter = mappedSources.entrySet().iterator(); iter.hasNext();) {
				final Entry mapping = (Entry) iter.next();
				final String sourceDir = mapping.getKey().toString();
				final String destinationDir = mapping.getValue().toString();
				final File source = new File(descriptor.getWorkingDirectory(), sourceDir);
				final File destination = new File(descriptor.getWorkingDirectory(), destinationDir);
				if (!destination.exists()) {
					destination.mkdirs();
				}
				try {
					getLogger().info("Moving sources from '" + sourceDir + "' to '" + destinationDir + "'");
					final List fileList = (List) files.get(sourceDir);
					if (fileList != null && !fileList.isEmpty()) {
						DirectoryUtils.copyFilesToDirectory(source, destination, fileList);
						for (final Iterator fileIter = fileList.iterator(); fileIter.hasNext();) {
							final String file = (String) fileIter.next();
							if (file.indexOf(File.separator) > 0) {
								final String dirname = file.substring(0, file.indexOf(File.separator));
								FileUtils.deleteDirectory(new File(source, dirname));
							} else {
								final File del = new File(source, file);
								FileUtils.forceDelete(del);
							}
						}
					}
					// Source directories should be empty now...
					// And if they are, delete them
					if (source.list() != null && source.list().length == 0) {
						FileUtils.deleteDirectory(source);
					}
				} catch (IOException e) {
					throw new MigrationExecutionException("Failed to move sources from '" + source.getAbsolutePath()
						+ "' to destination '" + destination.getAbsolutePath() + "'", e);
				}
			}
		} else {
			getLogger().info("Skipping directory migration");
		}
		processSiteResources(descriptor);
		result.setResultCode(MigrationResult.SUCCESS);
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult clean(MigrationDescriptor descriptor, MigrationEnvironment environment) {
		final MigrationResult result = new MigrationResult();
		result.setStartTime(System.currentTimeMillis());
		getLogger().info("Removing Site resources");
		try {
			FileUtils.deleteDirectory(new File(descriptor.getWorkingDirectory(), DEFAULT_SITE_DIRECTORY));
			result.setResultCode(MigrationResult.SUCCESS);
		} catch (IOException e) {
			result.setResultCode(MigrationResult.ERROR);
			getLogger().error("Failed to remove the Site resources from the project", e);
		}
		result.setEndTime(System.currentTimeMillis());
		return result;
	}

	/**
	 * Maps source directories to destination directories in a {@link Map} where the source directory is the key and
	 * the destination is the value Specify a null as value for a key and the directory for the key will be deleted
	 * 
	 * @param descriptor the {@link MigrationDescriptor}
	 * @return the Mapped directories
	 */
	protected abstract Map mapSourceAndDestinationDirectories(final MigrationDescriptor descriptor);

	/**
	 * Processes the Site resources for the phase
	 * 
	 * @param descriptor the {@link MigrationDescriptor}
	 * @throws MigrationExecutionException in case of processing exceptions
	 */
	protected abstract void processSiteResources(final MigrationDescriptor descriptor)
					throws MigrationExecutionException;

	/**
	 * Gets a Resource from the class-path
	 * 
	 * @param name the name of the resource
	 * @param loader the {@link ClassLoader} to use, may be null to use the {@link ClassLoader} of the
	 *            current thread
	 * @return the {@link InputStream} of the resource
	 */
	protected InputStream getResourceStream(String name, ClassLoader loader) {
		if (loader == null) {
			return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
		}
		return loader.getResourceAsStream(name);
	}

	/**
	 * list the files from the source directories
	 * 
	 * @param basedir the base directory where all the source directories are located in
	 * @param mappedSources the mapped source directories
	 * @return the {@link Map} of source directories with there files
	 */
	protected Map getFilesFromMappedSources(File basedir, Map mappedSources) {
		final Map mappedFiles = new HashMap();
		for (final Iterator iter = mappedSources.keySet().iterator(); iter.hasNext();) {
			final String dir = (String) iter.next();
			final File dirFile = new File(basedir, dir);
			String[] files;
			if (dirFile.exists()) {
				files = DirectoryUtils.scanDirectoryForFiles(dirFile, new String[] {"**/**"});
			} else {
				files = new String[0];
			}
			mappedFiles.put(dir, Arrays.asList(files));
		}
		return mappedFiles;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy