1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.BufferedInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.NullProgressMonitor; 29 import org.eclipse.core.runtime.Preferences; 30 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; 31 import org.eclipse.pde.core.IModelProviderEvent; 32 import org.eclipse.pde.core.IModelProviderListener; 33 import org.eclipse.pde.internal.core.feature.ExternalFeatureModel; 34 import org.eclipse.pde.internal.core.ifeature.IFeature; 35 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 36 37 public class ExternalFeatureModelManager implements 38 Preferences.IPropertyChangeListener { 39 40 45 private static IFeatureModel createModel(File manifest) { 46 ExternalFeatureModel model = new ExternalFeatureModel(); 47 model.setInstallLocation(manifest.getParent()); 48 InputStream stream = null; 49 try { 50 stream = new BufferedInputStream (new FileInputStream (manifest)); 51 model.load(stream, false); 52 return model; 53 } catch (Exception e) { 54 } finally { 55 if (stream != null) { 56 try { 57 stream.close(); 58 } catch (IOException e) { 59 } 60 } 61 } 62 return null; 63 } 64 65 private static IFeatureModel[] createModels(URL [] featurePaths, IProgressMonitor monitor) { 66 if (monitor == null) 67 monitor = new NullProgressMonitor(); 68 monitor.beginTask("", featurePaths.length); Map uniqueFeatures = new HashMap (); 70 for (int i = 0; i < featurePaths.length; i++) { 71 File manifest = new File (featurePaths[i].getFile(), "feature.xml"); if (!manifest.exists() || !manifest.isFile()) { 73 monitor.worked(1); 74 continue; 75 } 76 IFeatureModel model = createModel(manifest); 77 if (model != null && model.isLoaded()) { 78 IFeature feature = model.getFeature(); 79 uniqueFeatures.put( 80 feature.getId() + "_" + feature.getVersion(), model); } 82 monitor.worked(1); 83 } 84 Collection models = uniqueFeatures.values(); 85 return (IFeatureModel[]) models.toArray(new IFeatureModel[models.size()]); 86 } 87 88 private Vector fListeners = new Vector (); 89 90 private IFeatureModel[] fModels; 91 92 private String fPlatformHome; 93 94 private Preferences fPref; 95 96 public ExternalFeatureModelManager() { 97 fPref = PDECore.getDefault().getPluginPreferences(); 98 } 99 100 public void addModelProviderListener(IModelProviderListener listener) { 101 fListeners.add(listener); 102 } 103 104 private boolean equalPaths(String path1, String path2) { 105 if (path1 == null) { 106 if (path2 == null) { 107 return true; 108 } 109 return false; 110 } 111 if (path2 == null) { 112 return false; 113 } 114 return new File (path1).equals(new File (path2)); 115 116 } 117 118 private void fireModelProviderEvent(IModelProviderEvent e) { 119 for (Iterator iter = fListeners.iterator(); iter.hasNext();) { 120 IModelProviderListener listener = (IModelProviderListener) iter 121 .next(); 122 listener.modelsChanged(e); 123 } 124 } 125 126 130 private String getPathString(Object propertyValue) { 131 if (propertyValue != null && propertyValue instanceof String ) { 132 String path = (String ) propertyValue; 133 if (path.length() > 0) { 134 return path; 135 } 136 } 137 return null; 138 } 139 140 public static IFeatureModel[] createModels(String platformHome, ArrayList additionalLocations, IProgressMonitor monitor) { 141 if (platformHome != null && platformHome.length() > 0) { 142 URL [] featureURLs = PluginPathFinder.getFeaturePaths(platformHome); 143 144 if (additionalLocations.size() == 0) 145 return createModels(featureURLs, monitor); 146 147 File [] dirs = new File [additionalLocations.size()]; 148 for (int i = 0; i < dirs.length; i++) { 149 String directory = additionalLocations.get(i).toString(); 150 File dir = new File (directory, "features"); if (!dir.exists()) 152 dir = new File (directory); 153 dirs[i] = dir; 154 } 155 156 URL [] newUrls = PluginPathFinder.scanLocations(dirs); 157 158 URL [] result = new URL [featureURLs.length + newUrls.length]; 159 System.arraycopy(featureURLs, 0, result, 0, featureURLs.length); 160 System.arraycopy(newUrls, 0, result, featureURLs.length, newUrls.length); 161 return createModels(result, monitor); 162 } 163 return new IFeatureModel[0]; 164 } 165 166 public void loadModels(String platformHome, String additionalLocations) { 167 IFeatureModel[] oldModels = fModels != null ? fModels : new IFeatureModel[0]; 168 fModels = createModels(platformHome, parseAdditionalLocations(additionalLocations), null); 169 fPlatformHome = platformHome; 170 notifyListeners(oldModels, fModels); 171 } 172 173 private ArrayList parseAdditionalLocations(String additionalLocations) { 174 ArrayList result = new ArrayList (); 175 StringTokenizer tokenizer = new StringTokenizer (additionalLocations, ","); while (tokenizer.hasMoreTokens()) { 177 result.add(tokenizer.nextToken().trim()); 178 } 179 return result; 180 } 181 182 private void notifyListeners(IFeatureModel[] oldModels, 183 IFeatureModel[] newFeatureModels) { 184 if (oldModels.length > 0 || newFeatureModels.length > 0) { 185 int type = 0; 186 if (oldModels.length > 0) 187 type |= IModelProviderEvent.MODELS_REMOVED; 188 if (newFeatureModels.length > 0) 189 type |= IModelProviderEvent.MODELS_ADDED; 190 ModelProviderEvent replacedFeatures = new ModelProviderEvent(this, 191 type, newFeatureModels, oldModels, null); 192 fireModelProviderEvent(replacedFeatures); 193 } 194 195 } 196 197 private synchronized void platformPathChanged(String newHome) { 198 if (!equalPaths(newHome, fPlatformHome)) { 199 loadModels(newHome, fPref.getString(ICoreConstants.ADDITIONAL_LOCATIONS)); 200 } 201 } 202 203 public void propertyChange(PropertyChangeEvent event) { 204 if (!ICoreConstants.PLATFORM_PATH.equals(event.getProperty())) { 205 return; 206 } 207 String newHome = getPathString(event.getNewValue()); 208 platformPathChanged(newHome); 209 } 210 211 public void removeModelProviderListener(IModelProviderListener listener) { 212 fListeners.remove(listener); 213 } 214 215 public synchronized void shutdown() { 216 fPref.removePropertyChangeListener(this); 217 } 218 219 public synchronized void startup() { 220 fPref.addPropertyChangeListener(this); 221 loadModels(fPref.getString(ICoreConstants.PLATFORM_PATH), fPref.getString(ICoreConstants.ADDITIONAL_LOCATIONS)); 222 } 223 224 public synchronized void reload() { 225 loadModels(fPref.getString(ICoreConstants.PLATFORM_PATH), fPref.getString(ICoreConstants.ADDITIONAL_LOCATIONS)); 226 } 227 228 public IFeatureModel[] getModels() { 229 return fModels; 230 } 231 } 232 | Popular Tags |