Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.shared » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   package org.apache.sling.launchpad.base.shared;
   20   
   21   import java.io.File;
   22   import java.io.IOException;
   23   import java.net.MalformedURLException;
   24   import java.net.URL;
   25   import java.net.URLClassLoader;
   26   import java.util.Enumeration;
   27   import java.util.HashSet;
   28   import java.util.Set;
   29   import java.util.jar.JarEntry;
   30   import java.util.jar.JarFile;
   31   
   32   /**
   33    * The <code>LauncherClassLoader</code> extends the standard Java VM
   34    * <code>URLClassLoader</code> such, that classes and resources which are
   35    * contained in the launcher JAR File are never looked up in the parent class
   36    * loader.
   37    * <p>
   38    * This class loader shields the Sling OSGi framework completely from the
   39    * environment because
   40    * <ul>
   41    * <li>Classes and resources contained in packages provided by the launcher JAR
   42    * file are only looked up in the launcher JAR file</li>
   43    * <li>Classes and resources from the environment are only used from within the
   44    * framework if the framework is configured to do so by the
   45    * <code>org.osgi.framework.bootdelegation</code> or
   46    * <code>org.osgi.framework.systempackages</code> framework properties.</li>
   47    * </ul>
   48    * <p>
   49    * The first point is important if Sling is deployed into any container, which
   50    * provides some or all of the same packages as the Sling launcher JAR file. One
   51    * such example is the Glassfish v3 Prelude application service, which itself
   52    * runs in a Felix OSGi framework and sort of leaks the classes into the web
   53    * application.
   54    * <p>
   55    * In the general case, we cannot prevent any leaking of classes, which may also
   56    * be the OSGi core or compendium libraries, into Sling. So, this class loader
   57    * is the barrier for this leaking and shields Sling from the environment unless
   58    * explicitly configured to use this leaking.
   59    * <p>
   60    * Instances of this class loader are setup with the launcher JAR file as the
   61    * only contents of the <code>URLClassLoaders</code> class path and the class
   62    * loader of this class itself as the parent class loader.
   63    */
   64   public class LauncherClassLoader extends URLClassLoader {
   65   
   66       /**
   67        * Set of packages never to be used from the environment. Each package is
   68        * contained in this set in two forms: The Java package form where the
   69        * segments are separated by dots, e.g. org.osgi.framework, and the resource
   70        * form where the segments are separated by slash, e.g. org/osgi/framework.
   71        * This makes checking packages for classes and resources equaly simple
   72        * without requiring name mangling.
   73        */
   74       private final Set<String> launcherPackages;
   75   
   76       LauncherClassLoader(File launcherJar) throws MalformedURLException {
   77           super(new URL[] { launcherJar.toURI().toURL() },
   78               LauncherClassLoader.class.getClassLoader());
   79   
   80           Set<String> collectedPackages = new HashSet<String>();
   81   
   82           JarFile jar = null;
   83           try {
   84               jar = new JarFile(launcherJar, false);
   85               Enumeration<JarEntry> entries = jar.entries();
   86               while (entries.hasMoreElements()) {
   87                   String entryName = entries.nextElement().getName();
   88                   if (entryName.endsWith(".class")
   89                       && !entryName.startsWith("META-INF/")
   90                       && !entryName.startsWith("javax/")) {
   91                       String packageName = getPackageName(entryName, '/');
   92                       if (packageName != null
   93                           && collectedPackages.add(packageName)) {
   94                           collectedPackages.add(packageName.replace('/', '.'));
   95                       }
   96                   }
   97               }
   98           } catch (IOException ioe) {
   99               // might log or throw, don't know ??
  100           } finally {
  101               if (jar != null) {
  102                   try {
  103                       jar.close();
  104                   } catch (IOException ignore) {
  105                   }
  106               }
  107           }
  108   
  109           launcherPackages = collectedPackages;
  110       }
  111   
  112       /**
  113        * Load the name class and optionally resolve it, if found.
  114        * <p>
  115        * This method checks whether the package of the class is contained in the
  116        * launcher JAR file. If so, the launcher JAR file is looked up for the
  117        * class and class loading fails if not found. Otherwise the standard class
  118        * loading strategy is applied by calling the base class implementation.
  119        */
  120       @Override
  121       protected synchronized Class<?> loadClass(String name, boolean resolve)
  122               throws ClassNotFoundException {
  123           // First, check if the class has already been loaded
  124           Class<?> c = findLoadedClass(name);
  125           if (c == null) {
  126               if (containsPackage(name, '.')) {
  127                   // finds the class or throws a ClassNotFoundException if
  128                   // the class cannot be found, which is ok, since we only
  129                   // want the class from our jar file, if it contains the
  130                   // package.
  131                   c = findClass(name);
  132               } else {
  133                   return super.loadClass(name, resolve);
  134               }
  135           }
  136   
  137           if (resolve) {
  138               resolveClass(c);
  139           }
  140   
  141           return c;
  142       }
  143   
  144       /**
  145        * Return an URL to the requested resource.
  146        * <p>
  147        * This method checks whether the package of the resource is contained in
  148        * the launcher JAR file. If so, the launcher JAR file is looked up for the
  149        * resource and resource access fails if not found. Otherwise the standard
  150        * resource access strategy is applied by calling the base class
  151        * implementation.
  152        */
  153       @Override
  154       public URL getResource(String name) {
  155   
  156           // if the package of the name is contained in our jar file
  157           // file, return the resource or nothing
  158           if (containsPackage(name, '/')) {
  159               return findResource(name);
  160           }
  161   
  162           // try parent class loader only after having checked our packages
  163           return super.getResource(name);
  164       }
  165   
  166       /**
  167        * Returns the name of the package of the fully qualified name using the
  168        * given <code>separator</code> as the segment separator. If the
  169        * <code>name</code> does not contain the separator, the name is contained
  170        * in the root package and this method returns <code>null</code>.
  171        * <p>
  172        * Example: Called for <i>org.osgi.framework.Bundle</i> this method returns
  173        * <i>org.osgi.framework</i>.
  174        *
  175        * @param name The fully qualified name of the class or resource to check
  176        * @param separator The separator for package segments
  177        */
  178       private String getPackageName(String name, int separator) {
  179           int speIdx = name.lastIndexOf(separator);
  180           return (speIdx > 0) ? name.substring(0, speIdx) : null;
  181       }
  182   
  183       /**
  184        * Returns <code>true</code> if the launcher JAR file provides the package
  185        * to which the named class or resource belongs.
  186        *
  187        * @param name The fully qualified name of the class or resource to check
  188        * @param separator The separator for package segments
  189        */
  190       private boolean containsPackage(String name, int separator) {
  191           String packageName = getPackageName(name, separator);
  192           return (packageName == null)
  193                   ? false
  194                   : launcherPackages.contains(packageName);
  195       }
  196   }

Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.shared » [javadoc | source]