1 11 package org.eclipse.debug.internal.core.sourcelookup; 12 13 import java.io.IOException ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.zip.ZipFile ; 17 18 import org.eclipse.core.resources.IResourceChangeEvent; 19 import org.eclipse.core.resources.IResourceChangeListener; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.debug.core.DebugPlugin; 22 import org.eclipse.debug.core.ILaunch; 23 import org.eclipse.debug.core.ILaunchesListener2; 24 25 33 public class SourceLookupUtils { 34 35 41 private static HashMap fgZipFileCache = new HashMap (5); 42 private static ArchiveCleaner fgCleaner = null; 43 44 52 public static ZipFile getZipFile(String name) throws IOException { 53 synchronized (fgZipFileCache) { 54 if (fgCleaner == null) { 55 fgCleaner = new ArchiveCleaner(); 56 DebugPlugin.getDefault().getLaunchManager().addLaunchListener(fgCleaner); 57 ResourcesPlugin.getWorkspace().addResourceChangeListener(fgCleaner, IResourceChangeEvent.PRE_DELETE | IResourceChangeEvent.PRE_CLOSE); 58 } 59 ZipFile zip = (ZipFile )fgZipFileCache.get(name); 60 if (zip == null) { 61 zip = new ZipFile (name); 62 fgZipFileCache.put(name, zip); 63 } 64 return zip; 65 } 66 } 67 68 74 public static void closeArchives() { 75 synchronized (fgZipFileCache) { 76 Iterator iter = fgZipFileCache.values().iterator(); 77 while (iter.hasNext()) { 78 ZipFile file = (ZipFile )iter.next(); 79 synchronized (file) { 80 try { 81 file.close(); 82 } catch (IOException e) { 83 DebugPlugin.log(e); 84 } 85 } 86 } 87 fgZipFileCache.clear(); 88 } 89 } 90 91 94 public static void shutdown() { 95 closeArchives(); 96 if (fgCleaner != null) { 97 DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(fgCleaner); 98 ResourcesPlugin.getWorkspace().removeResourceChangeListener(fgCleaner); 99 } 100 } 101 102 106 static class ArchiveCleaner implements IResourceChangeListener, ILaunchesListener2 { 107 108 111 public void launchesRemoved(ILaunch[] launches) { 112 for (int i = 0; i < launches.length; i++) { 113 ILaunch launch = launches[i]; 114 if (!launch.isTerminated()) { 115 SourceLookupUtils.closeArchives(); 116 return; 117 } 118 } 119 } 120 121 124 public void launchesAdded(ILaunch[] launches) { 125 } 126 127 130 public void launchesChanged(ILaunch[] launches) { 131 } 132 133 136 public void resourceChanged(IResourceChangeEvent event) { 137 SourceLookupUtils.closeArchives(); 138 } 139 140 143 public void launchesTerminated(ILaunch[] launches) { 144 SourceLookupUtils.closeArchives(); 145 } 146 147 } 148 } 149 | Popular Tags |