KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.File JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Dictionary JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.ProjectScope;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.IPath;
34 import org.eclipse.core.runtime.Path;
35 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
36 import org.eclipse.jdt.core.IClasspathEntry;
37 import org.eclipse.jdt.core.IJavaProject;
38 import org.eclipse.jdt.core.JavaCore;
39 import org.eclipse.jdt.core.JavaModelException;
40 import org.eclipse.pde.core.build.IBuild;
41 import org.eclipse.pde.core.build.IBuildEntry;
42 import org.eclipse.pde.core.plugin.IFragmentModel;
43 import org.eclipse.pde.core.plugin.IPluginBase;
44 import org.eclipse.pde.core.plugin.IPluginLibrary;
45 import org.eclipse.pde.core.plugin.IPluginModelBase;
46 import org.eclipse.pde.core.plugin.PluginRegistry;
47 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
48
49 public class ClasspathHelper {
50     
51     private static final String JavaDoc DOT = "."; //$NON-NLS-1$
52

53     public static String JavaDoc getDevEntriesProperties(String JavaDoc fileName, boolean checkExcluded) {
54         File JavaDoc file = new File JavaDoc(fileName);
55         if (!file.exists()) {
56             File JavaDoc directory = file.getParentFile();
57             if (directory != null && (!directory.exists() || directory.isFile())) {
58                 directory.mkdirs();
59             }
60         }
61         Properties JavaDoc properties = new Properties JavaDoc();
62         IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
63         for (int i = 0; i < models.length; i++) {
64             String JavaDoc id = models[i].getPluginBase().getId();
65             if (id == null)
66                 continue;
67             String JavaDoc entry = writeEntry(getDevPaths(models[i], checkExcluded, null));
68             if (entry.length() > 0)
69                 properties.put(id, entry);
70         }
71         properties.put("@ignoredot@", "true"); //$NON-NLS-1$ //$NON-NLS-2$
72

73         FileOutputStream JavaDoc stream = null;
74         try {
75             stream = new FileOutputStream JavaDoc(fileName);
76             properties.store(stream, ""); //$NON-NLS-1$
77
stream.flush();
78             return new URL JavaDoc("file:" + fileName).toString(); //$NON-NLS-1$
79
} catch (IOException JavaDoc e) {
80             PDECore.logException(e);
81         } finally {
82             try {
83                 if (stream != null)
84                     stream.close();
85             } catch (IOException JavaDoc e) {
86             }
87         }
88         return getDevEntries(checkExcluded);
89     }
90
91     public static String JavaDoc getDevEntriesProperties(String JavaDoc fileName, Map JavaDoc map) {
92         File JavaDoc file = new File JavaDoc(fileName);
93         if (!file.exists()) {
94             File JavaDoc directory = file.getParentFile();
95             if (directory != null && (!directory.exists() || directory.isFile())) {
96                 directory.mkdirs();
97             }
98         }
99         Properties JavaDoc properties = new Properties JavaDoc();
100         Iterator JavaDoc iter = map.values().iterator();
101         while (iter.hasNext()) {
102             IPluginModelBase model = (IPluginModelBase)iter.next();
103             if (model.getUnderlyingResource() != null) {
104                 String JavaDoc entry = writeEntry(getDevPaths(model, true, map));
105                 if (entry.length() > 0)
106                     properties.put(model.getPluginBase().getId(), entry);
107             }
108         }
109         properties.put("@ignoredot@", "true"); //$NON-NLS-1$ //$NON-NLS-2$
110

111         FileOutputStream JavaDoc stream = null;
112         try {
113             stream = new FileOutputStream JavaDoc(fileName);
114             properties.store(stream, ""); //$NON-NLS-1$
115
stream.flush();
116             return new URL JavaDoc("file:" + fileName).toString(); //$NON-NLS-1$
117
} catch (IOException JavaDoc e) {
118             PDECore.logException(e);
119         } finally {
120             try {
121                 if (stream != null)
122                     stream.close();
123             } catch (IOException JavaDoc e) {
124             }
125         }
126         return getDevEntries(true);
127     }
128
129     public static String JavaDoc getDevEntries(boolean checkExcluded) {
130         IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
131         ArrayList JavaDoc list = new ArrayList JavaDoc();
132         for (int i = 0; i < models.length; i++) {
133             String JavaDoc id = models[i].getPluginBase().getId();
134             if (id == null || id.trim().length() == 0)
135                 continue;
136             IPath[] paths = getDevPaths(models[i], checkExcluded, null);
137             for (int j = 0; j < paths.length; j++) {
138                 list.add(paths[j]);
139             }
140         }
141         String JavaDoc entry = writeEntry((IPath[])list.toArray(new IPath[list.size()]));
142         return entry.length() > 0 ? entry : "bin"; //$NON-NLS-1$
143
}
144     
145     private static String JavaDoc writeEntry(IPath[] paths) {
146         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
147         for (int i = 0; i < paths.length; i++) {
148             buffer.append(paths[i].toString());
149             if (i < paths.length - 1)
150                 buffer.append(","); //$NON-NLS-1$
151
}
152         return buffer.toString();
153     }
154     
155     public static Dictionary JavaDoc getDevDictionary(IPluginModelBase model) {
156         if (model.getUnderlyingResource() == null)
157             return null;
158         
159         String JavaDoc id = model.getPluginBase().getId();
160         if (id == null || id.trim().length() == 0)
161             return null;
162         IPath[] paths = getDevPaths(model, false, null);
163         String JavaDoc entry = writeEntry(paths);
164         Hashtable JavaDoc map = new Hashtable JavaDoc(2);
165         map.put("@ignoredot@", "true"); //$NON-NLS-1$ //$NON-NLS-2$
166
map.put(id, entry.length() > 0 ? entry : "bin"); //$NON-NLS-1$
167
return map;
168     }
169     
170     // creates a map whose key is a Path to the source directory/jar and the value is a Path output directory or jar.
171
private static Map JavaDoc getClasspathMap(IProject project, boolean checkExcluded, boolean onlyJarsIfLinked, boolean absolutePaths) throws JavaModelException {
172         List JavaDoc excluded = getFoldersToExclude(project, checkExcluded);
173         IJavaProject jProject = JavaCore.create(project);
174         HashMap JavaDoc map = new HashMap JavaDoc();
175         IClasspathEntry[] entries = jProject.getRawClasspath();
176         for (int i = 0; i < entries.length; i++) {
177             // most of the paths we get will be project relative, so we need to make the paths relative
178
// we will have problems adding an "absolute" path that is workspace relative
179
IPath output = null, source = null;
180             if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
181                 source = entries[i].getPath();
182                 output = entries[i].getOutputLocation();
183                 if (output == null)
184                     output = jProject.getOutputLocation();
185             } else if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
186                 source = entries[i].getPath();
187                 output = entries[i].getPath();
188                 if (source.segmentCount() == 1)
189                     source = new Path(DOT);
190             }
191             if (output != null && !excluded.contains(output)) {
192                 IResource file = project.findMember(output.removeFirstSegments(1));
193                 // make the path either relative or absolute
194
if (file != null) {
195                     boolean isLinked = file.isLinked(IResource.CHECK_ANCESTORS);
196                     if (entries[i].getEntryKind() != IClasspathEntry.CPE_SOURCE && !isLinked && onlyJarsIfLinked)
197                         continue;
198                     output = (isLinked || absolutePaths) ? file.getLocation().makeAbsolute() : output.makeRelative();
199                 } else
200                     continue;
201                 ArrayList JavaDoc list = (ArrayList JavaDoc) map.get(source);
202                 if (list == null)
203                     list = new ArrayList JavaDoc();
204                 list.add(output);
205                 map.put(source, list);
206             }
207         }
208         return map;
209     }
210     
211     // find the corresponding paths for a library name. Searches for source folders first, but includes any libraries on the buildpath with the same name
212
private static IPath[] findLibrary(String JavaDoc libName, IProject project, Map JavaDoc classpathMap, IBuild build) {
213         ArrayList JavaDoc paths = new ArrayList JavaDoc();
214         IBuildEntry entry = (build != null) ? build.getEntry(IBuildEntry.JAR_PREFIX + libName) : null;
215         if (entry != null) {
216             String JavaDoc [] resources = entry.getTokens();
217             for (int j = 0; j < resources.length; j++) {
218                 IResource res = project.findMember(resources[j]);
219                 if (res != null) {
220                     ArrayList JavaDoc list = (ArrayList JavaDoc)classpathMap.get(res.getFullPath());
221                     if (list != null) {
222                         ListIterator JavaDoc li = list.listIterator();
223                         while (li.hasNext())
224                             paths.add(li.next());
225                     }
226                 }
227             }
228         }
229         
230         // search for a library that exists in jar form on the buildpath
231
IPath path = null;
232         if (libName.equals(DOT))
233             path = new Path(DOT);
234         else {
235             IResource res = project.findMember(libName);
236             if (res != null)
237                 path = res.getFullPath();
238         }
239         
240         ArrayList JavaDoc list = (ArrayList JavaDoc)classpathMap.get(path);
241         if (list != null) {
242             ListIterator JavaDoc li = list.listIterator();
243             while (li.hasNext())
244                 paths.add(li.next());
245         }
246         return (IPath[])paths.toArray(new IPath[paths.size()]);
247     }
248
249     private static IPath[] getDevPaths(IPluginModelBase model, boolean checkExcluded, Map JavaDoc pluginsMap) {
250         ArrayList JavaDoc result = new ArrayList JavaDoc();
251         IProject project = model.getUnderlyingResource().getProject();
252         IPluginBase base = model.getPluginBase();
253         IPluginLibrary[] libraries = base.getLibraries();
254         try {
255             if (project.hasNature(JavaCore.NATURE_ID)) {
256                 Map JavaDoc classpathMap = getClasspathMap(project, checkExcluded, !base.getId().equals("org.eclipse.osgi"), false); //$NON-NLS-1$
257
IFile file = project.getFile("build.properties"); //$NON-NLS-1$
258
boolean searchBuild = file.exists();
259                 if (searchBuild) {
260                     WorkspaceBuildModel bModel = new WorkspaceBuildModel(file);
261                     IBuild build = bModel.getBuild();
262                     // if it is a custom build, act like there is no build.properties (add everything)
263
IBuildEntry entry = build.getEntry("custom"); //$NON-NLS-1$
264
if (entry != null)
265                         searchBuild = false;
266                     else {
267                         if (libraries.length == 0) {
268                             IPath[] paths = findLibrary(DOT, project, classpathMap, build);
269                             for (int j = 0; j < paths.length; j++)
270                                 addPath(result, project, paths[j]);
271                         } else {
272                             for (int i = 0; i < libraries.length;i++) {
273                                 IPath[] paths = findLibrary(libraries[i].getName(), project, classpathMap, build);
274                                 if (paths.length == 0 && !libraries[i].getName().equals(DOT)) {
275                                     paths = findLibraryFromFragments(libraries[i].getName(), model, checkExcluded, pluginsMap);
276                                 }
277                                 for (int j = 0; j < paths.length; j++)
278                                     addPath(result, project, paths[j]);
279                             }
280                         }
281                     }
282                 }
283                 if (!searchBuild){
284                     // if no build.properties, add all output folders
285
Iterator JavaDoc it = classpathMap.entrySet().iterator();
286                     while (it.hasNext()) {
287                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
288                         ArrayList JavaDoc list = (ArrayList JavaDoc) entry.getValue();
289                         ListIterator JavaDoc li = list.listIterator();
290                         while (li.hasNext())
291                             addPath(result, project, (IPath)li.next());
292                     }
293                 }
294             }
295         } catch (JavaModelException e) {
296         } catch (CoreException e) {
297         }
298         return (IPath[])result.toArray(new IPath[result.size()]);
299     }
300     
301     // looks for fragments for a plug-in. Then searches the fragments for a specific library. Will return paths which are absolute (required by runtime)
302
private static IPath[] findLibraryFromFragments(String JavaDoc libName, IPluginModelBase model, boolean checkExcluded, Map JavaDoc plugins) {
303         IFragmentModel[] frags = PDEManager.findFragmentsFor(model);
304         for (int i = 0; i < frags.length; i++) {
305             if (plugins != null && !plugins.containsKey(frags[i].getBundleDescription().getSymbolicName()))
306                 continue;
307             // look in project first
308
if (frags[i].getUnderlyingResource() != null) {
309                 try {
310                     IProject project = frags[i].getUnderlyingResource().getProject();
311                     Map JavaDoc classpathMap = getClasspathMap(project, checkExcluded, false, true);
312                     IFile file = project.getFile("build.properties"); //$NON-NLS-1$
313
IBuild build = null;
314                     if (file.exists()) {
315                         WorkspaceBuildModel bModel = new WorkspaceBuildModel(file);
316                         build = bModel.getBuild();
317                     }
318                     IPath[] paths = findLibrary(libName, project, classpathMap, build);
319                     if (paths.length > 0)
320                         return paths;
321
322                 } catch (JavaModelException e) {
323                     continue;
324                 }
325             // if external plugin, look in child directories for library
326
} else {
327                 File JavaDoc file = new File JavaDoc(frags[i].getInstallLocation());
328                 if (file.isDirectory()) {
329                     file = new File JavaDoc(file, libName);
330                     if (file.exists())
331                         return new IPath[] { new Path(file.getPath()) };
332                 }
333             }
334         }
335         return new IPath[0];
336     }
337
338     private static void addPath(ArrayList JavaDoc result, IProject project, IPath path) {
339         IPath resultPath = null;
340         if (path.isAbsolute())
341             resultPath = path;
342         else if (path.segmentCount() > 0 && path.segment(0).equals(project.getName())) {
343             path = path.removeFirstSegments(1);
344             if (path.segmentCount() == 0)
345                 resultPath = new Path(DOT);
346             else {
347                 IResource resource = project.findMember(path);
348                 if (resource != null)
349                     resultPath = path;
350             }
351         }
352         
353         if (resultPath != null && !result.contains(resultPath))
354             result.add(resultPath);
355     }
356     
357     private static List JavaDoc getFoldersToExclude(IProject project, boolean checkExcluded) {
358         ArrayList JavaDoc list = new ArrayList JavaDoc();
359         if (checkExcluded) {
360             IEclipsePreferences pref = new ProjectScope(project).getNode(PDECore.PLUGIN_ID);
361             if (pref != null) {
362                 String JavaDoc binExcludes = pref.get(ICoreConstants.SELFHOSTING_BIN_EXCLUDES, ""); //$NON-NLS-1$
363
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(binExcludes, ","); //$NON-NLS-1$
364
while (tokenizer.hasMoreTokens()) {
365                     list.add(new Path(tokenizer.nextToken().trim()));
366                 }
367             }
368         }
369         return list;
370     }
371
372 }
373
Popular Tags