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

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

Introduction

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

Prototype

public static long checksumCRC32(File file) throws IOException 

Source Link

Document

Computes the checksum of a file using the CRC32 checksum routine.

Usage

From source file:org.ikasan.component.endpoint.filesystem.producer.FileProducer.java

protected void process(Object payload) {
    try {//w  ww  .j av a2s  .  c  o  m
        File file = new File(configuration.getFilename());
        if (file.exists()) {
            if (!configuration.isOverwrite()) {
                throw new EndpointException("File [" + configuration.getFilename()
                        + "] already exists and overwrite option is set to [" + configuration.isOverwrite()
                        + "]");
            }

            file.delete();
        }

        if (configuration.isUseTempFile()) {
            File tempFile = new File(configuration.getTempFilename());
            this.writeFile(tempFile, payload);
            FileUtils.moveFile(tempFile, file);
        } else {
            this.writeFile(file, payload);
        }

        if (configuration.isWriteChecksum()) {
            long checksum = FileUtils.checksumCRC32(file);
            File checksumFile = new File(configuration.getFilename() + ".cr32");
            FileUtils.writeStringToFile(checksumFile, String.valueOf(checksum), configuration.getEncoding());
        }
    } catch (IOException e) {
        throw new EndpointException(e);
    }
}

From source file:org.jboss.additional.testsuite.jdkall.present.domain.suites.ModelPersistenceTestCase.java

private void testDomainOperation(ModelNode operation) throws Exception {

    DomainClient client = domainMasterLifecycleUtil.getDomainClient();
    CfgFileDescription lastBackupDesc = getLatestBackup(domainCurrentCfgDir);
    CfgFileDescription lastMasterBackupDesc = getLatestBackup(masterCurrentCfgDir);
    CfgFileDescription lastSlaveBackupDesc = getLatestBackup(slaveCurrentCfgDir);
    long lastFileHash = domainLastCfgFile.exists() ? FileUtils.checksumCRC32(domainLastCfgFile) : -1;

    // execute operation so the model gets updated
    executeOperation(client, operation);

    // check that the automated snapshot of the domain has been generated
    CfgFileDescription newBackupDesc = getLatestBackup(domainCurrentCfgDir);
    Assert.assertNotNull("Model snapshot not found.", newBackupDesc);
    // check that the version is incremented by one
    Assert.assertTrue(lastBackupDesc.version == newBackupDesc.version - 1);

    // check that the both master and slave host snapshot have not been generated
    CfgFileDescription newMasterBackupDesc = getLatestBackup(masterCurrentCfgDir);
    CfgFileDescription newSlaveBackupDesc = getLatestBackup(slaveCurrentCfgDir);
    Assert.assertTrue(lastMasterBackupDesc.version == newMasterBackupDesc.version);
    Assert.assertTrue(lastSlaveBackupDesc.version == newSlaveBackupDesc.version);

    // check that the last cfg file has changed
    Assert.assertTrue(lastFileHash != FileUtils.checksumCRC32(domainLastCfgFile));
}

From source file:org.jboss.additional.testsuite.jdkall.present.domain.suites.ModelPersistenceTestCase.java

