KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.MessageFormat;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.ant.core.AntCorePlugin;
19 import org.eclipse.ant.core.AntCorePreferences;
20 import org.eclipse.ant.core.IAntClasspathEntry;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.debug.core.ILaunchConfiguration;
25 import org.eclipse.jdt.internal.launching.AbstractRuntimeClasspathEntry;
26 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
27 import org.eclipse.jdt.launching.JavaRuntime;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * A classpath entry that contains a set of archives for a particular
33  * ANT_HOME.
34  *
35  * @since 3.0
36  */

37 public class AntHomeClasspathEntry extends AbstractRuntimeClasspathEntry {
38     
39     public static final String JavaDoc TYPE_ID = "org.eclipse.ant.ui.classpathentry.antHome"; //$NON-NLS-1$
40

41     /**
42      * Local path on disk where Ant Home is located or <code>null</code>
43      * to indicate the use of the default Ant Home.
44      */

45     private String JavaDoc antHomeLocation = null;
46     
47     /**
48      * Creates an AntHome entry for the default AntHome installation.
49      */

50     public AntHomeClasspathEntry() {
51         antHomeLocation = null;
52     }
53     
54     /**
55      * Constructs an AntHome entry for the Ant installed at the specified
56      * root directory.
57      *
58      * @param antHome path in the local file system to an Ant installation
59      */

60     public AntHomeClasspathEntry(String JavaDoc antHome) {
61         antHomeLocation = antHome;
62     }
63         
64     /* (non-Javadoc)
65      * @see org.eclipse.jdt.internal.launching.AbstractRuntimeClasspathEntry#buildMemento(org.w3c.dom.Document, org.w3c.dom.Element)
66      */

67     protected void buildMemento(Document JavaDoc document, Element JavaDoc memento) throws CoreException {
68         if (antHomeLocation == null) {
69             memento.setAttribute("default", "true"); //$NON-NLS-1$//$NON-NLS-2$
70
} else {
71             memento.setAttribute("antHome", new Path(antHomeLocation).toString()); //$NON-NLS-1$
72
}
73     }
74     /* (non-Javadoc)
75      * @see org.eclipse.jdt.internal.launching.IRuntimeClasspathEntry2#initializeFrom(org.w3c.dom.Element)
76      */

77     public void initializeFrom(Element JavaDoc memento) throws CoreException {
78         String JavaDoc antHome = memento.getAttribute("antHome"); //$NON-NLS-1$
79
if (antHome != null && (antHome.length() > 0)) {
80             IPath path = new Path(antHome);
81             antHomeLocation = path.toOSString();
82         } else {
83             antHomeLocation = null;
84         }
85     }
86     /* (non-Javadoc)
87      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getTypeId()
88      */

89     public String JavaDoc getTypeId() {
90         return TYPE_ID;
91     }
92     /* (non-Javadoc)
93      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getRuntimeClasspathEntries(org.eclipse.debug.core.ILaunchConfiguration)
94      */

95     public IRuntimeClasspathEntry[] getRuntimeClasspathEntries(ILaunchConfiguration configuration) throws CoreException {
96         List JavaDoc libs = new ArrayList JavaDoc(40);
97         AntCorePreferences preferences = AntCorePlugin.getPlugin().getPreferences();
98         if (antHomeLocation == null) {
99             IAntClasspathEntry[] entries = preferences.getAntHomeClasspathEntries();
100             for (int i = 0; i < entries.length; i++) {
101                 IAntClasspathEntry entry = entries[i];
102                 libs.add(JavaRuntime.newStringVariableClasspathEntry(entry.getLabel()));
103             }
104         } else {
105             File JavaDoc lib= resolveAntHome();
106             IPath libDir = new Path(antHomeLocation).append("lib"); //$NON-NLS-1$
107
String JavaDoc[] fileNames = lib.list();
108             for (int i = 0; i < fileNames.length; i++) {
109                 String JavaDoc name = fileNames[i];
110                 IPath path = new Path(name);
111                 String JavaDoc fileExtension = path.getFileExtension();
112                 if ("jar".equalsIgnoreCase(fileExtension)) { //$NON-NLS-1$
113
libs.add(JavaRuntime.newArchiveRuntimeClasspathEntry(libDir.append(path)));
114                 }
115             }
116         }
117         return (IRuntimeClasspathEntry[]) libs.toArray(new IRuntimeClasspathEntry[libs.size()]);
118     }
119     
120     public File JavaDoc resolveAntHome() throws CoreException {
121         if (antHomeLocation == null) { //using the default ant home
122
return null;
123         }
124         IPath libDir= new Path(antHomeLocation).append("lib"); //$NON-NLS-1$
125
File JavaDoc lib= libDir.toFile();
126         File JavaDoc parentDir= lib.getParentFile();
127         if (parentDir == null || !parentDir.exists()) {
128             abort(MessageFormat.format(AntLaunchConfigurationMessages.AntHomeClasspathEntry_10, new String JavaDoc[] {antHomeLocation}), null);
129         }
130         if (!lib.exists() || !lib.isDirectory()) {
131             abort(MessageFormat.format(AntLaunchConfigurationMessages.AntHomeClasspathEntry_11, new String JavaDoc[] {antHomeLocation}), null);
132         }
133         return lib;
134     }
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#getName()
138      */

139     public String JavaDoc getName() {
140         if (antHomeLocation == null) {
141             return AntLaunchConfigurationMessages.AntHomeClasspathEntry_8;
142         }
143         return MessageFormat.format(AntLaunchConfigurationMessages.AntHomeClasspathEntry_9, new String JavaDoc[]{antHomeLocation});
144     }
145     
146     /* (non-Javadoc)
147      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry#getType()
148      */

149     public int getType() {
150         return IRuntimeClasspathEntry.OTHER;
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntry2#isComposite()
155      */

156     public boolean isComposite() {
157         return true;
158     }
159     
160     /* (non-Javadoc)
161      * @see java.lang.Object#equals(java.lang.Object)
162      */

163     public boolean equals(Object JavaDoc obj) {
164         return obj instanceof AntHomeClasspathEntry &&
165           equalsOrNull(antHomeLocation, ((AntHomeClasspathEntry)obj).antHomeLocation);
166     }
167     
168     /**
169      * Return whether s1 is equivalent to s2.
170      *
171      * @param s1
172      * @param s2
173      * @return whether s1 is equivalent to s2
174      */

175     private boolean equalsOrNull(String JavaDoc s1, String JavaDoc s2) {
176         if (s1 == null || s2 == null) {
177             return s1 == s2;
178         }
179         return s1.equalsIgnoreCase(s2);
180     }
181
182     /* (non-Javadoc)
183      * @see java.lang.Object#hashCode()
184      */

185     public int hashCode() {
186         return getClass().hashCode();
187     }
188     
189     /**
190      * Sets the ant home to use.
191      *
192      * @param path path to toor of an ant home installation
193      */

194     protected void setAntHome(String JavaDoc path) {
195         antHomeLocation = path;
196     }
197     
198     /**
199      * Returns the ant home location
200      *
201      * @return path to root ant installation directory
202      */

203     public String JavaDoc getAntHome() {
204         if (antHomeLocation == null) {
205             return AntCorePlugin.getPlugin().getPreferences().getAntHome();
206         }
207         return antHomeLocation;
208     }
209 }
210
Popular Tags