You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by pa...@apache.org on 2019/07/11 21:08:33 UTC

[sling-whiteboard] branch master updated: Update to latest feature model release

This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new c8c6582  Update to latest feature model release
c8c6582 is described below

commit c8c658226028b73cd028f18cb7fa1ab468fe8f2e
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Thu Jul 11 23:08:24 2019 +0200

    Update to latest feature model release
---
 connectfeaturelauncher/pom.xml                     |  11 +-
 .../extensions/connect/impl/PojoSRLauncher.java    | 152 +++++++++++++++++----
 .../extensions/connect/impl/PojoSRRunner.java      |   6 +-
 .../java/org/osgi/framework/FrameworkUtil.java     |  12 +-
 4 files changed, 151 insertions(+), 30 deletions(-)

diff --git a/connectfeaturelauncher/pom.xml b/connectfeaturelauncher/pom.xml
index 4330b02..e7202b4 100644
--- a/connectfeaturelauncher/pom.xml
+++ b/connectfeaturelauncher/pom.xml
@@ -54,6 +54,7 @@
                         <exclude>src/main/resources/META-INF/services/**</exclude>
                         <exclude>**/*.properties</exclude>
                         <exclude>launcher/**</exclude>
+                        <exclude>**/*.patch</exclude>
                     </excludes>
                 </configuration>
             </plugin>
@@ -80,14 +81,14 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature.launcher</artifactId>
-            <version>1.0.1-SNAPSHOT</version>
+            <version>1.0.4</version>
             <scope>provided</scope>
         </dependency>
 
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature</artifactId>
-            <version>1.0.1-SNAPSHOT</version>
+            <version>1.0.4</version>
             <scope>provided</scope>
         </dependency>
 
@@ -97,6 +98,12 @@
             <version>0.2.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.io</artifactId>
+            <version>1.0.4</version>
+            <scope>compile</scope>
+        </dependency>
 
         <!-- Testing dependencies -->
         <dependency>
diff --git a/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRLauncher.java b/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRLauncher.java
index 80bae3c..6217383 100644
--- a/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRLauncher.java
+++ b/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRLauncher.java
@@ -16,10 +16,18 @@
  */
 package org.apache.sling.feature.launcher.extensions.connect.impl;
 
-import java.io.File;
-import java.io.FileOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.CodeSource;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
@@ -27,6 +35,7 @@ import java.util.jar.Manifest;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.IOUtils;
 import org.apache.sling.feature.launcher.impl.launchers.FrameworkLauncher;
 import org.apache.sling.feature.launcher.spi.LauncherPrepareContext;
 import org.osgi.framework.Bundle;
