Example usage for java.nio.file Files isSameFile

List of usage examples for java.nio.file Files isSameFile

Introduction

In this page you can find the example usage for java.nio.file Files isSameFile.

Prototype

public static boolean isSameFile(Path path, Path path2) throws IOException 

Source Link

Document

Tests if two paths locate the same file.

Usage

From source file:org.apache.openaz.xacml.admin.components.PolicyWorkspace.java

protected boolean isOverwritingAPolicy(Path newPolicyPath, Path oldPolicyPath) throws Exception {
    ///* w ww  .  jav  a  2  s.c  o m*/
    // Check to see if we were editing an existing file. Then check if the
    // new file actually exists. Then check if we are overwriting the original old file
    //
    if (oldPolicyPath != null && Files.exists(newPolicyPath)
            && Files.isSameFile(newPolicyPath, oldPolicyPath)) {
        //
        // Yes its the same, overwriting it is expected.
        //
        logger.info("isOverwritingAPolicy");
        return true;
    }
    return false;
}

From source file:org.apache.openaz.xacml.admin.components.PolicyWorkspace.java

protected void savePolicy(final Path newPolicyPath, final Object policyData, Path oldPolicyPath) {
    ////from   w  w w .  j  a v a2 s.  c o  m
    // Are they overwriting another policy?
    //
    String version = "1.0";
    boolean delete = false;
    if (oldPolicyPath != null) {
        //
        // This policy name was being edited. Is it still the same?
        //
        try {
            delete = true;
            if (Files.exists(newPolicyPath) && Files.isSameFile(newPolicyPath, oldPolicyPath)) {
                delete = false;
            }
        } catch (Exception e) {
            logger.error("Could not determine if same file", e);
            return;
        }
        logger.info("Deleting old file: " + delete);
    }
    //
    // Are we now overwriting another file?
    //
    if (Files.exists(newPolicyPath)) {
        //
        // Yes
        //
        logger.info("Overwriting file");
        //
        // Overwrite is happening. Bump the version (IF WE CAN)
        //
        //TODO - What if user wants to change something other than the last number?  For example, changing 1.5.23 to 2.0.0.
        //TODO     We need a mechanism that allows the user to specify the new policy version (disallowing backtracking) if they desire
        //TODO     and get that new number (if any) passed down to here.  This code then becomes the "then" branch of "If new version has been specified..."
        try {
            int[] versionArray = StdPDPPolicy
                    .versionStringToArray(XACMLPolicyScanner.getVersion(newPolicyPath));
            // increment the right-most digit
            versionArray[versionArray.length - 1]++;
            version = StdPDPPolicy.versionArrayToString(versionArray);
        } catch (NumberFormatException | IOException e) {
            try {
                logger.warn("Previous version '" + XACMLPolicyScanner.getVersion(newPolicyPath)
                        + "' not a series of itegers");
            } catch (IOException e1) {
                logger.error("could not get previous version");
            }
            //TODO - This may not be wise since the intent is to increase the version number.  Perhaps we should abort this an go back to the user?
            version = "1.0";
        }
        if (policyData instanceof PolicySetType) {
            ((PolicySetType) policyData).setVersion(version);
        } else if (policyData instanceof PolicyType) {
            ((PolicyType) policyData).setVersion(version);
        }
    } else {
        //
        // Nope, a completely new file
        //
        logger.info("New file");
    }
    //
    // Is the root a PolicySet or Policy?
    //
    Path finalPolicyPath;
    if (policyData instanceof PolicySetType) {
        //
        // Write it out
        //
        finalPolicyPath = XACMLPolicyWriter.writePolicyFile(newPolicyPath, (PolicySetType) policyData);
    } else if (policyData instanceof PolicyType) {
        //
        // Write it out
        //
        finalPolicyPath = XACMLPolicyWriter.writePolicyFile(newPolicyPath, (PolicyType) policyData);
    } else {
        logger.error("Unknown data type sent back.");
        return;
    }
    //
    // Did it get written?
    //
    if (finalPolicyPath == null || !Files.exists(finalPolicyPath)) {
        logger.error("Failed to write policy file.");
        return;
    }
    //
    // Add it into our tree
    //
    this.addPolicyFileToTree(finalPolicyPath.getParent().toFile(), finalPolicyPath.toFile());
    //
    // Do we need to delete the old file?
    //
    if (oldPolicyPath != null && delete) {
        try {
            Files.delete(oldPolicyPath);
        } catch (Exception e) {
            logger.error("Failed to delete old policy", e);
        }
        if (self.treeWorkspace.removeItem(oldPolicyPath.toFile()) == false) {
            logger.warn("Failed to remove old policy path");
        }
    }
}