1 19 20 package org.netbeans.modules.masterfs; 21 22 import org.openide.filesystems.FileSystem; 23 import org.openide.util.Lookup; 24 import org.openide.util.LookupListener; 25 import org.openide.util.LookupEvent; 26 import org.netbeans.modules.masterfs.providers.AutoMountProvider; 27 import org.netbeans.modules.masterfs.providers.FileSystemProvider; 28 29 import java.io.IOException ; 30 import java.beans.PropertyVetoException ; 31 import java.util.*; 32 import java.awt.*; 33 34 public class ProviderCall { 35 static private Map providerMap; 36 static private Lookup.Result preventsLookupResultFromGC; 37 38 static void init () { 39 getProviderMap(); 40 } 41 42 static FileSystem createFileSystem(String resName) { 43 FileSystem retVal = null; 44 45 AutoMountProvider ap = getMountAble(resName); 46 47 if (ap != null) { 48 try { 49 retVal = ap.createFileSystem(resName); 50 } catch (IOException e) { 51 e.printStackTrace(); 52 return null; 53 } catch (PropertyVetoException e) { 54 e.printStackTrace(); 55 return null; 56 } 57 } 58 59 return retVal; 60 } 61 62 63 static boolean isMountAble(String resName) { 64 return getMountAble(resName) == null ? false : true; 65 } 66 67 static Image getIcon(String resName, int iconType) { 68 Image icon = null; 69 AutoMountProvider aProvider = ProviderCall.getMountAble(resName); 70 if (aProvider != null) { 71 icon = aProvider.getIcon(resName, iconType); 72 } 73 return icon; 74 } 75 76 77 private static AutoMountProvider getMountAble(String resName) { 78 Map providerMapCopy; 79 synchronized (getProviderMap()) { 80 providerMapCopy = new HashMap(getProviderMap()); 81 } 82 83 Iterator it = providerMapCopy.values().iterator(); 84 while (it.hasNext()) { 85 AutoMountProvider aProvider = (AutoMountProvider) it.next(); 86 if (aProvider == null) continue; 87 boolean retVal = aProvider.isRootOfFileSystem(resName); 88 if (retVal) return aProvider; 89 } 90 91 return null; 92 } 93 94 96 static private Map getProviderMap() { 97 synchronized (MasterFileSystem.class) { 98 if (providerMap == null) { 99 providerMap = Collections.synchronizedMap(new HashMap()); 100 final Lookup.Result providerResult = 101 Lookup.getDefault().lookup(new Lookup.Template(FileSystemProvider.class)); 102 preventsLookupResultFromGC = providerResult; 103 initFileSystemProviders(providerResult); 104 providerResult.addLookupListener(new LookupListener() { 105 public void resultChanged(LookupEvent ev) { 106 initFileSystemProviders(providerResult); 107 } 108 }); 109 } 110 } 111 return providerMap; 112 } 113 114 static private void initFileSystemProviders(Lookup.Result result) { 115 Set newProviders = new HashSet(result.allInstances()); 116 117 Map upToDateMap = new HashMap(newProviders.size()); 118 Iterator it = newProviders.iterator(); 119 while (it.hasNext()) { 120 FileSystemProvider provider = (FileSystemProvider) it.next(); 121 AutoMountProvider aProvider = (AutoMountProvider) getProviderMap().get(provider); 122 if (aProvider == null) { 123 if (!getProviderMap().keySet().contains(provider)) { 124 aProvider = provider.initialize(MountTable.getDefault().mountSupport); 125 } 126 } 127 upToDateMap.put(provider, aProvider); 128 } 129 getProviderMap().clear(); 130 getProviderMap().putAll(upToDateMap); 131 } 132 133 } 134 | Popular Tags |