JonDoe297
JonDoe297

Reputation: 1721

Eclipse plug-in development: persist data in memory

I'm developing an Eclipse plug-in. What it does is to add a menu option which opens a dialog. Something very simple, and it's working. Now, I need to retrieve the value of token from the memory before to open the ConnectDialog, and save it again every time the dialog is closed. I imagine it will be something like this:

public class ConnectHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);

        ConnectDialog connectDialog = new ConnectDialog(window.getShell());

        // HERE - get value from memory 
        String token = someKindOfEclipseSession.get("my.company.token");   

        connectDialog.setToken(token);
        connectDialog.open();
        token = connectDialog.getToken();

        // HERE - save value to memory
        someKindOfEclipseSession.put("my.company.token", token);

        return null;
    }
}

It's important that the value be saved into memory and not into a XML file because of security reasons. Of course, if the user closes Eclipse, it's okay that the value be forgotten.

I was looking for something like a Session, but I don't know what I should look for. May you help me, please?

Upvotes: 0

Views: 124

Answers (1)

greg-449
greg-449

Reputation: 111218

The simplest thing to do is to use a singleton class owned by your plugin's Activator.

So in the Activator

private MySession session;

public MySession getSession()
{
  if (session == null) {
    session = new MySession();
  }

  return session;
}

And you reference it in your handler with:

MySession session = Activator.getDefault().getSession();

where Activator is your activator class and MySession is a class you write to hold the session data.

Upvotes: 2

Related Questions