KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > launchConfigurations > ContributedClasspathEntriesEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ant.internal.ui.launchConfigurations;
12
13 import java.io.File JavaDoc;
14 import java.io.FilenameFilter JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.ant.core.AntCorePlugin;
22 import org.eclipse.ant.core.AntCorePreferences;
23 import org.eclipse.ant.core.IAntClasspathEntry;
24 import org.eclipse.ant.internal.ui.AntUIPlugin;
25 import org.eclipse.ant.internal.ui.AntUtil;
26 import org.eclipse.ant.internal.ui.IAntUIConstants;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.FileLocator;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.debug.core.ILaunchConfiguration;
33 import org.eclipse.jdt.internal.launching.AbstractRuntimeClasspathEntry;
34 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
35 import org.eclipse.jdt.launching.IVMInstall;
36 import org.eclipse.jdt.launching.JavaRuntime;
37 import org.eclipse.osgi.service.resolver.BundleDescription;
38 import org.osgi.framework.Bundle;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * A classpath entry that contains a contributed classpath entries
44  * via the <code>extraClasspathEntries</code> extension point.
45  *
46  * @since 3.0
47  */

48 public class ContributedClasspathEntriesEntry extends AbstractRuntimeClasspathEntry {
49     
50     public static final String JavaDoc TYPE_ID = "org.eclipse.ant.ui.classpathentry.extraClasspathEntries"; //$NON-NLS-1$
51

52     public static List JavaDoc fgSWTEntries= null;
53         
54     /**
55      * Default contructor required to instantiate persisted extensions.
56      */

57     public ContributedClasspathEntriesEntry() {
58     }
59     
60     /* (non-Javadoc)
61      * @see org.eclipse.jdt.internal.launching.AbstractRuntimeClasspathEntry#buildMemento(org.w3c.dom.Document, org.w3c.dom.Element)
62      */

63     protected void buildMemento(Document JavaDoc document, Element JavaDoc memento) throws CoreException {
64     }
65     
66     /* (non-Javadoc)
67      * @see org.eclipse.jdt.internal.launching.IRuntimeClasspathEntry2#initializeFrom(org.w3c.dom.Element)
68      */

69     public void initializeFrom(Element JavaDoc memento) throws CoreException {
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getTypeId()
74      */

75     public String JavaDoc getTypeId() {
76         return TYPE_ID;
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getRuntimeClasspathEntries(org.eclipse.debug.core.ILaunchConfiguration)
81      */

82     public IRuntimeClasspathEntry[] getRuntimeClasspathEntries(ILaunchConfiguration configuration) throws CoreException {
83         boolean separateVM= AntUtil.isSeparateJREAntBuild(configuration);
84         boolean setInputHandler= configuration.getAttribute(IAntUIConstants.SET_INPUTHANDLER, true);
85         AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences();
86         IAntClasspathEntry[] antClasspathEntries = prefs.getContributedClasspathEntries();
87         IAntClasspathEntry[] userEntries = prefs.getAdditionalClasspathEntries();
88         List JavaDoc rtes = new ArrayList JavaDoc(antClasspathEntries.length + userEntries.length);
89         IAntClasspathEntry entry;
90         for (int i = 0; i < antClasspathEntries.length; i++) {
91              entry= antClasspathEntries[i];
92             if (!separateVM || (separateVM && !entry.isEclipseRuntimeRequired())) {
93                 rtes.add(JavaRuntime.newStringVariableClasspathEntry(entry.getLabel()));
94             }
95         }
96         boolean haveToolsEntry= false;
97         String JavaDoc path;
98         for (int i = 0; i < userEntries.length; i++) {
99             entry = userEntries[i];
100             path= entry.getLabel();
101             IPath toolsPath= new Path(path);
102             if (toolsPath.lastSegment().equals("tools.jar")) { //$NON-NLS-1$
103
haveToolsEntry= true;
104                 // replace with dynamically resolved tools.jar based on
105
// the JRE being used
106
addToolsJar(configuration, rtes, path);
107             } else {
108                 rtes.add(JavaRuntime.newStringVariableClasspathEntry(path));
109             }
110         }
111         if (!haveToolsEntry) {
112             addToolsJar(configuration, rtes, null);
113         }
114         
115         if (setInputHandler && separateVM) {
116             addSWTJars(rtes);
117         }
118         
119         return (IRuntimeClasspathEntry[]) rtes.toArray(new IRuntimeClasspathEntry[rtes.size()]);
120     }
121     
122     private void addToolsJar(ILaunchConfiguration configuration, List JavaDoc rtes, String JavaDoc path) {
123         IRuntimeClasspathEntry tools = getToolsJar(configuration);
124         if (tools == null) {
125             if (path != null) {
126                 //use the global entry
127
rtes.add(JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(path)));
128             } else {
129                 //use the default vm install to try to find a tools.jar
130
IVMInstall install= JavaRuntime.getDefaultVMInstall();
131                 if (install != null) {
132                     IAntClasspathEntry entry = AntCorePlugin.getPlugin().getPreferences().getToolsJarEntry(new Path(install.getInstallLocation().getAbsolutePath()));
133                     if (entry != null) {
134                         rtes.add(JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(entry.getEntryURL().getPath())));
135                     }
136                 }
137             }
138         } else {
139             rtes.add(tools);
140         }
141     }
142     
143     private void addSWTJars(List JavaDoc rtes) {
144         if (fgSWTEntries == null) {
145             fgSWTEntries= new ArrayList JavaDoc();
146             Bundle bundle= Platform.getBundle("org.eclipse.swt"); //$NON-NLS-1$
147
BundleDescription description= Platform.getPlatformAdmin().getState(false).getBundle(bundle.getBundleId());
148             BundleDescription[] fragments= description.getFragments();
149             for (int i = 0; i < fragments.length; i++) {
150                 Bundle fragmentBundle= Platform.getBundle(fragments[i].getName());
151                 URL JavaDoc bundleURL;
152                 try {
153                     bundleURL = FileLocator.resolve(fragmentBundle.getEntry("/")); //$NON-NLS-1$
154
} catch (IOException JavaDoc e) {
155                     AntUIPlugin.log(e);
156                    continue;
157                 }
158                 String JavaDoc urlFileName= bundleURL.getFile();
159                 if (urlFileName.startsWith("file:")) { //$NON-NLS-1$
160
try {
161                         urlFileName= new URL JavaDoc(urlFileName).getFile();
162                         if (urlFileName.endsWith("!/")) { //$NON-NLS-1$
163
urlFileName= urlFileName.substring(0, urlFileName.length() - 2);
164                         }
165                     } catch (MalformedURLException JavaDoc e) {
166                         AntUIPlugin.log(e);
167                        continue;
168                     }
169                 }
170                 IPath fragmentPath= new Path(urlFileName);
171                 if (fragmentPath.getFileExtension() != null) { //JAR file
172
fgSWTEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(fragmentPath));
173                 } else { // folder
174
File JavaDoc bundleFolder= fragmentPath.toFile();
175                     if (!bundleFolder.isDirectory()) {
176                         continue;
177                     }
178                     String JavaDoc[] names= bundleFolder.list(new FilenameFilter JavaDoc() {
179                         public boolean accept(File JavaDoc dir, String JavaDoc name) {
180                             return name.endsWith(".jar"); //$NON-NLS-1$
181
}
182                     });
183                     for (int j = 0; j < names.length; j++) {
184                         String JavaDoc jarName = names[j];
185                         fgSWTEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(fragmentPath.append(jarName)));
186                     }
187                 }
188             }
189         }
190         rtes.addAll(fgSWTEntries);
191     }
192     
193     /**
194      * Returns the tools.jar to use for this launch configuration, or <code>null</code>
195      * if none.
196      *
197      * @param configuration configuration to resolve a tools.jar for
198      * @return associated tools.jar archive, or <code>null</code>
199      */

