Java tutorial
/* Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.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. */ package com.c4om.autoconf.ulysses.configanalyzer.configurationextractor; import static java.nio.file.StandardCopyOption.*; import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.commons.io.FileUtils; import com.c4om.autoconf.ulysses.configanalyzer.core.Application; import com.c4om.autoconf.ulysses.configanalyzer.core.ConfigureAppsTarget; import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.ConfigurationAnalysisContext; import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.ExtractedConfiguration; import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.impl.FileExtractedConfiguration; import com.c4om.autoconf.ulysses.configanalyzer.core.exceptions.ConfigurationExtractionException; import com.c4om.autoconf.ulysses.interfaces.Environment; import com.c4om.autoconf.ulysses.interfaces.Target; /** * This extractor just copies the contents from a directory on the local file system to a temporary location to work with them. * @author Pablo Alonso Rodrguez (Center for Open Middleware - UPM) * */ public class LocalCopyConfigurationsExtractor implements ConfigurationsExtractor { /** * Key for the property with the runtime configuration file/folder name. */ private static final String PROPERTY_KEY_RUNTIME_CONFIGURATION_FILENAME = "runtimeConfiguration.filename"; /** * Constructor */ public LocalCopyConfigurationsExtractor() { } /** * This method performs the extraction. * The context must contain: * <ul> * <li>One or more {@link ConfigureAppsTarget} returned by {@link ConfigurationAnalysisContext#getInputTargets()}. * All the referenced {@link Application} must return "file://" URIs at {@link Application#getConfigurationURIs()}.</li> * <li>An {@link Environment} object returned by {@link ConfigurationAnalysisContext#getInputEnvironment()}, from which * the configuration of the runtime where all the referenced applications are run.</li> * </ul> * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.configurationextractor.ConfigurationsExtractor#extractConfigurations(com.c4om.autoconf.ulysses.interfaces.configanalyzer.core.datastructures.ConfigurationAnalysisContext) */ @Override public void extractConfigurations(ConfigurationAnalysisContext context) throws ConfigurationExtractionException { try { Path tempFolderPath = Files.createTempDirectory("ConfigurationAnalyzer"); for (Target currentTarget : context.getInputTargets().values()) { if (!(currentTarget instanceof ConfigureAppsTarget)) { continue; } Properties configurationAnalyzerSettings = context.getConfigurationAnalyzerSettings(); String runtimeConfigurationDirName = configurationAnalyzerSettings .getProperty(PROPERTY_KEY_RUNTIME_CONFIGURATION_FILENAME); if (runtimeConfigurationDirName == null) { throw new ConfigurationExtractionException( "Property '" + PROPERTY_KEY_RUNTIME_CONFIGURATION_FILENAME + "' not found"); } ConfigureAppsTarget currentConfigureAppsTarget = (ConfigureAppsTarget) currentTarget; for (String appId : currentConfigureAppsTarget.getParameters().keySet()) { Path subfolderForApp = tempFolderPath.resolve(Paths.get(appId)); List<File> copiedFiles = new ArrayList<>(); Files.createDirectory(subfolderForApp); //We copy the application configuration files to the root of the directory. //We also copy the runtime configuration. Application currentApplication = (Application) currentConfigureAppsTarget.getParameters() .get(appId); for (URI configurationURI : currentApplication.getConfigurationURIs()) { //TODO: Let other configuration URIs live, specially, SVN-based ones, which should be extracted by other extractors. This would require some refactoring. if (!configurationURI.getScheme().equalsIgnoreCase("file")) { throw new ConfigurationExtractionException( "URI '" + configurationURI.toString() + "' does not have a 'file' scheme"); } Path configurationPath = Paths.get(configurationURI); Path configurationPathName = configurationPath.getFileName(); if (configurationPathName.toString().equals(runtimeConfigurationDirName)) { throw new ConfigurationExtractionException( "One of the application configuration files has the same name than the runtime configuration folder ('" + runtimeConfigurationDirName + "')"); } Path destinationConfigurationPath = Files.copy(configurationPath, subfolderForApp.resolve(configurationPathName), REPLACE_EXISTING); File destinationConfigurationFile = destinationConfigurationPath.toFile(); copiedFiles.add(destinationConfigurationFile); } File runtimeConfigurationDirectory = new File( context.getInputEnvironment().getApplicationConfigurations().get(appId)); File destinationRuntimeConfigurationDirectory = subfolderForApp .resolve(runtimeConfigurationDirName).toFile(); FileUtils.copyDirectory(runtimeConfigurationDirectory, destinationRuntimeConfigurationDirectory); copiedFiles.add(destinationRuntimeConfigurationDirectory); ExtractedConfiguration<File> extractedConfiguration = new FileExtractedConfiguration(appId, currentTarget, copiedFiles); context.getExtractedConfigurations().add(extractedConfiguration); } } } catch (IOException e) { throw new ConfigurationExtractionException(e); } } }