KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > PluginVersionInfo


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.help.internal.search;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.eclipse.core.runtime.*;
23 import org.eclipse.help.internal.base.util.*;
24 import org.osgi.framework.*;
25
26 /**
27  * Table of plugins. Records all plugins, their version, corresponding fragments
28  * versions The values are String in format:
29  * pluginID\npluginVersion\nfragment1ID\nfragment1Version\nfragment2ID\nfragment2Version
30  */

31 public class PluginVersionInfo extends HelpProperties {
32     private static final long serialVersionUID = 1L;
33
34     // Separates plugins and versions in value strings
35
protected static final String JavaDoc SEPARATOR = "\n"; //$NON-NLS-1$
36

37     File dir;
38
39     boolean doComparison = true;
40
41     boolean hasChanged = false;
42
43     boolean ignoreSavedVersions;
44
45     Collection JavaDoc added = new ArrayList JavaDoc();
46
47     Collection JavaDoc removed = new ArrayList JavaDoc();
48
49     /**
50      * Creates table of current contributing plugins and their fragments with
51      * versions.
52      *
53      * @param name
54      * the name of the file to serialize the data to
55      * @param docBundleIds
56      * Collection of String
57      * @param dir
58      * location to store the data
59      * @param ignoreSavedVersions
60      * if true, will cause detect change to ignore saved plugin
61      * version and behave like there was nothing saved
62      */

63     public PluginVersionInfo(String JavaDoc name, Collection JavaDoc docBundleIds, File dir,
64             boolean ignoreSavedVersions) {
65         super(name, dir);
66         this.dir = dir;
67         this.ignoreSavedVersions = ignoreSavedVersions;
68         createTable(docBundleIds);
69     }
70
71     protected void createTable(Collection JavaDoc docBundleIds) {
72         // create table of current contributions
73
for (Iterator JavaDoc it = docBundleIds.iterator(); it.hasNext();) {
74             String JavaDoc bundleId = (String JavaDoc) it.next();
75             Bundle pluginBundle = Platform.getBundle(bundleId);
76             if (pluginBundle == null) {
77                 continue;
78             }
79             StringBuffer JavaDoc pluginVersionAndFragments = new StringBuffer JavaDoc();
80             appendBundleInformation(pluginVersionAndFragments, bundleId,
81                     (String JavaDoc) pluginBundle.getHeaders().get(
82                             Constants.BUNDLE_VERSION));
83             Bundle[] fragmentBundles = Platform.getFragments(pluginBundle);
84             if (fragmentBundles != null) {
85                 for (int f = 0; f < fragmentBundles.length; f++) {
86                     if (fragmentBundles[f].getState() == Bundle.INSTALLED
87                             || fragmentBundles[f].getState() == Bundle.UNINSTALLED)
88                         continue;
89                     appendBundleInformation(pluginVersionAndFragments,
90                             fragmentBundles[f].getSymbolicName(),
91                             (String JavaDoc) fragmentBundles[f].getHeaders().get(
92                                     Constants.BUNDLE_VERSION));
93                 }
94             }
95             this.put(bundleId, pluginVersionAndFragments.toString());
96         }
97     }
98
99     protected void appendBundleInformation(StringBuffer JavaDoc buffer, String JavaDoc id,
100             String JavaDoc version) {
101         if (buffer.length()>0)
102             buffer.append(SEPARATOR);
103         buffer.append(id);
104         buffer.append(SEPARATOR);
105         buffer.append(version);
106     }
107
108     /**
109      * Detects changes in contributions or their version since last time the
110      * contribution table was saved.
111      *
112      * @return true if contributions have changed
113      */

114     public boolean detectChange() {
115         if (!doComparison)
116             return hasChanged;
117         // Create table of contributions present before last save()
118
HelpProperties oldContrs = new HelpProperties(this.name, dir);
119         if (!ignoreSavedVersions) {
120             oldContrs.restore();
121         }
122         // check if contributions changed
123
hasChanged = false;
124         for (Enumeration JavaDoc keysEnum = this.keys(); keysEnum.hasMoreElements();) {
125             String JavaDoc oneContr = (String JavaDoc) keysEnum.nextElement();
126             if (!oldContrs.containsKey(oneContr)) {
127                 // plugin has been added
128
added.add(oneContr);
129             } else {
130                 String JavaDoc versions = (String JavaDoc) this.get(oneContr);
131                 String JavaDoc oldVersions = (String JavaDoc) oldContrs.get(oneContr);
132                 if (!compare(versions, oldVersions)) {
133                     // plugin version changed or fragments changed
134
added.add(oneContr);
135                 }
136             }
137         }
138         for (Enumeration JavaDoc keysEnum = oldContrs.keys(); keysEnum
139                 .hasMoreElements();) {
140             String JavaDoc oneContr = (String JavaDoc) keysEnum.nextElement();
141             if (!this.containsKey(oneContr)) {
142                 // plugin has been removed
143
removed.add(oneContr);
144             } else {
145                 String JavaDoc versions = (String JavaDoc) this.get(oneContr);
146                 String JavaDoc oldVersions = (String JavaDoc) oldContrs.get(oneContr);
147                 if (!compare(versions, oldVersions)) {
148                     // plugin version changed or fragments changed
149
removed.add(oneContr);
150                 }
151             }
152         }
153         hasChanged = added.size() > 0 || removed.size() > 0;
154         doComparison = false;
155         return hasChanged;
156     }
157
158     /**
159      * @return String - Collection of IDs of contributions that were added or
160      * upgraded
161      */

162     public Collection JavaDoc getAdded() {
163         if (doComparison)
164             detectChange();
165         return added;
166     }
167
168     /**
169      * @return String - Collection of IDs of contributions that were removed or
170      * upgraded
171      */

172     public Collection JavaDoc getRemoved() {
173         if (doComparison)
174             detectChange();
175         return removed;
176     }
177
178     /**
179      * Saves contributions to a file. After this method is called, calls to
180      * detectChange() will return false.
181      *
182      * @return true if operation was successful
183      */

184     public boolean save() {
185         if (super.save()) {
186             doComparison = false;
187             hasChanged = false;
188             ignoreSavedVersions = false;
189             added = new ArrayList JavaDoc();
190             removed = new ArrayList JavaDoc();
191             return true;
192         }
193         return false;
194     }
195
196     /**
197      * Compares plugins and versions represented as a string for equality String
198      * have form id1\nverison1\nid2\nversion2 String are equal of they contain
199      * the same set of IDs and their corresponding version equal
200      *
201      * @return true if plugins and versions match
202      */

203     private boolean compare(String JavaDoc versions, String JavaDoc oldVersions) {
204         Map JavaDoc versionMap = new HashMap JavaDoc();
205         for (StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(versions, SEPARATOR, false); t
206                 .hasMoreTokens();) {
207             String JavaDoc pluginOrFragment = t.nextToken();
208             if (t.hasMoreTokens()) {
209                 versionMap.put(pluginOrFragment, t.nextToken());
210             }
211         }
212         Map JavaDoc oldVersionMap = new HashMap JavaDoc();
213         for (StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(oldVersions, SEPARATOR,
214                 false); t.hasMoreTokens();) {
215             String JavaDoc pluginOrFragment = t.nextToken();
216             if (t.hasMoreTokens()) {
217                 oldVersionMap.put(pluginOrFragment, t.nextToken());
218             }
219         }
220         return versionMap.equals(oldVersionMap);
221     }
222 }
223
Popular Tags