Example usage for java.io File setReadOnly

List of usage examples for java.io File setReadOnly

Introduction

In this page you can find the example usage for java.io File setReadOnly.

Prototype

public boolean setReadOnly() 

Source Link

Document

Marks the file or directory named by this abstract pathname so that only read operations are allowed.

Usage

From source file:org.dbgl.util.FileUtils.java

public static void fileSetReadOnly(final File file) {
    if (!file.setReadOnly())
        System.err.println(/*from   w  w w.  ja  v a 2  s.com*/
                Settings.getInstance().msg("general.error.setreadonlyfile", new Object[] { file.getPath() }));
}

From source file:org.lockss.repository.LockssRepositoryImpl.java

static void saveAuIdProperties(String location, Properties props) {
    //XXX these AU_ID_FILE entries need to be backed up elsewhere to avoid
    // single-point corruption
    File propDir = new File(location);
    if (!propDir.exists()) {
        logger.debug("Creating directory '" + propDir.getAbsolutePath() + "'");
        propDir.mkdirs();/*from   www.  ja  va2  s.  c  om*/
    }
    File propFile = new File(propDir, AU_ID_FILE);
    try {
        logger.debug3("Saving au id properties at '" + location + "'.");
        OutputStream os = new BufferedOutputStream(new FileOutputStream(propFile));
        props.store(os, "ArchivalUnit id info");
        os.close();
        propFile.setReadOnly();
    } catch (IOException ioe) {
        logger.error("Couldn't write properties for " + propFile.getPath() + ".", ioe);
        throw new LockssRepository.RepositoryStateException("Couldn't write au id properties file.");
    }
}

From source file:org.openflexo.toolbox.FileUtils.java

/**
 * An extension to Java's API rename method. Will attempt Java's method of doing the rename, if this fails, this method will then
 * attempt to forcibly copy the old file to the new file name, and then delete the old file. (This in appearance makes it look like a
 * file rename has occurred.) The method will also attempt to preserve the new file's modification times and permissions to equal that
 * of the original file's./* w  w  w.  j  av  a  2  s . c  o m*/
 * 
 * @param source
 *            File
 * @param destination
 *            File
 * @return boolean
 * @throws IOException
 */
