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.ado.biblio.desktop.AppConfiguration.java

private static Properties loadFileProperties(File file, boolean createIfNotExist) {
    Properties prop = new Properties();
    try {//  w  w w.ja v  a2s  .c o m
        if (!file.exists()) {
            if (createIfNotExist) {
                FileUtils.touch(file);
            } else {
                throw new IOException(
                        String.format("Properties file \"%s\" does not exits.", file.getAbsolutePath()));
            }
        }
        prop.load(new FileInputStream(file));
    } catch (IOException e) {
        throw new IllegalStateException("Cannot read application configuration file", e);
    }
    return prop;
}

From source file:org.ado.biblio.desktop.db.DatabaseConnection.java

@PostConstruct
private void init() {
    try {/*  w w w  . j a v  a 2 s  .  c o m*/
        Class.forName("org.sqlite.JDBC");
        if (!DATABASE_FILE.exists()) {
            FileUtils.touch(DATABASE_FILE);
        }
        connection = DriverManager
                .getConnection(String.format("jdbc:sqlite:%s", DATABASE_FILE.getAbsolutePath()));
    } catch (Exception e) {
        throw new IllegalStateException("Unable to establish connection to database", e);
    }

    try {
        databaseInitialize = new DatabaseInitialize(connection, DATABASE_VERSION);
        databaseInitialize.initializeDatabase();
    } catch (SQLException e) {
        throw new IllegalStateException("Unable to initialize database", e);
    }
}

From source file:org.ado.biblio.desktop.util.ImageUtils.java

public static File write(InputStream inputStream, String filename, String extension) throws IOException {
    notNull(inputStream, "inputStream cannot be null");
    notBlank(filename, "filename cannot be empty");
    notBlank(extension, "extension cannot be empty");

    final File file = new File(DIRECTORY, filename.concat(extension));
    FileUtils.touch(file);
    FileOutputStream out = new FileOutputStream(file);
    IOUtils.copy(inputStream, out);//from   w ww. ja  v  a 2s  .c  o m
    return file;
}

From source file:org.apache.accumulo.server.AccumuloTest.java

@Test
public void testLocateLogConfig() throws Exception {
    File confDir = new File(FileUtils.getTempDirectory(), "AccumuloTest" + System.currentTimeMillis());
    String confDirName = confDir.getAbsolutePath();
    assertTrue("Failed to make test configuration directory", confDir.mkdir());
    try {//from   w  w  w  . j a  v  a2 s .  co  m
        File appProps = new File(confDir, "flogger_logger.properties");
        String appPropsName = String.format("%s/flogger_logger.properties", confDirName);
        FileUtils.touch(appProps);
        File genericXml = new File(confDir, "generic_logger.xml");
        String genericXmlName = String.format("%s/generic_logger.xml", confDirName);
        FileUtils.touch(genericXml);

        assertEquals(appPropsName, Accumulo.locateLogConfig(confDirName, "flogger"));
        assertEquals(genericXmlName, Accumulo.locateLogConfig(confDirName, "flagger"));

        assertTrue("Failed to delete log configuration file", appProps.delete());
        assertEquals(genericXmlName, Accumulo.locateLogConfig(confDirName, "flogger"));
    } finally {
        FileUtils.deleteDirectory(confDir);
    }
}

From source file:org.apache.activemq.artemis.core.security.jaas.PropertiesLoginModuleTest.java

