Example usage for org.apache.commons.io FileUtils copyFileToDirectory

List of usage examples for org.apache.commons.io FileUtils copyFileToDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyFileToDirectory.

Prototype

public static void copyFileToDirectory(File srcFile, File destDir) throws IOException 

Source Link

Document

Copies a file to a directory preserving the file date.

Usage

From source file:org.cloudifysource.dsl.internal.packaging.Packager.java

/**
 * source folder structure: service.groovy something.zip install.sh start.sh
 * .../*from w  w  w . j av a  2 s .c o  m*/
 * <p/>
 * usmlib mylib1.jar mylib2.jar ...
 * <p/>
 * output folder: ext service.groovy something.zip install.sh start.sh ...
 * lib mylib1.jar mylib2.jar ... usm.jar
 * <p/>
 * META-INF spring pu.xml
 * 
 * @param srcFolder
 * @param recipeDirOrFile
 * @return
 * @throws IOException
 * @throws PackagingException
 */
private static File buildPuFolder(final Service service, final File recipeFile,
        final List<File> additionalServiceFiles) throws IOException, PackagingException {
    final File srcFolder = recipeFile.getParentFile();
    final File destPuFolder = File.createTempFile("gs_usm_", "");
    FileUtils.forceDelete(destPuFolder);
    FileUtils.forceMkdir(destPuFolder);
    logger.finer("created temp directory " + destPuFolder.getAbsolutePath());

    // create folders
    final File extFolder = new File(destPuFolder, "/ext");
    FileUtils.forceMkdir(extFolder);
    final File libFolder = new File(destPuFolder.getAbsolutePath(), "/lib");
    FileUtils.forceMkdir(libFolder);
    final File springFolder = new File(destPuFolder.getAbsolutePath(), "/META-INF/spring");
    FileUtils.forceMkdir(springFolder);

    logger.finer("created pu structure under " + destPuFolder);

    FileUtils.copyDirectory(srcFolder, extFolder);
    // Copy additional files to service directory
    if (additionalServiceFiles != null) {
        for (final File file : additionalServiceFiles) {
            FileUtils.copyFileToDirectory(file, extFolder);
        }
    }

    logger.finer("copied files from " + srcFolder.getAbsolutePath() + " to " + extFolder.getAbsolutePath());

    // copy all files from usmlib to lib
    final File srcUsmLibDir = new File(srcFolder, "usmlib");
    if (srcUsmLibDir.exists()) {
        FileUtils.copyDirectory(srcUsmLibDir, libFolder, SVNFileFilter.getFilter());
    }

    // copy usm.jar to lib
    // final File usmLibDir = getUsmLibDir(service);
    // final File srcUsmJar = new File(usmLibDir, "usm.jar");
    // if (!srcUsmJar.exists()) {
    // throw new PackagingException("could not find usm.jar at: " +
    // srcUsmJar);
    // }
    // FileUtils
    // .copyDirectory(usmLibDir, libFolder, SVNFileFilter.getFilter());
    // logger.finer("copied " + srcUsmJar.getName());

    // no pu.xml in source folder, lets copy the default one
    final InputStream puXmlStream = Packager.class.getClassLoader()
            .getResourceAsStream("META-INF/spring/default_usm_pu.xml");
    if (puXmlStream == null) {
        throw new PackagingException("can not find locate default pu.xml");
    }
    final File destPuXml = new File(springFolder, "pu.xml");
    FileUtils.copyInputStreamToFile(puXmlStream, destPuXml);
    logger.finer("copied pu.xml");
    try {
        puXmlStream.close();
    } catch (final IOException e) {
        logger.log(Level.SEVERE, "failed to close default_usm_pu.xml stream", e);
    }

    copyExtendedServiceFiles(service, recipeFile, extFolder);

    createManifestFile(destPuFolder);

    logger.finer("created pu folder " + destPuFolder.getAbsolutePath());
    return destPuFolder;
}

From source file:org.cloudifysource.dsl.internal.packaging.Packager.java

/***************
 * Packs an application folder into a zip file.
 * //  www.j a va2  s  . c  o  m
 * @param application
 *            the application object as read from the application file.
 * @param applicationDir
 *            the directory where the application was read from.
 * @param additionalServiceFiles
 *            additional files that should be packaged into each service
 *            directory.
 * @return the packaged zip file.
 * @throws IOException
 *             IOException.
 * @throws PackagingException
 *             PackagingException.
 */
