Blame SOURCES/0001-Fix-installer-plugin-loading.patch

b96910
From 0fd7b0d19bd96456292585e883e3ba2ebbaf579b Mon Sep 17 00:00:00 2001
b96910
From: Mikolaj Izdebski <mizdebsk@redhat.com>
b96910
Date: Wed, 21 Jun 2017 10:21:10 +0200
b96910
Subject: [PATCH] Fix installer plugin loading
b96910
b96910
---
b96910
 .../install/impl/ArtifactInstallerFactory.java     |  97 ++++++--
b96910
 .../tools/install/impl/IsolatedClassRealm.java     | 245 +++++++++++++++++++++
b96910
 2 files changed, 320 insertions(+), 22 deletions(-)
b96910
 create mode 100644 xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/IsolatedClassRealm.java
b96910
b96910
diff --git a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java
b96910
index 7a80571..e6a9a2d 100644
b96910
--- a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java
b96910
+++ b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java
b96910
@@ -15,8 +15,17 @@
b96910
  */
b96910
 package org.fedoraproject.xmvn.tools.install.impl;
b96910
 
b96910
+import java.io.BufferedReader;
b96910
+import java.io.IOException;
b96910
+import java.io.InputStream;
b96910
+import java.io.InputStreamReader;
b96910
+import java.nio.file.Files;
b96910
+import java.nio.file.Path;
b96910
+import java.nio.file.Paths;
b96910
 import java.util.Arrays;
b96910
-import java.util.Collection;
b96910
+import java.util.LinkedHashMap;
b96910
+import java.util.List;
b96910
+import java.util.Map;
b96910
 import java.util.Properties;
b96910
 
b96910
 import org.slf4j.Logger;
b96910
@@ -35,49 +44,93 @@ class ArtifactInstallerFactory
b96910
 
b96910
     private final ArtifactInstaller defaultArtifactInstaller;
b96910
 
b96910
-    private final ArtifactInstaller eclipseArtifactInstaller;
b96910
+    private final IsolatedClassRealm pluginRealm;
b96910
 
b96910
-    private static ArtifactInstaller loadPlugin( String className )
b96910
+    private final Map<String, ArtifactInstaller> cachedPluginsByType = new LinkedHashMap<>();
b96910
+
b96910
+    private final Map<String, ArtifactInstaller> cachedPluginsByImplClass = new LinkedHashMap<>();
b96910
+
b96910
+    private ArtifactInstaller tryLoadPlugin( String type )
b96910
     {
b96910
+        if ( cachedPluginsByType.containsKey( type ) )
b96910
+            return cachedPluginsByType.get( type );
b96910
+
b96910
         try
b96910
         {
b96910
-            return (ArtifactInstaller) ArtifactInstallerFactory.class.getClassLoader().loadClass( className ).newInstance();
b96910
+            String resourceName = ArtifactInstaller.class.getCanonicalName() + "/" + type;
b96910
+            InputStream resourceStream = pluginRealm != null ? pluginRealm.getResourceAsStream( resourceName ) : null;
b96910
+            if ( resourceStream == null )
b96910
+            {
b96910
+                logger.debug( "No XMvn Installer plugin found for packaging type {}", type );
b96910
+                cachedPluginsByType.put( type, null );
b96910
+                return null;
b96910
+            }
b96910
+
b96910
+            String pluginImplClass;
b96910
+            try ( BufferedReader resourceReader = new BufferedReader( new InputStreamReader( resourceStream ) ) )
b96910
+            {
b96910
+                pluginImplClass = resourceReader.readLine();
b96910
+            }
b96910
+
b96910
+            ArtifactInstaller pluggedInInstaller = cachedPluginsByImplClass.get( pluginImplClass );
b96910
+            if ( pluggedInInstaller == null )
b96910
+            {
b96910
+                pluggedInInstaller = (ArtifactInstaller) pluginRealm.loadClass( pluginImplClass ).newInstance();
b96910
+                cachedPluginsByImplClass.put( pluginImplClass, pluggedInInstaller );
b96910
+            }
b96910
+
b96910
+            cachedPluginsByType.put( type, pluggedInInstaller );
b96910
+            return pluggedInInstaller;
b96910
         }
b96910
-        catch ( ReflectiveOperationException e )
b96910
+        catch ( IOException | ReflectiveOperationException e )
b96910
         {
b96910
-            return null;
b96910
+            throw new RuntimeException( "Unable to load XMvn Installer plugin for packaging type " + type, e );
b96910
         }
b96910
     }
