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

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

Introduction

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

Prototype

public static void copyDirectory(File srcDir, File destDir) throws IOException 

Source Link

Document

Copies a whole directory to a new location preserving the file dates.

Usage

From source file:eu.trentorise.opendata.josman.Josmans.java

/**
 * Extracts the directory at resource path to target directory. First
 * directory is searched in local "src/main/resources" so the thing also
 * works when developing in the IDE. If not found then searches in jar file.
 *//*from w  ww.  j a v a  2  s .  c  o m*/
public static void copyDirFromResource(Class clazz, String dirPath, File destDir) {
    String sep = File.separator;
    File sourceDir = new File("src" + sep + "main" + sep + "resources", dirPath);

    if (sourceDir.exists()) {
        LOG.log(Level.INFO, "Copying directory from {0} to {1}  ...",
                new Object[] { sourceDir.getAbsolutePath(), destDir.getAbsolutePath() });
        try {
            FileUtils.copyDirectory(sourceDir, destDir);
            LOG.log(Level.INFO, "Done copying directory");
        } catch (IOException ex) {
            throw new RuntimeException("Couldn't copy the directory!", ex);
        }
    } else {

        File jarFile = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
        if (jarFile.isDirectory()
                && jarFile.getAbsolutePath().endsWith("target" + File.separator + "classes")) {
            LOG.info("Seems like you have Josman sources, will take resources from there");
            try {
                FileUtils.copyDirectory(
                        new File(jarFile.getAbsolutePath() + "/../../src/main/resources", dirPath), destDir);
                LOG.log(Level.INFO, "Done copying directory");
            } catch (IOException ex) {
                throw new RuntimeException("Couldn't copy the directory!", ex);
            }
        } else {
            LOG.log(Level.INFO, "Extracting jar {0} to {1}",
                    new Object[] { jarFile.getAbsolutePath(), destDir.getAbsolutePath() });
            copyDirFromJar(jarFile, destDir, dirPath);
            LOG.log(Level.INFO, "Done copying directory from JAR.");
        }

    }
}

From source file:azkaban.execapp.FlowRunnerTest.java

private ExecutableFlow prepareExecDir(File execDir, String flowName, int execId) throws IOException {
    synchronized (this) {
        FileUtils.copyDirectory(execDir, workingDir);
    }//from  www . j ava  2s .  c  om

    File jsonFlowFile = new File(workingDir, flowName + ".flow");
    @SuppressWarnings("unchecked")
    HashMap<String, Object> flowObj = (HashMap<String, Object>) JSONUtils.parseJSONFromFile(jsonFlowFile);

    Project project = new Project(1, "myproject");
    project.setVersion(2);

    Flow flow = Flow.flowFromObject(flowObj);
    ExecutableFlow execFlow = new ExecutableFlow(project, flow);
    execFlow.setExecutionId(execId);
    execFlow.setExecutionPath(workingDir.getPath());
    return execFlow;
}

From source file:com.arcusys.liferay.vaadinplugin.VaadinUpdater.java

private boolean extractVAADINFolder(String sourceDirPath, String jarName, String tmpFolderName,
        String destination) throws IOException {
    String vaadinJarFilePath = sourceDirPath + fileSeparator + jarName;
    JarFile vaadinJar = new JarFile(vaadinJarFilePath);
    String vaadinExtractedPath = sourceDirPath + tmpFolderName;
    outputLog.log("Extracting " + jarName);
    try {/*from  w  ww  .j  av a2  s  .c om*/
        Enumeration<JarEntry> entries = vaadinJar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            boolean extractSuccessful = ControlPanelPortletUtil.extractJarEntry(vaadinJar, entry,
                    vaadinExtractedPath);
            if (!extractSuccessful) {
                outputLog.log("Extraction failed: " + entry.getName());
                return true;
            }
        }
    } catch (Exception e) {
        log.warn("Extracting VAADIN folder failed.", e);
        upgradeListener.updateFailed("Extraction failed: " + e.getMessage());
        return true;
    } finally {
        vaadinJar.close();
    }

    String vaadinExtractedVaadinPath = vaadinExtractedPath + fileSeparator + "VAADIN" + fileSeparator;
    File vaadinExtractedVaadin = new File(vaadinExtractedVaadinPath);
    if (!vaadinExtractedVaadin.exists()) {
        upgradeListener.updateFailed("Could not find " + vaadinExtractedVaadinPath);
        return true;
    }

    FileUtils.copyDirectory(vaadinExtractedVaadin, new File(destination));
    return false;
}

