KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > sourcelookup > SourceLookupUtils


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.core.sourcelookup;
12
13 import java.io.IOException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.zip.ZipFile JavaDoc;
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 /**
26  * Utility and supporting methods for source location. Most of these
27  * utilities should be migrated to the DebugPlugin and LanuchManager
28  * when this facility becomes public API.
29  *
30  * @see org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector
31  * @since 3.0
32  */

33 public class SourceLookupUtils {
34         
35     /**
36      * Cache of shared zip files. Zip files are closed
37      * when this class's plug-in is shutdown, when a project
38      * is about to be closed or deleted, when a launch is
39      * removed, and when a debug target or process terminates.
40      */

41     private static HashMap JavaDoc fgZipFileCache = new HashMap JavaDoc(5);
42     private static ArchiveCleaner fgCleaner = null;
43     
44     /**
45      * Returns a zip file with the given name
46      *
47      * @param name zip file name
48      * @return The zip file with the given name
49      * @exception IOException if unable to create the specified zip
50      * file
51      */

52     public static ZipFile JavaDoc getZipFile(String JavaDoc name) throws IOException JavaDoc {
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 JavaDoc zip = (ZipFile JavaDoc)fgZipFileCache.get(name);
60             if (zip == null) {
61                 zip = new ZipFile JavaDoc(name);
62                 fgZipFileCache.put(name, zip);
63             }
64             return zip;
65         }
66     }
67     
68     /**
69      * Closes all zip files that have been opened,
70      * and removes them from the zip file cache.
71      * This method is only to be called by the debug
72      * plug-in.
73      */

74     public static void closeArchives() {
75         synchronized (fgZipFileCache) {
76             Iterator JavaDoc iter = fgZipFileCache.values().iterator();
77             while (iter.hasNext()) {
78                 ZipFile JavaDoc file = (ZipFile JavaDoc)iter.next();
79                 synchronized (file) {
80                     try {
81                         file.close();
82                     } catch (IOException JavaDoc e) {
83                         DebugPlugin.log(e);
84                     }
85                 }
86             }
87             fgZipFileCache.clear();
88         }
89     }
90     
91     /**
92      * Called when the debug plug-in shuts down.
93      */

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     /**
103      * Clears the cache of open zip files when a launch terminates,
104      * is removed, or when a project is about to be deleted or closed.
105      */

106     static class ArchiveCleaner implements IResourceChangeListener, ILaunchesListener2 {
107
108         /* (non-Javadoc)
109          * @see org.eclipse.debug.core.ILaunchesListener#launchesRemoved(org.eclipse.debug.core.ILaunch[])
110          */

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         /* (non-Javadoc)
122          * @see org.eclipse.debug.core.ILaunchesListener#launchesAdded(org.eclipse.debug.core.ILaunch[])
123          */

124         public void launchesAdded(ILaunch[] launches) {
125         }
126
127         /* (non-Javadoc)
128          * @see org.eclipse.debug.core.ILaunchesListener#launchesChanged(org.eclipse.debug.core.ILaunch[])
129          */

130         public void launchesChanged(ILaunch[] launches) {
131         }
132
133         /* (non-Javadoc)
134          * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
135          */

136         public void resourceChanged(IResourceChangeEvent event) {
137             SourceLookupUtils.closeArchives();
138         }
139
140         /* (non-Javadoc)
141          * @see org.eclipse.debug.core.ILaunchesListener2#launchesTerminated(org.eclipse.debug.core.ILaunch[])
142          */

143         public void launchesTerminated(ILaunch[] launches) {
144             SourceLookupUtils.closeArchives();
145         }
146         
147     }
148 }
149
Popular Tags