KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > OSGiPreferencesServiceManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Danail Nachev (ProSyst) - bug 188070
11  *******************************************************************************/

12 package org.eclipse.core.internal.preferences;
13
14 import java.util.Set JavaDoc;
15 import java.util.TreeSet JavaDoc;
16 import org.eclipse.core.runtime.preferences.ConfigurationScope;
17 import org.osgi.framework.*;
18 import org.osgi.service.prefs.BackingStoreException;
19 import org.osgi.service.prefs.Preferences;
20
21 /**
22  * <p>
23  * Class used to manage OSGi Preferences Service. Creates a new OSGiPreferencesServiceImpl
24  * object for every bundle that gets the Preferences Service. When a bundle ungets the
25  * Preference Service, it's preferences are flushed to disk.
26  * </p>
27  * <p>
28  * Also deletes saved preferences for bundles which are uninstalled.
29  * </p>
30  */

31 public class OSGiPreferencesServiceManager implements ServiceFactory, BundleListener {
32
33     private static final String JavaDoc ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI = "org.eclipse.core.internal.preferences.osgi"; //$NON-NLS-1$
34

35     //keys are bundles that use OSGi prefs
36
private Preferences prefBundles;
37
38     public OSGiPreferencesServiceManager(BundleContext context) {
39
40         context.addBundleListener(this);
41
42         //prefBundles = new InstanceScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI);
43
prefBundles = new ConfigurationScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI);
44
45         //clean up prefs for bundles that have been uninstalled
46
try {
47
48             //get list of currently installed bundles
49
Bundle[] allBundles = context.getBundles();
50             Set JavaDoc bundleQualifiers = new TreeSet JavaDoc();
51             for (int i = 0; i < allBundles.length; i++) {
52                 bundleQualifiers.add(getQualifier(allBundles[i]));
53             }
54
55             //get list of bundles we created prefs for
56
String JavaDoc[] prefsBundles = prefBundles.keys();
57
58             //remove prefs nodes for bundles that are no longer installed
59
for (int i = 0; i < prefsBundles.length; i++) {
60                 if (!bundleQualifiers.contains(prefsBundles[i])) {
61                     removePrefs(prefsBundles[i]);
62                 }
63             }
64
65         } catch (BackingStoreException e) {
66             //best effort
67
}
68     }
69
70     /**
71      * Creates a new OSGiPreferencesServiceImpl for each bundle.
72      */

73     public Object JavaDoc getService(Bundle bundle, ServiceRegistration registration) {
74         String JavaDoc qualifier = getQualifier(bundle);
75         //remember we created prefs for this bundle
76
Preferences bundlesNode = getBundlesNode();
77         bundlesNode.put(qualifier, ""); //$NON-NLS-1$
78
try {
79             bundlesNode.flush();
80         } catch (BackingStoreException e) {
81             //best effort
82
}
83         //return new OSGiPreferencesServiceImpl(new InstanceScope().getNode(getQualifier(bundle)));
84
return new OSGiPreferencesServiceImpl(new ConfigurationScope().getNode(getQualifier(bundle)));
85     }
86
87     /**
88      * Store preferences per bundle id
89      */

90     private String JavaDoc getQualifier(Bundle bundle) {
91         String JavaDoc qualifier = "org.eclipse.core.runtime.preferences.OSGiPreferences." + bundle.getBundleId(); //$NON-NLS-1$
92
return qualifier;
93     }
94
95     /**
96      * Flush the bundle's preferences.
97      */

98     public void ungetService(Bundle bundle, ServiceRegistration registration, Object JavaDoc service) {
99         try {
100             //new InstanceScope().getNode(getQualifier(bundle)).flush();
101
new ConfigurationScope().getNode(getQualifier(bundle)).flush();
102         } catch (BackingStoreException e) {
103             //best effort
104
}
105     }
106
107     /**
108      * If a bundle is uninstalled, delete all of it's preferences from the disk.
109      */

110     public void bundleChanged(BundleEvent event) {
111         if (event.getType() == BundleEvent.UNINSTALLED) {
112             try {
113                 removePrefs(getQualifier(event.getBundle()));
114             } catch (BackingStoreException e) {
115                 //best effort
116
}
117         }
118
119     }
120
121     protected void removePrefs(String JavaDoc qualifier) throws BackingStoreException {
122         //remove bundle's prefs
123
//new InstanceScope().getNode(qualifier).removeNode();
124
new ConfigurationScope().getNode(qualifier).removeNode();
125
126         //remove from our list of bundles with prefs
127
Preferences bundlesNode = getBundlesNode();
128         bundlesNode.remove(qualifier);
129         bundlesNode.flush();
130     }
131     
132     private Preferences getBundlesNode() {
133         try {
134             if (prefBundles == null || !prefBundles.nodeExists("")) { //$NON-NLS-1$
135
prefBundles = new ConfigurationScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI);
136             }
137             return prefBundles;
138         } catch (BackingStoreException e) {
139             // ignore
140
}
141         return null;
142     }
143 }
144
Popular Tags