KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > InstallRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  *******************************************************************************/

11 package org.eclipse.update.internal.core;
12
13 import java.io.*;
14 import java.util.HashMap JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.eclipse.update.configurator.*;
18 import org.eclipse.update.core.*;
19
20 /**
21  * Keeps track of all the features and plugins installed by Update mgr
22  * so they can be uninstalled later.
23  * The info is persisted in the .config/registry file and each entry has a key=key where
24  * for feature this key is feature_<id>_<version> and for plugins
25  * key is plugin_<id>_<version>. Normally, getVersionedIdentifier() will
26  * return <id>_<version>. Eg: feature_org.eclipse.platform_3.0.0
27  *
28  */

29 public class InstallRegistry extends Properties JavaDoc {
30
31     private static final long serialVersionUID = 1L;
32     private File file = null;
33     private final static String JavaDoc REGISTRY = "registry"; //$NON-NLS-1$
34
private static InstallRegistry instance;
35     
36     // plugins installed in this eclipse session
37
private HashMap JavaDoc justInstalledPlugins = new HashMap JavaDoc();
38     
39     /**
40      * Creates empty Properties.
41      */

42     private InstallRegistry() {
43         super();
44         String JavaDoc configFile =
45             ConfiguratorUtils
46                 .getCurrentPlatformConfiguration()
47                 .getConfigurationLocation()
48                 .getFile();
49         file = new File(configFile);
50         file = file.getParentFile();
51         file = new File(file, REGISTRY);
52         restore();
53     }
54
55     /**
56      * Singleton
57      */

58     public static InstallRegistry getInstance() {
59         if (instance == null)
60             instance = new InstallRegistry();
61         return instance;
62     }
63
64     /**
65      * Restores contents of the Properties from a file.
66      * @return true if persistant data was read in
67      */

68     public boolean restore() {
69         InputStream in = null;
70         boolean loaded = false;
71         clear();
72         // Test if we have a contribution file to start with
73
// If this is a clean start, then we will not have a
74
// contribution file. return false.
75
if (!file.exists())
76             return loaded;
77         try {
78             in = new FileInputStream(file);
79             super.load(in);
80             loaded = true;
81         } catch (IOException e) {
82             UpdateCore.log(e);
83         } finally {
84             if (in != null)
85                 try {
86                     in.close();
87                 } catch (IOException e) {
88                 }
89         }
90         return loaded;
91     }
92     /**
93      * Saves contents of the table to a file.
94      * @return true if operation was successful
95      */

96     public synchronized boolean save() {
97         OutputStream out = null;
98         boolean ret = false;
99         try {
100             out = new FileOutputStream(file);
101             super.store(out, "This is a generated file; do not edit."); //$NON-NLS-1$
102
ret = true;
103         } catch (IOException e) {
104             UpdateCore.log(e);
105         } finally {
106             try {
107                 if (out != null) {
108                     out.close();
109                 }
110             } catch (IOException e) {
111             }
112         }
113         return ret;
114     }
115     
116     /**
117      * Registers an installed feature so it can be uninstalled later.
118      * @param feature feature to register.
119      */

120     public static synchronized void registerFeature(IFeature feature) {
121         String JavaDoc name = "feature_"+feature.getVersionedIdentifier(); //$NON-NLS-1$
122
if (InstallRegistry.getInstance().get(name) == null) {
123             InstallRegistry.getInstance().put(name, name);
124             // we save after each registration
125
InstallRegistry.getInstance().save();
126         }
127     }
128     
129     /**
130      * Registers an installed feature so it can be uninstalled later.
131      * @param pluginEntry plugin to register.
132      */

133     public static synchronized void registerPlugin(IPluginEntry pluginEntry) {
134         String JavaDoc name = "plugin_"+pluginEntry.getVersionedIdentifier(); //$NON-NLS-1$
135
if (InstallRegistry.getInstance().get(name) == null) {
136             InstallRegistry.getInstance().put(name, name);
137             // we save after each registration
138
InstallRegistry.getInstance().save();
139         }
140         
141         // add plugin to the list of just installed plugins .
142
InstallRegistry.getInstance().justInstalledPlugins.put(name,name);
143     }
144     
145     /**
146      * Removes specified feature from registry
147      *
148      */

149     public static synchronized void unregisterFeature(IFeature feature) {
150         String JavaDoc name = "feature_"+feature.getVersionedIdentifier(); //$NON-NLS-1$
151
InstallRegistry.getInstance().remove(name);
152     }
153     
154     /**
155      * Removes specified plugin from registry
156      *
157      */

158     public static synchronized void unregisterPlugin(IPluginEntry pluginEntry) {
159         String JavaDoc name = "plugin_"+pluginEntry.getVersionedIdentifier(); //$NON-NLS-1$
160
InstallRegistry.getInstance().remove(name);
161         
162         // remove the plugin from the list of just installed plugins (if needed).
163
InstallRegistry.getInstance().justInstalledPlugins.remove(name);
164     }
165     
166     /**
167      * Returns true if the plugin was installed during this eclipse session
168      * @param pluginEntry
169      * @return
170      */

171     public boolean isPluginJustInstalled(IPluginEntry pluginEntry) {
172         String JavaDoc name = "plugin_"+pluginEntry.getVersionedIdentifier(); //$NON-NLS-1$
173
return InstallRegistry.getInstance().justInstalledPlugins.get(name) != null;
174     }
175     
176     /**
177      * This method is only needed for the update JUnit tests.
178      *
179      */

180     public static void cleanup() {
181         InstallRegistry.getInstance().justInstalledPlugins.clear();
182     }
183 }
184
Popular Tags