1 19 20 package org.netbeans.modules.java.j2seplatform.wizard; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.lang.InterruptedException ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.Set ; 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 49 public final class NewJ2SEPlatform extends J2SEPlatformImpl implements Runnable { 50 51 private static Set propertiesToFix = new HashSet (); 52 53 static { 55 propertiesToFix.add ("sun.boot.class.path"); propertiesToFix.add ("sun.boot.library.path"); propertiesToFix.add ("java.library.path"); propertiesToFix.add ("java.ext.dirs"); propertiesToFix.add ("java.home"); } 61 62 private boolean valid; 63 64 public static NewJ2SEPlatform create (FileObject installFolder) throws IOException { 65 assert installFolder != null; 66 Map platformProperties = new HashMap (); 67 return new NewJ2SEPlatform (null,Collections.singletonList(installFolder.getURL()),platformProperties,Collections.EMPTY_MAP); 68 } 69 70 private NewJ2SEPlatform (String name, List installFolders, Map platformProperties, Map systemProperties) { 71 super(name, name, installFolders, platformProperties, systemProperties,null,null); 72 } 73 74 public boolean isValid () { 75 return this.valid; 76 } 77 78 82 public void run() { 83 try { 84 FileObject java = findTool("java"); 85 if (java == null) 86 return; 87 File javaFile = FileUtil.toFile (java); 88 if (javaFile == null) 89 return; 90 String javapath = javaFile.getAbsolutePath(); 91 String filePath = File.createTempFile("nb-platformdetect", "properties").getAbsolutePath(); 92 getSDKProperties(javapath, filePath); 93 File f = new File (filePath); 94 Properties p = new Properties (); 95 InputStream is = new FileInputStream (f); 96 p.load(is); 97 Map m = new HashMap (p.size()); 98 for (Enumeration en = p.keys(); en.hasMoreElements(); ) { 99 String k = (String )en.nextElement(); 100 String v = (String ) 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 ex) { 109 this.valid = false; 110 } 111 } 112 113 114 120 private String fixSymLinks (String key, String value) { 121 if (Utilities.isUnix() && propertiesToFix.contains (key)) { 122 try { 123 String [] pathElements = value.split(File.pathSeparator); 124 boolean changed = false; 125 for (Iterator it = this.getInstallFolders().iterator(); it.hasNext();) { 126 File f = FileUtil.toFile ((FileObject) it.next()); 127 if (f != null) { 128 String path = f.getAbsolutePath(); 129 String 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 sb = new StringBuffer (); 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 ioe) { 151 } 153 } 154 return value; 155 } 156 157 158 private void getSDKProperties(String javaPath, String path) throws IOException { 159 Runtime runtime = Runtime.getRuntime(); 160 try { 161 String [] command = new String [5]; 162 command[0] = javaPath; 163 command[1] = "-classpath"; command[2] = InstalledFileLocator.getDefault().locate("modules/ext/org-netbeans-modules-java-j2seplatform-probe.jar", "org.netbeans.modules.java.j2seplatform", false).getAbsolutePath(); command[3] = "org.netbeans.modules.java.j2seplatform.wizard.SDKProbe"; 166 command[4] = path; 167 final Process process = runtime.exec(command); 168 process.waitFor(); 171 int exitValue = process.exitValue(); 172 if (exitValue != 0) 173 throw new IOException (); 174 } catch (InterruptedException ex) { 175 IOException e = new IOException (); 176 ErrorManager.getDefault().annotate(e,ex); 177 throw e; 178 } 179 } 180 } 181 182 183 184 185 186 187 188 189 | Popular Tags |