KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > wizard > NewJ2SEPlatform


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seplatform.wizard;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.lang.InterruptedException JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Set JavaDoc;
36 import org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
37 import org.netbeans.modules.java.j2seplatform.platformdefinition.Util;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.modules.InstalledFileLocator;
42 import org.openide.util.Utilities;
43
44 /**
45  * Rather dummy implementation of the Java Platform, but sufficient for communication
46  * inside the Wizard.
47  * Made public to allow ide/projectimport to reuse it
48  */

49 public final class NewJ2SEPlatform extends J2SEPlatformImpl implements Runnable JavaDoc {
50     
51     private static Set JavaDoc propertiesToFix = new HashSet JavaDoc ();
52     
53     //Properties used by IDE which should be fixed not to use resolved symlink
54
static {
55         propertiesToFix.add ("sun.boot.class.path"); //NOI18N
56
propertiesToFix.add ("sun.boot.library.path"); //NOI18N
57
propertiesToFix.add ("java.library.path"); //NOI18N
58
propertiesToFix.add ("java.ext.dirs"); //NOI18N
59
propertiesToFix.add ("java.home"); //NOI18N
60
}
61     
62     private boolean valid;
63
64     public static NewJ2SEPlatform create (FileObject installFolder) throws IOException JavaDoc {
65         assert installFolder != null;
66         Map JavaDoc platformProperties = new HashMap JavaDoc ();
67         return new NewJ2SEPlatform (null,Collections.singletonList(installFolder.getURL()),platformProperties,Collections.EMPTY_MAP);
68     }
69
70     private NewJ2SEPlatform (String JavaDoc name, List JavaDoc installFolders, Map JavaDoc platformProperties, Map JavaDoc systemProperties) {
71         super(name, name, installFolders, platformProperties, systemProperties,null,null);
72     }
73
74     public boolean isValid () {
75         return this.valid;
76     }
77
78     /**
79      * Actually performs the detection and stores relevant information
80      * in this Iterator
81      */

82     public void run() {
83         try {
84             FileObject java = findTool("java");
85             if (java == null)
86                 return;
87             File JavaDoc javaFile = FileUtil.toFile (java);
88             if (javaFile == null)
89                 return;
90             String JavaDoc javapath = javaFile.getAbsolutePath();
91             String JavaDoc filePath = File.createTempFile("nb-platformdetect", "properties").getAbsolutePath();
92             getSDKProperties(javapath, filePath);
93             File JavaDoc f = new File JavaDoc(filePath);
94             Properties JavaDoc p = new Properties JavaDoc();
95             InputStream JavaDoc is = new FileInputStream JavaDoc(f);
96             p.load(is);
97             Map JavaDoc m = new HashMap JavaDoc(p.size());
98             for (Enumeration JavaDoc en = p.keys(); en.hasMoreElements(); ) {
99                 String JavaDoc k = (String JavaDoc)en.nextElement();
100                 String JavaDoc v = (String JavaDoc) p.getProperty(k);
101                 v = fixSymLinks (k,v);
102                 m.put(k, v);
103             }
104             this.setSystemProperties(m);
105             this.valid = true;
106             is.close();
107             f.delete();
108         } catch (IOException JavaDoc ex) {
109             this.valid = false;
110         }
111     }
112     
113     
114     /**
115      * Fixes sun.boot.class.path property if it contains resolved
116      * symbolic link. On Suse the jdk is symlinked and during update
117      * the link is changed
118      *
119      */

120     private String JavaDoc fixSymLinks (String JavaDoc key, String JavaDoc value) {
121         if (Utilities.isUnix() && propertiesToFix.contains (key)) {
122             try {
123                 String JavaDoc[] pathElements = value.split(File.pathSeparator);
124                 boolean changed = false;
125                 for (Iterator JavaDoc it = this.getInstallFolders().iterator(); it.hasNext();) {
126                     File JavaDoc f = FileUtil.toFile ((FileObject) it.next());
127                     if (f != null) {
128                         String JavaDoc path = f.getAbsolutePath();
129                         String JavaDoc canonicalPath = f.getCanonicalPath();
130                         if (!path.equals(canonicalPath)) {
131                             for (int i=0; i<pathElements.length; i++) {
132                                 if (pathElements[i].startsWith(canonicalPath)) {
133                                     pathElements[i] = path + pathElements[i].substring(canonicalPath.length());
134                                     changed = true;
135                                 }
136                             }
137                         }
138                     }
139                 }
140                 if (changed) {
141                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
142                     for (int i = 0; i<pathElements.length; i++) {
143                         if (i > 0) {
144                             sb.append(File.pathSeparatorChar);
145                         }
146                         sb.append(pathElements[i]);
147                     }
148                     return sb.toString();
149                 }
150             } catch (IOException JavaDoc ioe) {
151                 //Return the original value
152
}
153         }
154         return value;
155     }
156
157
158     private void getSDKProperties(String JavaDoc javaPath, String JavaDoc path) throws IOException JavaDoc {
159         Runtime JavaDoc runtime = Runtime.getRuntime();
160         try {
161             String JavaDoc[] command = new String JavaDoc[5];
162             command[0] = javaPath;
163             command[1] = "-classpath"; //NOI18N
164
command[2] = InstalledFileLocator.getDefault().locate("modules/ext/org-netbeans-modules-java-j2seplatform-probe.jar", "org.netbeans.modules.java.j2seplatform", false).getAbsolutePath(); // NOI18N
165
command[3] = "org.netbeans.modules.java.j2seplatform.wizard.SDKProbe";
166             command[4] = path;
167             final Process JavaDoc process = runtime.exec(command);
168             // PENDING -- this may be better done by using ExecEngine, since
169
// it produces a cancellable task.
170
process.waitFor();
171             int exitValue = process.exitValue();
172             if (exitValue != 0)
173                 throw new IOException JavaDoc();
174         } catch (InterruptedException JavaDoc ex) {
175             IOException JavaDoc e = new IOException JavaDoc();
176             ErrorManager.getDefault().annotate(e,ex);
177             throw e;
178         }
179     }
180 }
181
182
183
184
185
186
187
188
189
Popular Tags