avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon-sandbox/merlin-jndi/src/java/org/apache/avalon/merlin/jndi ExceptionHelper.java Service.java ServiceFactory.java
Date Sat, 08 Mar 2003 04:22:07 GMT
mcconnell    2003/03/07 20:22:07

  Added:       merlin-jndi .cvsignore build.xml default.properties
               merlin-jndi/src/etc project.mf
               merlin-jndi/src/java/org/apache/avalon/merlin/jndi
                        ExceptionHelper.java Service.java
                        ServiceFactory.java
  Log:
  Initial commit of a Merlin to JNDI connector.
  
  Revision  Changes    Path
  1.1                  avalon-sandbox/merlin-jndi/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  build
  dist
  distributions
  merlin
  ant.properties
  checkstyle.cache
  velocity.log
  project.xml
  project.properties
  maven.log
  maven.xml
  target
  xdocs
  
  
  1.1                  avalon-sandbox/merlin-jndi/build.xml
  
  Index: build.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <!--  
  Copyright (C) The Apache Software Foundation. All rights reserved.
  This software is published under the terms of the Apache Software License
  version 1.1, a copy of which has been included with this distribution in
  the LICENSE.txt file.
  
  @author  Avalon Development Team
  @version 1.0 12/03/2001
  -->
  
  <project name="merlin-jndi" default="install" basedir=".">
  
    <property file="${basedir}/../ant.properties"/>
    <property file="${basedir}/ant.properties"/>
    <property file="${user.home}/.ant.properties"/>
    <property file="${basedir}/../default.properties"/>
    <property file="${basedir}/default.properties"/>
  
    <!-- Classpath for product -->
    <path id="project.class.path">
      <pathelement path="${java.class.path}"/>
      <fileset dir="lib">
        <include name="*.jar"/>
      </fileset>
    </path>
  
    <target name="main" depends="jar" description="Build the JNDI connector."/>
    <target name="rebuild" depends="clean,main" description="Rebuild the project."/>
  
    <target name="context" depends="dependencies">
      <mkdir dir="${lib.dir}"/>
      <uptodate property="uptodate" targetfile="${build.lib}/${jar.name}">
        <srcfiles dir="${src.dir}">
          <include name="**/*.*"/>
        </srcfiles>
        <srcfiles dir="${lib.dir}">
          <include name="**/*.*"/>
        </srcfiles>
      </uptodate>
    </target>
  
    <target name="dependencies" depends="check-jndi"/>
  
    <target name="flags">
          <available property="jndi.present"
                 classname="javax.naming.spi.ObjectFactory"
                 classpathref="project.class.path" />
    </target>
  
    <target name="check-jndi" depends="flags" unless="jndi.present">
        <echo>You must download the JNDI package and place it in the local ./lib directory.
</echo>
    </target>
  
    <!-- Compiles the bootstrap source code -->
    <target name="compile" depends="context" 
         description="Compiles the bootstrap source code" unless="uptodate" if="jndi.present"
>
      
      <echo message="compiling jndi connector"/>
      <mkdir dir="${build.classes}"/>
  
      <javac srcdir="${java.dir}"
         destdir="${build.classes}"
         debug="${build.debug}"
         optimize="${build.optimize}"
         deprecation="${build.deprecation}"
         target="1.2">
        <src path="${java.dir}" />
        <classpath refid="project.class.path" />
      </javac>
  
    </target>
  
    <!--
    Creation of the jndi connector jar file. 
    -->
  
    <target name="jar" depends="compile" 
        description="Generates the jar files" unless="uptodate">
  
      <mkdir dir="${build.lib}"/>
  
      <copy todir="${build.classes}">
        <fileset dir="${java.dir}">
          <include name="*.properties"/>
        </fileset>
      </copy>
  
      <jar jarfile="${build.lib}/${jar.name}"
         basedir="${build.classes}"
         compress="${build.compress}" manifest="${src.dir}/etc/project.mf">
      </jar>
  
    </target>
  
    <target name="install" depends="jar">
        <mkdir dir="${local.repository}"/>
        <copy toDir="${local.repository}" file="${build.lib}/${jar.name}"/>
    </target>
  
    <!-- Creates all the Javadocs -->
    <target name="javadocs" depends="compile" 
            description="Generates the javadocs" unless="skip.javadocs">
      <mkdir dir="${build.javadocs}"/>
      <javadoc sourcepath="${java.dir}" packagenames="*,*.*"
             destdir="${build.javadocs}" overview="${java.dir}/package.html">
        <classpath refid="project.class.path" />
        <doclet name="com.sun.tools.doclets.standard.Standard">
          <param name="-author"/>
          <param name="-version"/>
          <param name="-doctitle" value="${Name}"/>
          <param name="-windowtitle" value="${Name} API"/>
          <param name="-link" value="http://java.sun.com/j2se/1.4/docs/api/"/>
          <param name="-link" value="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/"/>
          <param name="-bottom"
             value="&quot;Copyright &#169; ${year} Apache Avalon Project. All Rights
