Example usage for java.io FilePermission FilePermission

List of usage examples for java.io FilePermission FilePermission

Introduction

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

Prototype


FilePermission(String path, int mask) 

Source Link

Document

Creates a new FilePermission object using an action mask.

Usage

From source file:com.sshtools.sshvnc.SshVNCPanel.java

private void initActions() {

    //  Create the action menu groups

    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("File", "File",

            'f', 0));

    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Edit", "Edit",

            'e', 10));

    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("View", "View",

            'v', 20));

    vncTab = new VNCTab( /*vnc*/);

    newAction = new NewAction() {

        public void actionPerformed(ActionEvent evt) {

            SshToolsConnectionProfile newProfile = newConnectionProfile(null);

            if (newProfile != null) {

                connect(newProfile, true);

            }/*from  w w w .  j a v a 2 s. co  m*/

            else {

                log.info("New connection cancelled");

            }

        }

    };

    registerAction(newAction);

    closeAction = new CloseAction() {

        public void actionPerformed(ActionEvent evt) {

            closing = true;
            // Close on a thread to avoid blocking the event queue
            Thread thread = new Thread() {
                public void run() {
                    closeConnection(true);
                }
            };

            thread.start();

        }

    };

    registerAction(closeAction);

    refreshAction = new RefreshAction() {

        public void actionPerformed(ActionEvent evt) {

            refresh();

        }

    };

    registerAction(refreshAction);

    ctrlAltDelAction = new CtrlAltDelAction() {

        public void actionPerformed(ActionEvent evt) {

            try {

                vnc.sendCtrlAltDel();

            }

            catch (IOException ioe) {

                closeConnection(true);

                showErrorMessage(SshVNCPanel.this, "Error", ioe);

            }

        }

    };

    registerAction(ctrlAltDelAction);

    clipboardAction = new ClipboardAction() {

        public void actionPerformed(ActionEvent evt) {

            vnc.setClipboardVisible(!vnc.isClipboardVisible());

        }

    };

    registerAction(clipboardAction);

    if (getApplication().getMRUModel() != null) {

        registerAction(

                mruAction = new MRUActionImpl(getApplication().getMRUModel()));

    }

    connectionPropertiesAction = new ConnectionPropertiesAction() {

        public void actionPerformed(ActionEvent evt) {

            editConnection(getCurrentConnectionProfile());

        }

    };

    registerAction(connectionPropertiesAction);

    //  Only allow opening of files if allowed by the security manager

    try {

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "read"));

        }

        openAction = new OpenAction() {

            public void actionPerformed(ActionEvent evt) {

                open();

            }

        };

        registerAction(openAction);

    }

    catch (AccessControlException ace) {

        ace.printStackTrace();

    }

    //  Only allow saving of files if allowed by the security manager

    try {

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "write"));

        }

        saveAction = new SaveAction() {

            public void actionPerformed(ActionEvent evt) {

                saveConnection(false, getCurrentConnectionFile(),

                        getCurrentConnectionProfile());

            }

        };

        registerAction(saveAction);

        saveAsAction = new SaveAsAction() {

            public void actionPerformed(ActionEvent evt) {

                saveConnection(true, getCurrentConnectionFile(),

                        getCurrentConnectionProfile());

            }

        };

        registerAction(saveAsAction);

        recordAction = new RecordAction() {

            public void actionPerformed(ActionEvent evt) {

                startRecording();

            }

        };

        registerAction(recordAction);

        stopAction = new StopAction() {

            public void actionPerformed(ActionEvent evt) {

                stopRecording();

            }

        };

        registerAction(stopAction);

    }

    catch (AccessControlException ace) {

        ace.printStackTrace();

    }

    //  Only allow editing of connection file if read / write is allowed

    try {

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "write"));

        }

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "read"));

        }

        editAction = new EditAction() {

            public void actionPerformed(ActionEvent evt) {

                editConnection();

            }

        };

        registerAction(editAction);

    }

    catch (AccessControlException ace) {

        ace.printStackTrace();

    }

    java.util.List providers = SessionProviderFactory.getInstance().getSessionProviders();
    SessionProvider provider;
    SessionProviderAction action;
    for (Iterator it = providers.iterator(); it.hasNext();) {
        provider = (SessionProvider) it.next();
        action = new SessionProviderAction(provider);
        sessionActions.put(action.getActionCommand(), action);
        action.addActionListener(this);
        registerAction(action);
    }

}

