KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.core.resources.IWorkspace;
15 import org.eclipse.core.resources.IWorkspaceDescription;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.jdt.core.ClasspathVariableInitializer;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.launching.IVMInstall;
24 import org.eclipse.jdt.launching.JavaRuntime;
25 import org.eclipse.jdt.launching.LibraryLocation;
26
27 public class JavaClasspathVariablesInitializer extends ClasspathVariableInitializer {
28
29     /**
30      * The monitor to use for progress reporting.
31      * May be null
32      */

33     private IProgressMonitor fMonitor;
34     
35     /**
36      * @see ClasspathVariableInitializer#initialize(String)
37      */

38     public void initialize(String JavaDoc variable) {
39         IVMInstall vmInstall= JavaRuntime.getDefaultVMInstall();
40         if (vmInstall != null) {
41             IPath newPath= null;
42             LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vmInstall);
43             // look for rt.jar or classes.zip (both may exist, so do exhaustive search)
44
LibraryLocation rtjar = null;
45             LibraryLocation classeszip = null;
46             for (int i = 0; i < locations.length; i++) {
47                 LibraryLocation location = locations[i];
48                 String JavaDoc name = location.getSystemLibraryPath().lastSegment();
49                 if (name.equalsIgnoreCase("rt.jar")) { //$NON-NLS-1$
50
rtjar = location;
51                 } else if (name.equalsIgnoreCase("classes.zip")) { //$NON-NLS-1$
52
classeszip = location;
53                 }
54             }
55             // rt.jar if present, then classes.zip, else the first library
56
LibraryLocation systemLib = rtjar;
57             if (systemLib == null) {
58                 systemLib = classeszip;
59             }
60             if (systemLib == null && locations.length > 0) {
61                 systemLib = locations[0];
62             }
63             if (systemLib != null) {
64                 if (variable.equals(JavaRuntime.JRELIB_VARIABLE)) {
65                     newPath= systemLib.getSystemLibraryPath();
66                 } else if (variable.equals(JavaRuntime.JRESRC_VARIABLE)) {
67                     newPath= systemLib.getSystemLibrarySourcePath();
68                 } else if (variable.equals(JavaRuntime.JRESRCROOT_VARIABLE)){
69                     newPath= systemLib.getPackageRootPath();
70                 }
71                 if (newPath == null) {
72                     return;
73                 }
74                 IWorkspace workspace= ResourcesPlugin.getWorkspace();
75                 IWorkspaceDescription wsDescription= workspace.getDescription();
76                 boolean wasAutobuild= wsDescription.isAutoBuilding();
77                 try {
78                     setAutobuild(workspace, false);
79                     setJREVariable(newPath, variable);
80                 } catch (CoreException ce) {
81                     LaunchingPlugin.log(ce);
82                     return;
83                 } finally {
84                     try {
85                         setAutobuild(workspace, wasAutobuild);
86                     } catch (CoreException ce) {
87                         LaunchingPlugin.log(ce);
88                     }
89                 }
90             }
91         }
92     }
93     
94     private void setJREVariable(IPath newPath, String JavaDoc var) throws CoreException {
95         JavaCore.setClasspathVariable(var, newPath, getMonitor());
96     }
97     
98     private boolean setAutobuild(IWorkspace ws, boolean newState) throws CoreException {
99         IWorkspaceDescription wsDescription= ws.getDescription();
100         boolean oldState= wsDescription.isAutoBuilding();
101         if (oldState != newState) {
102             wsDescription.setAutoBuilding(newState);
103             ws.setDescription(wsDescription);
104         }
105         return oldState;
106     }
107     
108     protected IProgressMonitor getMonitor() {
109         if (fMonitor == null) {
110             return new NullProgressMonitor();
111         }
112         return fMonitor;
113     }
114
115 }
116
Popular Tags