@Test
public void testLoginReload() throws Exception {
    File targetPropDir = new File("target/loginReloadTest");
    File usersFile = new File(targetPropDir, "users.properties");
    File rolesFile = new File(targetPropDir, "roles.properties");

    //Set up initial properties
    FileUtils.copyFile(new File(getClass().getResource("/users.properties").toURI()), usersFile);
    FileUtils.copyFile(new File(getClass().getResource("/roles.properties").toURI()), rolesFile);

    LoginContext context = new LoginContext("PropertiesLoginReload", new UserPassHandler("first", "secret"));
    context.login();/*from   w w  w . ja  va2  s . c  o m*/
    Subject subject = context.getSubject();

    //test initial principals
    assertEquals("Should have three principals", 3, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertEquals("Should have two group principals", 2, subject.getPrincipals(RolePrincipal.class).size());

    context.logout();

    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());

    //Modify the file and test that the properties are reloaded
    Thread.sleep(1000);
    FileUtils.copyFile(new File(getClass().getResource("/usersReload.properties").toURI()), usersFile);
    FileUtils.copyFile(new File(getClass().getResource("/rolesReload.properties").toURI()), rolesFile);
    FileUtils.touch(usersFile);
    FileUtils.touch(rolesFile);

    //Use new password to verify  users file was reloaded
    context = new LoginContext("PropertiesLoginReload", new UserPassHandler("first", "secrets"));
    context.login();
    subject = context.getSubject();

    //Check that the principals changed
    assertEquals("Should have three principals", 2, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertEquals("Should have one group principals", 1, subject.getPrincipals(RolePrincipal.class).size());

    context.logout();

    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());
}

From source file:org.apache.activemq.jaas.PropertiesLoginModuleTest.java

public void testLoginReload() throws Exception {
    File targetPropDir = new File("target/loginReloadTest");
    File sourcePropDir = new File("src/test/resources");
    File usersFile = new File(targetPropDir, "users.properties");
    File groupsFile = new File(targetPropDir, "groups.properties");

    //Set up initial properties
    FileUtils.copyFile(new File(sourcePropDir, "users.properties"), usersFile);
    FileUtils.copyFile(new File(sourcePropDir, "groups.properties"), groupsFile);

    LoginContext context = new LoginContext("PropertiesLoginReload", new UserPassHandler("first", "secret"));
    context.login();/*from   ww  w  . ja  v a2s  . c  o m*/
    Subject subject = context.getSubject();

    //test initial principals
    assertEquals("Should have three principals", 3, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertEquals("Should have two group principals", 2, subject.getPrincipals(GroupPrincipal.class).size());

    context.logout();

    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());

    //Modify the file and test that the properties are reloaded
    Thread.sleep(1000);
    FileUtils.copyFile(new File(sourcePropDir, "usersReload.properties"), usersFile);
    FileUtils.copyFile(new File(sourcePropDir, "groupsReload.properties"), groupsFile);
    FileUtils.touch(usersFile);
    FileUtils.touch(groupsFile);

    //Use new password to verify  users file was reloaded
    context = new LoginContext("PropertiesLoginReload", new UserPassHandler("first", "secrets"));
    context.login();
    subject = context.getSubject();

    //Check that the principals changed
    assertEquals("Should have three principals", 2, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertEquals("Should have one group principals", 1, subject.getPrincipals(GroupPrincipal.class).size());

    context.logout();

    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());
}

From source file:org.apache.axis2.classloader.JarFileClassLoaderTest.java

@Override
protected void setUp() throws Exception {
    tmpDir = new File(System.getProperty("java.io.tmpdir"), getClass().getName());
    if (tmpDir.exists()) {
        FileUtils.deleteDirectory(tmpDir);
    }/*  w  w  w  . j a v a 2 s .  c  om*/
    // Create the following files in the tmp directory:
    // outside
    // root/a
    // root/dir/b
    FileUtils.touch(new File(tmpDir, "outside"));
    File root = new File(tmpDir, "root");
    root.mkdir();
    FileUtils.touch(new File(root, "a"));
    File dir = new File(root, "dir");
    dir.mkdir();
    FileUtils.touch(new File(dir, "b"));
}

From source file:org.apache.hadoop.gateway.GatewayAppFuncTest.java

