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

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

Introduction

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

Prototype

public static void touch(File file) throws IOException 

Source Link

Document

Implements the same behaviour as the "touch" utility on Unix.

Usage

From source file:org.opencastproject.capture.impl.SchedulerImplTest.java

public void setupFakeMediaPackageWithMediaFiles() {
    String directory = "scheduler-restart-test";
    setupFakeMediaPackageWithoutMediaFiles();
    File uidFile = new File("./target/" + directory, "2nd-Capture");
    try {//  w  w w . j  a v  a 2s . com
        FileUtils.forceMkdir(uidFile);
        FileUtils.touch(new File(uidFile.getAbsolutePath(), "screen_out.mpg"));
        FileUtils.touch(new File(uidFile.getAbsolutePath(), "camera_out.mpg"));
        FileUtils.touch(new File(uidFile.getAbsolutePath(), "audio_out.mp3"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.opencastproject.util.FileSupportTest.java

@Before
public void setUp() throws IOException {
    fileSupportTestsDirectory = new File(System.getProperty("java.io.tmpdir"), "fileSupportTestsDirectory");
    fileSupportTestsDestinationDirectory = new File(System.getProperty("java.io.tmpdir"),
            "fileSupportTestsDestinationDirectory");
    fileToLink = new File(fileSupportTestsDirectory.getAbsolutePath(), "file-to-link");
    linkLocation = new File(fileSupportTestsDirectory.getAbsolutePath(), "link-location");
    // Create test directory
    FileUtils.forceMkdir(fileSupportTestsDirectory);
    Assert.assertTrue("Can't read from test directory " + fileSupportTestsDirectory.getAbsolutePath(),
            fileSupportTestsDirectory.canRead());
    Assert.assertTrue("Can't write to test directory " + fileSupportTestsDirectory.getAbsolutePath(),
            fileSupportTestsDirectory.canWrite());
    // Create file that we could link. 
    FileUtils.touch(fileToLink);
    Assert.assertTrue("Can't read from file directory " + fileToLink.getAbsolutePath(), fileToLink.canRead());
}

From source file:org.opencastproject.workspace.impl.WorkspaceImpl.java

/**
 * OSGi service activation callback./*from  w w w .  j a  v  a2s  . c o  m*/
 * 
 * @param cc
 *          the OSGi component context
 */
public void activate(ComponentContext cc) {
    if (this.wsRoot == null) {
        if (cc != null && cc.getBundleContext().getProperty(WORKSPACE_ROOTDIR_KEY) != null) {
            // use rootDir from CONFIG
            this.wsRoot = cc.getBundleContext().getProperty(WORKSPACE_ROOTDIR_KEY);
            logger.info("CONFIG " + WORKSPACE_ROOTDIR_KEY + ": " + this.wsRoot);
        } else if (cc != null && cc.getBundleContext().getProperty("org.opencastproject.storage.dir") != null) {
            // create rootDir by adding "workspace" to the default data directory
            this.wsRoot = PathSupport
                    .concat(cc.getBundleContext().getProperty("org.opencastproject.storage.dir"), "workspace");
            logger.warn("CONFIG " + WORKSPACE_ROOTDIR_KEY + " is missing: falling back to " + this.wsRoot);
        } else {
            throw new IllegalStateException("Configuration '" + WORKSPACE_ROOTDIR_KEY + "' is missing");
        }
    }

    // Create the root directory
    File f = new File(this.wsRoot);
    if (!f.exists()) {
        try {
            FileUtils.forceMkdir(f);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    // Set up the garbage file collection timer
    if (cc != null && cc.getBundleContext().getProperty("org.opencastproject.workspace.gc.period") != null) {
        String period = cc.getBundleContext().getProperty("org.opencastproject.workspace.gc.period");
        if (period != null) {
            try {
                garbageCollectionPeriodInSeconds = Long.parseLong(period);
            } catch (NumberFormatException e) {
                logger.warn(
                        "Workspace garbage collection period can not be set to {}. Please choose a valid number "
                                + "for the 'org.opencastproject.workspace.gc.period' setting",
                        period);
            }
        }
    }

    // Test whether hard linking between working file repository and workspace is possible
    if (wfr instanceof PathMappable) {
        File srcFile = new File(wfrRoot, ".linktest");
        File targetFile = new File(wsRoot, ".linktest");
        try {
            FileUtils.touch(srcFile);
        } catch (IOException e) {
            throw new IllegalStateException("The working file repository seems read-only", e);
        }
        linkingEnabled = FileSupport.supportsLinking(srcFile, targetFile);
        if (linkingEnabled)
            logger.info("Hard links between the working file repository and the workspace enabled");
        else {
            logger.warn("Hard links between the working file repository and the workspace are not possible");
            logger.warn("This will increase the overall amount of disk space used");
        }
    }

    // Activate garbage collection
    if (cc != null && cc.getBundleContext().getProperty("org.opencastproject.workspace.gc.max.age") != null) {
        String age = cc.getBundleContext().getProperty("org.opencastproject.workspace.gc.max.age");
        if (age != null) {
            try {
                maxAgeInSeconds = Long.parseLong(age);
            } catch (NumberFormatException e) {
                logger.warn(
                        "Workspace garbage collection max age can not be set to {}. Please choose a valid number "
                                + "for the 'org.opencastproject.workspace.gc.max.age' setting",
                        age);
            }
        }
    }

    registeredMXBean = JmxUtil.registerMXBean(workspaceBean, JMX_WORKSPACE_TYPE);
}

From source file:org.opencastproject.workspace.impl.WorkspaceImpl.java

/**
 * {@inheritDoc}//from  w  ww .j a  va 2  s.c o  m
 * 
 * @see org.opencastproject.workspace.api.Workspace#put(java.lang.String, java.lang.String, java.lang.String,
 *      java.io.InputStream)
 */
@Override
public URI put(String mediaPackageID, String mediaPackageElementID, String fileName, InputStream in)
        throws IOException {
    String safeFileName = PathSupport.toSafeName(fileName);
    URI uri = wfr.getURI(mediaPackageID, mediaPackageElementID, fileName);

    // Determine the target location in the workspace
    File workspaceFile = null;
    FileOutputStream out = null;
    synchronized (wsRoot) {
        workspaceFile = getWorkspaceFile(uri, true);
        FileUtils.touch(workspaceFile);
    }

    // Try hard linking first and fall back to tee-ing to both the working file repository and the workspace
    if (linkingEnabled) {
        // The WFR stores an md5 hash along with the file, so we need to use the API and not try to write (link) the file
        // there ourselves
        wfr.put(mediaPackageID, mediaPackageElementID, fileName, in);
        File workingFileRepoDirectory = new File(PathSupport.concat(new String[] { wfrRoot,
                WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID }));
        File workingFileRepoCopy = new File(workingFileRepoDirectory, safeFileName);
        FileSupport.link(workingFileRepoCopy, workspaceFile, true);
    } else {
        InputStream tee = null;
        try {
            out = new FileOutputStream(workspaceFile);
            tee = new TeeInputStream(in, out, true);
            wfr.put(mediaPackageID, mediaPackageElementID, fileName, tee);
        } finally {
            IOUtils.closeQuietly(tee);
            IOUtils.closeQuietly(out);
        }
    }

    return uri;
}

From source file:org.opencastproject.workspace.impl.WorkspaceImpl.java

/**
 * {@inheritDoc}//from  w  w  w  .  j  a v  a  2 s . c  o  m
 * 
 * @see org.opencastproject.workspace.api.Workspace#putInCollection(java.lang.String, java.lang.String,
 *      java.io.InputStream)
 */
@Override
public URI putInCollection(String collectionId, String fileName, InputStream in) throws IOException {
    String safeFileName = PathSupport.toSafeName(fileName);
    URI uri = wfr.getCollectionURI(collectionId, fileName);

    // Determine the target location in the workspace
    InputStream tee = null;
    File tempFile = null;
    FileOutputStream out = null;
    try {
        synchronized (wsRoot) {
            tempFile = getWorkspaceFile(uri, true);
            FileUtils.touch(tempFile);
            out = new FileOutputStream(tempFile);
        }

        // Try hard linking first and fall back to tee-ing to both the working file repository and the workspace
        if (linkingEnabled) {
            tee = in;
            wfr.putInCollection(collectionId, fileName, tee);
            FileUtils.forceMkdir(tempFile.getParentFile());
            File workingFileRepoDirectory = new File(PathSupport.concat(
                    new String[] { wfrRoot, WorkingFileRepository.COLLECTION_PATH_PREFIX, collectionId }));
            File workingFileRepoCopy = new File(workingFileRepoDirectory, safeFileName);
            FileSupport.link(workingFileRepoCopy, tempFile, true);
        } else {
            tee = new TeeInputStream(in, out, true);
            wfr.putInCollection(collectionId, fileName, tee);
        }
    } catch (IOException e) {
        FileUtils.deleteQuietly(tempFile);
        throw e;
    } finally {
        IoSupport.closeQuietly(tee);
        IoSupport.closeQuietly(out);
    }
    return uri;
}

From source file:org.openengsb.core.services.internal.deployer.connector.ConnectorDeployerServiceTest.java

@Test
public void testRootService_shouldHaveLowerRanking() throws Exception {
    File connectorFile = new File(temporaryFolder.getRoot() + "/etc/mydomain+aconnector+myroot.connector");
    FileUtils.touch(connectorFile);
    FileUtils.writeStringToFile(connectorFile, testConnectorData);

    connectorDeployerService.install(connectorFile);

    ServiceReference reference = bundleContext.getServiceReferences(NullDomain.class.getName(), "")[0];
    Integer ranking = (Integer) reference.getProperty(Constants.SERVICE_RANKING);
    assertThat(ranking, lessThan(0));/*www . j  av  a 2s . c om*/
}

From source file:org.openengsb.core.services.internal.deployer.connector.ConnectorDeployerServiceTest.java

@Test
public void testNormalService_shouldHaveNoRankingAdded() throws Exception {
    File connectorFile = new File(temporaryFolder.getRoot() + "/config/mydomain+aconnector+myroot.connector");
    FileUtils.touch(connectorFile);
    FileUtils.writeStringToFile(connectorFile, testConnectorData);

    connectorDeployerService.install(connectorFile);

    ServiceReference reference = bundleContext.getServiceReferences(NullDomain.class.getName(), "")[0];

    assertThat(reference.getProperty(Constants.SERVICE_RANKING), nullValue());
}

From source file:org.openengsb.core.services.internal.deployer.connector.ConnectorDeployerServiceTest.java

@Test
public void testOverridenRanking_shouldNotBeAltered() throws Exception {
    File connectorFile = new File(temporaryFolder.getRoot() + "/etc/mydomain+aconnector+myroot.connector");
    FileUtils.touch(connectorFile);
    FileUtils.writeStringToFile(connectorFile,
            testConnectorData + "\n" + "property." + Constants.SERVICE_RANKING + "=24");

    connectorDeployerService.install(connectorFile);

    ServiceReference ref = bundleContext.getServiceReferences(NullDomain.class.getName(),
            String.format("(%s=%s)", Constants.SERVICE_RANKING, 24))[0];
    assertThat(ref, not(nullValue()));/*from   w w w.  j a v  a2s . com*/
}

From source file:org.openengsb.domain.report.FileSystemReportStore.java

private void storeReportPart(File reportFile, int partIndex, ReportPart part) throws IOException {
    File partFile = new File(reportFile, partIndex + getFileEnding(part.getContentType()));
    writeMetadata(reportFile, partIndex, part);
    FileUtils.touch(partFile);
    if (part.getContent() != null) {
        FileUtils.writeByteArrayToFile(partFile, part.getContent());
    }//from   w  ww . ja v  a2s.com
}

From source file:org.openengsb.persistence.context.filebackend.ContextFilePersistenceService.java

@SuppressWarnings("unchecked")
@Override/*from  www .  ja  v  a 2s  .  c  om*/
public void persist(ConfigItem<Map<String, String>> config)
        throws PersistenceException, InvalidConfigurationException {
    Preconditions.checkArgument(supports((Class<? extends ConfigItem<?>>) config.getClass()),
            "Argument type not supported");
    Preconditions.checkNotNull(config.getMetaData(), "Invalid metadata");

    String contextFileName = getFileNameForMetaData(config.getMetaData());
    File contextPersistenceFile = new File(storageFolder, contextFileName);
    try {
        FileUtils.touch(contextPersistenceFile);
    } catch (IOException e) {
        throw new PersistenceException(
                String.format("Could not persist context configuration file %s", contextFileName), e);
    }
    LOGGER.info("Created context configuration file %s", contextFileName);
}