public static File packApplication(final Application application, final File applicationDir,
        final List<File> additionalServiceFiles) throws IOException, PackagingException {

    boolean hasExtendedServices = false;
    for (final Service service : application.getServices()) {
        if (!service.getExtendedServicesPaths().isEmpty()) {
            hasExtendedServices = true;
            break;
        }
    }
    File applicationFolderToPack = applicationDir;
    // If there are no extended service we don't need to prepare an
    // application folder to pack with all the
    // extended services content.
    if (hasExtendedServices) {
        final File destApplicationFolder = createCopyDirectory(applicationFolderToPack);

        for (final Service service : application.getServices()) {
            final File extFolder = new File(destApplicationFolder + "/" + service.getName());
            final File recipeFile = DSLReader.findDefaultDSLFile(DSLUtils.SERVICE_DSL_FILE_NAME_SUFFIX,
                    new File(applicationDir + "/" + service.getName()));
            copyExtendedServiceFiles(service, recipeFile, extFolder);
        }
        // Pack the prepared folder instead of the original application
        // folder.
        applicationFolderToPack = destApplicationFolder;
    }

    if ((additionalServiceFiles != null) && (!additionalServiceFiles.isEmpty())) {
        // if a copy directory was already created, use the existing one,
        // otherwise
        // create a new one.
        if (applicationFolderToPack == applicationDir) {
            applicationFolderToPack = createCopyDirectory(applicationFolderToPack);
        }
        final List<Service> services = application.getServices();
        for (final Service service : services) {
            final File serviceDir = new File(applicationFolderToPack, service.getName());
            if (!serviceDir.exists()) {
                throw new PackagingException("Could not find service folder at: " + serviceDir);
            }
            if (!serviceDir.isDirectory()) {
                throw new PackagingException("Was expecting a directory at: " + serviceDir);
            }

            for (final File fileToCopy : additionalServiceFiles) {
                FileUtils.copyFileToDirectory(fileToCopy, serviceDir);
            }
        }
    }

    // zip the application folder.
    return createZipFile("application", applicationFolderToPack);
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.ec2.bigdata.Ec2TwitterExampleTest.java

@Override
protected void beforeBootstrap() throws Exception {
    String suiteName = System.getProperty("iTests.suiteName");
    if (SGTestHelper.isDevMode() || (suiteName != null && "CLOUDIFY_XAP".equalsIgnoreCase(suiteName))) {
        /* copy premium license to cloudify-overrides in order to run xap pu's */
        String overridesFolder = getService().getPathToCloudFolder() + "/upload/cloudify-overrides";
        File cloudifyPremiumLicenseFile = new File(SGTestHelper.getBuildDir() + "/gslicense.xml");
        FileUtils.copyFileToDirectory(cloudifyPremiumLicenseFile, new File(overridesFolder));
    }//from w w  w  .  j  a  v a2  s  . c om
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.ec2.bigdata.RepetativeInstallAndUninstallStockDemoWithProblemAtInstallEc2Test.java

@BeforeClass
public void bootstrap() throws Exception {

    LogUtils.log("put cloudify-xap license in upload dir, needed to run stockdemo app");
    File xapLicense = new File(
            SGTestHelper.getSGTestRootDir() + "/src/main/resources/apps/cloudify/cloud/gslicense.xml");
    File cloudifyOverrides = new File(cloudPluginDir.getAbsolutePath() + "/upload/cloudify-overrides");
    if (!cloudifyOverrides.exists()) {
        cloudifyOverrides.mkdir();//from   w  ww  .  j a va2s .  c  o  m
    }
    try {
        FileUtils.copyFileToDirectory(xapLicense, cloudifyOverrides);
    } catch (IOException e) {
        AssertFail(e.getMessage());
    }

    service = new Ec2CloudService(this.getClass().getName());

    super.bootstrap(service);

    restUrl = super.getRestUrl();

    cloudPluginDir = new File(service.getPathToCloudFolder());
    String hostIp = restUrl.substring(0, restUrl.lastIndexOf(':'));
    try {
        stockdemoUrl = new URL(hostIp + ":8080/stockdemo.StockDemo/");
    } catch (MalformedURLException e) {
        AssertFail(e.getMessage(), e);
    }
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.ec2.bigdata.StockDemoApplicationTest.java

@Override
protected void beforeBootstrap() throws Exception {
    String suiteName = System.getProperty("iTests.suiteName");
    if (suiteName != null && "CLOUDIFY_XAP".equalsIgnoreCase(suiteName)) {
        /* copy premium license to cloudify-overrides in order to run xap pu's */
        String overridesFolder = getService().getPathToCloudFolder() + "/upload/cloudify-overrides";
        File cloudifyPremiumLicenseFile = new File(SGTestHelper.getSGTestRootDir() + "/config/gslicense.xml");
        FileUtils.copyFileToDirectory(cloudifyPremiumLicenseFile, new File(overridesFolder));
    }/*from ww  w. jav  a2 s .c o m*/
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.ec2.EC2FedoraTest.java

private void addTemplate() throws IOException {
    EC2TemplatesFolderHandler folderHandler = handler.createNewTemplatesFolderHandler();
    EC2TemplateDetails template = new EC2TemplateDetails();
    template.setTemplateName(TEMPLATE_NAME);
    template.setForServiceInstallation(true);
    template.setImageId(IMAGE_ID);/* www . j  av a 2 s.  co  m*/
    template.setLocationId(LOCATION_ID);
    template.setHardwareId(HARDWARE_ID);
    template.setKeyFile(PEM_FILE_NAME);
    template.setKeyPair(KEY_PER);
    template.setMachineMemoryMB(MACHINE_MEMORY_MB);
    template.setUsername(USERNAME);
    template.setRemoteDirectory(REMOTE_DIR);

    // add template to folder handler
    TemplateDetails addedTemplate = folderHandler.addCustomTemplate(template);
    try {
        // copy the pem file to the upload directory
        File uploadFolder = new File(addedTemplate.getTemplateFolder(), addedTemplate.getUploadDirName());
        FileUtils.copyFileToDirectory(new File(PEM_FILE_PATH), uploadFolder);
        FileUtils.copyFileToDirectory(new File(POST_BOOTSTRAP_PATH), uploadFolder);

        // add the template to the cloud
        handler.addTemplatesToCloudUsingRestAPI(folderHandler);
        handler.assertExpectedList();
    } catch (RestClientException e) {
        Assert.fail(e.getMessageFormattedText());
    } catch (AddTemplatesException e) {
        Assert.fail(e.toString());
    }
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.ec2.InstallTomcatOnSuseTest.java

private void copyPemFile() throws IOException {
    File pemFile = new File(PEM_FILE_PATH);
    File uploadFolder = new File(TEMPLATE_UPLOAD_FOLDER_PATH);
    FileUtils.copyFileToDirectory(pemFile, uploadFolder);
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.services.byon.ByonCloudService.java

@Override
public void injectCloudAuthenticationDetails() throws IOException {
    getProperties().put("username", user);
    getProperties().put("password", password);
    getProperties().put("keyFile", keyFile);

    Map<String, String> propsToReplace = new HashMap<String, String>();
    propsToReplace.put("machineNamePrefix \"cloudify_agent_\"",
            "machineNamePrefix \"" + getMachinePrefix() + "-cloudify-agent\"");
    propsToReplace.put("managementGroup \"cloudify_manager\"",
            "managementGroup \"" + getMachinePrefix() + "-cloudify-manager\"");
    propsToReplace.put("// cloudifyUrl", "cloudifyUrl");

    if (!sudo) {//w  ww.j  a  v  a 2s .c om
        propsToReplace.put("privileged true", "privileged false");
    }

    if (StringUtils.isNotBlank(ipList)) {
        propsToReplace.put("0\\.0\\.0\\.0", ipList);
    }

    propsToReplace.put("numberOfManagementMachines 1",
            "numberOfManagementMachines " + getNumberOfManagementMachines());
    propsToReplace.put("\"org.cloudifysource.clearRemoteDirectoryOnStart\":\"false\"",
            "\"org.cloudifysource.clearRemoteDirectoryOnStart\":\"true\"");
    propsToReplace.put("/tmp/gs-files", "/tmp/tgrid/gs-files");
    this.getAdditionalPropsToReplace().putAll(propsToReplace);

    // add a pem file
    final File fileToCopy = new File(CREDENTIALS_FOLDER + "/cloud/" + getCloudName() + "/" + keyFile);
    final File targetLocation = new File(getPathToCloudFolder() + "/upload/");
    FileUtils.copyFileToDirectory(fileToCopy, targetLocation);

    replaceCloudifyURL();
    replaceBootstrapManagementScript();
    replacePreBootstrapScript();

    // byon-cloud.groovy in SGTest has this variable defined for cloud overrides tests.
    // that why we need to add a properties file with a default value so that bootstrap will work 
    // on other tests.
    getProperties().put("myEnvVariable", ENV_VARIABLE_VALUE);
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.templates.TemplateCreator.java

public TemplateDetails createTemplate(final String templateName, final File templateFile,
        final File templateFolder, final String uploadDirName) {

    TemplateDetails template = getNewTempalte();
    template.setTemplateFolder(templateFolder);

    // name//from www. j  a va  2s . co m
    String updatedTemplateName = templateName;
    if (updatedTemplateName == null) {
        updatedTemplateName = templateFolder.getName();
    }
    template.setTemplateName(updatedTemplateName);

    // template file
    File updatedTemplateFile = templateFile;
    if (updatedTemplateFile == null) {
        updatedTemplateFile = new File(templateFolder,
                updatedTemplateName + DSLUtils.TEMPLATE_DSL_FILE_NAME_SUFFIX);
    } else {
        if (!updatedTemplateFile.exists()) {
            updatedTemplateFile = new File(templateFolder, updatedTemplateFile.getName());
        } else {
            if (!templateFolder.equals(updatedTemplateFile.getParentFile())) {
                try {
                    FileUtils.copyFileToDirectory(updatedTemplateFile, templateFolder);
                } catch (IOException e) {
                    Assert.fail("caught IOException while tried to copy template's file ["
                            + updatedTemplateFile.getAbsolutePath() + "] to folder ["
                            + templateFolder.getAbsolutePath() + "]. error was: " + e.getMessage());
                }
            }
        }
    }
    template.setTemplateFile(updatedTemplateFile);
    replaceStringInFile(getBasicTemplateFile(), updatedTemplateFile, TEMPLATE_NAME_STRING, updatedTemplateName);

    // upload directory
    String updatedUploadDirName = uploadDirName;
    if (updatedUploadDirName == null) {
        updatedUploadDirName = UPLOAD_DIR_NAME_PREFIX + "_" + templateFolder.getName();
    }
    template.setUploadDirName(updatedUploadDirName);
    final File uploadFolder = new File(templateFolder, updatedUploadDirName);
    uploadFolder.mkdir();

    // bootstrap management file
    final File updatedBootstrapManagementFile = new File(uploadFolder, BOOTSTRAP_MANAGEMENT_FILE_NAME);
    replaceStringInFile(getBasicBootstrapManagementFile(), updatedBootstrapManagementFile,
            UPLOAD_DIR_NAME_STRING, updatedUploadDirName);

    // properties file
    File templatePropsFile = createPropertiesFileForTemplate(template);
    template.setTemplatePropertiesFile(templatePropsFile);

    return template;
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.cloud.templates.TemplateCreator.java

public File createPropertiesFileForTemplate(final TemplateDetails template) {
    final Properties props = new Properties();
    if (template.getUploadDirName() != null) {
        props.put(UPLOAD_PROPERTY_NAME, template.getUploadDirName());
    }//www  .j  av  a2s  .c o m
    File templateFolder = template.getTemplateFolder();
    File templatePropsFile = template.getTemplatePropertiesFile();
    if (templatePropsFile == null) {
        final String templateFileName = template.getTemplateFile().getName();
        final int templateFileNamePrefixEndIndex = templateFileName.indexOf(".");
        final String templateFileNamePrefix = templateFileName.substring(0, templateFileNamePrefixEndIndex);
        final String proeprtiesFileName = templateFileNamePrefix + DSLUtils.PROPERTIES_FILE_SUFFIX;
        templatePropsFile = new File(templateFolder, proeprtiesFileName);

    } else {
        if (!templatePropsFile.exists()) {
            templatePropsFile = new File(templateFolder, templatePropsFile.getName());
        } else if (!templateFolder.equals(templatePropsFile.getParentFile())) {
            try {
                FileUtils.copyFileToDirectory(templatePropsFile, templateFolder);
            } catch (IOException e) {
                Assert.fail("failed to copy properties file [" + templatePropsFile.getAbsolutePath()
                        + "] to directory [" + templateFolder.getAbsolutePath() + "]. error was: "
                        + e.getMessage());
            }
        }
    }
    try {
        IOUtils.writePropertiesToFile(props, templatePropsFile);
    } catch (IOException e) {
        Assert.fail("failed to write properties to file [" + templatePropsFile.getAbsolutePath());
    }
    return templatePropsFile;
}