1 19 package org.netbeans.modules.project.libraries; 20 21 import java.io.IOException ; 22 import java.lang.ref.Reference ; 23 import java.lang.ref.SoftReference ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import org.netbeans.spi.project.libraries.LibraryTypeProvider; 27 import org.openide.ErrorManager; 28 import org.openide.cookies.InstanceCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileSystem; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.filesystems.Repository; 33 import org.openide.loaders.DataFolder; 34 import org.openide.loaders.FolderInstance; 35 36 public final class LibraryTypeRegistry extends FolderInstance { 37 38 private static final String REGISTRY = "org-netbeans-api-project-libraries/LibraryTypeProviders"; private static FileObject findProvidersFolder() { 40 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 41 FileObject folder = sfs.findResource(REGISTRY); 42 if (folder == null) { 43 try { 45 folder = FileUtil.createFolder(sfs.getRoot(), REGISTRY); 46 } catch (IOException e) { 47 throw (IllegalStateException ) new IllegalStateException ("Cannot make folder " + REGISTRY + ": " + e).initCause(e); 49 } 50 } 51 return folder; 52 } 53 54 private static Reference <LibraryTypeRegistry> instance; 55 56 private LibraryTypeRegistry () { 57 super(DataFolder.findFolder(findProvidersFolder())); 58 } 59 60 public LibraryTypeProvider[] getLibraryTypeProviders () { 61 try { 62 return (LibraryTypeProvider[]) this.instanceCreate (); 63 } catch (IOException ioe) { 64 ErrorManager.getDefault().notify(ioe); 65 } 66 catch (ClassNotFoundException cnf) { 67 ErrorManager.getDefault().notify(cnf); 68 } 69 return new LibraryTypeProvider[0]; 70 } 71 72 public LibraryTypeProvider getLibraryTypeProvider (String libraryType) { 73 assert libraryType != null; 74 try { 75 LibraryTypeProvider[] providers = (LibraryTypeProvider[]) this.instanceCreate (); 76 for (int i = 0; i < providers.length; i++) { 77 if (libraryType.equals(providers[i].getLibraryType())) 78 return providers[i]; 79 } 80 } catch (IOException ioe) { 81 ErrorManager.getDefault().notify (ioe); 82 } 83 catch (ClassNotFoundException cnfe) { 84 ErrorManager.getDefault().notify (cnfe); 85 } 86 return null; 87 } 88 89 90 protected Object createInstance(InstanceCookie[] cookies) throws IOException , ClassNotFoundException { 91 List <LibraryTypeProvider> installers = new ArrayList <LibraryTypeProvider>(cookies.length); 92 for (InstanceCookie cake : cookies) { 93 LibraryTypeProvider o = null; 94 try { 95 if (cake instanceof InstanceCookie.Of && !(((InstanceCookie.Of)cake).instanceOf(LibraryTypeProvider.class))) 96 continue; 97 o = (LibraryTypeProvider) cake.instanceCreate(); 98 } catch (IOException ex) { 99 } catch (ClassNotFoundException ex) { 100 } 101 if (o != null) 102 installers.add(o); 103 } 104 return installers.toArray(new LibraryTypeProvider[installers.size()]); 105 } 106 107 108 public static synchronized LibraryTypeRegistry getDefault () { 109 LibraryTypeRegistry regs = null; 110 if (instance == null || (regs = instance.get()) == null) { 111 regs = new LibraryTypeRegistry(); 112 instance = new SoftReference <LibraryTypeRegistry>(regs); 113 } 114 return regs; 115 } 116 117 } 118 | Popular Tags |