KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JREContainerInitializer


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.launching;
12
13
14 import java.io.File JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jdt.core.ClasspathContainerInitializer;
24 import org.eclipse.jdt.core.IClasspathAttribute;
25 import org.eclipse.jdt.core.IClasspathContainer;
26 import org.eclipse.jdt.core.IClasspathEntry;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
30 import org.eclipse.jdt.launching.IVMInstall;
31 import org.eclipse.jdt.launching.IVMInstallType;
32 import org.eclipse.jdt.launching.JavaRuntime;
33 import org.eclipse.jdt.launching.LibraryLocation;
34 import org.eclipse.jdt.launching.VMStandin;
35 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
36 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
37
38 import com.ibm.icu.text.MessageFormat;
39
40 /**
41  * Resolves a container for a JRE classpath container entry.
42  */

43 public class JREContainerInitializer extends ClasspathContainerInitializer {
44
45     /**
46      * @see ClasspathContainerInitializer#initialize(IPath, IJavaProject)
47      */

48     public void initialize(IPath containerPath, IJavaProject project) throws CoreException {
49         if (JREContainer.DEBUG_JRE_CONTAINER) {
50             System.out.println("<JRE_CONTAINER> initialize()"); //$NON-NLS-1$
51
System.out.println("\tPath: " + containerPath.toString()); //$NON-NLS-1$
52
System.out.println("\tProj: " + project.getProject().getName()); //$NON-NLS-1$
53
}
54         int size = containerPath.segmentCount();
55         if (size > 0) {
56             if (containerPath.segment(0).equals(JavaRuntime.JRE_CONTAINER)) {
57                 IVMInstall vm = resolveVM(containerPath);
58                 JREContainer container = null;
59                 if (vm != null) {
60                     if (JREContainer.DEBUG_JRE_CONTAINER) {
61                         System.out.println("\tResolved VM: " + vm.getName()); //$NON-NLS-1$
62
}
63                     container = new JREContainer(vm, containerPath, project);
64                 } else {
65                     if (JREContainer.DEBUG_JRE_CONTAINER) {
66                         System.out.println("\t*** FAILED RESOLVE VM ***"); //$NON-NLS-1$
67
}
68                 }
69                 JavaCore.setClasspathContainer(containerPath, new IJavaProject[] {project}, new IClasspathContainer[] {container}, null);
70             } else {
71                 if (JREContainer.DEBUG_JRE_CONTAINER) {
72                     System.out.println("\t*** INVALID JRE CONTAINER PATH ***"); //$NON-NLS-1$
73
}
74             }
75         } else {
76             if (JREContainer.DEBUG_JRE_CONTAINER) {
77                 System.out.println("\t*** NO SEGMENTS IN CONTAINER PATH ***"); //$NON-NLS-1$
78
}
79         }
80     }
81     
82     /**
83      * Returns the VM install associated with the container path, or <code>null</code>
84      * if it does not exist.
85      */

86     public static IVMInstall resolveVM(IPath containerPath) {
87         IVMInstall vm = null;
88         if (containerPath.segmentCount() > 1) {
89             // specific JRE
90
String JavaDoc id = getExecutionEnvironmentId(containerPath);
91             if (id != null) {
92                 if (JREContainer.DEBUG_JRE_CONTAINER) {
93                     System.out.println("<JRE_CONTAINER> resolveVM(IPath)"); //$NON-NLS-1$
94
System.out.println("\tEE: " + id); //$NON-NLS-1$
95
}
96                 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
97                 IExecutionEnvironment environment = manager.getEnvironment(id);
98                 if (environment != null) {
99                     vm = resolveVM(environment);
100                 } else {
101                     if (JREContainer.DEBUG_JRE_CONTAINER) {
102                         System.out.println("\t*** NO ENVIRONMENT ***"); //$NON-NLS-1$
103
}
104                 }
105             } else {
106                 String JavaDoc vmTypeId = getVMTypeId(containerPath);
107                 String JavaDoc vmName = getVMName(containerPath);
108                 IVMInstallType vmType = JavaRuntime.getVMInstallType(vmTypeId);
109                 if (vmType != null) {
110                     vm = vmType.findVMInstallByName(vmName);
111                 }
112             }
113         } else {
114             // workspace default JRE
115
vm = JavaRuntime.getDefaultVMInstall();
116         }
117         return vm;
118     }
119     
120     /**
121      * Returns the VM install bound to the given execution environment
122      * or <code>null</code>.
123      *
124      * @param environment
125      * @return vm install or <code>null</code>
126      * @since 3.2
127      */

128     public static IVMInstall resolveVM(IExecutionEnvironment environment) {
129         if (JREContainer.DEBUG_JRE_CONTAINER) {
130             System.out.println("<JRE_CONTAINER> resolveVM(IExecutionEnvironment)"); //$NON-NLS-1$
131
}
132         IVMInstall vm = environment.getDefaultVM();
133         if (vm == null) {
134             IVMInstall[] installs = environment.getCompatibleVMs();
135             // take the first strictly compatible vm if there is no default
136
if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) {
137                 System.out.println("\t*** NO COMPATIBLE VMS ***"); //$NON-NLS-1$
138
}
139             for (int i = 0; i < installs.length; i++) {
140                 IVMInstall install = installs[i];
141                 if (environment.isStrictlyCompatible(install)) {
142                     vm = install;
143                     if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) {
144                         System.out.println("\tPerfect Match: " + vm.getName()); //$NON-NLS-1$
145
}
146                     break;
147                 }
148             }
149             // use the first vm failing that
150
if (vm == null && installs.length > 0) {
151                 vm = installs[0];
152                 if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) {
153                     System.out.println("\tFirst Match: " + vm.getName()); //$NON-NLS-1$
154
}
155             }
156         } else {
157             if (JREContainer.DEBUG_JRE_CONTAINER) {
158                 System.out.println("\tUser Default VM: " + vm.getName()); //$NON-NLS-1$
159
}
160         }
161         return vm;
162     }
163     
164     /**
165      * Returns the segment from the path containing the execution environment id
166      * or <code>null</code>
167      *
168      * @param path container path
169      * @return ee id
170      */