@Test //( timeout = TestUtils.MEDIUM_TIMEOUT )
public void testDeploymentCleanup() throws Exception {
    LOG_ENTER();/*w w  w. jav a 2 s  . c  om*/

    String username = "guest";
    String password = "guest-password";

    int oldVersionLimit = config.getGatewayDeploymentsBackupVersionLimit();

    try {
        gateway.stop();
        config.setGatewayDeploymentsBackupVersionLimit(1);
        startGatewayServer();

        String topoStr = TestUtils.merge(DAT, "test-dynamic-app-topology.xml", params);
        File topoFile = new File(config.getGatewayTopologyDir(), "test-topology.xml");
        FileUtils.writeStringToFile(topoFile, topoStr);
        topos.reloadTopologies();

        File deployDir = new File(config.getGatewayDeploymentDir());
        String[] topoDirs1 = deployDir.list();
        assertThat(topoDirs1, is(arrayWithSize(1)));

        given()
                //.log().all()
                .auth().preemptive().basic(username, password).expect()
                //.log().all()
                .statusCode(HttpStatus.SC_OK).body(is(clusterUrl + "/dynamic-app-path/?null")).when()
                .get(clusterUrl + "/dynamic-app-path");

        TestUtils.waitUntilNextSecond();
        FileUtils.touch(topoFile);

        topos.reloadTopologies();
        String[] topoDirs2 = deployDir.list();
        assertThat(topoDirs2, is(arrayWithSize(2)));
        assertThat(topoDirs2, hasItemInArray(topoDirs1[0]));

        given()
                //.log().all()
                .auth().preemptive().basic(username, password).expect()
                //.log().all()
                .statusCode(HttpStatus.SC_OK).body(is(clusterUrl + "/dynamic-app-path/?null")).when()
                .get(clusterUrl + "/dynamic-app-path");

        TestUtils.waitUntilNextSecond();
        FileUtils.touch(topoFile);
        topos.reloadTopologies();

        String[] topoDirs3 = deployDir.list();
        assertThat(topoDirs3, is(arrayWithSize(2)));
        assertThat(topoDirs3, not(hasItemInArray(topoDirs1[0])));

        given()
                //.log().all()
                .auth().preemptive().basic(username, password).expect()
                //.log().all()
                .statusCode(HttpStatus.SC_OK).body(is(clusterUrl + "/dynamic-app-path/?null")).when()
                .get(clusterUrl + "/dynamic-app-path");

    } finally {
        gateway.stop();
        config.setGatewayDeploymentsBackupAgeLimit(oldVersionLimit);
        startGatewayServer();
    }

    LOG_EXIT();
}

From source file:org.apache.hadoop.gateway.services.topology.DefaultTopologyServiceTest.java

private File createFile(File parent, String name, String resource, long timestamp) throws IOException {
    File file = new File(parent, name);
    if (!file.exists()) {
        FileUtils.touch(file);
    }//from  w  ww  .ja  v  a  2  s .c o m
    InputStream input = ClassLoader.getSystemResourceAsStream(resource);
    OutputStream output = FileUtils.openOutputStream(file);
    IOUtils.copy(input, output);
    //KNOX-685: output.flush();
    input.close();
    output.close();
    file.setLastModified(timestamp);
    assertTrue("Failed to create test file " + file.getAbsolutePath(), file.exists());
    assertTrue("Failed to populate test file " + file.getAbsolutePath(), file.length() > 0);

    return file;
}

From source file:org.apache.hadoop.hive.ql.exec.TestUtilities.java

public void testGetJarFilesByPath() {
    File f = Files.createTempDir();
    String jarFileName1 = f.getAbsolutePath() + File.separator + "a.jar";
    String jarFileName2 = f.getAbsolutePath() + File.separator + "b.jar";
    File jarFile = new File(jarFileName1);
    try {/*from w  ww .  ja  va  2 s.  c o m*/
        FileUtils.touch(jarFile);
        HashSet<String> jars = (HashSet) Utilities.getJarFilesByPath(f.getAbsolutePath());
        Assert.assertEquals(Sets.newHashSet(jarFile.getAbsolutePath()), jars);

        File jarFile2 = new File(jarFileName2);
        FileUtils.touch(jarFile2);
        String newPath = "file://" + jarFileName1 + "," + "file://" + jarFileName2;
        jars = (HashSet) Utilities.getJarFilesByPath(newPath);

        Assert.assertEquals(Sets.newHashSet("file://" + jarFileName1, "file://" + jarFileName2), jars);
    } catch (IOException e) {
        LOG.error("failed to copy file to reloading folder", e);
        Assert.fail(e.getMessage());
    } finally {
        FileUtils.deleteQuietly(f);
    }
}