200     private IRuntimeClasspathEntry getToolsJar(ILaunchConfiguration configuration) {
201         try {
202             IVMInstall install = JavaRuntime.computeVMInstall(configuration);
203             if (install != null) {
204                 IAntClasspathEntry entry = AntCorePlugin.getPlugin().getPreferences().getToolsJarEntry(new Path(install.getInstallLocation().getAbsolutePath()));
205                 if (entry != null) {
206                     return JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(entry.getEntryURL().getPath()));
207                 }
208             }
209         } catch (CoreException ce) {
210             //likely dealing with a non-Java project
211
}
212             
213         return null;
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getName()
218      */

219     public String JavaDoc getName() {
220         return AntLaunchConfigurationMessages.ContributedClasspathEntriesEntry_1;
221     }
222     /* (non-Javadoc)
223      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry#getType()
224      */

225     public int getType() {
226         return IRuntimeClasspathEntry.OTHER;
227     }
228     /* (non-Javadoc)
229      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#isComposite()
230      */

231     public boolean isComposite() {
232         return true;
233     }
234     /* (non-Javadoc)
235      * @see java.lang.Object#equals(java.lang.Object)
236      */

237     public boolean equals(Object JavaDoc obj) {
238         return obj instanceof ContributedClasspathEntriesEntry;
239     }
240     /* (non-Javadoc)
241      * @see java.lang.Object#hashCode()
242      */

243     public int hashCode() {
244         return getClass().hashCode();
245     }
246 }
247
Popular Tags