KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > operations > DuplicateConflictsValidator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.operations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Hashtable JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.update.configuration.IConfiguredSite;
20 import org.eclipse.update.configuration.IInstallConfiguration;
21 import org.eclipse.update.core.IFeature;
22 import org.eclipse.update.core.IFeatureReference;
23 import org.eclipse.update.core.IIncludedFeatureReference;
24 import org.eclipse.update.core.VersionedIdentifier;
25 import org.eclipse.update.internal.core.Messages;
26 import org.eclipse.update.operations.IInstallFeatureOperation;
27
28
29 /**
30  *
31  */

32 public class DuplicateConflictsValidator {
33
34     public static class IdEntry {
35         IConfiguredSite csite;
36         IFeature feature;
37
38         public IdEntry(IFeature feature, IConfiguredSite csite) {
39             this.feature = feature;
40             this.csite = csite;
41             if (csite == null) {
42                 System.out.println("csite null"); //$NON-NLS-1$
43
}
44         }
45         public boolean isInstallCandidate() {
46             return csite != null;
47         }
48         public IFeature getFeature() {
49             return feature;
50         }
51
52         public String JavaDoc getIdentifier() {
53             return feature.getVersionedIdentifier().getIdentifier();
54         }
55         public IConfiguredSite getConfiguredSite() {
56             if (csite != null)
57                 return csite;
58             return feature.getSite().getCurrentConfiguredSite();
59         }
60         public boolean sameLevel(IdEntry entry) {
61             VersionedIdentifier vid = feature.getVersionedIdentifier();
62             VersionedIdentifier evid =
63                 entry.getFeature().getVersionedIdentifier();
64             return vid.equals(evid);
65         }
66         public String JavaDoc toString() {
67             IConfiguredSite configSite = getConfiguredSite();
68             String JavaDoc version =
69                 feature.getVersionedIdentifier().getVersion().toString();
70             String JavaDoc location = configSite.getSite().getURL().getFile();
71             return NLS.bind(Messages.DuplicateConflictsDialog_conflict, (new String JavaDoc[] { version, location }));
72         }
73     }
74
75     public static ArrayList JavaDoc computeDuplicateConflicts(
76         IInstallFeatureOperation job,
77         IInstallConfiguration config,
78         IConfiguredSite targetSite,
79         IFeatureReference[] optionalFeatures) {
80         Hashtable JavaDoc featureTable = new Hashtable JavaDoc();
81         try {
82             computePresentState(featureTable, config);
83             computeNewFeature(
84                 job.getFeature(),
85                 targetSite,
86                 featureTable,
87                 optionalFeatures);
88             return computeConflicts(featureTable);
89         } catch (CoreException e) {
90             return null;
91         }
92     }
93
94     public static ArrayList JavaDoc computeDuplicateConflicts(
95         IInstallFeatureOperation[] jobs,
96         IInstallConfiguration config) {
97         Hashtable JavaDoc featureTable = new Hashtable JavaDoc();
98         computePresentState(featureTable, config);
99         computeNewFeatures(jobs, featureTable);
100         return computeConflicts(featureTable);
101     }
102
103     private static ArrayList JavaDoc computeConflicts(Hashtable JavaDoc featureTable) {
104         ArrayList JavaDoc result = null;
105         for (Enumeration JavaDoc iterator = featureTable.elements();
106             iterator.hasMoreElements();
107             ) {
108             ArrayList JavaDoc candidate = (ArrayList JavaDoc) iterator.nextElement();
109             if (candidate.size() == 1)
110                 continue;
111             ArrayList JavaDoc conflict = checkForConflict(candidate);
112             if (conflict != null) {
113                 if (result == null)
114                     result = new ArrayList JavaDoc();
115                 result.add(conflict);
116             }
117         }
118         return result;
119     }
120
121     private static ArrayList JavaDoc checkForConflict(ArrayList JavaDoc candidate) {
122         IdEntry firstEntry = null;
123         for (int i = 0; i < candidate.size(); i++) {
124             IdEntry entry = (IdEntry) candidate.get(i);
125             if (firstEntry == null)
126                 firstEntry = entry;
127             else if (!entry.sameLevel(firstEntry))
128                 return candidate;
129         }
130         return null;
131     }
132
133     private static void computePresentState(
134         Hashtable JavaDoc table,
135         IInstallConfiguration config) {
136         IConfiguredSite[] csites = config.getConfiguredSites();
137         for (int i = 0; i < csites.length; i++) {
138             IConfiguredSite csite = csites[i];
139             IFeatureReference[] refs = csite.getConfiguredFeatures();
140             for (int j = 0; j < refs.length; j++) {
141                 try {
142                     addEntry(refs[j].getFeature(null), csite, table);
143                 } catch (CoreException e) {
144                     // don't let one bad feature stop the loop
145
}
146             }
147         }
148     }
149
150     private static void computeNewFeatures(
151         IInstallFeatureOperation[] jobs,
152         Hashtable JavaDoc featureTable) {
153         for (int i = 0; i < jobs.length; i++) {
154             IInstallFeatureOperation job = jobs[i];
155             IConfiguredSite targetSite = job.getTargetSite();
156             IFeature newFeature = job.getFeature();
157             try {
158                 computeNewFeature(newFeature, targetSite, featureTable, null);
159             } catch (CoreException e) {
160             }
161         }
162     }
163
164     private static void computeNewFeature(
165         IFeature feature,
166         IConfiguredSite csite,
167         Hashtable JavaDoc table,
168         IFeatureReference[] optionalFeatures)
169         throws CoreException {
170         addEntry(feature, csite, table);
171         IIncludedFeatureReference[] irefs =
172             feature.getIncludedFeatureReferences();
173         for (int i = 0; i < irefs.length; i++) {
174             IIncludedFeatureReference iref = irefs[i];
175             boolean add = true;
176
177             if (iref.isOptional() && optionalFeatures != null) {
178                 boolean found = false;
179                 for (int j = 0; j < optionalFeatures.length; j++) {
180                     IFeatureReference checked = optionalFeatures[j];
181                     if (checked.equals(iref)) {
182                         found = true;
183                         break;
184                     }
185                 }
186                 add = found;
187             }
188             if (add)
189                 computeNewFeature(
190                     iref.getFeature(null),
191                     csite,
192                     table,
193                     optionalFeatures);
194         }
195     }
196
197     private static void addEntry(
198         IFeature feature,
199         IConfiguredSite csite,
200         Hashtable JavaDoc featureTable) {
201         String JavaDoc id = feature.getVersionedIdentifier().getIdentifier();
202         ArrayList JavaDoc entries = (ArrayList JavaDoc) featureTable.get(id);
203         if (entries == null) {
204             entries = new ArrayList JavaDoc();
205             featureTable.put(id, entries);
206         }
207         IdEntry entry = new IdEntry(feature, csite);
208         boolean replaced = false;
209         for (int i = 0; i < entries.size(); i++) {
210             IdEntry existingEntry = (IdEntry) entries.get(i);
211             IConfiguredSite existingSite = existingEntry.getConfiguredSite();
212             if (existingSite.equals(entry.getConfiguredSite())) {
213                 // same site - replace it if not new
214
if (entry.isInstallCandidate()) {
215                     entries.set(i, entry);
216                     entries.remove(existingEntry);
217                 }
218                 replaced = true;
219                 break;
220             }
221         }
222         if (!replaced)
223             entries.add(entry);
224     }
225 }
226
Popular Tags