From source file:de.xirp.mail.MailManager.java

/**
 * Exports the corresponding//from w  ww. j  a  va2  s.  c  om
 * {@link de.xirp.mail.Mail} for the given
 * {@link de.xirp.mail.MailDescriptor} to the given
 * path. A new folder is created there, the folder will contain
 * the content of the mail: text and attachments.
 * 
 * @param path
 *            The path to export the mail to.
 * @param md
 *            The mail descriptor for the mail to export.
 * @throws SerializationException
 *             if something wnt wrong while loading the mail.
 * @throws IOException
 *             if something went wrong while loading the mail.
 * @see de.xirp.mail.MailDescriptor
 */
public static void exportMail(String path, MailDescriptor md) throws SerializationException, IOException {

    Mail mail = getMail(md);
    File outputFolder = new File(path, "mailexport_" //$NON-NLS-1$
            + md.getDescriptorKey());

    File tempDir = new File(Constants.TMP_DIR);
    File tempOutputFolder = new File(tempDir, "xirpmailexport"); //$NON-NLS-1$
    tempOutputFolder.mkdir();

    File text = new File(tempOutputFolder, "mail.txt"); //$NON-NLS-1$
    text.createNewFile();

    /* write mailtext, encoding is Unicode */
    FileUtils.writeStringToFile(text,
            mail.getSubject() + Constants.LINE_SEPARATOR + Constants.LINE_SEPARATOR + mail.getText(),
            "Unicode"); //$NON-NLS-1$

    File attachmentFolder = new File(tempOutputFolder, "attachments"); //$NON-NLS-1$
    attachmentFolder.mkdir();

    for (Attachment a : mail.getAttachments()) {
        File attachment = new File(attachmentFolder, a.getFileName());
        attachment.createNewFile();
        FileUtils.writeByteArrayToFile(attachment, a.getAttachmentFileContent());
    }

    FileUtils.copyDirectory(tempOutputFolder, outputFolder);
}

From source file:edu.isi.wings.portal.classes.domains.Domain.java