@@ -39,7 +48,7 @@ public class PojoSRLauncher extends FrameworkLauncher
     @Override
     public void prepare(LauncherPrepareContext context, ArtifactId frameworkId, Feature app) throws Exception
     {
-        super.prepare(context, frameworkId, app);
+        context.addAppJar(context.getArtifactFile(frameworkId));
 
         String filter = "";
         if (frameworkId.getArtifactId().equals("org.apache.felix.connect"))
@@ -48,11 +57,10 @@ public class PojoSRLauncher extends FrameworkLauncher
             {
                 for (Artifact artifact : bundles)
                 {
-                    File file = context.getArtifactFile(artifact.getId());
-
-                    try (JarFile jar = new JarFile(file, false)) {
+                    URL url = context.getArtifactFile(artifact.getId());
+                    try (JarFile jar = IOUtils.getJarFileFromURL(url, true, null)) {
                         Manifest mf = jar.getManifest();
-                        String bsn = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+                        String bsn = mf.getMainAttributes() != null ? mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME) : null;
                         if (bsn != null)
                         {
                             String cp = mf.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
@@ -71,21 +79,11 @@ public class PojoSRLauncher extends FrameworkLauncher
                                         JarEntry content = jar.getJarEntry(entry);
                                         if (content != null && !content.isDirectory())
                                         {
-                                            File target = File.createTempFile(entry, ".jar");
                                             try
                                             {
-                                                try (InputStream input = jar.getInputStream(content))
-                                                {
-                                                    try (FileOutputStream output = new FileOutputStream(target))
-                                                    {
-                                                        byte[] buffer = new byte[64 * 1024];
-                                                        for (int i = input.read(buffer); i != -1; i = input.read(buffer))
-                                                        {
-                                                            output.write(buffer, 0, i);
-                                                        }
-                                                    }
-                                                    context.addAppJar(target);
-                                                }
+                                                String urlString = "jar:" + IOUtils.getFileFromURL(getJarFileURL(url, content), true, null).toURI().toURL() + "!/";
+                                                System.out.println(urlString);
+                                                context.addAppJar(new URL(urlString));
                                             }
                                             catch (Exception ex)
                                             {
@@ -95,19 +93,19 @@ public class PojoSRLauncher extends FrameworkLauncher
                                     }
                                     else
                                     {
-                                        context.addAppJar(file);
+                                        context.addAppJar(url);
                                         filter += "(" + Constants.BUNDLE_SYMBOLICNAME + "=" +  bsn + ")";
                                     }
                                 }
                             }
                             else
                             {
-                                context.addAppJar(file);
+                                context.addAppJar(url);
                                 filter += "(" + Constants.BUNDLE_SYMBOLICNAME + "=" +  bsn + ")";
                             }
                         }
                         else {
-                            context.addAppJar(file);
+                            context.addAppJar(url);
                         }
 
                     } catch (Exception ex) {
@@ -123,6 +121,17 @@ public class PojoSRLauncher extends FrameworkLauncher
         }
     }
 
+    private URL getJarFileURL(URL jar, JarEntry entry) throws Exception {
+        String urlString = jar.toExternalForm();
+        if (jar.getProtocol().equals("jar")) {
+
+            return new URL(urlString + (urlString.endsWith("!/") ? entry.getName() : "!/" + entry.getName()));
+        }
+        else {
+            return new URL("jar:" + urlString +  "!/" + entry.getName());
+        }
+    }
+
     @Override
     protected String getFrameworkRunnerClass()
     {
@@ -139,6 +148,7 @@ public class PojoSRLauncher extends FrameworkLauncher
 
     private class PojoSRLauncherClassLoader extends LauncherClassLoader implements BundleReference {
 
+
         @Override
         public Bundle getBundle()
         {
@@ -159,7 +169,101 @@ public class PojoSRLauncher extends FrameworkLauncher
             return (name.startsWith("org.osgi.framework.") && !name.startsWith(FrameworkUtil.class.getName())) ||
                 name.startsWith("org.osgi.resource.") ?
                 PojoSRLauncher.class.getClassLoader().loadClass(name) :
-                super.findClass(name);
+                define(name);
+        }
+
+        private Class define(String name) throws ClassNotFoundException
+        {
+            URL resource = findResource(name.replace(".", "/" ) + ".class");
+            if (resource != null) {
+                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+                try (InputStream input = resource.openStream() ) {
+                    byte[] b = new byte[64 * 1024];
+                    for (int i = input.read(b);i != -1; i = input.read(b)) {
+                        buffer.write(b, 0, i);
+                    }
+
+                    return super.defineClass(name, buffer.toByteArray(),0, buffer.size(),
+                        new CodeSource(new URL(resource.toExternalForm().substring(0, resource.toExternalForm().lastIndexOf("!/") + 2)), (java.security.cert.Certificate[]) null));
+                }
+                catch (IOException e)
+                {
+                    throw new ClassNotFoundException(e.getMessage());
+                }
+            }
+            else {
+                throw new ClassNotFoundException(name);
+            }
+        }
+
+        public void indexURLs()
+        {
+            if (init.compareAndSet(false, true))
+            {
+                for (URL url : super.getURLs())
+                {
+                    try (JarFile jar = IOUtils.getJarFileFromURL(url, true, null))
+                    {
+                        for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
+                        {
+                            JarEntry entry = entries.nextElement();
+                            String[] paths;
+                            if (entry.isDirectory())
+                            {
+                                paths = new String[]{entry.getName(), entry.getName().substring(0, entry.getName().length() - 1),
+                                    "/" + entry.getName(), "/" + entry.getName().substring(0, entry.getName().length() - 1)};
+                            }
+                            else
+                            {
+                                paths = new String[]{entry.getName(), "/" + entry.getName()};
+                            }
+
+                            for (String path : paths)
+                            {
+                                resourcesIDX.compute(path, (key, value) ->
+                                {
+                                    if (value == null)
+                                    {
+                                        value = new ArrayList<>();
+                                    }
+                                    try
+                                    {
+                                        value.add(getJarFileURL(url, entry));
+                                    }
+                                    catch (Exception e)
+                                    {
+                                        e.printStackTrace();
+                                    }
+                                    return value;
+                                });
+                            }
+                        }
+                    }
+                    catch (IOException e)
+                    {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+
+        private volatile AtomicBoolean init = new AtomicBoolean(false);
+
+        private final ConcurrentHashMap<String, List<URL>> resourcesIDX = new ConcurrentHashMap<String, List<URL>>();
+
+        @Override
+        public URL findResource(String name)
+        {
+            indexURLs();
+            List<URL> urls = resourcesIDX.getOrDefault(name, Collections.emptyList());
+            return urls.isEmpty() ? null : urls.get(0);
+        }
+
+        @Override
+        public Enumeration<URL> findResources(String name) throws IOException
+        {
+            indexURLs();
+            return Collections.enumeration(resourcesIDX.getOrDefault(name, Collections.emptyList()));
         }
     }
 }
diff --git a/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRRunner.java b/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRRunner.java
index 2f505da..5d62c37 100644
--- a/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRRunner.java
+++ b/connectfeaturelauncher/src/main/java/org/apache/sling/feature/launcher/extensions/connect/impl/PojoSRRunner.java
@@ -16,7 +16,7 @@
  */
 package org.apache.sling.feature.launcher.extensions.connect.impl;
 
-import java.io.File;
+import java.net.URL;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -28,13 +28,13 @@ import org.osgi.framework.launch.Framework;
 
 public class PojoSRRunner extends FrameworkRunner
 {
-    public PojoSRRunner(Map<String, String> frameworkProperties, Map<Integer, List<File>> bundlesMap, List<Object[]> configurations, List<File> installables) throws Exception
+    public PojoSRRunner(Map<String, String> frameworkProperties, Map<Integer, List<URL>> bundlesMap, List<Object[]> configurations, List<URL> installables) throws Exception
     {
         super(frameworkProperties, bundlesMap, configurations, installables);
     }
 
     @Override
-    protected void setupFramework(Framework framework, Map<Integer, List<File>> bundlesMap) throws BundleException
+    protected void setupFramework(Framework framework, Map<Integer, List<URL>> bundlesMap) throws BundleException
     {
         try
         {
diff --git a/connectfeaturelauncher/src/main/java/org/osgi/framework/FrameworkUtil.java b/connectfeaturelauncher/src/main/java/org/osgi/framework/FrameworkUtil.java
index ffdb543..0c11657 100644
--- a/connectfeaturelauncher/src/main/java/org/osgi/framework/FrameworkUtil.java
+++ b/connectfeaturelauncher/src/main/java/org/osgi/framework/FrameworkUtil.java
@@ -21,6 +21,7 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.AbstractMap;
@@ -206,7 +207,16 @@ public class FrameworkUtil
 	 * @since 1.5
 	 */
 	public static Bundle getBundle(final Class<?> classFromBundle) {
-		return BUNDLE == null ? null : BUNDLE.getBundleContext().getBundle("jar:" + classFromBundle.getProtectionDomain().getCodeSource().getLocation().toExternalForm() + "!/");
+		return BUNDLE == null ? null : BUNDLE.getBundleContext().getBundle(getURL(classFromBundle.getProtectionDomain().getCodeSource().getLocation()));
+	}
+
+	private static String getURL(URL url) {
+		if (url.getProtocol().equals("jar")) {
+			return url.toExternalForm();
+		}
+		else {
+			return "jar:" + url.toExternalForm() + "!/";
+		}
 	}
 
 	/**