From source file:com.sshtools.powervnc.PowerVNCPanel.java

private void initActions() {

    //  Create the action menu groups

    System.out.println("initActions");
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("File", "File",

            'f', 0));

    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Edit", "Edit",

            'e', 10));

    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("View", "View",

            'v', 20));

    vncTab = new VNCTab( /*vnc*/);

    desktopAction = new DesktopAction() {

        public void actionPerformed(ActionEvent evt) {

            try {
                authenticationComplete(false);
            } catch (SshException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//ww w  . j  a v  a2 s. co m
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            /*
                    newProfile = newConnectionProfile(null);
                    
                    if (newProfile != null) {
                    
                      connect(newProfile, true);
                    
                    }
                    
                    else {
                    
                      log.info("New connection cancelled");
                    
                    }
            */
        }

    };

    registerAction(desktopAction);

    closeAction = new CloseAction() {

        public void actionPerformed(ActionEvent evt) {

            closing = true;
            // Close on a thread to avoid blocking the event queue
            Thread thread = new Thread() {
                public void run() {
                    closeConnection(true);
                }
            };

            thread.start();

        }

    };

    registerAction(closeAction);

    filemanagerAction = new FileManagerAction() {

        public void actionPerformed(ActionEvent evt) {

            new JFtp(ssh);
            //         refresh();

        }

    };

    registerAction(filemanagerAction);

    ctrlAltDelAction = new CtrlAltDelAction() {

        public void actionPerformed(ActionEvent evt) {

            try {

                vnc.sendCtrlAltDel();

            }

            catch (IOException ioe) {

                closeConnection(true);

                showErrorMessage(PowerVNCPanel.this, "Error", ioe);

            }

        }

    };

    registerAction(ctrlAltDelAction);

    clipboardAction = new ClipboardAction() {

        public void actionPerformed(ActionEvent evt) {

            vnc.setClipboardVisible(!vnc.isClipboardVisible());

        }

    };

    registerAction(clipboardAction);

    if (getApplication().getMRUModel() != null) {

        registerAction(

                mruAction = new MRUActionImpl(getApplication().getMRUModel()));

    }

    connectionPropertiesAction = new ConnectionPropertiesAction() {

        public void actionPerformed(ActionEvent evt) {

            editConnection(getCurrentConnectionProfile());

        }

    };

    registerAction(connectionPropertiesAction);

    //  Only allow opening of files if allowed by the security manager

    try {

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "read"));

        }

        openAction = new OpenAction() {

            public void actionPerformed(ActionEvent evt) {

                open();

            }

        };

        registerAction(openAction);

    }

    catch (AccessControlException ace) {

        ace.printStackTrace();

    }

    //  Only allow saving of files if allowed by the security manager

    try {

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "write"));

        }

        saveAction = new SaveAction() {

            public void actionPerformed(ActionEvent evt) {

                saveConnection(false, getCurrentConnectionFile(),

                        getCurrentConnectionProfile());

            }

        };

        registerAction(saveAction);

        saveAsAction = new SaveAsAction() {

            public void actionPerformed(ActionEvent evt) {

                saveConnection(true, getCurrentConnectionFile(),

                        getCurrentConnectionProfile());

            }

        };

        registerAction(saveAsAction);

        recordAction = new RecordAction() {

            public void actionPerformed(ActionEvent evt) {

                startRecording();

            }

        };

        registerAction(recordAction);

        stopAction = new StopAction() {

            public void actionPerformed(ActionEvent evt) {

                stopRecording();

            }

        };

        registerAction(stopAction);

    }

    catch (AccessControlException ace) {

        ace.printStackTrace();

    }

    //  Only allow editing of connection file if read / write is allowed

    try {

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "write"));

        }

        if (System.getSecurityManager() != null) {

            AccessController.checkPermission(

                    new FilePermission("<<ALL FILES>>", "read"));

        }

        editAction = new EditAction() {

            public void actionPerformed(ActionEvent evt) {

                editConnection();

            }

        };

        registerAction(editAction);

    }

    catch (AccessControlException ace) {

        ace.printStackTrace();

    }

    java.util.List providers = SessionProviderFactory.getInstance().getSessionProviders();
    SessionProvider provider;
    SessionProviderAction action;
    for (Iterator it = providers.iterator(); it.hasNext();) {
        provider = (SessionProvider) it.next();
        action = new SessionProviderAction(provider);
        sessionActions.put(action.getActionCommand(), action);
        action.addActionListener(this);
        registerAction(action);
    }

}

