KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > ClasspathComputer


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.wizards.plugin;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.jdt.core.IAccessRule;
24 import org.eclipse.jdt.core.IClasspathAttribute;
25 import org.eclipse.jdt.core.IClasspathEntry;
26 import org.eclipse.jdt.core.IJavaModelStatus;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.IPackageFragmentRoot;
29 import org.eclipse.jdt.core.JavaConventions;
30 import org.eclipse.jdt.core.JavaCore;
31 import org.eclipse.jdt.core.JavaModelException;
32 import org.eclipse.jdt.launching.JavaRuntime;
33 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
34 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
35 import org.eclipse.osgi.service.resolver.BundleDescription;
36 import org.eclipse.pde.core.build.IBuild;
37 import org.eclipse.pde.core.build.IBuildEntry;
38 import org.eclipse.pde.core.build.IBuildModel;
39 import org.eclipse.pde.core.plugin.IPluginLibrary;
40 import org.eclipse.pde.core.plugin.IPluginModelBase;
41 import org.eclipse.pde.internal.core.ClasspathUtilCore;
42 import org.eclipse.pde.internal.core.ExecutionEnvironmentAnalyzer;
43 import org.eclipse.pde.internal.core.JavadocLocationManager;
44 import org.eclipse.pde.internal.core.PDECore;
45 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
46 import org.eclipse.pde.internal.core.util.CoreUtility;
47 import org.eclipse.team.core.RepositoryProvider;
48
49 public class ClasspathComputer {
50     
51     private static Hashtable JavaDoc fSeverityTable = null;
52     private static final int SEVERITY_ERROR = 3;
53     private static final int SEVERITY_WARNING = 2;
54     private static final int SEVERITY_IGNORE = 1;
55     
56     public static void setClasspath(IProject project, IPluginModelBase model) throws CoreException {
57         IClasspathEntry[] entries = getClasspath(project, model, false);
58         JavaCore.create(project).setRawClasspath(entries, null);
59     }
60     
61     public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear) throws CoreException {
62
63         ArrayList JavaDoc result = new ArrayList JavaDoc();
64                 
65         IBuild build = getBuild(project);
66
67         // add own libraries/source
68
addSourceAndLibraries(project, model, build, clear, result);
69     
70         // add JRE and set compliance options
71
String JavaDoc ee = getExecutionEnvironment(model.getBundleDescription());
72         result.add(createJREEntry(ee));
73         setComplianceOptions(JavaCore.create(project), ExecutionEnvironmentAnalyzer.getCompliance(ee));
74
75         // add pde container
76
result.add(createContainerEntry());
77
78         IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
79         IJavaProject javaProject = JavaCore.create(project);
80         IJavaModelStatus validation =
81             JavaConventions.validateClasspath(
82                                 javaProject,
83                                 entries,
84                                 javaProject.getOutputLocation());
85         if (!validation.isOK()) {
86             PDECore.logErrorMessage(validation.getMessage());
87             throw new CoreException(validation);
88         }
89         return (IClasspathEntry[])result.toArray(new IClasspathEntry[result.size()]);
90     }
91
92     public static void addSourceAndLibraries(IProject project, IPluginModelBase model, IBuild build, boolean clear,
93             ArrayList JavaDoc result) throws CoreException {
94         
95         HashSet JavaDoc paths = new HashSet JavaDoc();
96
97         // keep existing source folders
98
if (!clear) {
99             IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath();
100             for (int i = 0; i < entries.length; i++) {
101                 IClasspathEntry entry = entries[i];
102                 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
103                     if (paths.add(entry.getPath()))
104                         result.add(entry);
105                 }
106             }
107         }
108
109         IClasspathAttribute[] attrs = getClasspathAttributes(project, model);
110         IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
111         for (int i = 0; i < libraries.length; i++) {
112             IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$
113
if (buildEntry != null) {
114                 addSourceFolder(buildEntry, project, paths, result);
115             } else {
116                 if (libraries[i].getName().equals(".")) //$NON-NLS-1$
117
addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result);
118                 else
119                     addLibraryEntry(project, libraries[i], attrs, result);
120             }
121         }
122         if (libraries.length == 0) {
123             if (build != null) {
124                 IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$
125
if (buildEntry != null) {
126                     addSourceFolder(buildEntry, project, paths, result);
127                 }
128             } else if (ClasspathUtilCore.hasBundleStructure(model)) {
129                 addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result);
130             }
131         }
132     }
133     
134     private static IClasspathAttribute[] getClasspathAttributes(IProject project, IPluginModelBase model) {
135         IClasspathAttribute[] attributes = new IClasspathAttribute[0];
136         if (!RepositoryProvider.isShared(project)) {
137             JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager();
138             String JavaDoc javadoc = manager.getJavadocLocation(model);
139             if (javadoc != null) {
140                 attributes = new IClasspathAttribute[]
141                    {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadoc)};
142             }
143         }
144         return attributes;
145     }
146     
147     private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet JavaDoc paths, ArrayList JavaDoc result) throws CoreException {
148         String JavaDoc[] folders = buildEntry.getTokens();
149         for (int j = 0; j < folders.length; j++) {
150             String JavaDoc folder = folders[j];
151             IPath path = project.getFullPath().append(folder);
152             if (paths.add(path)) {
153                 if (project.findMember(folder) == null) {
154                     CoreUtility.createFolder(project.getFolder(folder));
155                 } else {
156                     IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(path.toString());
157                     if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
158                         result.add(root.getRawClasspathEntry());
159                         continue;
160                     }
161                 }
162                 result.add(JavaCore.newSourceEntry(path));
163             }
164         }
165     }
166     
167     protected static IBuild getBuild(IProject project) throws CoreException {
168         IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
169
IBuildModel buildModel = null;
170         if (buildFile.exists()) {
171             buildModel = new WorkspaceBuildModel(buildFile);
172             buildModel.load();
173         }
174         return (buildModel != null) ? buildModel.getBuild() : null;
175     }
176     
177     private static void addLibraryEntry(IProject project, IPluginLibrary library, IClasspathAttribute[] attrs, ArrayList JavaDoc result) throws JavaModelException {
178         String JavaDoc name = ClasspathUtilCore.expandLibraryName(library.getName());
179         IResource jarFile = project.findMember(name);
180         if (jarFile == null)
181             return;
182         
183         IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(jarFile);
184         if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
185             IClasspathEntry oldEntry = root.getRawClasspathEntry();
186             if (oldEntry.getSourceAttachmentPath() != null && !result.contains(oldEntry)) {
187                 result.add(oldEntry);
188                 return;
189             }
190         }
191             
192         IResource resource = project.findMember(getSourceZipName(name));
193         IPath srcAttachment = resource != null ? resource.getFullPath() : null;
194         IClasspathEntry entry = JavaCore.newLibraryEntry(jarFile.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, library.isExported());
195         if (!result.contains(entry))
196             result.add(entry);
197     }
198
199     private static void addJARdPlugin(IProject project, String JavaDoc filename, IClasspathAttribute[] attrs, ArrayList JavaDoc result) {
200         String JavaDoc name = ClasspathUtilCore.expandLibraryName(filename);
201         IResource jarFile = project.findMember(name);
202         if (jarFile != null) {
203             IResource resource = project.findMember(getSourceZipName(name));
204             IPath srcAttachment = resource != null ? resource.getFullPath() : jarFile.getFullPath();
205             IClasspathEntry entry =
206                 JavaCore.newLibraryEntry(jarFile.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, true);
207             if (!result.contains(entry))
208                 result.add(entry);
209         }
210     }
211
212     public static String JavaDoc getSourceZipName(String JavaDoc libraryName) {
213         int dot = libraryName.lastIndexOf('.');
214         return (dot != -1) ? libraryName.substring(0, dot) + "src.zip" : libraryName; //$NON-NLS-1$
215
}
216     
217     private static String JavaDoc getExecutionEnvironment(BundleDescription bundleDescription) {
218         if (bundleDescription != null) {
219             String JavaDoc[] envs = bundleDescription.getExecutionEnvironments();
220             if (envs.length > 0)
221                 return envs[0];
222         }
223         return null;
224     }
225     
226     public static void setComplianceOptions(IJavaProject project, String JavaDoc compliance) {
227         Map JavaDoc map = project.getOptions(false);
228         if (compliance == null) {
229             if (map.size() > 0) {
230                 map.remove(JavaCore.COMPILER_COMPLIANCE);
231                 map.remove(JavaCore.COMPILER_SOURCE);
232                 map.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
233                 map.remove(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER);
234                 map.remove(JavaCore.COMPILER_PB_ENUM_IDENTIFIER);
235             } else {
236                 return;
237             }
238         } else if (JavaCore.VERSION_1_6.equals(compliance)) {
239             map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
240             map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
241             map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
242             map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
243             map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
244         } else if (JavaCore.VERSION_1_5.equals(compliance)) {
245             map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
246             map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
247             map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
248             map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
249             map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
250         } else if (JavaCore.VERSION_1_4.equals(compliance)) {
251             map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
252             map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
253             map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
254             updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.WARNING);
255             updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.WARNING);
256         } else if (JavaCore.VERSION_1_3.equals(compliance)) {
257             map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_3);
258             map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
259             map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_1);
260             updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.IGNORE);
261             updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.IGNORE);
262         }
263         project.setOptions(map);
264     }
265     
266     private static void updateSeverityComplianceOption(Map JavaDoc map, String JavaDoc key, String JavaDoc value) {
267         Integer JavaDoc current_value = null;
268         Integer JavaDoc new_value = null;
269         String JavaDoc current_string_value = null;
270         int current_int_value = 0;
271         int new_int_value = 0;
272         // Initialize the severity table (only once)
273
if (fSeverityTable == null) {
274             fSeverityTable = new Hashtable JavaDoc(SEVERITY_ERROR);
275             fSeverityTable.put(JavaCore.IGNORE, new Integer JavaDoc(SEVERITY_IGNORE));
276             fSeverityTable.put(JavaCore.WARNING, new Integer JavaDoc(SEVERITY_WARNING));
277             fSeverityTable.put(JavaCore.ERROR, new Integer JavaDoc(SEVERITY_ERROR));
278         }
279         // Get the current severity
280
current_string_value = (String JavaDoc)map.get(key);
281         if (current_string_value != null) {
282             current_value = (Integer JavaDoc)fSeverityTable.get(current_string_value);
283             if (current_value != null) {
284                 current_int_value = current_value.intValue();
285             }
286         }
287         // Get the new severity
288
new_value = (Integer JavaDoc)fSeverityTable.get(value);
289         if (new_value != null) {
290             new_int_value = new_value.intValue();
291         }
292         // If the current severity is not higher than the new severity, replace it
293
if (new_int_value > current_int_value) {
294             map.put(key, value);
295         }
296     }
297     
298     
299     public static IClasspathEntry createJREEntry(String JavaDoc ee) {
300         IPath path = null;
301         if (ee != null) {
302             IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
303             IExecutionEnvironment env = manager.getEnvironment(ee);
304             if (env != null)
305                 path = JavaRuntime.newJREContainerPath(env);
306         }
307         if (path == null)
308             path = JavaRuntime.newDefaultJREContainerPath();
309         return JavaCore.newContainerEntry(path);
310     }
311     
312     public static IClasspathEntry createContainerEntry() {
313         return JavaCore.newContainerEntry(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH);
314     }
315
316 }
317
Popular Tags