171     public static String JavaDoc getExecutionEnvironmentId(IPath path) {
172         String JavaDoc name = getVMName(path);
173         if (name != null) {
174             name = decodeEnvironmentId(name);
175             IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
176             IExecutionEnvironment environment = manager.getEnvironment(name);
177             if (environment != null) {
178                 return environment.getId();
179             }
180         }
181         return null;
182     }
183     
184     /**
185      * Returns whether the given path identifies a vm by exeuction environment.
186      *
187      * @param path
188      * @return whether the given path identifies a vm by exeuction environment
189      */

190     public static boolean isExecutionEnvironment(IPath path) {
191         return getExecutionEnvironmentId(path) != null;
192     }
193     
194     /**
195      * Escapes foward slashes in environment id.
196      *
197      * @param id
198      * @return esaped name
199      */

200     public static String JavaDoc encodeEnvironmentId(String JavaDoc id) {
201         return id.replace('/', '%');
202     }
203     
204     public static String JavaDoc decodeEnvironmentId(String JavaDoc id) {
205         return id.replace('%', '/');
206     }
207     
208     /**
209      * Returns the VM type identifier from the given container ID path.
210      *
211      * @return the VM type identifier from the given container ID path
212      */

213     public static String JavaDoc getVMTypeId(IPath path) {
214         return path.segment(1);
215     }
216     
217     /**
218      * Returns the VM name from the given container ID path.
219      *
220      * @return the VM name from the given container ID path
221      */

222     public static String JavaDoc getVMName(IPath path) {
223         return path.segment(2);
224     }
225     
226     /**
227      * The container can be updated if it refers to an existing VM.
228      *
229      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
230      */

231     public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) {
232         if (containerPath != null && containerPath.segmentCount() > 0) {
233             if (JavaRuntime.JRE_CONTAINER.equals(containerPath.segment(0))) {
234                 return resolveVM(containerPath) != null;
235             }
236         }
237         return false;
238     }
239     
240     private static final IStatus READ_ONLY= new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, new String JavaDoc(), null);
241     private static final IStatus NOT_SUPPORTED= new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED, new String JavaDoc(), null);
242     
243     /* (non-Javadoc)
244      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getAccessRulesStatus(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
245      */