From source file:com.stratuscom.harvester.deployer.StarterServiceDeployer.java

void grantPermissionsToWorkDirectoryFor(ApplicationEnvironment env) {
    Permission[] perms = new Permission[] {
            new FilePermission(env.getWorkingDirectory().getAbsolutePath() + "/-", "read,write,delete") };
    grantPermissions(env.getClassLoader(), perms);
}

From source file:com.sshtools.common.ui.SshToolsApplicationClientPanel.java

/**
 *
 *
 * @param disconnect//  www .  j  a va2  s  .  c  om
 */
public void closeConnection(boolean disconnect) {
    //
    if (isNeedSave() && (currentConnectionFile != null)) { // Stop save dialog box when not using a pre-existing profile.
        //  Only allow saving of files if allowed by the security manager
        try {
            if (System.getSecurityManager() != null) {
                AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));

                if (JOptionPane.showConfirmDialog(this, "You have unsaved changes to the connection "
                        + ((currentConnectionFile == null) ? "<Untitled>" : currentConnectionFile.getName())
                        + ".\nDo you want to save the changes now?", "Unsaved changes",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                    saveConnection(false, getCurrentConnectionFile(), getCurrentConnectionProfile());
                    setNeedSave(false);
                }
            }
        } catch (AccessControlException ace) {
            log.warn("Changes made to connection, but security manager won't allow saving of files.");
        }
    }

    //setCurrentConnectionFile(null);
}

From source file:com.sshtools.sshterm.SshTermSessionPanel.java

private void initActions() {
    //  Create the action menu groups
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("File", "File", 'f', 0));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Edit", "Edit", 'e', 10));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("View", "View", 'v', 20));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Help", "Help", 'h', 90));
    actions = new Vector();

    connectionPropertiesAction = new ConnectionPropertiesActionImpl();
    registerAction(connectionPropertiesAction);

    // newAction = new NewAction();
    //  registerAction(newAction);
    //  Only allow opening of files if allowed by the security manager
    try {/* ww  w.jav  a 2  s  .c  o m*/
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "read"));
        }

        //openAction = new OpenAction();
        // registerAction(openAction);
        playAction = new PlayAction();
        registerAction(playAction);
    } catch (AccessControlException ace) {
        log.warn("File reading actions are not available");
    }

    //  Only allow saving of files if allowed by the security manager
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));
        }

        // saveAction = new SaveAction();
        // registerAction(saveAction);
        //  saveAsAction = new SaveAsAction();
        //  registerAction(saveAsAction);
        recordAction = new RecordAction();
        registerAction(recordAction);
        stopAction = new StopAction();
        registerAction(stopAction);
    } catch (AccessControlException ace) {
        log.warn("File write actions are not available");
    }

    //  Only allow editing of connection file if read / write is allowed
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));
        }

        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "read"));
        }

        // editAction = new EditActionImpl();
        // registerAction(editAction);
    } catch (AccessControlException ace) {
        log.warn("Read / write actions are not available");
    }

    //  Checking if printing is allowed
    if (pageFormat != null) {
        try {
            if (System.getSecurityManager() != null) {
                AccessController.checkPermission(new RuntimePermission("queuePrintJob"));
            }

            printAction = new PrintActionImpl();
            registerAction(printAction);
            printPreviewAction = new PrintPreviewActionImpl();
            registerAction(printPreviewAction);
        } catch (AccessControlException ace) {
            log.warn("Print actions are not available");
        }
    }

    //  Always allow refreshing of terminal
    refreshAction = new RefreshActionImpl();
    registerAction(refreshAction);

    //  Always allow closing of connect
    closeAction = new CloseAction();
    registerAction(closeAction);

    //  Copy / Paste
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new AWTPermission("accessClipboard"));
        }

        copyAction = new CopyActionImpl();
        registerAction(copyAction);
        pasteAction = new PasteActionImpl();
        registerAction(pasteAction);
    } catch (AccessControlException ace) {
    }

    //  Theres no point in having the keygen action if we can't write to local file
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));
        }

        //  keygenAction = new KeygenAction();
        // registerAction(keygenAction);
    } catch (AccessControlException ace) {
        log.warn("Keygen actions is not available");
    }

    //  Clear action
    clearAction = new ClearActionImpl();
    registerAction(clearAction);

    // Remove stuff we dont want
    deregisterAction(getAction("Options"));
    setActionVisible("New Window", false);
    setActionVisible("About", false);
}

