1 11 package org.eclipse.update.internal.operations; 12 13 import java.util.ArrayList ; 14 import java.util.Enumeration ; 15 import java.util.Hashtable ; 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 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"); } 44 } 45 public boolean isInstallCandidate() { 46 return csite != null; 47 } 48 public IFeature getFeature() { 49 return feature; 50 } 51 52 public String 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 toString() { 67 IConfiguredSite configSite = getConfiguredSite(); 68 String version = 69 feature.getVersionedIdentifier().getVersion().toString(); 70 String location = configSite.getSite().getURL().getFile(); 71 return NLS.bind(Messages.DuplicateConflictsDialog_conflict, (new String [] { version, location })); 72 } 73 } 74 75 public static ArrayList computeDuplicateConflicts( 76 IInstallFeatureOperation job, 77 IInstallConfiguration config, 78 IConfiguredSite targetSite, 79 IFeatureReference[] optionalFeatures) { 80 Hashtable featureTable = new Hashtable (); 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 computeDuplicateConflicts( 95 IInstallFeatureOperation[] jobs, 96 IInstallConfiguration config) { 97 Hashtable featureTable = new Hashtable (); 98 computePresentState(featureTable, config); 99 computeNewFeatures(jobs, featureTable); 100 return computeConflicts(featureTable); 101 } 102 103 private static ArrayList computeConflicts(Hashtable featureTable) { 104 ArrayList result = null; 105 for (Enumeration iterator = featureTable.elements(); 106 iterator.hasMoreElements(); 107 ) { 108 ArrayList candidate = (ArrayList ) iterator.nextElement(); 109 if (candidate.size() == 1) 110 continue; 111 ArrayList conflict = checkForConflict(candidate); 112 if (conflict != null) { 113 if (result == null) 114 result = new ArrayList (); 115 result.add(conflict); 116 } 117 } 118 return result; 119 } 120 121 private static ArrayList checkForConflict(ArrayList 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 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 } 146 } 147 } 148 } 149 150 private static void computeNewFeatures( 151 IInstallFeatureOperation[] jobs, 152 Hashtable 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 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 featureTable) { 201 String id = feature.getVersionedIdentifier().getIdentifier(); 202 ArrayList entries = (ArrayList ) featureTable.get(id); 203 if (entries == null) { 204 entries = new ArrayList (); 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 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 |