I'm trying to find which class has requested the permission dynamically in a custom security manager. I was not able to find any API which helps me to get the codebase location of the invoking class. Below is what I'm trying to do,
I have a testApp class which tries to write to a file,
package test.ProfilingSecurityManager;
import java.io.PrintWriter;
public class TestApp {
public static void main(String [] args) throws Exception {
System.setSecurityManager(new NewProfilingSecurityManger());
PrintWriter writer = new PrintWriter("profile.txt");
writer.println("Test line");
writer.close();
}
}
The over-ridden method in the custom security manager is below,
public void checkPermission(final Permission permission) {
try {
// see what the parent security manager code says
super.checkPermission(permission);
}
catch (Exception e) {
// find the code base which requested this permission
// I can get the call stack here
Class [] sourceClasses = getClassContext();
Class invokingClass = sourceClasses[sourceClasses.length - 1];
// I can also get the accesscontrol context here
// using -AccessController.getContext()
// How do i find the codebase location of the class
// which needed this permission here
}
}
I need to find the codebase location of TestApp when the exception is thrown inside the checkPermission method. Could some one help me out on how to do this?
Thanks