From source file:com.sshtools.sshterm.SshTerminalPanel.java

private void initActions() {
    //  Create the action menu groups
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("File", "File", 'f', 0));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Edit", "Edit", 'e', 10));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("View", "View", 'v', 20));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Proxy", "Proxy", 'p', 80));
    registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Help", "Help", 'h', 90));
    actions = new Vector();

    // MRU/*from ww w  .  jav a 2 s. c o m*/
    if (getApplication().getMRUModel() != null) {
        registerAction(mruAction = new MRUActionImpl(getApplication().getMRUModel()));
    }

    //
    connectionPropertiesAction = new ConnectionPropertiesActionImpl();
    registerAction(connectionPropertiesAction);
    newAction = new NewAction();
    registerAction(newAction);

    //  Only allow opening of files if allowed by the security manager
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "read"));
        }

        openAction = new OpenAction();
        registerAction(openAction);
        playAction = new PlayAction();
        registerAction(playAction);
    } catch (AccessControlException ace) {
        log.warn("File reading actions are not available");
    }

    //  Only allow saving of files if allowed by the security manager
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));
        }

        saveAction = new SaveAction();
        registerAction(saveAction);
        saveAsAction = new SaveAsAction();
        registerAction(saveAsAction);
        recordAction = new RecordAction();
        registerAction(recordAction);
        stopAction = new StopAction();
        registerAction(stopAction);
    } catch (AccessControlException ace) {
        log.warn("File write actions are not available");
    }

    //  Only allow editing of connection file if read / write is allowed
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));
        }

        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "read"));
        }

        editAction = new EditActionImpl();
        registerAction(editAction);
    } catch (AccessControlException ace) {
        log.warn("Read / write actions are not available");
    }

    //  Checking if printing is allowed
    if (pageFormat != null) {
        try {
            if (System.getSecurityManager() != null) {
                AccessController.checkPermission(new RuntimePermission("queuePrintJob"));
            }

            printAction = new PrintActionImpl();
            registerAction(printAction);
            printPreviewAction = new PrintPreviewActionImpl();
            registerAction(printPreviewAction);
        } catch (AccessControlException ace) {
            log.warn("Print actions are not available");
        }
    }

    //  Always allow refreshing of terminal
    refreshAction = new RefreshActionImpl();
    registerAction(refreshAction);

    //  Always allow closing of connect
    closeAction = new CloseAction();
    registerAction(closeAction);

    //  Copy / Paste
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new AWTPermission("accessClipboard"));
        }

        copyAction = new CopyActionImpl();
        registerAction(copyAction);
        pasteAction = new PasteActionImpl();
        registerAction(pasteAction);
    } catch (AccessControlException ace) {
    }

    //  Theres no point in having the keygen action if we can't write to local file
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new FilePermission("<<ALL FILES>>", "write"));
        }

        keygenAction = new KeygenAction();
        registerAction(keygenAction);
    } catch (AccessControlException ace) {
        log.warn("Keygen actions is not available");
    }

    //  Clear action
    clearAction = new ClearActionImpl();
    registerAction(clearAction);

    // GSI options
    proxyInfoAction = new ProxyInfoAction();
    registerAction(proxyInfoAction);
    proxyDestroyAction = new ProxyDestroyAction();
    registerAction(proxyDestroyAction);

    // Secure Tunneling
    /*try {
      SessionProvider provider = SessionProviderFactory.getInstance().getProvider("tunneling");
      if(provider!=null) {
        tunnelingAction = (StandardAction)new SessionProviderAction(
    provider);
        registerAction(tunnelingAction);
      }
         }
         catch (Throwable t) {
      log.info(
          "Secure Tunneling not available on CLASSPATH");
         }
         //  ShiFT action
         try {
      SessionProvider provider = SessionProviderFactory.getInstance().getProvider("shift");
      if(provider!=null) {
        shiftAction = (StandardAction)new SessionProviderAction(
    provider);
        registerAction(shiftAction);
      }
         }
         catch (Throwable t) {
      log.info(
          "ShiFT not available on CLASSPATH");
         }*/

    java.util.List providers = SessionProviderFactory.getInstance().getSessionProviders();
    SessionProvider provider;
    SessionProviderAction action;
    for (Iterator it = providers.iterator(); it.hasNext();) {
        provider = (SessionProvider) it.next();
        action = new SessionProviderAction(provider);
        sessionActions.put(action.getActionCommand(), action);
        registerAction(action);
    }
}

