1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.jdt.core.IAccessRule; 23 import org.eclipse.jdt.core.IClasspathAttribute; 24 import org.eclipse.jdt.core.IClasspathEntry; 25 import org.eclipse.jdt.core.JavaCore; 26 import org.eclipse.osgi.service.resolver.BundleDescription; 27 import org.eclipse.pde.core.plugin.IPluginLibrary; 28 import org.eclipse.pde.core.plugin.IPluginModelBase; 29 import org.eclipse.pde.core.plugin.PluginRegistry; 30 31 public class PDEClasspathContainer { 32 33 public class Rule { 34 IPath path; 35 boolean discouraged; 36 public boolean equals(Object other) { 37 if (!(other instanceof Rule)) 38 return false; 39 return discouraged == ((Rule)other).discouraged && path.equals(((Rule)other).path); 40 } 41 42 public String toString() { 43 return discouraged ? path.toString() + " [discouraged]" : path.toString(); } 45 } 46 47 private static HashMap ACCESSIBLE_RULES = new HashMap (); 48 private static HashMap DISCOURAGED_RULES = new HashMap (); 49 50 private static final IAccessRule EXCLUDE_ALL_RULE = 51 JavaCore.newAccessRule(new Path("**/*"), IAccessRule.K_NON_ACCESSIBLE|IAccessRule.IGNORE_IF_BETTER); 53 protected void addProjectEntry(IProject project, Rule[] rules, ArrayList entries) throws CoreException { 54 if (project.hasNature(JavaCore.NATURE_ID)) { 55 IClasspathEntry entry = null; 56 if (rules != null) { 57 IAccessRule[] accessRules = getAccessRules(rules); 58 entry = JavaCore.newProjectEntry( 59 project.getFullPath(), 60 accessRules, 61 true, 62 new IClasspathAttribute[0], 63 false); 64 } else { 65 entry = JavaCore.newProjectEntry(project.getFullPath()); 66 } 67 if (!entries.contains(entry)) 68 entries.add(entry); 69 } 70 } 71 72 public static IClasspathEntry[] getExternalEntries(IPluginModelBase model) throws CoreException { 73 ArrayList entries = new ArrayList (); 74 addExternalPlugin(model, new Rule[0], entries); 75 return (IClasspathEntry[])entries.toArray(new IClasspathEntry[entries.size()]); 76 } 77 78 protected static void addExternalPlugin(IPluginModelBase model, Rule[] rules, ArrayList entries) throws CoreException { 79 if (new File (model.getInstallLocation()).isFile()) { 80 IPath srcPath = ClasspathUtilCore.getSourceAnnotation(model, "."); if (srcPath == null) 82 srcPath = new Path(model.getInstallLocation()); 83 addLibraryEntry(new Path(model.getInstallLocation()), srcPath, rules, getClasspathAttributes(model), entries); 84 } else { 85 IPluginLibrary[] libraries = model.getPluginBase().getLibraries(); 86 for (int i = 0; i < libraries.length; i++) { 87 if (IPluginLibrary.RESOURCE.equals(libraries[i].getType())) 88 continue; 89 model = (IPluginModelBase)libraries[i].getModel(); 90 String name = libraries[i].getName(); 91 String expandedName = ClasspathUtilCore.expandLibraryName(name); 92 IPath path = getPath(model, expandedName); 93 if (path == null && !model.isFragmentModel() && ClasspathUtilCore.containsVariables(name)) { 94 model = resolveLibraryInFragments(model, expandedName); 95 if (model != null && model.isEnabled()) 96 path = getPath(model, expandedName); 97 } 98 if (path != null && !path.toFile().isDirectory()) 99 addLibraryEntry(path, ClasspathUtilCore.getSourceAnnotation(model, expandedName), rules, getClasspathAttributes(model), entries); 100 } 101 } 102 } 103 104 protected static void addLibraryEntry(IPath path, IPath srcPath, Rule[] rules, IClasspathAttribute[] attributes, ArrayList entries) { 105 IClasspathEntry entry = null; 106 if (rules != null) { 107 entry = JavaCore.newLibraryEntry( 108 path, 109 srcPath, 110 null, 111 getAccessRules(rules), 112 attributes, 113 false); 114 } else { 115 entry = JavaCore.newLibraryEntry(path, srcPath, null, new IAccessRule[0], attributes, false); 116 } 117 if (!entries.contains(entry)) { 118 entries.add(entry); 119 } 120 } 121 122 protected static IAccessRule[] getAccessRules(Rule[] rules) { 123 IAccessRule[] accessRules = new IAccessRule[rules.length + 1]; 124 for (int i = 0; i < rules.length; i++) { 125 Rule rule = rules[i]; 126 accessRules[i] = rule.discouraged ? getDiscouragedRule(rule.path) : getAccessibleRule(rule.path); 127 } 128 accessRules[rules.length] = EXCLUDE_ALL_RULE; 129 return accessRules; 130 } 131 132 private static synchronized IAccessRule getAccessibleRule(IPath path) { 133 IAccessRule rule = (IAccessRule)ACCESSIBLE_RULES.get(path); 134 if (rule == null) { 135 rule = JavaCore.newAccessRule(path, IAccessRule.K_ACCESSIBLE); 136 ACCESSIBLE_RULES.put(path, rule); 137 } 138 return rule; 139 } 140 141 private static IClasspathAttribute[] getClasspathAttributes(IPluginModelBase model) { 142 JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager(); 143 String location = manager.getJavadocLocation(model); 144 if (location == null) 145 return new IClasspathAttribute[0]; 146 return new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, location)}; 147 } 148 149 private static synchronized IAccessRule getDiscouragedRule(IPath path) { 150 IAccessRule rule = (IAccessRule)DISCOURAGED_RULES.get(path); 151 if (rule == null) { 152 rule = JavaCore.newAccessRule(path, IAccessRule.K_DISCOURAGED); 153 DISCOURAGED_RULES.put(path, rule); 154 } 155 return rule; 156 } 157 158 protected static IPath getPath(IPluginModelBase model, String libraryName) { 159 IResource resource = model.getUnderlyingResource(); 160 if (resource != null) { 161 IResource jarFile = resource.getProject().findMember(libraryName); 162 return (jarFile != null) ? jarFile.getFullPath() : null; 163 } 164 File file = new File (model.getInstallLocation(), libraryName); 165 return file.exists() ? new Path(file.getAbsolutePath()) : null; 166 } 167 168 protected static IPluginModelBase resolveLibraryInFragments(IPluginModelBase model, String libraryName) { 169 BundleDescription desc = model.getBundleDescription(); 170 if (desc != null) { 171 BundleDescription[] fragments = desc.getFragments(); 172 for (int i = 0; i < fragments.length; i++) { 173 if (new File (fragments[i].getLocation(), libraryName).exists()) 174 return PluginRegistry.findModel(fragments[i]); 175 } 176 } 177 return null; 178 } 179 180 } 181 | Popular Tags |