246     public IStatus getAccessRulesStatus(IPath containerPath, IJavaProject project) {
247         return READ_ONLY;
248     }
249     
250     /* (non-Javadoc)
251      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getSourceAttachmentStatus(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
252      */

253     public IStatus getSourceAttachmentStatus(IPath containerPath, IJavaProject project) {
254         return Status.OK_STATUS;
255     }
256     
257     /* (non-Javadoc)
258      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getAttributeStatus(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, java.lang.String)
259      */

260     public IStatus getAttributeStatus(IPath containerPath, IJavaProject project, String JavaDoc attributeKey) {
261         if (attributeKey.equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
262             return Status.OK_STATUS;
263         }
264         if (attributeKey.equals(JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY)) {
265             return Status.OK_STATUS;
266         }
267         return NOT_SUPPORTED;
268     }
269
270     /**
271      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#requestClasspathContainerUpdate(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathContainer)
272      */

273     public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException {
274         IVMInstall vm = resolveVM(containerPath);
275         if (vm == null) {
276             IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_VM_INSTALL_DOES_NOT_EXIST, MessageFormat.format(LaunchingMessages.JREContainerInitializer_JRE_referenced_by_classpath_container__0__does_not_exist__1, new String JavaDoc[]{containerPath.toString()}), null);
277             throw new CoreException(status);
278         }
279         // update of the vm with new library locations
280

281         IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();
282         LibraryLocation[] libs = new LibraryLocation[entries.length];
283         for (int i = 0; i < entries.length; i++) {
284             IClasspathEntry entry = entries[i];
285             if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
286                 IPath path = entry.getPath();
287                 File JavaDoc lib = path.toFile();
288                 if (lib.exists() && lib.isFile()) {
289                     IPath srcPath = entry.getSourceAttachmentPath();
290                     if (srcPath == null) {
291                         srcPath = Path.EMPTY;
292                     }
293                     IPath rootPath = entry.getSourceAttachmentRootPath();
294                     if (rootPath == null) {
295                         rootPath = Path.EMPTY;
296                     }
297                     URL JavaDoc javadocLocation = null;
298                     IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
299                     for (int j = 0; j < extraAttributes.length; j++) {
300                         IClasspathAttribute attribute = extraAttributes[j];
301                         if (attribute.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
302                             String JavaDoc url = attribute.getValue();
303                             if (url != null && url.trim().length() > 0) {
304                                 try {
305                                     javadocLocation = new URL JavaDoc(url);
306                                 } catch (MalformedURLException JavaDoc e) {
307                                     LaunchingPlugin.log(e);
308                                 }
309                             }
310                         }
311                     }
312                     libs[i] = new LibraryLocation(path, srcPath, rootPath, javadocLocation);
313                 } else {
314                     IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, MessageFormat.format(LaunchingMessages.JREContainerInitializer_Classpath_entry__0__does_not_refer_to_an_existing_library__2, new String JavaDoc[]{entry.getPath().toString()}), null);
315                     throw new CoreException(status);
316                 }
317             } else {
318                 IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, MessageFormat.format(LaunchingMessages.JREContainerInitializer_Classpath_entry__0__does_not_refer_to_a_library__3, new String JavaDoc[]{entry.getPath().toString()}), null);
319                 throw new CoreException(status);
320             }
321         }
322         VMStandin standin = new VMStandin(vm);
323         standin.setLibraryLocations(libs);
324         standin.convertToRealVM();
325         JavaRuntime.saveVMConfiguration();
326     }
327
328     /**
329      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
330      */

331     public String JavaDoc getDescription(IPath containerPath, IJavaProject project) {
332         String JavaDoc tag = getExecutionEnvironmentId(containerPath);
333         if (tag == null && containerPath.segmentCount() > 2) {
334             tag = getVMName(containerPath);
335         }
336         if (tag != null) {
337             return MessageFormat.format(LaunchingMessages.JREContainer_JRE_System_Library_1, new String JavaDoc[]{tag});
338         }
339         return LaunchingMessages.JREContainerInitializer_Default_System_Library_1;
340     }
341 }
342
Popular Tags