From source file:org.apache.catalina.loader.WebappClassLoader.java

/**
 * If there is a Java SecurityManager create a read FilePermission
 * or JndiPermission for the file directory path.
 *
 * @param path file directory path//from   w w  w . j av a2  s . c  o  m
 */
public void addPermission(String path) {
    if (path == null) {
        return;
    }

    if (securityManager != null) {
        Permission permission = null;
        if (path.startsWith("jndi:") || path.startsWith("jar:jndi:")) {
            if (!path.endsWith("/")) {
                path = path + "/";
            }
            permission = new JndiPermission(path + "*");
            addPermission(permission);
        } else {
            if (!path.endsWith(File.separator)) {
                permission = new FilePermission(path, "read");
                addPermission(permission);
                path = path + File.separator;
            }
            permission = new FilePermission(path + "-", "read");
            addPermission(permission);
        }
    }
}

From source file:org.apache.catalina.loader.WebappLoader.java

/**
 * Configure associated class loader permissions.
 *//*from ww  w  .ja v a2  s.co  m*/
private void setPermissions() {

    if (System.getSecurityManager() == null)
        return;
    if (!(container instanceof Context))
        return;

    // Tell the class loader the root of the context
    ServletContext servletContext = ((Context) container).getServletContext();

    // Assigning permissions for the work directory
    File workDir = (File) servletContext.getAttribute(Globals.WORK_DIR_ATTR);
    if (workDir != null) {
        try {
            String workDirPath = workDir.getCanonicalPath();
            classLoader.addPermission(new FilePermission(workDirPath, "read,write"));
            classLoader
                    .addPermission(new FilePermission(workDirPath + File.separator + "-", "read,write,delete"));
        } catch (IOException e) {
            // Ignore
        }
    }

    try {

        URL rootURL = servletContext.getResource("/");
        classLoader.addPermission(rootURL);

        String contextRoot = servletContext.getRealPath("/");
        if (contextRoot != null) {
            try {
                contextRoot = (new File(contextRoot)).getCanonicalPath();
                classLoader.addPermission(contextRoot);
            } catch (IOException e) {
                // Ignore
            }
        }

        URL classesURL = servletContext.getResource("/WEB-INF/classes/");
        classLoader.addPermission(classesURL);
        URL libURL = servletContext.getResource("/WEB-INF/lib/");
        classLoader.addPermission(libURL);

        if (contextRoot != null) {

            if (libURL != null) {
                File rootDir = new File(contextRoot);
                File libDir = new File(rootDir, "WEB-INF/lib/");
                try {
                    String path = libDir.getCanonicalPath();
                    classLoader.addPermission(path);
                } catch (IOException e) {
                }
            }

        } else {

            if (workDir != null) {
                if (libURL != null) {
                    File libDir = new File(workDir, "WEB-INF/lib/");
                    try {
                        String path = libDir.getCanonicalPath();
                        classLoader.addPermission(path);
                    } catch (IOException e) {
                    }
                }
                if (classesURL != null) {
                    File classesDir = new File(workDir, "WEB-INF/classes/");
                    try {
                        String path = classesDir.getCanonicalPath();
                        classLoader.addPermission(path);
                    } catch (IOException e) {
                    }
                }
            }

        }

    } catch (MalformedURLException e) {
    }

}

From source file:org.apache.jasper.compiler.JspRuntimeContext.java

/**
 * Method used to initialize SecurityManager data.
 */// www  .  j a va2 s.  c  om
