restrict file access permissions to a program

Hello,I would like to know if it is possible to restrict file access permissions to certains files, defined inside my program. I can't modify the user's java.policy file. Thanks
[200 byte] By [JeanBeulza] at [2007-9-23]
# 1
if u come across something then pls tell me also
crack_ita at 2007-7-10 > top of java,Security,Other Security APIs, Tools, and Issues...
# 2

Actually, it's quite simple :)

You have to make a MyPolicy class (extending Policy), and declare one instance of that one.

private class MyPolicy extends Policy {

private PermissionCollection m_pc = null;

public MyPolicy() {

m_pc = new MyPermissionCollection();

m_pc.add((Permission) new FilePermission("tmp/-", "read,write,delete"));

}

}

You have to make a MyPermissionCollection class (extending PermissionCollection), and declare one instance of that one.

private class MyPermissionCollection extends PermissionCollection {

public void add(Permission permission) {

m_permissions.put(new Integer(permission.toString().hashCode()),

permission);

}

public String toString() {

return m_permissions.toString();

}

}

Your test class will be :

public Class Test {

public Test() {

MyPolicy mp = new MyPolicy();

Policy.setPolicy(mp);

System.setSecurityManager(new SecurityManager());

/* code trying to write a file in /tmp/ should be ok */

/* code trying to write a file in /etc/ should throw an AccessControlException */

}

}

JeanBeulza at 2007-7-10 > top of java,Security,Other Security APIs, Tools, and Issues...