private void testHostOperation(ModelNode operation, Host controller, Host target) throws Exception {

    DomainClient client = controller.equals(Host.MASTER) ? domainMasterLifecycleUtil.getDomainClient()
            : domainSlaveLifecycleUtil.getDomainClient();

    CfgFileDescription lastDomainBackupDesc = getLatestBackup(domainCurrentCfgDir);
    CfgFileDescription lastMasterBackupDesc = getLatestBackup(masterCurrentCfgDir);
    CfgFileDescription lastSlaveBackupDesc = getLatestBackup(slaveCurrentCfgDir);
    long lastDomainFileHash = domainLastCfgFile.exists() ? FileUtils.checksumCRC32(domainLastCfgFile) : -1;
    long lastMasterFileHash = masterLastCfgFile.exists() ? FileUtils.checksumCRC32(masterLastCfgFile) : -1;
    long lastSlaveFileHash = slaveLastCfgFile.exists() ? FileUtils.checksumCRC32(slaveLastCfgFile) : -1;

    // execute operation so the model gets updated
    executeOperation(client, operation);

    // check that the automated snapshot of the domain has not been generated
    CfgFileDescription newDomainBackupDesc = getLatestBackup(domainCurrentCfgDir);
    Assert.assertTrue(lastDomainBackupDesc.version == newDomainBackupDesc.version);

    // check that only the appropriate host snapshot has been generated
    CfgFileDescription newMasterBackupDesc = getLatestBackup(masterCurrentCfgDir);
    CfgFileDescription newSlaveBackupDesc = getLatestBackup(slaveCurrentCfgDir);
    if (target == Host.MASTER) {
        Assert.assertTrue(lastMasterBackupDesc.version == newMasterBackupDesc.version - 1);
        Assert.assertTrue(lastSlaveBackupDesc.version == newSlaveBackupDesc.version);
        Assert.assertTrue(lastMasterFileHash != FileUtils.checksumCRC32(masterLastCfgFile));
        Assert.assertTrue(lastSlaveFileHash == FileUtils.checksumCRC32(slaveLastCfgFile));
    } else {/*from  w  ww.ja v a2 s.c  o m*/
        Assert.assertTrue(lastMasterBackupDesc.version == newMasterBackupDesc.version);
        Assert.assertTrue(lastSlaveBackupDesc.version == newSlaveBackupDesc.version - 1);
        Assert.assertTrue(lastMasterFileHash == FileUtils.checksumCRC32(masterLastCfgFile));
        Assert.assertTrue(lastSlaveFileHash != FileUtils.checksumCRC32(slaveLastCfgFile));
    }
    Assert.assertTrue(lastDomainBackupDesc.version == newDomainBackupDesc.version);
    Assert.assertTrue(lastDomainFileHash == FileUtils.checksumCRC32(domainLastCfgFile));

}

From source file:org.jboss.additional.testsuite.jdkall.present.domain.suites.ModelPersistenceTestCase.java

@Test
public void testTakeAndDeleteSnapshot() throws Exception {

    DomainClient client = domainMasterLifecycleUtil.getDomainClient();

    // take snapshot
    ModelNode op = ModelUtil.createOpNode(null, "take-snapshot");
    ModelNode result = executeOperation(client, op);

    // check that the snapshot file exists
    String snapshotFileName = result.asString();
    File snapshotFile = new File(snapshotFileName);
    Assert.assertTrue(snapshotFile.exists());

    // compare with current cfg
    long snapshotHash = FileUtils.checksumCRC32(snapshotFile);
    long lastHash = FileUtils.checksumCRC32(domainLastCfgFile);
    Assert.assertTrue(snapshotHash == lastHash);

    // delete snapshot
    op = ModelUtil.createOpNode(null, "delete-snapshot");
    op.get("name").set(snapshotFile.getName());
    executeOperation(client, op);/* w w w . j  ava  2  s .  c  om*/

    // check that the file is deleted
    Assert.assertFalse("Snapshot file still exists.", snapshotFile.exists());

}

From source file:org.jboss.additional.testsuite.jdkall.present.domain.suites.ModelPersistenceTestCase.java

private CfgFileDescription getLatestBackup(File dir) throws IOException {

    int lastVersion = 0;
    File lastFile = null;//from   w  w w .j  a va2s  . com

    File[] children;
    if (dir.isDirectory() && (children = dir.listFiles()) != null) {
        for (File file : children) {

            String fileName = file.getName();
            String[] nameParts = fileName.split("\\.");
            if (!(nameParts[0].contains(DOMAIN_NAME) || nameParts[0].contains(MASTER_NAME)
                    || nameParts[0].contains(SLAVE_NAME))) {
                continue;
            }
            if (!nameParts[2].equals("xml")) {
                continue;
            }
            int version = Integer.valueOf(nameParts[1].substring(1));
            if (version > lastVersion) {
                lastVersion = version;
                lastFile = file;
            }
        }
    }
    return new CfgFileDescription(lastVersion, lastFile,
            (lastFile != null) ? FileUtils.checksumCRC32(lastFile) : 0);
}

From source file:org.jboss.as.logging.LoggingOperationsSubsystemTestCase.java

