1 19 20 package org.netbeans.modules.java.platform; 21 22 import java.io.IOException ; 23 import java.lang.ref.*; 24 import java.util.*; 25 import org.netbeans.spi.java.platform.CustomPlatformInstall; 26 import org.netbeans.spi.java.platform.GeneralPlatformInstall; 27 28 import org.openide.cookies.InstanceCookie; 29 import org.openide.filesystems.*; 30 import org.openide.loaders.*; 31 import org.netbeans.spi.java.platform.PlatformInstall; 32 33 39 public class InstallerRegistry { 40 static final String INSTALLER_REGISTRY_FOLDER = "org-netbeans-api-java/platform/installers"; 42 static Reference defaultInstance = new WeakReference(null); 43 44 private Provider provider; 45 private List platformInstalls; 47 InstallerRegistry(FileObject registryResource) { 48 assert registryResource != null; 49 this.provider = new Provider (registryResource); 50 } 51 52 55 InstallerRegistry (GeneralPlatformInstall[] platformInstalls) { 56 assert platformInstalls != null; 57 this.platformInstalls = Arrays.asList(platformInstalls); 58 } 59 60 64 public List getInstallers () { 65 return filter(getAllInstallers(),PlatformInstall.class); 66 } 67 68 public List getCustomInstallers () { 69 return filter(getAllInstallers(),CustomPlatformInstall.class); 70 } 71 72 public List getAllInstallers () { 73 if (this.platformInstalls != null) { 74 return platformInstalls; 76 } 77 else { 78 Object o = Collections.EMPTY_LIST; 79 try { 80 assert this.provider != null; 81 o = provider.instanceCreate(); 82 } catch (IOException ex) { 83 } catch (ClassNotFoundException ex) { 84 } 85 return (List) o; 86 } 87 } 88 89 90 91 94 public static InstallerRegistry getDefault() { 95 Object o = defaultInstance.get(); 96 if (o != null) 97 return (InstallerRegistry)o; 98 o = new InstallerRegistry(Repository.getDefault().getDefaultFileSystem().findResource( 99 INSTALLER_REGISTRY_FOLDER)); 100 defaultInstance = new WeakReference(o); 101 return (InstallerRegistry)o; 102 } 103 104 105 111 static InstallerRegistry prepareForUnitTest (GeneralPlatformInstall[] platformInstalls) { 112 InstallerRegistry regs = new InstallerRegistry (platformInstalls); 113 defaultInstance = new WeakReference(regs); 114 return regs; 115 } 116 117 118 private static List filter (List list, Class clazz) { 119 List result = new ArrayList (list.size()); 120 for (Iterator it = list.iterator(); it.hasNext();) { 121 Object item = it.next(); 122 if (clazz.isInstance(item)) { 123 result.add (item); 124 } 125 } 126 return result; 127 } 128 129 private static class Provider extends FolderInstance { 130 131 Provider (FileObject registryResource) { 132 super(DataFolder.findFolder(registryResource)); 133 } 134 135 136 protected Object createInstance(InstanceCookie[] cookies) throws java.io.IOException , ClassNotFoundException { 137 List installers = new ArrayList(cookies.length); 138 for (int i = 0; i < cookies.length; i++) { 139 InstanceCookie cake = cookies[i]; 140 Object o = null; 141 try { 142 if (cake instanceof InstanceCookie.Of && 143 !((((InstanceCookie.Of)cake).instanceOf(PlatformInstall.class)) || 144 (((InstanceCookie.Of)cake).instanceOf(CustomPlatformInstall.class)))) 145 continue; 146 o = cake.instanceCreate(); 147 } catch (IOException ex) { 148 } catch (ClassNotFoundException ex) { 149 } 150 if (o != null) 151 installers.add(o); 152 } 153 return installers; 154 } 155 } 156 } 157 | Popular Tags |