1 19 20 package org.netbeans.modules.projectimport.jbuilder.parsing; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.logging.Logger ; 28 import org.netbeans.modules.java.j2seplatform.wizard.NewJ2SEPlatform; 29 import org.netbeans.modules.projectimport.j2seimport.AbstractProject; 30 import org.netbeans.modules.projectimport.j2seimport.LoggerFactory; 31 import org.netbeans.spi.java.platform.PlatformInstall; 32 import org.openide.cookies.InstanceCookie; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.filesystems.Repository; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 39 40 44 public final class JdkSupport { 45 static final String INSTALLER_REGISTRY_FOLDER = "org-netbeans-api-java/platform/installers"; private static final Logger logger = 47 LoggerFactory.getDefault().createLogger(JdkSupport.class); 48 49 50 51 public static File getJKDDirectory(final String jdkLibraryName, File projectDir) { 52 AbstractProject.UserLibrary aulb = null; 53 try { 54 projectDir = (projectDir == null) ? UserLibrarySupport.getUserHomeLib() : projectDir; 55 Collection installers = getPlatformInstallers(); 56 if (installers.size() == 0) { 57 logger.finest("No registered PlatformInstall"); } 59 60 if (installers.size() > 0 && projectDir != null) { 61 aulb = UserLibrarySupport.getInstance(jdkLibraryName, projectDir); 62 } 63 64 if (aulb != null) { 65 Iterator it = aulb.getLibraries().iterator(); 66 if (it.hasNext()) { 67 AbstractProject.Library al = (AbstractProject.Library)it.next(); 68 File arc = al.getArchiv(); 69 if (arc == null) { 70 logger.finest("user library: "+aulb.getName()+ " contains no archiv (reference is null) "); } 72 73 for (Iterator instances = installers.iterator(); instances.hasNext();) { 74 PlatformInstall pi = (PlatformInstall)instances.next(); 75 for (File toTest = arc; toTest != null; toTest = toTest.getParentFile()) { 76 FileObject foToTest = FileUtil.toFileObject(toTest); 77 if (foToTest != null && pi.accept(foToTest)) { 78 NewJ2SEPlatform platform = NewJ2SEPlatform.create(foToTest); 79 platform.run(); 80 if (platform.isValid()) { 81 return toTest; 82 } 83 } else { 84 if (foToTest == null) { 85 logger.finest("for archiv: " + arc + " toTest: " + toTest); } else if (!pi.accept(foToTest)) { 87 logger.finest("foToTest: " + foToTest + " not accepted by " + pi.getClass()); } 89 } 90 } 91 92 } 93 } 94 95 } 96 } catch (IOException iex) { 97 org.openide.ErrorManager.getDefault().notify(iex); 98 } 101 102 103 return null; 104 } 105 106 private static Collection getPlatformInstallers() { 107 Collection result = new HashSet (); 108 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(INSTALLER_REGISTRY_FOLDER); 109 assert fo != null; 110 assert fo.isFolder(); 111 FileObject[] children = fo.getChildren(); 112 for (int i = 0; i < children.length; i++) { 113 try { 114 DataObject doi = DataObject.find(children[i]); 115 InstanceCookie ic = (InstanceCookie)doi.getCookie(InstanceCookie.class); 116 if (ic != null) { 117 if (PlatformInstall.class.isAssignableFrom(ic.instanceClass())) { 118 result.add(ic.instanceCreate()); 119 } 120 } 121 } catch(DataObjectNotFoundException dex) { 122 org.openide.ErrorManager.getDefault().notify(dex); 123 continue; 124 } catch (IOException iex) { 125 org.openide.ErrorManager.getDefault().notify(iex); 126 continue; 127 } catch (ClassNotFoundException cex) { 128 org.openide.ErrorManager.getDefault().notify(cex); 129 continue; 130 } 131 } 132 133 return result; 134 } 135 } 136 | Popular Tags |