KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > ExternalFeatureModelManager


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.pde.internal.core;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.util.Vector JavaDoc;
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     /**
41      *
42      * @param manifest
43      * @return ExternalFeatureModel or null
44      */

45     private static IFeatureModel createModel(File JavaDoc manifest) {
46         ExternalFeatureModel model = new ExternalFeatureModel();
47         model.setInstallLocation(manifest.getParent());
48         InputStream JavaDoc stream = null;
49         try {
50             stream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(manifest));
51             model.load(stream, false);
52             return model;
53         } catch (Exception JavaDoc e) {
54         } finally {
55             if (stream != null) {
56                 try {
57                     stream.close();
58                 } catch (IOException JavaDoc e) {
59                 }
60             }
61         }
62         return null;
63     }
64
65     private static IFeatureModel[] createModels(URL JavaDoc[] featurePaths, IProgressMonitor monitor) {
66         if (monitor == null)
67             monitor = new NullProgressMonitor();
68         monitor.beginTask("", featurePaths.length); //$NON-NLS-1$
69
Map JavaDoc uniqueFeatures = new HashMap JavaDoc();
70         for (int i = 0; i < featurePaths.length; i++) {
71             File JavaDoc manifest = new File JavaDoc(featurePaths[i].getFile(), "feature.xml"); //$NON-NLS-1$
72
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); //$NON-NLS-1$
81
}
82             monitor.worked(1);
83         }
84         Collection JavaDoc models = uniqueFeatures.values();
85         return (IFeatureModel[]) models.toArray(new IFeatureModel[models.size()]);
86     }
87
88     private Vector JavaDoc fListeners = new Vector JavaDoc();
89
90     private IFeatureModel[] fModels;
91
92     private String JavaDoc 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 JavaDoc path1, String JavaDoc 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 JavaDoc(path1).equals(new File JavaDoc(path2));
115
116     }
117
118     private void fireModelProviderEvent(IModelProviderEvent e) {
119         for (Iterator JavaDoc iter = fListeners.iterator(); iter.hasNext();) {
120             IModelProviderListener listener = (IModelProviderListener) iter
121                     .next();
122             listener.modelsChanged(e);
123         }
124     }
125
126     /**
127      * @param propertyValue
128      * @return String or null
129      */

130     private String JavaDoc getPathString(Object JavaDoc propertyValue) {
131         if (propertyValue != null && propertyValue instanceof String JavaDoc) {
132             String JavaDoc path = (String JavaDoc) propertyValue;
133             if (path.length() > 0) {
134                 return path;
135             }
136         }
137         return null;
138     }
139
140     public static IFeatureModel[] createModels(String JavaDoc platformHome, ArrayList JavaDoc additionalLocations, IProgressMonitor monitor) {
141         if (platformHome != null && platformHome.length() > 0) {
142             URL JavaDoc[] featureURLs = PluginPathFinder.getFeaturePaths(platformHome);
143             
144             if (additionalLocations.size() == 0)
145                 return createModels(featureURLs, monitor);
146             
147             File JavaDoc[] dirs = new File JavaDoc[additionalLocations.size()];
148             for (int i = 0; i < dirs.length; i++) {
149                 String JavaDoc directory = additionalLocations.get(i).toString();
150                 File JavaDoc dir = new File JavaDoc(directory, "features"); //$NON-NLS-1$
151
if (!dir.exists())
152                     dir = new File JavaDoc(directory);
153                 dirs[i] = dir;
154             }
155             
156             URL JavaDoc[] newUrls = PluginPathFinder.scanLocations(dirs);
157             
158             URL JavaDoc[] result = new URL JavaDoc[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 JavaDoc platformHome, String JavaDoc 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 JavaDoc parseAdditionalLocations(String JavaDoc additionalLocations) {
174         ArrayList JavaDoc result = new ArrayList JavaDoc();
175         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(additionalLocations, ","); //$NON-NLS-1$
176
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 JavaDoc 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 JavaDoc 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