Reserved.&quot;"/>
        </doclet>
      </javadoc>
    </target>
  
    <!-- Cleans up build and distribution directories -->
    <target name="clean" description="Cleans up the project">
      <delete dir="${build.dir}" />
    </target>
  
  </project>
  
  
  
  1.1                  avalon-sandbox/merlin-jndi/default.properties
  
  Index: default.properties
  ===================================================================
  # -------------------------------------------------------------------
  # B U I L D  P R O P E R T I E S
  # -------------------------------------------------------------------
  # Specifies default property values
  # Overridden by ../default.properties and all ant.properties
  # Not user-editable; use ant.properties files instead
  
  family=merlin
  codename=merlin-jndi
  name=${codename}
  Name=Merlin JNDI COnnector
  dir-name=merlin-jndi
  version=1.0
  package-version=1.0
  year=2003
  
  local.repository = ../lib
  remote.repository = http://www.osm.net/repository
  
  # --------------------------------------------------
  #  directory structure
  # --------------------------------------------------
  
  src.dir = src
  java.dir = ${src.dir}/java
  etc.dir = ${src.dir}/etc
  lib.dir = lib
  dist.dir = dist
  
  # --------------------------------------------------
  
  #  Settings used to configure compile environment
  build.debug = off
  build.optimize = on
  build.deprecation = on
  build.compress = true
  
  #  location of intermediate products
  build.dir = build
  build.lib = ${build.dir}/lib
  build.classes = ${build.dir}/classes
  build.docs = ${build.dir}/docs
  build.javadocs = ${build.docs}/api
  
  #  name of jar file
  jar.name = ${name}-${package-version}.jar
  
  
  
  1.1                  avalon-sandbox/merlin-jndi/src/etc/project.mf
  
  Index: project.mf
  ===================================================================
  Manifest-Version: 1.0
  Created-By: Apache Avalon
  Extension-Name: merlin-jndi
  Specification-Vendor: Apache Software Foundation
  Specification-Version: 1.0
  Implementation-Vendor: Apache Software Foundation
  Implementation-Vendor-Id: ASF
  Implementation-Version: 1.0
  
  
  
  1.1                  avalon-sandbox/merlin-jndi/src/java/org/apache/avalon/merlin/jndi/ExceptionHelper.java
  
  Index: ExceptionHelper.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
      "Apache Software Foundation"  must not be used to endorse or promote
      products derived  from this  software without  prior written
      permission. For written permission, please contact apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.avalon.merlin.jndi;
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.lang.reflect.Method;
  import java.util.StringTokenizer;
  
  /**
   * General utilities supporting the packaging of exception messages.
   * @author <a href="mailto:mcconnell@osm.net">Stephen McConnell</a>
   */
  class ExceptionHelper
  {
      static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
  
      /**
       * Prints the exception to the standard error out together with
       * cause statements.
       * @param e the exception to print
       */
      public static void printException( Throwable e )
      {
          printException( null, e );
      }
  
      /**
       * Prints the exception to the standard error out together with
       * cause statements.
       * @param label label identifying the error
       * @param e the exception to print
       */
      public static void printException( String label, Throwable e )
      {
          printException( label, e, null );
      }
  
      /**
       * Prints the exception to the standard error out together with
       * source and cause statements.
       * @param label label identifying the error
       * @param e the exception to print
       * @param source the source of the request
       */
      public static void printException( String label, Throwable e, Object source )
      {
          printException( label, e, source, false );
      }
  
      /**
       * Prints the exception to the standard error out together with
       * source and cause statements.
       * @param label label identifying the error
       * @param e the exception to print
       * @param source the source of the request
       * @param trace boolean value inidication if a stack trace though be included
       */
      public static void printException( String label, Throwable e, Object source, boolean
trace )
      {
          if( e == null )
          {
              throw new NullPointerException( "throwable" );
          }
          java.io.PrintStream out = System.err;
          synchronized( out )
          {
              out.println( "==================================================================="
);
              if( label != null )
              {
                  out.println( "Message: " + label );
              }
              if( source != null )
              {
                  out.println( "Source: " + source );
              }
              out.println( "Exception: " + e.toString() );
              if( getCause( e ) != null )
              {
                  printCause( out, e );
              }
              out.println( "==================================================================="
);
              if( trace )
              {
                  Throwable root = getLastThrowable( e );
                  root.printStackTrace();
                  out.println( "==================================================================="
);
              }
          }
      }
  
      /**
       * Returns the exception and causal exceptions as a formatted string.
       * @param message the header message
       * @param e the exception
       * @return String the formatting string
       */
      public static String packException( final String message, final Throwable e )
      {
          return packException( message, e, true );
      }
  
      /**
       * Returns the exception and causal exceptions as a formatted string.
       * @param message the header message
       * @param e the exception
       * @return String the formatting string
       */
      public static String packException( final String message, final Throwable e, boolean
stack )
      {
          StringBuffer buffer = new StringBuffer();
          buffer.append( "Message: " + message );
          buffer.append( "\n==================================================================="
);
          if( e == null )
          {
              return buffer.toString();
          }
          else
          {
              buffer.append( "\n\nException: " + e.getClass().getName() );
              buffer.append( "\n" + e.getMessage() );
              packCause( buffer, getCause( e ) ).toString();
              buffer.append( "\n\n==================================================================="
);
          }
          Throwable root = getLastThrowable( e );
          if(( root != null ) && stack )
          {
              String[] trace = captureStackTrace( root );
              for( int i = 0; i < trace.length; i++ )
              {
                  buffer.append( "\n" + trace[ i ] );
              }
          }
          buffer.append( "\n==================================================================="
);
          return buffer.toString();
      }
  
      private static StringBuffer packCause( StringBuffer buffer, Throwable cause )
      {
          if( cause == null )
          {
              return buffer;
          }
          buffer.append( "\n\nCause: " + cause.getClass().getName() );
          buffer.append( "\n" + cause.getMessage() );
          return packCause( buffer, getCause( cause ) );
      }
  
      private static Throwable getLastThrowable( Throwable exception )
      {
          Throwable cause = getCause( exception );
          if( cause != null )
          {
              return getLastThrowable( cause );
          }
          return exception;
      }
  
      private static Throwable getCause( Throwable exception )
      {
          if( exception == null )
          {
              throw new NullPointerException( "exception" );
          }
  
          try
          {
              Method method = exception.getClass().getMethod( "getCause", new Class[ 0 ] );
              return (Throwable)method.invoke( exception, new Object[ 0 ] );
          }
          catch( Throwable e )
          {
              return null;
          }
      }
  
      /**
       * Captures the stack trace associated with this exception.
       *
       * @param throwable a <code>Throwable</code>
       * @return an array of Strings describing stack frames.
       */
      private static String[] captureStackTrace( final Throwable throwable )
      {
          final StringWriter sw = new StringWriter();
          throwable.printStackTrace( new PrintWriter( sw, true ) );
          return splitString( sw.toString(), LINE_SEPARATOR );
      }
  
      /**
       * Splits the string on every token into an array of stack frames.
       *
       * @param string the string to split
       * @param onToken the token to split on
       * @return the resultant array
       */
      private static String[] splitString( final String string, final String onToken )
      {
          final StringTokenizer tokenizer = new StringTokenizer( string, onToken );
          final String[] result = new String[ tokenizer.countTokens() ];
  
          for( int i = 0; i < result.length; i++ )
          {
              result[ i ] = tokenizer.nextToken();
          }
  
          return result;
      }
  
      private static void printCause( java.io.PrintStream out, Throwable e )
      {
          Throwable cause = getCause( e );
          out.println( "Cause: " + cause.toString() );
          if( getCause( cause ) != null )
          {
              printCause( out, cause );
          }
      }
  }
  
  
  
  1.1                  avalon-sandbox/merlin-jndi/src/java/org/apache/avalon/merlin/jndi/Service.java
  
  Index: Service.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
      "Apache Software Foundation"  must not be used to endorse or promote
      products derived  from this  software without  prior written
      permission. For written permission, please contact apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.avalon.merlin.jndi;
  
  import javax.naming.*;
  
  /**
   * A referenceable service.
   */
  public class Service implements Referenceable 
  {
      String m_service;
      
      public Service( String service ) 
      {
  	  m_service = service;
      }
      
      public Reference getReference() throws NamingException 
      {
  	  return new Reference(
  	    Service.class.getName(),
  	    new StringRefAddr("service", m_service ),
  	    ServiceFactory.class.getName(),
  	    null );
      }
  
      public String toString() {
  	return m_service;
      }
  }
  
  
  
  1.1                  avalon-sandbox/merlin-jndi/src/java/org/apache/avalon/merlin/jndi/ServiceFactory.java
  
  Index: ServiceFactory.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
      "Apache Software Foundation"  must not be used to endorse or promote
      products derived  from this  software without  prior written
      permission. For written permission, please contact apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.avalon.merlin.jndi;
  
  import javax.naming.*;
  import javax.naming.spi.ObjectFactory;
  import java.util.Hashtable;
  import java.util.Enumeration;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.Hashtable;
  import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  
  /**
   * This is an object factory that when given a reference for a service
   * object, will create an instance of the corresponding service.
   */
  public class ServiceFactory implements ObjectFactory 
  {
      private static final String MERLIN_KERNEL_LOADER_CLASSNAME = 
        "org.apache.avalon.merlin.kernel.impl.DefaultKernelLoader";
  
      private Object m_kernel;
  
     /**
      * Return a service instance if the supplied object is a reference
      * with the classname corresponding to the Service class, else null.
      *
      */
      public Object getObjectInstance(
       Object object, final Name name, Context context, final Hashtable environment ) throws
Exception 
      {
          System.out.println("object: " + object );
          System.out.println("name: " + name );
          System.out.println("context: " + context );
          System.out.println("environment: " + environment );
  
          if( m_kernel == null )
          {
              m_kernel = bootstrapKernel( object );
          }
  
          return m_kernel;
      }
  
      private Object bootstrapKernel( Object object ) throws Exception
      {
          try
          {
              if( object == null )
              {
                  throw new Exception( "object");
              }
              if( !( object instanceof Reference ) )
              {
                  throw new Exception( "Cannot resolve JNDI bootstrap properties.");
              }
  
              Reference reference = (Reference) object;
  
              File home = getFile( "home", reference );
              File system = getFile( "system", reference );
              File kernel = getFile( "kernel", reference );
              URL block = getURL( "block", reference );
  
              System.out.println("HOME:   " + home );
              System.out.println("SYSTEM: " + system );
              System.out.println("BLOCK:  " + block );
              System.out.println("KERNEL: " + kernel );
  
              File config = null;
              try
              {
                  config = getFile( "config", reference );
                  System.out.println("CONFIG: " + config );
              }
              catch( Throwable e )
              {
                  // optional
              }
  
              String debug = "INFO";
              try
              {
                  debug = getString( "debug", reference );
                  System.out.println("DEBUG: " + debug );
              }
              catch( Throwable e )
              {
                  // optional
              }
  
  
              File lib = new File( system, "lib" );
              File shared = new File( lib, "shared" );
              File sys = new File( lib, "system" );
              URL[] targets = getJarFiles( shared );
              URL[] libs = getJarFiles( sys, targets );
              ClassLoader current = Thread.currentThread().getContextClassLoader();
              ClassLoader common = new URLClassLoader( targets, current );
              ClassLoader internal =  new URLClassLoader( libs, current );
  
              Map map = new Hashtable();
  
              //
              // build the kernel loader context
              //
  
              map.put( "urn:merlin:classloader.common", common );
              map.put( "urn:merlin:classloader.system", internal);
              map.put( "urn:merlin:home", home );
              map.put( "urn:merlin:system", system );
              map.put( "urn:merlin:kernel.profile", kernel );
              map.put( "urn:merlin:block.url", block );
              map.put( "urn:merlin:debug", debug );
              if( config != null )
              {
                  map.put( "urn:merlin:block.config", config.toURL() );
              }
  
              return loadKernel( internal, map );
          }
          catch( Throwable e )
          {
              final String error = 
                "Unexpected error while bootstrapping the kernel.";
              final String message = ExceptionHelper.packException( error, e );
              throw new Exception( message ); 
          }
      }
  
      private Object loadKernel( ClassLoader loader, Map map )
      {
          Thread.currentThread().setContextClassLoader( loader );
          Object kernelLoader = null;
          Class clazz;
  
          //
          // load the kernel loader class from the supplied classloader
          //
  
          try
          {
              clazz = loader.loadClass( MERLIN_KERNEL_LOADER_CLASSNAME );
          }
          catch( Throwable e )
          {
              final String error = 
                "Internal error during loader class creation.";
              throw new RuntimeException( error, e );
          }
  
          //
          // instantiate the loader
          //
  
          try
          {
              Constructor constructor = clazz.getConstructor( new Class[0] );
              kernelLoader = constructor.newInstance( new Object[0] );
          }
          catch( Throwable e )
          {
              final String error = 
                "Internal error during loader instantiation.";
              throw new RuntimeException( error, e );
          }
  
          //
          // create the kernel instance
          //
  
          try
          {
              Method method = kernelLoader.getClass().getMethod( "build", new Class[]{ Map.class
} );
              return method.invoke( kernelLoader, new Object[]{ map } );
          }
          catch( Throwable e )
          {
              final String error = 
                "Internal error during kernel resolution.";
              throw new RuntimeException( error, e );         
          }
      }
  
      private static URL[] getJarFiles( File base )
      {
          if( base == null )
          {
              throw new NullPointerException( "base" );
          }
          return getJarFiles( base, new URL[0] );
      }
  
      private static URL[] getJarFiles( File base, URL[] urls )
      {
          if( base == null )
          {
              throw new NullPointerException( "base" );
          }
          List list = new ArrayList();
          for( int i=0; i<urls.length; i++ )
          {
              list.add( urls[i] );
          }
          populateJars( list, base );
          return (URL[]) list.toArray( new URL[0] );
      }
  
      private static void populateJars( List list, File base )
      {
          if( list == null )
          {
              throw new NullPointerException( "list" );
          }
          if( base == null )
          {
              throw new NullPointerException( "base" );
          }
          try
          {
              File[] files = base.listFiles();
              for( int i=0; i<files.length; i++ )
              {
                  File file = files[i];
                  if( file.getName().endsWith( ".jar" ) )
                  {
                      list.add( file.toURL() );
                  }
              }
          }
          catch( Throwable e )
          {
              final String error =
                "Unexpected error while scanning files in shared directory: " + base;
              throw new RuntimeException( error, e );
          }
      }
  
  
      private File getFile( String key, Reference reference ) throws Exception
      {
          RefAddr ref = reference.get( key );
          if( ref == null )
          {
              throw new Exception( "Missing resolve " + key + " ref.");
          }
          String value = (String) ref.getContent();
          if( value == null )
          {
              throw new Exception( "Null " + key + " ref value.");
          }
          return new File( value );
      }
     
      private URL getURL( String key, Reference reference ) throws Exception
      {
          RefAddr ref = reference.get( key );
          if( ref == null )
          {
              throw new Exception( "Missing resolve " + key + " ref.");
          }
          String value = (String) ref.getContent();
          if( value == null )
          {
              throw new Exception( "Null " + key + " ref value.");
          }
          return new URL( value );
      }    
  
      private String getString( String key, Reference reference ) throws Exception
      {
          RefAddr ref = reference.get( key );
          if( ref == null )
          {
              throw new Exception( "Missing resolve " + key + " ref.");
          }
          String value = (String) ref.getContent();
          if( value == null )
          {
              throw new Exception( "Null " + key + " ref value.");
          }
          return value;
      }    
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message