private void initSecurity() {

    // Setup the PermissionCollection for this web app context
    // based on the permissions configured for the root of the
    // web app context directory, then add a file read permission
    // for that directory.
    Policy policy = Policy.getPolicy();
    if (policy != null) {
        try {
            // Get the permissions for the web app context
            String docBase = context.getRealPath("/");
            if (docBase == null) {
                docBase = options.getScratchDir().toString();
            }
            String codeBase = docBase;
            if (!codeBase.endsWith(File.separator)) {
                codeBase = codeBase + File.separator;
            }
            File contextDir = new File(codeBase);
            URL url = contextDir.getCanonicalFile().toURL();
            codeSource = new CodeSource(url, null);
            permissionCollection = policy.getPermissions(codeSource);

            // Create a file read permission for web app context directory
            if (!docBase.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(docBase, "read"));
                docBase = docBase + File.separator;
            } else {
                permissionCollection
                        .add(new FilePermission(docBase.substring(0, docBase.length() - 1), "read"));
            }
            docBase = docBase + "-";
            permissionCollection.add(new FilePermission(docBase, "read"));

            // Create a file read permission for web app tempdir (work)
            // directory
            String workDir = options.getScratchDir().toString();
            if (!workDir.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(workDir, "read"));
                workDir = workDir + File.separator;
            }
            workDir = workDir + "-";
            permissionCollection.add(new FilePermission(workDir, "read"));

            // Allow the JSP to access org.apache.jasper.runtime.HttpJspBase
            permissionCollection.add(new RuntimePermission("accessClassInPackage.org.apache.jasper.runtime"));

            if (parentClassLoader instanceof URLClassLoader) {
                URL[] urls = parentClassLoader.getURLs();
                String jarUrl = null;
                String jndiUrl = null;
                for (int i = 0; i < urls.length; i++) {
                    if (jndiUrl == null && urls[i].toString().startsWith("jndi:")) {
                        jndiUrl = urls[i].toString() + "-";
                    }
                    if (jarUrl == null && urls[i].toString().startsWith("jar:jndi:")) {
                        jarUrl = urls[i].toString();
                        jarUrl = jarUrl.substring(0, jarUrl.length() - 2);
                        jarUrl = jarUrl.substring(0, jarUrl.lastIndexOf('/')) + "/-";
                    }
                }
                if (jarUrl != null) {
                    permissionCollection.add(new FilePermission(jarUrl, "read"));
                    permissionCollection.add(new FilePermission(jarUrl.substring(4), "read"));
                }
                if (jndiUrl != null)
                    permissionCollection.add(new FilePermission(jndiUrl, "read"));
            }
        } catch (Exception e) {
            context.log("Security Init for context failed", e);
        }
    }
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

/**
 * This is a copy of {@link URLClassLoader#getPermissions(CodeSource)}.
 *
 * Returns the permissions for the given codesource object.
 * The implementation of this method first calls super.getPermissions
 * and then adds permissions based on the URL of the codesource.
 * <p>/*from   w  w  w .  j av  a  2s .  com*/
 * If the protocol of this URL is "jar", then the permission granted
 * is based on the permission that is required by the URL of the Jar
 * file.
 * <p>
 * If the protocol is "file"
 * and the path specifies a file, then permission to read that
 * file is granted. If protocol is "file" and the path is
 * a directory, permission is granted to read all files
 * and (recursively) all files and subdirectories contained in
 * that directory.
 * <p>
 * If the protocol is not "file", then
 * to connect to and accept connections from the URL's host is granted.
 * @param codesource the codesource
 * @return the permissions granted to the codesource
 */
@Override
protected PermissionCollection getPermissions(CodeSource codesource) {
    final PermissionCollection perms = super.getPermissions(codesource);
    final URL url = codesource.getLocation();
    Permission p;
    URLConnection urlConnection;
    try {
        urlConnection = url.openConnection();
        p = urlConnection.getPermission();
    } catch (IOException ignored) {
        p = null;
        urlConnection = null;
    }
    if (p instanceof FilePermission) {
        // if the permission has a separator char on the end,
        // it means the codebase is a directory, and we need
        // to add an additional permission to read recursively
        String path = p.getName();
        if (path.endsWith(File.separator)) {
            path += "-";
            p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
        }
    } else if ((p == null) && (url.getProtocol().equals("file"))) {
        String path = url.getFile().replace('/', File.separatorChar);
        path = ParseUtil.decode(path);
        if (path.endsWith(File.separator)) {
            path += "-";
        }
        p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
    } else {
        URL locUrl = url;
        if (urlConnection instanceof JarURLConnection) {
            locUrl = ((JarURLConnection) urlConnection).getJarFileURL();
        }
        final String host = locUrl.getHost();
        if (host != null && (host.length() > 0)) {
            p = new SocketPermission(host, SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
        }
    }
    // make sure the person that created this class loader
    // would have this permission

    if (p != null) {
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            final Permission fp = p;
            doPrivileged(new PrivilegedAction<Void>() {
                @Override
                public Void run() throws SecurityException {
                    sm.checkPermission(fp);
                    return null;
                }
            }, _acc);
        }
        perms.add(p);
    }
    return perms;
}