KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > macosx > MacOSXVMInstallType


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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  * Jeff Myers myersj@gmail.com - fix for #75201
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.launching.macosx;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.text.MessageFormat JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.core.runtime.Status;
25
26 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
27 import org.eclipse.jdt.internal.launching.LibraryInfo;
28 import org.eclipse.jdt.internal.launching.StandardVMType;
29
30 import org.eclipse.jdt.launching.IVMInstall;
31 import org.eclipse.jdt.launching.JavaRuntime;
32 import org.eclipse.jdt.launching.VMStandin;
33
34 /**
35  * This plugins into the org.eclipse.jdt.launching.vmInstallTypes extension point
36  */

37 public class MacOSXVMInstallType extends StandardVMType {
38     
39     /*
40      * The directory structure for Java VMs is as follows:
41      *
42      * /System/Library/Frameworks/JavaVM.framework/Versions/
43      * 1.3.1
44      * Classes
45      * classes.jar
46      * ui.jar
47      * Home
48      * src.jar
49      * 1.4.1
50      * Classes
51      * classes.jar
52      * ui.jar
53      * Home
54      * src.jar
55      * CurrentJDK -> 1.3.1
56      */

57      
58     private static final String JavaDoc JAVA_VM_NAME= "Java HotSpot(TM) Client VM"; //$NON-NLS-1$
59

60     /** The OS keeps all the JVM versions in this directory */
61     private static final String JavaDoc JVM_VERSION_LOC= "/System/Library/Frameworks/JavaVM.framework/Versions/"; //$NON-NLS-1$
62
/** The name of a Unix link to MacOS X's default VM */
63     private static final String JavaDoc CURRENT_JVM= "CurrentJDK"; //$NON-NLS-1$
64
/** The root of a JVM */
65     private static final String JavaDoc JVM_ROOT= "Home"; //$NON-NLS-1$
66
/** The doc (for all JVMs) lives here (if the developer kit has been expanded)*/
67     private static final String JavaDoc JAVADOC_LOC= "/Developer/Documentation/Java/Reference/"; //$NON-NLS-1$
68
/** The doc for 1.4.1 is kept in a sub directory of the above. */
69     private static final String JavaDoc JAVADOC_SUBDIR= "/doc/api"; //$NON-NLS-1$
70

71                 
72     public String JavaDoc getName() {
73         return MacOSXLaunchingPlugin.getString("MacOSXVMType.name"); //$NON-NLS-1$
74
}
75     
76     public IVMInstall doCreateVMInstall(String JavaDoc id) {
77         return new MacOSXVMInstall(this, id);
78     }
79             
80     /*
81      * @see IVMInstallType#detectInstallLocation()
82      */

83     public File JavaDoc detectInstallLocation() {
84         
85         String JavaDoc javaVMName= System.getProperty("java.vm.name"); //$NON-NLS-1$
86
if (javaVMName == null || !JAVA_VM_NAME.equals(javaVMName))
87             return null;
88
89         // find all installed VMs
90
File JavaDoc defaultLocation= null;
91         File JavaDoc versionDir= new File JavaDoc(JVM_VERSION_LOC);
92         if (versionDir.exists() && versionDir.isDirectory()) {
93             File JavaDoc currentJDK= new File JavaDoc(versionDir, CURRENT_JVM);
94             try {
95                 currentJDK= currentJDK.getCanonicalFile();
96             } catch (IOException JavaDoc ex) {
97                 // NeedWork
98
}
99             File JavaDoc[] versions= versionDir.listFiles();
100             for (int i= 0; i < versions.length; i++) {
101                 String JavaDoc version= versions[i].getName();
102                 File JavaDoc home= new File JavaDoc(versions[i], JVM_ROOT);
103                 if (home.exists()) {
104                     boolean isDefault= currentJDK.equals(versions[i]);
105                     IVMInstall install= findVMInstall(version);
106                     if (install == null && !CURRENT_JVM.equals(version)) {
107                         VMStandin vm= new VMStandin(this, version);
108                         vm.setInstallLocation(home);
109                         String JavaDoc format= MacOSXLaunchingPlugin.getString(isDefault
110                                                     ? "MacOSXVMType.jvmDefaultName" //$NON-NLS-1$
111
: "MacOSXVMType.jvmName"); //$NON-NLS-1$
112
vm.setName(MessageFormat.format(format, new Object JavaDoc[] { version } ));
113                         vm.setLibraryLocations(getDefaultLibraryLocations(home));
114                         URL JavaDoc doc= getDefaultJavadocLocation(home);
115                         if (doc != null)
116                             vm.setJavadocLocation(doc);
117                         
118                         IVMInstall rvm= vm.convertToRealVM();
119                         if (isDefault) {
120                             defaultLocation= home;
121                             try {
122                                 JavaRuntime.setDefaultVMInstall(rvm, null);
123                             } catch (CoreException e) {
124                                 LaunchingPlugin.log(e);
125                             }
126                         }
127                     } else {
128                         if (isDefault) {
129                             defaultLocation= home;
130                             try {
131                                 JavaRuntime.setDefaultVMInstall(install, null);
132                             } catch (CoreException e) {
133                                 LaunchingPlugin.log(e);
134                             }
135                         }
136                     }
137                 }
138             }
139         }
140         return defaultLocation;
141     }
142
143     /**
144      * Returns default library info for the given install location.
145      *
146      * @param installLocation
147      * @return LibraryInfo
148      */

149     protected LibraryInfo getDefaultLibraryInfo(File JavaDoc installLocation) {
150
151         File JavaDoc classes = new File JavaDoc(installLocation, "../Classes"); //$NON-NLS-1$
152
File JavaDoc lib1= new File JavaDoc(classes, "classes.jar"); //$NON-NLS-1$
153
File JavaDoc lib2= new File JavaDoc(classes, "ui.jar"); //$NON-NLS-1$
154

155         String JavaDoc[] libs = new String JavaDoc[] { lib1.toString(),lib2.toString() };
156         
157         File JavaDoc lib = new File JavaDoc(installLocation, "lib"); //$NON-NLS-1$
158
File JavaDoc extDir = new File JavaDoc(lib, "ext"); //$NON-NLS-1$
159
String JavaDoc[] dirs = null;
160         if (extDir == null)
161             dirs = new String JavaDoc[0];
162         else
163             dirs = new String JavaDoc[] {extDir.getAbsolutePath()};
164
165         File JavaDoc endDir = new File JavaDoc(lib, "endorsed"); //$NON-NLS-1$
166
String JavaDoc[] endDirs = null;
167         if (endDir == null)
168             endDirs = new String JavaDoc[0];
169         else
170             endDirs = new String JavaDoc[] {endDir.getAbsolutePath()};
171         
172         return new LibraryInfo("???", libs, dirs, endDirs); //$NON-NLS-1$
173
}
174     
175     protected IPath getDefaultSystemLibrarySource(File JavaDoc libLocation) {
176         File JavaDoc parent= libLocation.getParentFile();
177         while (parent != null) {
178             File JavaDoc home= new File JavaDoc(parent, JVM_ROOT);
179             File JavaDoc parentsrc= new File JavaDoc(home, "src.jar"); //$NON-NLS-1$
180
if (parentsrc.isFile()) {
181                 setDefaultRootPath("src");//$NON-NLS-1$
182
return new Path(parentsrc.getPath());
183             }
184             parentsrc= new File JavaDoc(home, "src.zip"); //$NON-NLS-1$
185
if (parentsrc.isFile()) {
186                 setDefaultRootPath(""); //$NON-NLS-1$
187
return new Path(parentsrc.getPath());
188             }
189             parent = parent.getParentFile();
190         }
191         setDefaultRootPath(""); //$NON-NLS-1$
192
return Path.EMPTY; //$NON-NLS-1$
193
}
194
195     /**
196      * @see org.eclipse.jdt.launching.IVMInstallType#validateInstallLocation(java.io.File)
197      */

198     public IStatus validateInstallLocation(File JavaDoc javaHome) {
199         String JavaDoc id= MacOSXLaunchingPlugin.getUniqueIdentifier();
200         File JavaDoc java= new File JavaDoc(javaHome, "bin"+File.separator+"java"); //$NON-NLS-2$ //$NON-NLS-1$
201
if (java.isFile())
202             return new Status(IStatus.OK, id, 0, "ok", null); //$NON-NLS-1$
203
return new Status(IStatus.ERROR, id, 0, MacOSXLaunchingPlugin.getString("MacOSXVMType.error.notRoot"), null); //$NON-NLS-1$
204
}
205     
206     /**
207      * @see org.eclipse.jdt.launching.AbstractVMInstallType#getDefaultJavadocLocation(java.io.File)
208      */

209     public URL JavaDoc getDefaultJavadocLocation(File JavaDoc installLocation) {
210         
211         // try in local filesystem
212
String JavaDoc id= null;
213         try {
214             String JavaDoc post= File.separator + JVM_ROOT;
215             String JavaDoc path= installLocation.getCanonicalPath();
216             if (path.startsWith(JVM_VERSION_LOC) && path.endsWith(post))
217                 id= path.substring(JVM_VERSION_LOC.length(), path.length()-post.length());
218         } catch (IOException JavaDoc ex) {
219             // we use the fall back from below
220
}
221         if (id != null) {
222             String JavaDoc s= JAVADOC_LOC + id + JAVADOC_SUBDIR; //$NON-NLS-1$
223
File JavaDoc docLocation= new File JavaDoc(s);
224             if (!docLocation.exists()) {
225                 s= JAVADOC_LOC + id;
226                 docLocation= new File JavaDoc(s);
227                 if (!docLocation.exists())
228                     s= null;
229             }
230             if (s != null) {
231                 try {
232                     return new URL JavaDoc("file", "", s); //$NON-NLS-1$ //$NON-NLS-2$
233
} catch (MalformedURLException JavaDoc ex) {
234                     // we use the fall back from below
235
}
236             }
237         }
238         
239         // fall back
240
return super.getDefaultJavadocLocation(installLocation);
241     }
242 }
243
Popular Tags