public static Domain renameDomain(Domain domain, String newname, Config config) {
    Properties props = config.getProperties(domain);
    DataCreationAPI dc = DataFactory.getCreationAPI(props);
    ComponentCreationAPI acc = ComponentFactory.getCreationAPI(props, false);
    ComponentCreationAPI ccc = ComponentFactory.getCreationAPI(props, true);
    TemplateCreationAPI tc = TemplateFactory.getCreationAPI(props);

    File tempdir;//from  ww w .  java 2s  . co  m
    try {
        tempdir = File.createTempFile("domain-", "-temp");
        if (!tempdir.delete() || !tempdir.mkdirs())
            return null;
    } catch (IOException e1) {
        return null;
    }

    Domain todom = Domain.createDefaultDomain(newname, config.getUserDir(), config.getExportUserUrl());
    todom.saveDomain();

    props = config.getProperties(todom);
    DataCreationAPI todc = DataFactory.getCreationAPI(props);
    ComponentCreationAPI toacc = ComponentFactory.getCreationAPI(props, false);
    ComponentCreationAPI toccc = ComponentFactory.getCreationAPI(props, true);
    TemplateCreationAPI totc = TemplateFactory.getCreationAPI(props);

    // Copy into non-triple-store apis
    todc.copyFrom(dc);
    toacc.copyFrom(acc);
    toccc.copyFrom(ccc);
    totc.copyFrom(tc);

    // Copy legacy data/code directories to new data/code storage directory
    File srcDataDir = new File(
            domain.getDomainDirectory() + fsep + domain.getDataLibrary().getStorageDirectory());
    File destDataDir = new File(
            todom.getDomainDirectory() + fsep + todom.getDataLibrary().getStorageDirectory());
    File srcCodeDir = new File(
            domain.getDomainDirectory() + fsep + domain.getConcreteComponentLibrary().getStorageDirectory());
    File destCodeDir = new File(
            todom.getDomainDirectory() + fsep + todom.getConcreteComponentLibrary().getStorageDirectory());
    try {
        if (srcDataDir.isDirectory())
            FileUtils.copyDirectory(srcDataDir, destDataDir);
        if (srcCodeDir.isDirectory()) {
            FileUtils.copyDirectory(srcCodeDir, destCodeDir);
            // FIXME: Setting executable permissions on all files for now
            for (File f : FileUtils.listFiles(destCodeDir, null, true)) {
                f.setExecutable(true);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    deleteDomain(domain, config, true);

    return todom;
}

From source file:hudson.plugins.jobConfigHistory.FileHistoryDaoTest.java

/**
 * Test of getHistoryDir method, of class FileHistoryDao.
 *///from  ww w .j  a v  a2  s . c  o m
@Test
public void testGetHistoryDirWithFolder() throws IOException {
    String folderName = "FolderName";
    final File destDir = unpackResourceZip.getResource("config-history/jobs/" + folderName + "/jobs/Test1");
    // Create folders
    FileUtils.copyDirectory(unpackResourceZip.getResource("config-history/jobs/Test1"), destDir);
    final File configFile = unpackResourceZip.getResource("jobs/" + folderName + "/jobs/Test1/config.xml");
    File result = sutWithUserAndNoDuplicateHistory.getHistoryDir(configFile);
    assertEquals(destDir, result);
}

From source file:com.collective.celos.CelosClientServerTest.java

@Test
public void testSchedulerIteratorPast() throws Exception {

    ScheduledTime timeInPast = new ScheduledTime("2000-12-01T00:00Z");
    File src = new File(Thread.currentThread().getContextClassLoader()
            .getResource("com/collective/celos/client/wf-list").toURI());
    FileUtils.copyDirectory(src, workflowsDir);

    List<SlotState> slotStates = celosClient.getWorkflowStatus(new WorkflowID("workflow-4")).getSlotStates();
    Assert.assertEquals(slotStates.size(), SLOTS_IN_CELOS_SERVER_SLIDING_WINDOW);

    celosClient.iterateScheduler(timeInPast);

    ScheduledTime inclusivePeriod = timeInPast.plusSeconds(1);
    slotStates = celosClient.getWorkflowStatus(new WorkflowID("workflow-4"), inclusivePeriod).getSlotStates();

    for (SlotState slotState : slotStates) {
        Assert.assertEquals(slotState.getStatus(), SlotState.Status.WAITING);
        Assert.assertEquals(slotState.getExternalID(), null);
        Assert.assertEquals(slotState.getRetryCount(), 0);
    }/*from  w w w  .  j  ava 2 s . c  o  m*/

    celosClient.iterateScheduler(timeInPast);
    slotStates = celosClient.getWorkflowStatus(new WorkflowID("workflow-4"), inclusivePeriod).getSlotStates();

    for (SlotState slotState : slotStates) {
        Assert.assertEquals(slotState.getStatus(), SlotState.Status.WAITING);
        Assert.assertEquals(slotState.getExternalID(), null);
        Assert.assertEquals(slotState.getRetryCount(), 0);
    }

    celosClient.iterateScheduler(inclusivePeriod);
    slotStates = celosClient.getWorkflowStatus(new WorkflowID("workflow-4"), inclusivePeriod).getSlotStates();
    Assert.assertEquals(slotStates.get(0).getStatus(), SlotState.Status.READY);
    for (SlotState slotState : slotStates.subList(1, slotStates.size())) {
        Assert.assertEquals(slotState.getStatus(), SlotState.Status.WAITING);
        Assert.assertEquals(slotState.getExternalID(), null);
        Assert.assertEquals(slotState.getRetryCount(), 0);
    }
}