private void testAddRemoveFileHandler(final String loggingProfile) throws Exception {
    final KernelServices kernelServices = boot();
    final String fileHandlerName = "test-file-handler";

    File logFile = createLogFile();

    // Add file handler
    addFileHandler(kernelServices, loggingProfile, fileHandlerName, org.jboss.logmanager.Level.INFO, logFile,
            true);//from   ww w  .j  a  va2s .  c o  m

    final ModelNode rootLoggerAddress = createRootLoggerAddress(loggingProfile).toModelNode();
    // Ensure the model doesn't contain any erroneous attributes
    ModelNode op = SubsystemOperations.createReadResourceOperation(rootLoggerAddress);
    ModelNode result = executeOperation(kernelServices, op);
    final ModelNode rootLoggerResource = SubsystemOperations.readResult(result);
    validateResourceAttributes(rootLoggerResource,
            Logging.join(RootLoggerResourceDefinition.ATTRIBUTES, CommonAttributes.FILTER));

    // Ensure the handler is listed
    op = SubsystemOperations.createReadAttributeOperation(rootLoggerAddress, CommonAttributes.HANDLERS);
    ModelNode handlerResult = executeOperation(kernelServices, op);
    List<String> handlerList = SubsystemOperations.readResultAsList(handlerResult);
    assertTrue(String.format("Handler '%s' was not found. Result: %s", fileHandlerName, handlerResult),
            handlerList.contains(fileHandlerName));
    doLog(loggingProfile, LEVELS, "Test123");

    // Remove handler from logger
    op = SubsystemOperations.createOperation(
            RootLoggerResourceDefinition.ROOT_LOGGER_REMOVE_HANDLER_OPERATION_NAME, rootLoggerAddress);
    op.get(CommonAttributes.NAME.getName()).set(fileHandlerName);
    executeOperation(kernelServices, op);

    // Ensure the handler is not listed
    op = SubsystemOperations.createReadAttributeOperation(rootLoggerAddress, CommonAttributes.HANDLERS);
    handlerResult = executeOperation(kernelServices, op);
    handlerList = SubsystemOperations.readResultAsList(handlerResult);
    assertFalse(String.format("Handler '%s' was not removed. Result: %s", fileHandlerName, handlerResult),
            handlerList.contains(fileHandlerName));

    // Remove the handler
    removeFileHandler(kernelServices, loggingProfile, fileHandlerName, false);

    // check generated log file
    assertTrue(FileUtils.readFileToString(logFile).contains("Test123"));

    // verify that the logger is stopped, no more logs are coming to the file
    long checksum = FileUtils.checksumCRC32(logFile);
    doLog(loggingProfile, LEVELS, "Test123");
    assertEquals(checksum, FileUtils.checksumCRC32(logFile));
}

From source file:org.jboss.as.test.integration.management.api.logging.HandlerTestCase.java

@Test
public void testAddRemoveFileHandler() throws Exception {

    File logFile = new File(tempDir, "test-fh.log");
    if (logFile.exists())
        assertTrue(logFile.delete());//from ww  w.j  av a2  s. c  o m

    // add file handler
    ModelNode op = createOpNode("subsystem=logging/file-handler=test-fh", "add");
    op.get("name").set("test-fh");
    op.get("level").set("INFO");
    op.get("file").get("path").set(logFile.getAbsolutePath());
    executeOperation(op);

    // register it with root logger
    op = createOpNode("subsystem=logging/root-logger=ROOT", "root-logger-assign-handler");
    op.get("name").set("test-fh");
    executeOperation(op);

    // check it is listed in root-logger
    op = createOpNode("subsystem=logging/root-logger=ROOT", "read-attribute");
    op.get("name").set("handlers");
    ModelNode handlers = executeOperation(op);
    List<String> loggers = ModelUtil.modelNodeAsStingList(handlers);
    assertTrue(loggers.contains("test-fh"));

    // force server to issue a log message
    String response = HttpRequest.get(url.toString() + "/LoggingServlet", 10, TimeUnit.SECONDS);
    assertTrue(response.contains("Logging servlet."));

    // deregister handler from logger
    op = createOpNode("subsystem=logging/root-logger=ROOT", "root-logger-unassign-handler");
    op.get("name").set("test-fh");
    executeOperation(op);

    // check it is not listed in root-logger
    op = createOpNode("subsystem=logging/root-logger=ROOT", "read-attribute");
    op.get("name").set("handlers");
    handlers = executeOperation(op);
    loggers = ModelUtil.modelNodeAsStingList(handlers);
    assertFalse(loggers.contains("test-fh"));

    // remove handler
    op = createOpNode("subsystem=logging/file-handler=test-fh", "remove");
    executeOperation(op);

    // check generated log file
    String log = FileUtils.readFileToString(logFile);
    assertTrue(log.contains("Logging servlet."));

    // verify that the logger is stopped, no more logs are comming to the file
    long checksum = FileUtils.checksumCRC32(logFile);
    response = HttpRequest.get(url.toString() + "/LoggingServlet", 10, TimeUnit.SECONDS);
    assertTrue(response.contains("Logging servlet."));
    assertEquals(checksum, FileUtils.checksumCRC32(logFile));

    // remove log file
    assertTrue(logFile.delete());
}

