1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.Dictionary ; 19 import java.util.HashMap ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.ListIterator ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.StringTokenizer ; 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 DOT = "."; 53 public static String getDevEntriesProperties(String fileName, boolean checkExcluded) { 54 File file = new File (fileName); 55 if (!file.exists()) { 56 File directory = file.getParentFile(); 57 if (directory != null && (!directory.exists() || directory.isFile())) { 58 directory.mkdirs(); 59 } 60 } 61 Properties properties = new Properties (); 62 IPluginModelBase[] models = PluginRegistry.getWorkspaceModels(); 63 for (int i = 0; i < models.length; i++) { 64 String id = models[i].getPluginBase().getId(); 65 if (id == null) 66 continue; 67 String entry = writeEntry(getDevPaths(models[i], checkExcluded, null)); 68 if (entry.length() > 0) 69 properties.put(id, entry); 70 } 71 properties.put("@ignoredot@", "true"); 73 FileOutputStream stream = null; 74 try { 75 stream = new FileOutputStream (fileName); 76 properties.store(stream, ""); stream.flush(); 78 return new URL ("file:" + fileName).toString(); } catch (IOException e) { 80 PDECore.logException(e); 81 } finally { 82 try { 83 if (stream != null) 84 stream.close(); 85 } catch (IOException e) { 86 } 87 } 88 return getDevEntries(checkExcluded); 89 } 90 91 public static String getDevEntriesProperties(String fileName, Map map) { 92 File file = new File (fileName); 93 if (!file.exists()) { 94 File directory = file.getParentFile(); 95 if (directory != null && (!directory.exists() || directory.isFile())) { 96 directory.mkdirs(); 97 } 98 } 99 Properties properties = new Properties (); 100 Iterator iter = map.values().iterator(); 101 while (iter.hasNext()) { 102 IPluginModelBase model = (IPluginModelBase)iter.next(); 103 if (model.getUnderlyingResource() != null) { 104 String 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"); 111 FileOutputStream stream = null; 112 try { 113 stream = new FileOutputStream (fileName); 114 properties.store(stream, ""); stream.flush(); 116 return new URL ("file:" + fileName).toString(); } catch (IOException e) { 118 PDECore.logException(e); 119 } finally { 120 try { 121 if (stream != null) 122 stream.close(); 123 } catch (IOException e) { 124 } 125 } 126 return getDevEntries(true); 127 } 128 129 public static String getDevEntries(boolean checkExcluded) { 130 IPluginModelBase[] models = PluginRegistry.getWorkspaceModels(); 131 ArrayList list = new ArrayList (); 132 for (int i = 0; i < models.length; i++) { 133 String 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 entry = writeEntry((IPath[])list.toArray(new IPath[list.size()])); 142 return entry.length() > 0 ? entry : "bin"; } 144 145 private static String writeEntry(IPath[] paths) { 146 StringBuffer buffer = new StringBuffer (); 147 for (int i = 0; i < paths.length; i++) { 148 buffer.append(paths[i].toString()); 149 if (i < paths.length - 1) 150 buffer.append(","); } 152 return buffer.toString(); 153 } 154 155 public static Dictionary getDevDictionary(IPluginModelBase model) { 156 if (model.getUnderlyingResource() == null) 157 return null; 158 159 String id = model.getPluginBase().getId(); 160 if (id == null || id.trim().length() == 0) 161 return null; 162 IPath[] paths = getDevPaths(model, false, null); 163 String entry = writeEntry(paths); 164 Hashtable map = new Hashtable (2); 165 map.put("@ignoredot@", "true"); map.put(id, entry.length() > 0 ? entry : "bin"); return map; 168 } 169 170 private static Map getClasspathMap(IProject project, boolean checkExcluded, boolean onlyJarsIfLinked, boolean absolutePaths) throws JavaModelException { 172 List excluded = getFoldersToExclude(project, checkExcluded); 173 IJavaProject jProject = JavaCore.create(project); 174 HashMap map = new HashMap (); 175 IClasspathEntry[] entries = jProject.getRawClasspath(); 176 for (int i = 0; i < entries.length; i++) { 177 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 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 list = (ArrayList ) map.get(source); 202 if (list == null) 203 list = new ArrayList (); 204 list.add(output); 205 map.put(source, list); 206 } 207 } 208 return map; 209 } 210 211 private static IPath[] findLibrary(String libName, IProject project, Map classpathMap, IBuild build) { 213 ArrayList paths = new ArrayList (); 214 IBuildEntry entry = (build != null) ? build.getEntry(IBuildEntry.JAR_PREFIX + libName) : null; 215 if (entry != null) { 216 String [] 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 list = (ArrayList )classpathMap.get(res.getFullPath()); 221 if (list != null) { 222 ListIterator li = list.listIterator(); 223 while (li.hasNext()) 224 paths.add(li.next()); 225 } 226 } 227 } 228 } 229 230 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 list = (ArrayList )classpathMap.get(path); 241 if (list != null) { 242 ListIterator 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 pluginsMap) { 250 ArrayList result = new ArrayList (); 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 classpathMap = getClasspathMap(project, checkExcluded, !base.getId().equals("org.eclipse.osgi"), false); IFile file = project.getFile("build.properties"); boolean searchBuild = file.exists(); 259 if (searchBuild) { 260 WorkspaceBuildModel bModel = new WorkspaceBuildModel(file); 261 IBuild build = bModel.getBuild(); 262 IBuildEntry entry = build.getEntry("custom"); 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 Iterator it = classpathMap.entrySet().iterator(); 286 while (it.hasNext()) { 287 Map.Entry entry = (Map.Entry ) it.next(); 288 ArrayList list = (ArrayList ) entry.getValue(); 289 ListIterator 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 private static IPath[] findLibraryFromFragments(String libName, IPluginModelBase model, boolean checkExcluded, Map 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 if (frags[i].getUnderlyingResource() != null) { 309 try { 310 IProject project = frags[i].getUnderlyingResource().getProject(); 311 Map classpathMap = getClasspathMap(project, checkExcluded, false, true); 312 IFile file = project.getFile("build.properties"); 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 } else { 327 File file = new File (frags[i].getInstallLocation()); 328 if (file.isDirectory()) { 329 file = new File (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 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 getFoldersToExclude(IProject project, boolean checkExcluded) { 358 ArrayList list = new ArrayList (); 359 if (checkExcluded) { 360 IEclipsePreferences pref = new ProjectScope(project).getNode(PDECore.PLUGIN_ID); 361 if (pref != null) { 362 String binExcludes = pref.get(ICoreConstants.SELFHOSTING_BIN_EXCLUDES, ""); StringTokenizer tokenizer = new StringTokenizer (binExcludes, ","); while (tokenizer.hasMoreTokens()) { 365 list.add(new Path(tokenizer.nextToken().trim())); 366 } 367 } 368 } 369 return list; 370 } 371 372 } 373 | Popular Tags |