b96910
 
b96910
     public ArtifactInstallerFactory( Configurator configurator )
b96910
     {
b96910
         defaultArtifactInstaller = new DefaultArtifactInstaller( configurator );
b96910
-        // FIXME Don't hardcode plugin class name
b96910
-        eclipseArtifactInstaller = loadPlugin( "org.fedoraproject.p2.xmvn.EclipseArtifactInstaller" );
b96910
+
b96910
+        Path pluginDir = Paths.get( "/usr/share/xmvn/lib/installer" );
b96910
+        if ( Files.isDirectory( pluginDir ) )
b96910
+        {
b96910
+            ClassLoader parentClassLoader = ArtifactInstallerFactory.class.getClassLoader();
b96910
+            pluginRealm = new IsolatedClassRealm( parentClassLoader );
b96910
+            pluginRealm.addJarDirectory( pluginDir );
b96910
+            PLUGIN_IMPORTS.forEach( pluginRealm::importPackage );
b96910
+        }
b96910
+        else
b96910
+        {
b96910
+            pluginRealm = null;
b96910
+        }
b96910
     }
b96910
 
b96910
     /**
b96910
-     * List of Tycho pacgkaging types.
b96910
+     * List of packages imported from XMvn Installer class loader to plug-in realms.
b96910
      */
b96910
-    private static final Collection<String> ECLIPSE_PACKAGING_TYPES = Arrays.asList( "eclipse-plugin", //
b96910
-                                                                                     "eclipse-test-plugin", //
b96910
-                                                                                     "eclipse-feature", //
b96910
-                                                                                     "eclipse-repository", //
b96910
-                                                                                     "eclipse-application", //
b96910
-                                                                                     "eclipse-update-site", //
b96910
-                                                                                     "eclipse-target-definition" );
b96910
+    private static final List<String> PLUGIN_IMPORTS = Arrays.asList( // XMvn API
b96910
+                                                                      "org.fedoraproject.xmvn.artifact", //
b96910
+                                                                      "org.fedoraproject.xmvn.config", //
b96910
+                                                                      "org.fedoraproject.xmvn.deployer", //
b96910
+                                                                      "org.fedoraproject.xmvn.locator", //
b96910
+                                                                      "org.fedoraproject.xmvn.metadata", //
b96910
+                                                                      "org.fedoraproject.xmvn.resolver", //
b96910
+                                                                      // XMvn Installer SPI
b96910
+                                                                      "org.fedoraproject.xmvn.tools.install", //
b96910
+                                                                      // SLF4J API
b96910
+                                                                      "org.slf4j" //
b96910
+    );
b96910
 
b96910
     @SuppressWarnings( "unused" )
b96910
     public ArtifactInstaller getInstallerFor( Artifact artifact, Properties properties )
