1 19 package org.netbeans.modules.java.j2seplatform; 20 21 import java.io.IOException ; 22 import java.util.concurrent.Callable ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import java.util.logging.Logger ; 26 import org.openide.cookies.InstanceCookie; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.Repository; 29 import org.openide.loaders.DataObject; 30 import org.openide.modules.ModuleInstall; 31 import org.openide.util.Exceptions; 32 import org.openide.util.Exceptions; 33 import org.netbeans.api.java.platform.JavaPlatform; 34 import org.netbeans.api.java.platform.JavaPlatformManager; 35 import org.netbeans.api.java.platform.Specification; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.spi.project.support.ant.EditableProperties; 38 import org.netbeans.spi.project.support.ant.PropertyUtils; 39 import org.netbeans.modules.java.j2seplatform.platformdefinition.PlatformConvertor; 40 import org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl; 41 42 43 public class J2SEPlatformModule extends ModuleInstall { 44 45 private static final String DEFAULT_PLATFORM = "Services/Platforms/org-netbeans-api-java-Platform/default_platform.xml"; 47 public void restored() { 48 super.restored(); 49 updateBuildProperties(); 51 } 52 53 public static void updateBuildProperties() { 55 ProjectManager.mutex().postWriteRequest( 56 new Runnable () { 57 public void run () { 58 try { 59 recoverDefaultPlatform (); 60 EditableProperties ep = PropertyUtils.getGlobalProperties(); 61 boolean save = updateSourceLevel(ep); 62 save |= updateBuildProperties (ep); 63 if (save) { 64 PropertyUtils.putGlobalProperties (ep); 65 } 66 } catch (IOException ioe) { 67 Exceptions.printStackTrace(ioe); 68 } 69 } 70 }); 71 } 72 73 private static boolean updateSourceLevel(EditableProperties ep) { 74 JavaPlatform platform = JavaPlatformManager.getDefault().getDefaultPlatform(); 75 String ver = platform.getSpecification().getVersion().toString(); 76 if (!ver.equals(ep.getProperty("default.javac.source"))) { ep.setProperty("default.javac.source", ver); ep.setProperty("default.javac.target", ver); return true; 80 } else { 81 return false; 82 } 83 } 84 85 86 private static boolean updateBuildProperties (EditableProperties ep) { 87 boolean changed = false; 88 JavaPlatform[] installedPlatforms = JavaPlatformManager.getDefault().getPlatforms(null, new Specification ("j2se",null)); for (int i=0; i<installedPlatforms.length; i++) { 90 if (!installedPlatforms[i].equals (JavaPlatformManager.getDefault().getDefaultPlatform()) && installedPlatforms[i] instanceof J2SEPlatformImpl) { 92 String systemName = ((J2SEPlatformImpl)installedPlatforms[i]).getAntName(); 93 String key = PlatformConvertor.createName(systemName,"home"); if (!ep.containsKey (key)) { 95 try { 96 PlatformConvertor.generatePlatformProperties(installedPlatforms[i], systemName, ep); 97 changed = true; 98 } catch (PlatformConvertor.BrokenPlatformException b) { 99 Logger.getLogger(J2SEPlatformModule.class.getName()).info("Platform: " + installedPlatforms[i].getDisplayName() +" is missing: " + b.getMissingTool()); 100 } catch (IOException ioe) { 101 Exceptions.printStackTrace(ioe); 102 } 103 } 104 } 105 } 106 return changed; 107 } 108 109 private static void recoverDefaultPlatform () { 110 final FileObject defaultPlatform = Repository.getDefault().getDefaultFileSystem().findResource(DEFAULT_PLATFORM); 111 if (defaultPlatform != null) { 112 try { 113 DataObject dobj = DataObject.find(defaultPlatform); 114 boolean valid = false; 115 InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class); 116 if (ic != null) { 117 try { 118 ic.instanceCreate(); 119 valid = true; 120 } catch (Exception e) { 121 } 123 } 124 if (!valid) { 125 Logger.getLogger("global").log(Level.WARNING,"default_platform.xml is broken, regenerating."); 126 Object attr = defaultPlatform.getAttribute("removeWritables"); if (attr instanceof Callable ) { 128 ((Callable )attr).call (); 129 } 130 } 131 } catch (Exception e) { 132 Exceptions.printStackTrace(e); 133 } 134 } 135 else { 136 Logger.getLogger("global").log(Level.WARNING,"The default platform is hidden."); } 138 } 139 } 140 | Popular Tags |