incubator-easyant-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jben...@apache.org
Subject svn commit: r1089626 - /incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ivy/repository/JarRepository.java
Date Wed, 06 Apr 2011 21:20:03 GMT
Author: jbenois
Date: Wed Apr  6 21:20:03 2011
New Revision: 1089626

URL: http://svn.apache.org/viewvc?rev=1089626&view=rev
Log:
Improved the JarResolver in order to work fine when EasyAnt is embed in Equinox OSGi container.
Very useful for EasyAnt For Eclipse.


Modified:
    incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ivy/repository/JarRepository.java

Modified: incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ivy/repository/JarRepository.java
URL: http://svn.apache.org/viewvc/incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ivy/repository/JarRepository.java?rev=1089626&r1=1089625&r2=1089626&view=diff
==============================================================================
--- incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ivy/repository/JarRepository.java
(original)
+++ incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ivy/repository/JarRepository.java
Wed Apr  6 21:20:03 2011
@@ -19,9 +19,15 @@ package org.apache.easyant.core.ivy.repo
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Method;
 import java.net.JarURLConnection;
 import java.net.URL;
-import java.util.*;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.regex.Matcher;
@@ -42,18 +48,29 @@ public class JarRepository extends URLRe
     private HashMap<String, Directory> jarCache = new HashMap<String, Directory>();
 
     public List list(String parent) throws IOException {
-        if (parent.startsWith("jar")) {
+    	URL url = new URL(parent);    	
+    	if ("jar".equals(url.getProtocol())) {
             //extract path in parent
             Matcher matcher = URL_PATTERN.matcher(parent);
             matcher.find();
-
+            
             String baseUrl = matcher.group(1);
             String path = matcher.group(2);
             
             //find the parent path in the directory.
-            Directory directory = getDirectory(baseUrl, parent).findEntry(path);
-            return new ArrayList<String>(directory.getChildren());
-        }
+            Directory directory = getDirectoryFromJar(baseUrl, url).findEntry(path);
+            return new ArrayList<String>(directory.getChildren());   
+        
+        // Hook required to embed Easyant in OSGi container
+        // Equinox used a custom "bundleresource" protocol 
+        // (Other OSGi implementations like Felix use file:// ou jar:// standard protocols)
       
+        }else if ("bundleresource".equals(url.getProtocol())) {        	          
+             String baseUrl = url.getHost();
+             String path = url.getPath();
+             //find the parent path in the directory.
+             Directory directory = getDirectoryFromEquinoxBundle(baseUrl, url).findEntry(path);
+             return new ArrayList<String>(directory.getChildren());
+		}    	       
         return super.list(parent);
     }
 
@@ -69,12 +86,11 @@ public class JarRepository extends URLRe
      * @param baseName the base URL of the jar file
      * @param jarUrl complete URL to the entry being searched
      */
-    private Directory getDirectory(String baseName, String jarUrl) throws IOException {
+    private Directory getDirectoryFromJar(String baseName, URL jarUrl) throws IOException
{
         synchronized (jarCache) {
             Directory cached = jarCache.get(baseName);
-            if (cached == null) {
-                URL url = new URL(jarUrl);
-                JarURLConnection conn = (JarURLConnection) url.openConnection();
+            if (cached == null) {                                         
+                JarURLConnection conn = (JarURLConnection) jarUrl.openConnection();
                 JarFile file = conn.getJarFile();
                 if (file == null) {
                     return emptyNode;
@@ -85,7 +101,36 @@ public class JarRepository extends URLRe
                         cached.addEntry(entry.getName());
                     }
                     jarCache.put(baseName, cached);
-                }
+                }                
+            }
+            return cached;
+        }
+    }
+    
+    private static Method getLocalURL;
+    /**
+     * get a sorted list of entries in the given OSGi bundle
+     * @param baseName the base URL of the OSGi bundle
+     * @param jarUrl complete URL to the entry being searched
+     */
+    private Directory getDirectoryFromEquinoxBundle(String baseName, URL jarUrl) throws IOException
{
+        synchronized (jarCache) {
+            Directory cached = jarCache.get(baseName);
+            if (cached == null) {
+            	try {
+	                URLConnection conn = jarUrl.openConnection();
+	                if(getLocalURL==null && "org.eclipse.osgi.framework.internal.core.BundleURLConnection".equals(conn.getClass().getName())){
+	                	JarRepository.getLocalURL = conn.getClass().getMethod("getLocalURL", null);
+						getLocalURL.setAccessible(true);
+	                }
+	                if(getLocalURL!=null && conn!=null){
+	                	URL localJarUrl = (URL)getLocalURL.invoke(conn, null);	               
	
+	                	Directory directory = getDirectoryFromJar(baseName, localJarUrl);	    
           	
+	                	return directory;
+	                }
+            	} catch (Throwable throwable) {
+					throw new IOException("Cannot get directory from Equinox OSGi bundle", throwable);
+				}              
             }
             return cached;
         }



Mime
View raw message