Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 379112 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.gemini.web.core/src/main/java/org/eclipse/gemini/web/internal/url/WebBundleScanner.java (-14 / +89 lines)
Lines 19-33 package org.eclipse.gemini.web.internal.url; Link Here
19
import java.io.File;
19
import java.io.File;
20
import java.io.FileInputStream;
20
import java.io.FileInputStream;
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.io.InputStream;
22
import java.net.URI;
23
import java.net.URI;
23
import java.net.URISyntaxException;
24
import java.net.URISyntaxException;
24
import java.net.URL;
25
import java.net.URL;
26
import java.util.Collection;
27
import java.util.Enumeration;
25
import java.util.HashSet;
28
import java.util.HashSet;
26
import java.util.Set;
29
import java.util.Set;
27
import java.util.jar.Attributes;
30
import java.util.jar.Attributes;
28
import java.util.jar.JarEntry;
31
import java.util.jar.JarEntry;
32
import java.util.jar.JarFile;
29
import java.util.jar.JarInputStream;
33
import java.util.jar.JarInputStream;
30
import java.util.jar.Manifest;
34
import java.util.jar.Manifest;
35
import java.util.zip.ZipEntry;
31
36
32
import org.slf4j.Logger;
37
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
38
import org.slf4j.LoggerFactory;
Lines 59-64 final class WebBundleScanner { Link Here
59
    private final Object monitor = new Object();
64
    private final Object monitor = new Object();
60
65
61
    private final URL source;
66
    private final URL source;
67
    private final String localSourcePath;
68
    private final Collection<ZipEntry> sourceZipEntries = new HashSet<ZipEntry>();
62
69
63
    private final WebBundleScannerCallback callBack;
70
    private final WebBundleScannerCallback callBack;
64
71
Lines 81-86 final class WebBundleScanner { Link Here
81
        this.source = source;
88
        this.source = source;
82
        this.callBack = callBack;
89
        this.callBack = callBack;
83
        this.findClassesInNestedJars = findClassesInNestedJars;
90
        this.findClassesInNestedJars = findClassesInNestedJars;
91
        this.localSourcePath = getLocalSourcePath(source);
84
    }
92
    }
