Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 340688 - PreferenceInitializer with Extension org.eclipse.core.runtime.preferences never called
Summary: PreferenceInitializer with Extension org.eclipse.core.runtime.preferences nev...
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Runtime (show other bugs)
Version: 3.6.2   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: platform-runtime-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-22 13:10 EDT by Artur Kronenberg CLA
Modified: 2011-03-23 09:37 EDT (History)
2 users (show)

See Also:


Attachments
Test project with a PreferenceInitializer (4.44 KB, application/zip)
2011-03-22 13:10 EDT, Artur Kronenberg CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Artur Kronenberg CLA 2011-03-22 13:10:23 EDT
Created attachment 191702 [details]
Test project with a PreferenceInitializer

Hi,

I tried creating a plugin that initializes a couple of Default value preferences in the DefaultScope. I added the following extension:

<plugin>
   <extension
         point="org.eclipse.core.runtime.preferences">
      <initializer
            class="com.test.preferences.init.Initializer">
      </initializer>
   </extension>
</plugin>

I implemented this class: 

public class Initializer extends AbstractPreferenceInitializer {
    @Override
    public void initializeDefaultPreferences() {
        IEclipsePreferences node = new DefaultScope().getNode( Activator.PLUGIN_ID );
        node.put( "KEY", "VALUE");
    }
}

The class Initializer is never called when starting the Plug-In. 

I found a bunch of forum topics of people who had a similar problem but no responses whether there is some additional steps necessary to have this extension running. 
I took a look at the org.eclipse.ui project and it seems like the same thing is done there. For some reason it is working in there.

I'll attach my test project to this bug so you can have a look.
Comment 1 Curtis Windatt CLA 2011-03-22 14:44:07 EDT
Moving to Platform for comment.
Comment 2 Artur Kronenberg CLA 2011-03-23 05:30:51 EDT
With further debugging I found that the initializer is running through 
PreferenceServiceRegistryHelper#applyRuntimeDefaults(String name, WeakReference pluginReference)

I don't understand though why the name parameter is in the method. This method is called for a bunch of times for different plug-ins. 

I can't really see how it's decided who is called with this method. Is there a trick why this method needs to be called like twenty something times (in my launch config at least ) and iterate through every extension? I would think that by default this method should be called once and call the initialize method for every plug-in once. 

For now my workaround is to call the initializing at Activator's start method.
Comment 3 DJ Houghton CLA 2011-03-23 08:48:05 EDT
This method is called the first time someone accesses the default preferences for your plug-in.

We don't aggressively call it for every plug-in on startup because that would in turn start every plug-in in the system and negate the effects of lazy-loading.

I'll take a look at your test project to ensure we are seeing the intended behaviour.
Comment 4 Artur Kronenberg CLA 2011-03-23 09:06:51 EDT
Hi,

I tried calling the DefaultScope and the initializer got called. I am confused in one point and I am sorry if this is an obvious question but I've read on the FAQ (http://wiki.eclipse.org/FAQ_What_is_a_preference_scope%3F) page for the preferences scopes the following about the DefaultScope:

"
Default scope. This scope is not stored on disk at all but can be used to store default values for all your keys. When values are not found in other scopes, the default scope is consulted last to provide reasonable default values. 
"

So I expected the following behavior:

If I create the initializer and put in my default values in the DefaultScope then whenever I call the InstanceScope it would look up the key, see if there is a value stored for that key and if there is not, it would return the DefaultScope's value. If there is no value at all, it would return the second argument of the calling method on the InstanceScope. 

I get that there are two arguments on the method that is calling the preferences but I am not sure now what the DefaultScope is for (Or the Initializer for that matter).
I can't really initialize the InstanceScope, because I would overwrite my set values everytime the application starts. I don't have to initialize the DefaultScope because it is never called since I am putting in the Default argument in the calling method of the InstanceScope#get argument. Am I missing something?

Kind regards
-- artur
Comment 5 DJ Houghton CLA 2011-03-23 09:37:36 EDT
Yes, you've missed something but it isn't your fault... the preferences API is confusing. There are many hoops that we have to jump through in order to maintain backwards compatibility and enable the old Preferences APIs to continue working.

The short story is that when you ask for preferences in the Default scope, then that's all you get. The same for the Instance, Configuration, Project, etc scopes. There is no chaining or free lookups going on.

If you want to do a lookup across multiple scopes your best bet is to check out the IPreferencesService interface.

Here are a couple of documents that might help you:
  http://www.eclipse.org/eclipse/platform-core/documents/user_settings/faq.html
  http://www.eclipse.org/eclipse/platform-core/documents/user_settings/plugin_customization.html