From source file:org.jboss.as.test.integration.management.api.ModelPersistenceTestCase.java

@Test
public void testSimpleOperation() throws Exception {

    CfgFileDescription lastBackupDesc = getLatestBackup(currentCfgDir);

    long lastFileHash = lastCfgFile.exists() ? FileUtils.checksumCRC32(lastCfgFile) : -1;

    // execute operation so the model gets updated
    ModelNode op = createOpNode("system-property=test", "add");
    op.get("value").set("test");
    executeOperation(op);/*from w w w .ja v a  2  s  . c o m*/

    // check that the automated snapshat has been generated
    CfgFileDescription newBackupDesc = getLatestBackup(currentCfgDir);
    assertNotNull("Model snapshot not found.", newBackupDesc);
    // check that the version is incremented by one
    assertTrue(lastBackupDesc.version == newBackupDesc.version - 1);

    // check that the last cfg file has changed
    assertTrue(lastFileHash != FileUtils.checksumCRC32(lastCfgFile));

    // remove testing attribute
    op = createOpNode("system-property=test", "remove");
    executeOperation(op);

    // check that the snapshot has been updated again
    lastBackupDesc = newBackupDesc;
    newBackupDesc = getLatestBackup(currentCfgDir);
    assertNotNull("Model snapshot not found.", newBackupDesc);
    // check that the version is incremented by one
    assertTrue(lastBackupDesc.version == newBackupDesc.version - 1);
}

From source file:org.jboss.as.test.integration.management.api.ModelPersistenceTestCase.java

@Test
public void testTakeAndDeleteSnapshot() throws Exception {

    // take snapshot

    ModelNode op = createOpNode(null, "take-snapshot");
    ModelNode result = executeOperation(op);

    // check that the snapshot file exists
    String snapshotFileName = result.asString();
    File snapshotFile = new File(snapshotFileName);
    assertTrue(snapshotFile.exists());/*from   w w w.  ja v a 2 s  .c  o  m*/

    // compare with current cfg        
    long snapshotHash = FileUtils.checksumCRC32(snapshotFile);
    long lastHash = FileUtils.checksumCRC32(lastCfgFile);
    assertTrue(snapshotHash == lastHash);

    // delete snapshot
    op = createOpNode(null, "delete-snapshot");
    op.get("name").set(snapshotFile.getName());
    result = executeOperation(op);

    // check that the file is deleted
    assertFalse("Snapshot file stil exists.", snapshotFile.exists());

}

From source file:org.jboss.as.test.integration.management.api.ModelPersistenceTestCase.java

private CfgFileDescription getLatestBackup(File dir) throws IOException {

    int lastVersion = 0;
    File lastFile = null;/*from w ww.j  a  v  a2  s. c o m*/

    if (dir.isDirectory())
        for (File file : dir.listFiles()) {

            String fileName = file.getName();
            String[] nameParts = fileName.split("\\.");
            if (!nameParts[0].contains("standalone"))
                continue;
            if (!nameParts[2].equals("xml"))
                continue;
            int version = Integer.valueOf(nameParts[1].substring(1));
            if (version > lastVersion) {
                lastVersion = version;
                lastFile = file;
            }
        }
    return new CfgFileDescription(lastVersion, lastFile,
            (lastFile != null) ? FileUtils.checksumCRC32(lastFile) : 0);
}