incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amitg...@apache.org
Subject svn commit: r1571975 - in /sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared: LauncherClassLoader.java Loader.java
Date Wed, 26 Feb 2014 09:26:17 GMT
Author: amitgupt
Date: Wed Feb 26 09:26:16 2014
New Revision: 1571975

URL: http://svn.apache.org/r1571975
Log:
SLING-3370 - Bootdelegation support for third party library

Applied patch with some modifications, Thanks Rohit

Modified:
    sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/LauncherClassLoader.java
    sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Loader.java

Modified: sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/LauncherClassLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/LauncherClassLoader.java?rev=1571975&r1=1571974&r2=1571975&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/LauncherClassLoader.java
(original)
+++ sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/LauncherClassLoader.java
Wed Feb 26 09:26:16 2014
@@ -23,8 +23,10 @@ import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
@@ -73,25 +75,41 @@ public class LauncherClassLoader extends
      */
     private final Set<String> launcherPackages;
 
-    LauncherClassLoader(File launcherJar) throws MalformedURLException {
+    LauncherClassLoader(File launcherJar, File[] extJars) throws MalformedURLException {
         super(new URL[] { launcherJar.toURI().toURL() },
             LauncherClassLoader.class.getClassLoader());
 
         Set<String> collectedPackages = new HashSet<String>();
 
+        //process launcher jar
+        processJarPackages(launcherJar, collectedPackages);
+
+        //process extension jars
+        List<File> extJarFileList = getExtJarFileList(extJars);
+
+        //add external jars to classloader
+        for(File extJarFile:extJarFileList){
+            addURL(extJarFile.toURI().toURL());
+            processJarPackages(extJarFile, collectedPackages);
+        }
+
+        launcherPackages = collectedPackages;
+    }
+
+    private void processJarPackages(File jarFile, Set<String> packageSet ){
         JarFile jar = null;
         try {
-            jar = new JarFile(launcherJar, false);
+            jar = new JarFile(jarFile, false);
             Enumeration<JarEntry> entries = jar.entries();
             while (entries.hasMoreElements()) {
                 String entryName = entries.nextElement().getName();
                 if (entryName.endsWith(".class")
-                    && !entryName.startsWith("META-INF/")
-                    && !entryName.startsWith("javax/")) {
+                        && !entryName.startsWith("META-INF/")
+                        && !entryName.startsWith("javax/")) {
                     String packageName = getPackageName(entryName, '/');
                     if (packageName != null
-                        && collectedPackages.add(packageName)) {
-                        collectedPackages.add(packageName.replace('/', '.'));
+                            && packageSet.add(packageName)) {
+                        packageSet.add(packageName.replace('/', '.'));
                     }
                 }
             }
@@ -105,8 +123,17 @@ public class LauncherClassLoader extends
                 }
             }
         }
+    }
 
-        launcherPackages = collectedPackages;
+
+    private List<File> getExtJarFileList(File[] extJars) throws MalformedURLException
{
+        List<File> jarList = new ArrayList<File>();
+        for (File extJarFile : extJars) {
+            if (extJarFile != null && extJarFile.exists()) {
+                jarList.add(extJarFile);
+            }
+        }
+        return jarList;
     }
 
     /**

Modified: sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Loader.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Loader.java?rev=1571975&r1=1571974&r2=1571975&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Loader.java
(original)
+++ sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Loader.java
Wed Feb 26 09:26:16 2014
@@ -22,6 +22,7 @@ import java.beans.Introspector;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileOutputStream;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -50,6 +51,13 @@ public class Loader {
      */
     private final File launchpadHome;
 
+    private final File extLibHome;
+
+    /**
+     * Default External Library Home
+     */
+    private static final String EXTENSION_LIB_PATH="ext";
+
     /**
      * Creates a loader instance to load from the given launchpad home folder.
      * Besides ensuring the existence of the launchpad home folder, the
@@ -70,6 +78,7 @@ public class Loader {
         }
 
         this.launchpadHome = getLaunchpadHomeFile(launchpadHome);
+        extLibHome = getExtensionLibHome();
         removeOldLauncherJars();
     }
 
@@ -98,10 +107,10 @@ public class Loader {
 
         final ClassLoader loader;
         try {
-            loader = new LauncherClassLoader(launcherJarFile);
+            loader = new LauncherClassLoader(launcherJarFile, getExtLibs());
         } catch (MalformedURLException e) {
             throw new IllegalArgumentException(
-                "Cannot create an URL from the Sling Launcher JAR path name", e);
+                "Cannot create an URL from the  JAR path name", e);
         }
 
         try {
@@ -220,9 +229,6 @@ public class Loader {
     /**
      * Removes old candidate launcher JAR files leaving the most recent one as
      * the launcher JAR file to use on next Sling startup.
-     *
-     * @param launchpadHome The Sling home directory location containing the
-     *            candidate launcher JAR files.
      */
     private void removeOldLauncherJars() {
         final File[] launcherJars = getLauncherJarFiles();
@@ -319,8 +325,6 @@ public class Loader {
      * {@link SharedConstants#LAUNCHER_JAR_REL_PATH}. This list may be empty if
      * the launcher JAR file has not been installed yet.
      *
-     * @param launchpadHome The sling home directory where the launcher JAR
-     *            files are stored
      * @return The list of candidate launcher JAR files, which may be empty.
      *         <code>null</code> is returned if an IO error occurs trying to
      *         list the files.
@@ -402,4 +406,48 @@ public class Loader {
     /** Meant to be overridden to display or log info */
     protected void info(String msg) {
     }
+
+    private File getExtensionLibHome(){
+        //check if sling home is initialized
+        if(launchpadHome == null || !launchpadHome.exists()){
+            throw new IllegalArgumentException("Sling Home  has not been initialized" );
+        }
+        //assumes launchpadHome is initialized
+        File extLibFile=new File(launchpadHome, EXTENSION_LIB_PATH);
+        if (extLibFile.exists()) {
+            if (!extLibFile.isDirectory()) {
+                throw new IllegalArgumentException("Sling  Extension Lib Home " + extLibFile
+                        + " exists but is not a directory");
+            }
+        }
+
+        info("Sling  Extension Lib Home : " + extLibFile);
+        return extLibFile;
+    }
+
+    private File[] getExtLibs(){
+        if (extLibHome == null || !extLibHome.exists()) {
+            info("External Libs Home (ext) is null or does not exists.");
+            return new File[]{};
+        }
+        File[] libs = extLibHome.listFiles(new FilenameFilter() {
+            public boolean accept(File dir, String name) {
+                return (name.endsWith(".jar"));
+            }
+        });
+
+        if (libs == null) {
+            libs = new File[]{};
+        }
+        StringBuilder logStringBldr = new StringBuilder("Sling Extension jars found = [ ");
+
+        for(File lib:libs){
+            logStringBldr.append(lib);
+            logStringBldr.append(",");
+        }
+
+        logStringBldr.append(" ] ");
+        info(logStringBldr.toString());
+        return libs;
+    }
 }



Mime
View raw message