Example usage for java.lang SecurityManager checkWrite

List of usage examples for java.lang SecurityManager checkWrite

Introduction

In this page you can find the example usage for java.lang SecurityManager checkWrite.

Prototype

public void checkWrite(String file) 

Source Link

Document

Throws a SecurityException if the calling thread is not allowed to write to the file specified by the string argument.

Usage

From source file:Main.java

public static void main(String[] args) {

    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new Main();

    System.setSecurityManager(sm);

    sm.checkWrite("test.txt");

    System.out.println("Allowed!");
}

From source file:Main.java

public static void main(String[] args) {

    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new Main();

    System.setSecurityManager(sm);

    FileDescriptor fd = new FileDescriptor();
    sm.checkWrite(fd);

    System.out.println("Allowed!");
}

From source file:com.google.ratel.util.RatelUtils.java

/**
 * Returns true if Click resources (JavaScript, CSS, images etc) packaged in jars can be deployed to the root directory of the webapp,
 * false otherwise./*  w  w  w .  j  av  a  2  s . c om*/
 * <p/>
 * This method will return false in restricted environments where write access to the underlying file system is disallowed. Examples
 * where write access is not allowed include the WebLogic JEE server (this can be changed though) and Google App Engine.
 *
 * @param servletContext the application servlet context
 * @return true if writes are allowed, false otherwise
 */
public static boolean isResourcesDeployable(ServletContext servletContext) {
    try {
        boolean canWrite = (servletContext.getRealPath("/") != null);
        if (!canWrite) {
            return false;
        }

        // Since Google App Engine returns a value for getRealPath, check
        // SecurityManager if writes are allowed
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite("/");
        }
        return true;
    } catch (Throwable e) {
        return false;
    }
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Returns true if Click resources (JavaScript, CSS, images etc) packaged
 * in jars can be deployed to the root directory of the webapp, false
 * otherwise.//from w w w .j  a  v a2 s  .  c om
 * <p/>
 * This method will return false in restricted environments where write
 * access to the underlying file system is disallowed. Examples where
 * write access is not allowed include the WebLogic JEE server (this can be
 * changed though) and Google App Engine.
 *
 * @param servletContext the application servlet context
 * @return true if writes are allowed, false otherwise
 */
public static boolean isResourcesDeployable(ServletContext servletContext) {
    try {
        boolean canWrite = (servletContext.getRealPath("/") != null);
        if (!canWrite) {
            return false;
        }

        // Since Google App Engine returns a value for getRealPath, check
        // SecurityManager if writes are allowed
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite("/click");
        }
        return true;
    } catch (Throwable e) {
        return false;
    }
}

From source file:org.apache.fop.render.afp.AFPForeignAttributeReader.java

/**
 * Returns the resource level/* w ww  .java  2s . c o m*/
 *
 * @param foreignAttributes the foreign attributes
 * @return the resource level
 */
public AFPResourceLevel getResourceLevel(Map/*<QName, String>*/ foreignAttributes) {
    AFPResourceLevel resourceLevel = null;
    if (foreignAttributes != null && !foreignAttributes.isEmpty()) {
        if (foreignAttributes.containsKey(RESOURCE_LEVEL)) {
            String levelString = (String) foreignAttributes.get(RESOURCE_LEVEL);
            resourceLevel = AFPResourceLevel.valueOf(levelString);
            // if external get resource group file attributes
            if (resourceLevel != null && resourceLevel.isExternal()) {
                String resourceGroupFile = (String) foreignAttributes.get(RESOURCE_GROUP_FILE);
                if (resourceGroupFile == null) {
                    String msg = RESOURCE_GROUP_FILE + " not specified";
                    LOG.error(msg);
                    throw new UnsupportedOperationException(msg);
                }
                File resourceExternalGroupFile = new File(resourceGroupFile);
                SecurityManager security = System.getSecurityManager();
                try {
                    if (security != null) {
                        security.checkWrite(resourceExternalGroupFile.getPath());
                    }
                } catch (SecurityException ex) {
                    String msg = "unable to gain write access to external resource file: " + resourceGroupFile;
                    LOG.error(msg);
                }

                try {
                    boolean exists = resourceExternalGroupFile.exists();
                    if (exists) {
                        LOG.warn("overwriting external resource file: " + resourceGroupFile);
                    }
                    resourceLevel.setExternalFilePath(resourceGroupFile);
                } catch (SecurityException ex) {
                    String msg = "unable to gain read access to external resource file: " + resourceGroupFile;
                    LOG.error(msg);
                }
            }
        }
    }
    return resourceLevel;
}