b96910
     {
b96910
         String type = properties.getProperty( "type" );
b96910
-        if ( type != null && ECLIPSE_PACKAGING_TYPES.contains( type ) )
b96910
+        if ( type != null )
b96910
         {
b96910
-            if ( eclipseArtifactInstaller != null )
b96910
-                return eclipseArtifactInstaller;
b96910
-
b96910
-            logger.error( "Unable to load XMvn P2 plugin, Eclipse artifact installation will be impossible" );
b96910
-            throw new RuntimeException( "Unable to load XMvn P2 plugin" );
b96910
+            ArtifactInstaller pluggedInInstaller = tryLoadPlugin( type );
b96910
+            if ( pluggedInInstaller != null )
b96910
+                return pluggedInInstaller;
b96910
         }
b96910
 
b96910
         return defaultArtifactInstaller;
b96910
diff --git a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/IsolatedClassRealm.java b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/IsolatedClassRealm.java
b96910
new file mode 100644
b96910
index 0000000..3324604
b96910
--- /dev/null
b96910
+++ b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/IsolatedClassRealm.java
b96910
@@ -0,0 +1,245 @@
b96910
+/*-
b96910
+ * Copyright (c) 2014-2017 Red Hat, Inc.
b96910
+ *
b96910
+ * Licensed under the Apache License, Version 2.0 (the "License");
b96910
+ * you may not use this file except in compliance with the License.
b96910
+ * You may obtain a copy of the License at
b96910
+ *
b96910
+ *     http://www.apache.org/licenses/LICENSE-2.0
b96910
+ *
b96910
+ * Unless required by applicable law or agreed to in writing, software
b96910
+ * distributed under the License is distributed on an "AS IS" BASIS,
b96910
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b96910
+ * See the License for the specific language governing permissions and
b96910
+ * limitations under the License.
b96910
+ */
b96910
+package org.fedoraproject.xmvn.tools.install.impl;
b96910
+
b96910
+import java.io.IOException;
b96910
+import java.net.MalformedURLException;
b96910
+import java.net.URL;
b96910
+import java.net.URLClassLoader;
b96910
+import java.nio.file.DirectoryStream;
b96910
+import java.nio.file.Files;
b96910
+import java.nio.file.Path;
b96910
+import java.util.Collection;
b96910
+import java.util.Collections;
b96910
+import java.util.Enumeration;
b96910
+import java.util.HashSet;
b96910
+import java.util.LinkedHashSet;
b96910
+import java.util.Set;
b96910
+
b96910
+/**
b96910
+ * A generic, isolated class loader.
b96910
+ * 

b96910
+ * This class loader has its own classpath, separate from the primary Java classpath. It has a parent class loader, to
b96910
+ * which it delegates loading a set of imported classes. All other classes are loaded from its own classpath.
b96910
+ * 
b96910
+ * @author Mikolaj Izdebski
b96910
+ */
b96910
+class IsolatedClassRealm
b96910
+    extends URLClassLoader
b96910
+{
b96910
+    static
b96910
+    {
b96910
+        registerAsParallelCapable();
b96910
+    }
b96910
+
b96910
+    private final ClassLoader parent;
b96910
+
b96910
+    private final Set<String> imports = new HashSet<>();
b96910
+
b96910
+    private final Set<String> importsAll = new HashSet<>();
b96910
+
b96910
+    public IsolatedClassRealm( ClassLoader parent )
b96910
+    {
b96910
+        super( new URL[0], null );
b96910
+        this.parent = parent;
b96910
+    }
b96910
+
b96910
+    public void addJar( Path jar )
b96910
+    {
b96910
+        try
b96910
+        {
b96910
+            addURL( jar.toUri().toURL() );
b96910
+        }
b96910
+        catch ( MalformedURLException e )
b96910
+        {
b96910
+            throw new RuntimeException( e );
b96910
+        }
b96910
+    }
b96910
+
b96910
+    public void addJarDirectory( Path dir )
b96910
+    {
b96910
+        try ( DirectoryStream<Path> stream = Files.newDirectoryStream( dir, "*.jar" ) )
b96910
+        {
b96910
+            for ( Path path : stream )
b96910
+            {
b96910
+                addJar( path );
b96910
+            }
b96910
+        }
b96910
+        catch ( IOException e )
b96910
+        {
b96910
+            throw new RuntimeException( e );
b96910
+        }
b96910
+    }
b96910
+
b96910
+    public void importPackage( String packageName )
b96910
+    {
b96910
+        imports.add( packageName );
b96910
+    }
b96910
+
b96910
+    public void importAllPackages( String packageName )
b96910
+    {
b96910
+        importsAll.add( packageName );
b96910
+    }
b96910
+
b96910
+    boolean isImported( String name )
b96910
+    {
b96910
+        int index = name.lastIndexOf( '/' );
b96910
+
b96910
+        if ( index >= 0 )
b96910
+        {
b96910
+            name = name.replace( '/', '.' );
b96910
+        }
b96910
+        else
b96910
+        {
b96910
+            index = Math.max( name.lastIndexOf( '.' ), 0 );
b96910
+        }
b96910
+
b96910
+        String namespace = name.substring( 0, index );
b96910
+
b96910
+        if ( imports.contains( namespace ) )
b96910
+            return true;
b96910
+
b96910
+        while ( !namespace.isEmpty() )
b96910
+        {
b96910
+            if ( importsAll.contains( namespace ) )
b96910
+                return true;
b96910
+
b96910
+            namespace = namespace.substring( 0, Math.max( namespace.lastIndexOf( '.' ), 0 ) );
b96910
+        }
b96910
+
b96910
+        return false;
b96910
+    }
b96910
+
b96910
+    @Override
b96910
+    public Class loadClass( String name )
b96910
+        throws ClassNotFoundException
b96910
+    {
b96910
+        return loadClass( name, false );
b96910
+    }
b96910
+
b96910
+    @Override
b96910
+    protected Class loadClass( String name, boolean resolve )
b96910
+        throws ClassNotFoundException
b96910
+    {
b96910
+        if ( isImported( name ) )
b96910
+        {
b96910
+            try
b96910
+            {
b96910
+                return parent.loadClass( name );
b96910
+            }
b96910
+            catch ( ClassNotFoundException e )
b96910
+            {
b96910
+            }
b96910
+        }
b96910
+
b96910
+        try
b96910
+        {
b96910
+            return super.loadClass( name, resolve );
b96910
+        }
b96910
+        catch ( ClassNotFoundException e )
b96910
+        {
b96910
+        }
b96910
+
b96910
+        synchronized ( getClassLoadingLock( name ) )
b96910
+        {
b96910
+            Class clazz = findLoadedClass( name );
b96910
+            if ( clazz != null )
b96910
+            {
b96910
+                return clazz;
b96910
+            }
b96910
+
b96910
+            try
b96910
+            {
b96910
+                return super.findClass( name );
b96910
+            }
b96910
+            catch ( ClassNotFoundException e )
b96910
+            {
b96910
+            }
b96910
+        }
b96910
+
b96910
+        throw new ClassNotFoundException( name );
b96910
+    }
b96910
+
b96910
+    @Override
b96910
+    protected Class findClass( String name )
b96910
+        throws ClassNotFoundException
b96910
+    {
b96910
+        throw new ClassNotFoundException( name );
b96910
+    }
b96910
+
b96910
+    @Override
b96910
+    public URL getResource( String name )
b96910
+    {
b96910
+        if ( isImported( name ) )
b96910
+        {
b96910
+            URL resource = parent.getResource( name );
b96910
+            if ( resource != null )
b96910
+            {
b96910
+                return resource;
b96910
+            }
b96910
+        }
b96910
+
b96910
+        URL resource = super.getResource( name );
b96910
+        if ( resource != null )
b96910
+        {
b96910
+            return resource;
b96910
+        }
b96910
+
b96910
+        resource = super.findResource( name );
b96910
+        if ( resource != null )
b96910
+        {
b96910
+            return resource;
b96910
+        }
b96910
+
b96910
+        return null;
b96910
+    }
b96910
+
b96910
+    @Override
b96910
+    public Enumeration<URL> getResources( String name )
b96910
+        throws IOException
b96910
+    {
b96910
+        Collection<URL> resources = new LinkedHashSet<>();
b96910
+
b96910
+        if ( isImported( name ) )
b96910
+        {
b96910
+            try
b96910
+            {
b96910
+                resources.addAll( Collections.list( parent.getResources( name ) ) );
b96910
+            }
b96910
+            catch ( IOException e )
b96910
+            {
b96910
+            }
b96910
+        }
b96910
+
b96910
+        try
b96910
+        {
b96910
+            resources.addAll( Collections.list( super.getResources( name ) ) );
b96910
+        }
b96910
+        catch ( IOException e )
b96910
+        {
b96910
+        }
b96910
+
b96910
+        try
b96910
+        {
b96910
+            resources.addAll( Collections.list( super.findResources( name ) ) );
b96910
+        }
b96910
+        catch ( IOException e )
b96910
+        {
b96910
+        }
b96910
+
b96910
+        return Collections.enumeration( resources );
b96910
+    }
b96910
+}
b96910
-- 
b96910
2.9.3
b96910