001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.jdt.core.IAccessRule;
022: import org.eclipse.jdt.core.IClasspathAttribute;
023: import org.eclipse.jdt.core.IClasspathEntry;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.osgi.service.resolver.BundleDescription;
026: import org.eclipse.pde.core.plugin.IPluginLibrary;
027: import org.eclipse.pde.core.plugin.IPluginModelBase;
028: import org.eclipse.pde.core.plugin.PluginRegistry;
029:
030: public class PDEClasspathContainer {
031:
032: public class Rule {
033: IPath path;
034: boolean discouraged;
035:
036: public boolean equals(Object other) {
037: if (!(other instanceof Rule))
038: return false;
039: return discouraged == ((Rule) other).discouraged
040: && path.equals(((Rule) other).path);
041: }
042:
043: public String toString() {
044: return discouraged ? path.toString() + " [discouraged]" : path.toString(); //$NON-NLS-1$
045: }
046: }
047:
048: private static HashMap ACCESSIBLE_RULES = new HashMap();
049: private static HashMap DISCOURAGED_RULES = new HashMap();
050:
051: private static final IAccessRule EXCLUDE_ALL_RULE = JavaCore
052: .newAccessRule(
053: new Path("**/*"), IAccessRule.K_NON_ACCESSIBLE | IAccessRule.IGNORE_IF_BETTER); //$NON-NLS-1$
054:
055: protected void addProjectEntry(IProject project, Rule[] rules,
056: ArrayList entries) throws CoreException {
057: if (project.hasNature(JavaCore.NATURE_ID)) {
058: IClasspathEntry entry = null;
059: if (rules != null) {
060: IAccessRule[] accessRules = getAccessRules(rules);
061: entry = JavaCore.newProjectEntry(project.getFullPath(),
062: accessRules, true, new IClasspathAttribute[0],
063: false);
064: } else {
065: entry = JavaCore.newProjectEntry(project.getFullPath());
066: }
067: if (!entries.contains(entry))
068: entries.add(entry);
069: }
070: }
071:
072: public static IClasspathEntry[] getExternalEntries(
073: IPluginModelBase model) throws CoreException {
074: ArrayList entries = new ArrayList();
075: addExternalPlugin(model, new Rule[0], entries);
076: return (IClasspathEntry[]) entries
077: .toArray(new IClasspathEntry[entries.size()]);
078: }
079:
080: protected static void addExternalPlugin(IPluginModelBase model,
081: Rule[] rules, ArrayList entries) throws CoreException {
082: if (new File(model.getInstallLocation()).isFile()) {
083: IPath srcPath = ClasspathUtilCore.getSourceAnnotation(
084: model, "."); //$NON-NLS-1$
085: if (srcPath == null)
086: srcPath = new Path(model.getInstallLocation());
087: addLibraryEntry(new Path(model.getInstallLocation()),
088: srcPath, rules, getClasspathAttributes(model),
089: entries);
090: } else {
091: IPluginLibrary[] libraries = model.getPluginBase()
092: .getLibraries();
093: for (int i = 0; i < libraries.length; i++) {
094: if (IPluginLibrary.RESOURCE.equals(libraries[i]
095: .getType()))
096: continue;
097: model = (IPluginModelBase) libraries[i].getModel();
098: String name = libraries[i].getName();
099: String expandedName = ClasspathUtilCore
100: .expandLibraryName(name);
101: IPath path = getPath(model, expandedName);
102: if (path == null && !model.isFragmentModel()
103: && ClasspathUtilCore.containsVariables(name)) {
104: model = resolveLibraryInFragments(model,
105: expandedName);
106: if (model != null && model.isEnabled())
107: path = getPath(model, expandedName);
108: }
109: if (path != null && !path.toFile().isDirectory())
110: addLibraryEntry(path, ClasspathUtilCore
111: .getSourceAnnotation(model, expandedName),
112: rules, getClasspathAttributes(model),
113: entries);
114: }
115: }
116: }
117:
118: protected static void addLibraryEntry(IPath path, IPath srcPath,
119: Rule[] rules, IClasspathAttribute[] attributes,
120: ArrayList entries) {
121: IClasspathEntry entry = null;
122: if (rules != null) {
123: entry = JavaCore.newLibraryEntry(path, srcPath, null,
124: getAccessRules(rules), attributes, false);
125: } else {
126: entry = JavaCore.newLibraryEntry(path, srcPath, null,
127: new IAccessRule[0], attributes, false);
128: }
129: if (!entries.contains(entry)) {
130: entries.add(entry);
131: }
132: }
133:
134: protected static IAccessRule[] getAccessRules(Rule[] rules) {
135: IAccessRule[] accessRules = new IAccessRule[rules.length + 1];
136: for (int i = 0; i < rules.length; i++) {
137: Rule rule = rules[i];
138: accessRules[i] = rule.discouraged ? getDiscouragedRule(rule.path)
139: : getAccessibleRule(rule.path);
140: }
141: accessRules[rules.length] = EXCLUDE_ALL_RULE;
142: return accessRules;
143: }
144:
145: private static synchronized IAccessRule getAccessibleRule(IPath path) {
146: IAccessRule rule = (IAccessRule) ACCESSIBLE_RULES.get(path);
147: if (rule == null) {
148: rule = JavaCore.newAccessRule(path,
149: IAccessRule.K_ACCESSIBLE);
150: ACCESSIBLE_RULES.put(path, rule);
151: }
152: return rule;
153: }
154:
155: private static IClasspathAttribute[] getClasspathAttributes(
156: IPluginModelBase model) {
157: JavadocLocationManager manager = PDECore.getDefault()
158: .getJavadocLocationManager();
159: String location = manager.getJavadocLocation(model);
160: if (location == null)
161: return new IClasspathAttribute[0];
162: return new IClasspathAttribute[] { JavaCore
163: .newClasspathAttribute(
164: IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
165: location) };
166: }
167:
168: private static synchronized IAccessRule getDiscouragedRule(
169: IPath path) {
170: IAccessRule rule = (IAccessRule) DISCOURAGED_RULES.get(path);
171: if (rule == null) {
172: rule = JavaCore.newAccessRule(path,
173: IAccessRule.K_DISCOURAGED);
174: DISCOURAGED_RULES.put(path, rule);
175: }
176: return rule;
177: }
178:
179: protected static IPath getPath(IPluginModelBase model,
180: String libraryName) {
181: IResource resource = model.getUnderlyingResource();
182: if (resource != null) {
183: IResource jarFile = resource.getProject().findMember(
184: libraryName);
185: return (jarFile != null) ? jarFile.getFullPath() : null;
186: }
187: File file = new File(model.getInstallLocation(), libraryName);
188: return file.exists() ? new Path(file.getAbsolutePath()) : null;
189: }
190:
191: protected static IPluginModelBase resolveLibraryInFragments(
192: IPluginModelBase model, String libraryName) {
193: BundleDescription desc = model.getBundleDescription();
194: if (desc != null) {
195: BundleDescription[] fragments = desc.getFragments();
196: for (int i = 0; i < fragments.length; i++) {
197: if (new File(fragments[i].getLocation(), libraryName)
198: .exists())
199: return PluginRegistry.findModel(fragments[i]);
200: }
201: }
202: return null;
203: }
204:
205: }
|