Working with Sessions

Often times, we have a need to persist some specifics related to our custom plugin in addition to the ones from INTViewer. For example, if you have your own top component class, you might need to save and restore the content of that top component.

INTViewer accomplishes this with the help of ISession interface. The custom plugin has to do the following to save plugin related session.

  • Implement ISession interface with an empty constructor. This interface has a method getSessionId() which should return a unique name that identifies your plugin session. It also is an IPersistent which will allow you to save / restore your plugin specific details.
  • Create a com.interactive.intviewerapi.ISession file in the META-INF/services folder of your plugin. This file should only contain the string com-myplugin-MyPluginSession, or use the layer.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
    <folder name="Services">
        <file name="windowcounter.CounterSession.instance" />
    </folder>
</filesystem>

Key Notes:

  • A new instance of MyPluginSession ( from example below ) will be created every time you save / restore the session file. Hence all the variables should be managed through a singleton class as shown by the MyPluginSessionVariablesMgr in the example below.
  • Click on File-> Save Session menu to save the session to a .cfg file. Open the .cfg file to confirm the session details are saved under the tag "<Session>".
  • Also, keep in mind that the session variables are restored last inside INTViewer.

Example:

import com.interactive.intviewerapi.IMemento;
import com.interactive.intviewerapi.ISession;
import com.interactive.intviewerapi.PersistentException;
/**
 * This will allow INTViewer to save the "MyPlugin" module
 */
public class MyPluginSession implements ISession {
    /**
     * Returns "MyPlugin" module as the unique id for "MyPlugin" Sessions
     * @return
     */
    public String getSessionId() {
        return "MyPlugin";
    }
    /**
     * Save MyPluginSession
     * @param memento
     */
    public void saveState(IMemento memento) {
        IMemento m = memento.createChild("MyPlugin");
        MyPluginSessionVariablesMgr.getInstance().saveState(m);
    }
    /**
     * Restore MyPluginSession
     * @param memento
     * @throws com.interactive.intviewerapi.PersistentException
     */
    public void restoreState(IMemento memento) throws PersistentException {
        IMemento m = memento.getChild("MyPlugin");
        if (m != null) {
              MyPluginSessionVariablesMgr.getInstance().restoreState(m);
        }
    }
}