85
93
86
    /**
94
    /**
Lines 92-97 final class WebBundleScanner { Link Here
92
    void scanWar() throws IOException {
100
    void scanWar() throws IOException {
93
        synchronized (this.monitor) {
101
        synchronized (this.monitor) {
94
            this.scannedJars.clear();
102
            this.scannedJars.clear();
103
            this.sourceZipEntries.clear();
95
            if (isDirectory()) {
104
            if (isDirectory()) {
96
                scanWarDirectory();
105
                scanWarDirectory();
97
            } else {
106
            } else {
Lines 208-221 final class WebBundleScanner { Link Here
208
                }
217
                }
209
            }
218
            }
210
        }
219
        }
211
220
        
212
        JarEntry entry;
221
        if (this.findClassesInNestedJars) {
213
        while ((entry = jis.getNextJarEntry()) != null) {
222
	        JarEntry entry;
214
            String entryName = entry.getName();
223
	        while ((entry = jis.getNextJarEntry()) != null) {
215
            if (entryName.endsWith(CLASS_SUFFIX)) {
224
	            String entryName = entry.getName();
216
                notifyClassFound(entryName);
225
	            if (entryName.endsWith(CLASS_SUFFIX)) {
217
            }
226
	                notifyClassFound(entryName);
218
        }
227
	            }
228
	        }
229
    	}
219
    }
230
    }
220
231
221
    private static Path getNormalisedDirectoryPath(String jarEntryName) {
232
    private static Path getNormalisedDirectoryPath(String jarEntryName) {
Lines 246-253 final class WebBundleScanner { Link Here
246
            throw new IllegalStateException("Unexpected URISyntaxException.", e);
257
            throw new IllegalStateException("Unexpected URISyntaxException.", e);
247
        }
258
        }
248
    }
259
    }
260
    
261
    private void scanNestedJarInWarFile(final String jarPath) throws IOException {
262
    	if (this.localSourcePath == null) {
263
    		scanNestedJarInWarFileWithStream(jarPath);			
264
			return;
265
		}
266
    	
267
    	scanNestedJarInWarFileWithZipFile(jarPath, this.localSourcePath);
268
    }
269
    
270
    private String getLocalSourcePath(final URL url) { 
271
    	if (!FILE_SCHEME.equals(this.source.getProtocol())) {
272
    		return null;
273
    	}    	
274
    	try {
275
    		final String realPath = url.toURI().getPath();
276
        	if ("".equals(realPath)) {
277
        		return null;
278
        	}
279
        	return realPath;
280
        } catch (URISyntaxException _) {
281
        	return null;
282
        }
283
    }
249
284
250
    private void scanNestedJarInWarFile(String jarPath) throws IOException {
285
    private void scanNestedJarInWarFileWithStream(String jarPath) throws IOException {
251
        JarInputStream jis = new JarInputStream(this.source.openStream());
286
        JarInputStream jis = new JarInputStream(this.source.openStream());
252
        try {
287
        try {
253
            JarEntry entry;
288
            JarEntry entry;
Lines 263-275 final class WebBundleScanner { Link Here
263
        } finally {
298
        } finally {
264
            IOUtils.closeQuietly(jis);
299
            IOUtils.closeQuietly(jis);
265
        }
300
        }
266
301
    }
302
    
303
    private void scanNestedJarInWarFileWithZipFile(String jarPath, String localSourcePath) throws IOException {        
304
        JarFile jarFile = null;        
305
        try {
306
        	InputStream foundInputStream = null;
307
	        String foundZipEntryName = null;        	
308
	    	if (sourceZipEntries.isEmpty()) {//then search and cache all entries
309
	    		jarFile = new JarFile(localSourcePath);
310
	    		Enumeration<JarEntry> jarFileEntries = jarFile.entries(); 
311
	    		while (jarFileEntries.hasMoreElements()) {
312
	    			final ZipEntry zipEntry = jarFileEntries.nextElement();
313
	    			// 1. cache
314
	    			sourceZipEntries.add(zipEntry);
315
	    			// 2. search if it is not found still
316
	    			if ((foundZipEntryName == null) && jarPath.endsWith(zipEntry.getName())) {
317
	    				foundZipEntryName = zipEntry.getName();
318
	    				foundInputStream = jarFile.getInputStream(zipEntry);
319
	    			}    			
320
	    		}
321
	    	} else {//search entry in cache
322
	    		for (ZipEntry zipEntry: sourceZipEntries) {
323
	    			if (jarPath.endsWith(zipEntry.getName())) {    				
324
	    				jarFile = new JarFile(localSourcePath);
325
	    				foundZipEntryName = zipEntry.getName();
326
	    				foundInputStream = jarFile.getInputStream(zipEntry);    				
327
	    				break;
328
	    			}
329
	    			
330
	    		}
331
	    	}
332
	    	
333
	    	if ((foundZipEntryName != null) && driveCallBackIfNewJarFound(foundZipEntryName)) {
334
	            JarInputStream nestedJis = new JarInputStream(foundInputStream);
335
	            doScanNestedJar(foundZipEntryName, nestedJis);
336
	        }
337
        } finally {//quiet close
338
        	if (jarFile != null) {
339
	        	try {		    		
340
	        		jarFile.close();		    		
341
	        	} catch (IOException _) {}
342
        	}
343
    	}    
267
    }
344
    }
268
345
269
    private void notifyClassFound(String entryName) {
346
	private void notifyClassFound(String entryName) {
270
        if (this.findClassesInNestedJars) {
347
        this.callBack.classFound(entryName);        
271
            this.callBack.classFound(entryName);
272
        }
273
    }
348
    }
274
349
275
    private boolean isDirectory() {
350
    private boolean isDirectory() {

Return to bug 379112