public static boolean rename(File source, File destination) throws IOException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        // First (very important on Windows) delete the destination if it exists (rename will fail on Windows if destination
        // exists)
        if (destination.exists()) {
            destination.delete();
        }
        // Do a normal API rename attempt
        if (source.renameTo(destination)) {
            return true;
        }
        FileUtils.createNewFile(destination);
        // API rename attempt failed, forcibly copy
        bis = new BufferedInputStream(new FileInputStream(source));
        bos = new BufferedOutputStream(new FileOutputStream(destination));

        // Do the copy
        pipeStreams(bos, bis);

        // Close the files
        bos.flush();

        // Close the files
        bis.close();
        bos.close();

        // Attempt to preserve file modification times
        destination.setLastModified(source.lastModified());
        if (!source.canWrite()) {
            destination.setReadOnly();
        }

        // Delete the original
        source.delete();

        bis = null;
        bos = null;
        return true;
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (bos != null) {
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.pdfsam.ui.JsonWorkspaceServiceTest.java

@Test(expected = RuntimeException.class)
public void saveWorkspaceReadOnly() throws IOException {
    File file = folder.newFile();
    file.setReadOnly();
    Map<String, Map<String, String>> data = new HashMap<>();
    Map<String, String> moduleData = new HashMap<>();
    moduleData.put("key", "value");
    data.put("module", moduleData);
    victim.saveWorkspace(data, file);//from  ww  w  . j  av a2  s  .  c  om
}

From source file:org.silverpeas.core.security.encryption.ContentEncryptionServiceTest.java

/**
 * Creates the key file with the specified actual key in hexadecimal.
 * @param key the key used in a content encryption and to store in the key file.
 * @throws Exception if the key file creation failed.
 *///from w  w  w. ja v  a2  s  .  co m
public void createKeyFileWithTheActualKey(String key) throws Exception {
    File keyFile = new File(ACTUAL_KEY_FILE_PATH);
    if (keyFile.exists()) {
        keyFile.setWritable(true);
    } else {
        System.out.println("WARNIIIINNNNNG: the key file " + ACTUAL_KEY_FILE_PATH + " DOESN'T EXIST!");
    }
    String encryptedKey = encryptKey(key);
    String encryptedContent = StringUtil.asBase64(CIPHER_KEY.getRawKey()) + " " + encryptedKey;
    Files.copy(new ByteArrayInputStream(encryptedContent.getBytes()), keyFile.toPath(), REPLACE_EXISTING);
    keyFile.setReadOnly();
}

From source file:org.silverpeas.core.security.encryption.ContentEncryptionServiceTest.java

/**
 * Creates the key file with the specified deprecated key in hexadecimal.
 * @param key the key used in a content encryption and to store in the old key file.
 * @throws Exception if the key file creation failed.
 *///w ww.ja v  a2 s. c o  m
public void createKeyFileWithTheDeprecatedKey(String key) throws Exception {
    File keyFile = new File(DEPRECATED_KEY_FILE_PATH);
    if (keyFile.exists()) {
        keyFile.setWritable(true);
    }
    String encryptedKey = encryptKey(key);
    String encryptedContent = StringUtil.asBase64(CIPHER_KEY.getRawKey()) + " " + encryptedKey;
    Files.copy(new ByteArrayInputStream(encryptedContent.getBytes()), keyFile.toPath(), REPLACE_EXISTING);
    keyFile.setReadOnly();
}

From source file:org.squale.squalix.tools.compiling.java.JCompilingTask.java

/**
 * @param bundlePath le paramtre dfinissant le chemin vers le bundle eclipse
 * @return le chemin dfinitif et vrifi vers la distribution d'eclipse pour la compilation RCP
 *//*  w  w  w.ja v a2 s .c o m*/
private String getEclipseBundleFile(StringParameterBO bundlePath) {
    // On rcupre la version de distribution d'eclipse  utiliser
    // Si il s'agit d'un rpertoire, on le dzippe sinon on utilise le chemin tel quel
    String bundleDir = "";
    if (null != bundlePath) {
        try {
            File bundleFile = FileUtility.getAbsoluteFile((String) mData.getData(TaskData.VIEW_PATH),
                    new File(bundlePath.getValue()));
            // On passe le fichie en readonly
            bundleFile.setReadOnly();
            if (bundleFile.isDirectory()) {
                bundleDir = bundleFile.getAbsolutePath();
            } else {
                // On extrait l'archive
                FileUtility.copyOrExtractInto(bundleFile, new File(mConfiguration.getEclipseBundleDir()));
                // on affecte le chemin donn dans la configuration
                bundleDir = mConfiguration.getEclipseBundleDir();
            }
        } catch (Exception e) {
            // On va juste lancer un warning en affichant l'erreur car la compilation peut-tre
            // russir sans le bundle
            String exceptionMsg = (e.getMessage() == null) ? e.getMessage() : e.toString();
            String message = CompilingMessages.getString("java.warning.eclipse_bundle_not_correct",
                    new String[] { bundlePath.getValue(), exceptionMsg });
            LOGGER.warn(message);
            initError(message);
        }
    }
    return bundleDir;
}

From source file:org.trellisldp.rosid.file.FileResourceServiceTest.java

@Test(expected = IOException.class)
public void testUnwritableRoot() throws IOException {
    final Map<String, String> config = new HashMap<>();
    config.put("repository", partitions.get("repository") + "/root3");
    final File root = new File(URI.create(config.get("repository")));
    assertTrue(root.mkdir());//from w  w w  . j  a va  2  s.  c om
    assumeTrue(root.setReadOnly());
    final ResourceService altService = new FileResourceService(config, curator, mockProducer, mockEventService,
            mockIdSupplier, false);
}

From source file:org.wso2.developerstudio.eclipse.greg.base.model.RegistryResourceNode.java

/**
 * get the content of the given version//from ww w .  ja va 2  s.c  o m
 * 
 * @param version
 * @param filePath
 * @return
 */
public VersionContent getVersionContent(String version, String filePath) {
    String versionName = getNameofTheResource(version);
    version = appendPath(getParent(), versionName);
    if (retrievedVersionsContent == null) {
        retrievedVersionsContent = new HashMap<String, VersionContent>();
    }
    VersionContent vc = null;
    if (!retrievedVersionsContent.containsKey(versionName)) {
        try {
            File content = null;
            if (getResourceType() == RegistryResourceType.RESOURCE) {
                content = getConnectionInfo().getRegistry().getContent(
                        (getLatestVersion().equals(version)) ? getRegistryResourcePath() : version, filePath);
                if (!getLatestVersion().equals(version)) {
                    content.setReadOnly();
                }
            }
            retrievedVersionsContent.put(versionName, new VersionContent(content, this, versionName));
        } catch (InvalidRegistryURLException e) {
            e.printStackTrace();
        } catch (UnknownRegistryException e) {
            e.printStackTrace();
        } catch (RegistryContentRetrieveException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (filePath != null) {
        vc = retrievedVersionsContent.get(versionName);
        try {
            vc.setFile(getConnectionInfo().getRegistry().getContent(
                    (getLatestVersion().equals(version)) ? getRegistryResourcePath() : version, filePath));
        } catch (IOException e) {
            log.error(e);
        } catch (InvalidRegistryURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownRegistryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RegistryContentRetrieveException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (vc == null)
        vc = retrievedVersionsContent.get(versionName);
    return vc;
}

From source file:org.wso2.developerstudio.eclipse.registry.base.model.RegistryResourceNode.java

/**
 * get the content of the given version//from ww w. j  a  v  a2  s .  co m
 * 
 * @param version
 * @param filePath
 * @return
 */
public VersionContent getVersionContent(String version, String filePath) {

    VersionContent vc = null;
    if (!isnew && !rename) {
        String versionName = getNameofTheResource(version);
        version = appendPath(getParent(), versionName);
        if (retrievedVersionsContent == null) {
            retrievedVersionsContent = new HashMap<String, VersionContent>();
        }
        vc = null;
        if (!retrievedVersionsContent.containsKey(versionName)) {
            try {
                File content = null;
                if (getResourceType() == RegistryResourceType.RESOURCE) {
                    content = getConnectionInfo().getRegistry().getContent(
                            (getLatestVersion().equals(version)) ? getRegistryResourcePath() : version,
                            filePath);
                    if (!getLatestVersion().equals(version)) {
                        content.setReadOnly();
                    }
                }
                retrievedVersionsContent.put(versionName, new VersionContent(content, this, versionName));
            } catch (InvalidRegistryURLException e) {
                e.printStackTrace();
            } catch (UnknownRegistryException e) {
                e.printStackTrace();
            } catch (RegistryContentRetrieveException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (filePath != null) {
            vc = retrievedVersionsContent.get(versionName);
            try {
                vc.setFile(getConnectionInfo().getRegistry().getContent(
                        (getLatestVersion().equals(version)) ? getRegistryResourcePath() : version, filePath));
            } catch (IOException e) {
                log.error(e);
            } catch (InvalidRegistryURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnknownRegistryException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (RegistryContentRetrieveException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (vc == null) {
            vc = retrievedVersionsContent.get(versionName);
        }
    }
    return vc;

}