John Smith
John Smith

Reputation: 787

How to store password from preference page in Eclipse?

I'm trying to store a password for updating in preference page field.

I did this using ISecurePreferences but the problem is the node doesn't exists after the instance is closed.

@Override
public boolean performOk() {
    // TODO Auto-generated method stub
    System.out.println("perform ok");
    ISecurePreferences root = SecurePreferencesFactory.getDefault();

    //System.out.println("Children names: "+root.);

    ISecurePreferences node = root.node("password");
    try {
        System.out.println("Password to store: "+passwordField.getStringValue());
        node.put("password",passwordField.getStringValue(),true);
        System.out.println("stored");


    } catch (StorageException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Storage failed");
    }       
    return true;
}

In preference initializer :

@Override
public void initializeDefaultPreferences() {
    // TODO Auto-generated method stub

    System.out.println("Initializing preferences");

    //IPreferenceStore prefStore = Activator.getDefault().getPreferenceStore();
    ISecurePreferences root = SecurePreferencesFactory.getDefault();
    if(root.nodeExists("password")){
        ISecurePreferences node = root.node("password");                    
        try {
            System.out.println("password taken: "+node.get("password",""));
        } catch (StorageException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    }
    else
    {
        System.out.println("node doesn't exists");
    }
}

In the same execution of the Eclipse I can store and retrieve the password.

Can you help me indicating what I have to do in order to persist the informations between instances ?

Upvotes: 0

Views: 437

Answers (1)

greg-449
greg-449

Reputation: 111142

I think you have to call the flush() method of ISecurePreferences to save the preferences you have set.

Upvotes: 1

Related Questions