How to remove a File from the File System in a XPages System Event

Viewed 115

I want to remove a File, when a Session is terminated, however, when trying to modify a File from the File System, I get a Notes Security Exception.

Here is what I have tried:

File f = getSourceFile();    
f.delete(); // java.lang.SecurityException: Zugriff auf die Datei {0} bzw. diese zu ändern ist nicht zulässig (Accessing or modifiying this File is prohibited)

FileUtils.clearFile(f); // java.lang.IllegalStateException: NotesContext is not initialized for the Thread 

This is how I retrieve my File:

public File getSourceFile() {
    return new File(this.absolutePath);
}

I also tried using doPrivileged but that caused the same IllegalStateException:

AccessController.doPrivileged(new PrivilegedAction() { // java.lang.IllegalStateException: NotesContext is not initialized for the Thread
    public Object run() {   
        File f = getSourceFile();
            if(f.exists()){
                FileUtil.deleteFile(f);
            }
    }
});

I guess it would be Possible to reinitialize the NotesContext somehow, but I don't think that would be an elegant solution.

The Stacktraces:

(Illegal State)

java.lang.IllegalStateException: NotesContext not initialized for the thread
at com.ibm.domino.xsp.module.nsf.NotesContext.getCurrent(NotesContext.java:123)
at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.findClass(ModuleClassLoader.java:438)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:924)
at java.lang.ClassLoader.loadClass(ClassLoader.java:869)
at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.loadClass(ModuleClassLoader.java:403)
at java.lang.ClassLoader.loadClass(ClassLoader.java:852)
at de.iV.beans.ticket.file.FileWrapper.clear(FileWrapper.java:90)
at de.iV.beans.ticket.Ticket.clearFiles(Ticket.java:155)
at de.iV.session.CustomSessionListener.sessionDestroyed(CustomSessionListener.java:46)
at com.ibm.xsp.application.ApplicationSessionListener.sessionDestroyed(ApplicationSessionListener.java:53)
at com.ibm.designer.runtime.domino.adapter.ComponentModule.notifySessionRemoved(ComponentModule.java:370)
at com.ibm.designer.runtime.domino.adapter.ComponentModule.checkSessionTimeout(ComponentModule.java:239)
at com.ibm.domino.xsp.module.nsf.NSFComponentModule.checkTimeout(NSFComponentModule.java:213)
at com.ibm.domino.xsp.module.nsf.NSFService.checkTimeout(NSFService.java:281)
at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.checkTimeout(LCDEnvironment.java:247)
at com.ibm.designer.runtime.domino.bootstrap.BootstrapEnvironment.tellCommand(BootstrapEnvironment.java:388)
at com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.tell(XspCmdManager.java:92

(Security)

    java.lang.SecurityException: Zugriff auf die Datei {0} bzw. diese zu ändern ist nicht zulässig
at lotus.notes.AgentSecurityManager.checkDelete(Unknown Source)
at java.io.File.delete(File.java:1047)
at de.iV.beans.ticket.file.FileWrapper.clear(FileWrapper.java:94)
at de.iV.beans.ticket.Ticket.clearFiles(Ticket.java:155)
at de.iV.session.CustomSessionListener.sessionDestroyed(CustomSessionListener.java:46)
at com.ibm.xsp.application.ApplicationSessionListener.sessionDestroyed(ApplicationSessionListener.java:53)
at com.ibm.designer.runtime.domino.adapter.ComponentModule.notifySessionRemoved(ComponentModule.java:370)
at com.ibm.designer.runtime.domino.adapter.ComponentModule.checkSessionTimeout(ComponentModule.java:239)
at com.ibm.domino.xsp.module.nsf.NSFComponentModule.checkTimeout(NSFComponentModule.java:213)
at com.ibm.domino.xsp.module.nsf.NSFService.checkTimeout(NSFService.java:281)
at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.checkTimeout(LCDEnvironment.java:247)
at com.ibm.designer.runtime.domino.bootstrap.BootstrapEnvironment.tellCommand(BootstrapEnvironment.java:388)
at com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.tell(XspCmdManager.java:92)

I was not able to figure out a workaround.

2 Answers

You have 2 options:

  • Edit java.policy (or create java.pol - not on 11.0)
  • Move your code into an osgi plugin

For the later: it isn't as scary as it sounds. Your plugin would live in an updatesite.nsf

Check this deck for getting started info.

I did a quick test, and everything worked without a problem. ND9 & ND10.

A Session listener, a file deletion in "Session Destroyed"-Event, no java.policy modification.

EDIT: Ah, got it. It has nothing to do with the file deletion. It is a ClassLoader problem when accessing classes in your NSF from the "Session Destroyed"-Event.

Related