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/repository/main/src/java/org/apache/avalon/repository Bootstrapper.java
Date Wed, 19 Nov 2003 18:17:48 GMT
mcconnell    2003/11/19 10:17:48

  Modified:    repository/main/src/java/org/apache/avalon/repository
                        Bootstrapper.java
  Log:
  Update the bootstrapper so we can do some tests.
  
  Revision  Changes    Path
  1.5       +110 -65   avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/Bootstrapper.java
  
  Index: Bootstrapper.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/Bootstrapper.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Bootstrapper.java	19 Nov 2003 17:28:37 -0000	1.4
  +++ Bootstrapper.java	19 Nov 2003 18:17:48 -0000	1.5
  @@ -113,50 +113,49 @@
       /**
        * Invokes the main application entry point for a class.
        * 
  -     * @param a_fqcn the fully qualified class name
        * @param a_args the arguments to pass-thro to the main of the application
        * @throws RepositoryException on any class loading or invocation failures
        */
  -    public void main( String a_fqcn, String[] a_args )
  +    public void main( String main, String[] a_args )
           throws RepositoryException
       {
           Class l_clazz = null ;
  -        Method l_main = null ;
  +        Method l_mainMethod = null ;
           
           try 
           {
  -            l_clazz = m_loader.loadClass( a_fqcn ) ;
  -            l_main = l_clazz.getMethod( a_fqcn, new Class[] {String[].class} ) ;
  +            l_clazz = m_loader.loadClass( main ) ;
  +            l_mainMethod = l_clazz.getMethod( main, new Class[] {String[].class} ) ;
               
  -            if ( l_main.getReturnType() != Void.TYPE )
  +            if ( l_mainMethod.getReturnType() != Void.TYPE )
               {
  -                throw new RepositoryException( "Invalid main() in " + a_fqcn 
  +                throw new RepositoryException( "Invalid main() in " + main 
                           + " has a non-void return type of " 
  -                        + l_main.getReturnType() ) ;
  +                        + l_mainMethod.getReturnType() ) ;
               }
               
  -            l_main.invoke( null, a_args ) ;
  +            l_mainMethod.invoke( null, a_args ) ;
           }
           catch ( InvocationTargetException e )
           {
               throw new RepositoryException( "Failed to invoke target main()"
  -                    + " in class " + a_fqcn ) ;
  +                    + " in class " + main ) ;
           }
           catch ( IllegalAccessException e )
           {
               throw new RepositoryException( "Cannot invoke non-public main()"
  -                    + " in class " + a_fqcn ) ;
  +                    + " in class " + main ) ;
           }
           catch ( NoSuchMethodException e )
           {
  -            throw new RepositoryException( a_fqcn + " Does not contain a main "
  +            throw new RepositoryException( main + " Does not contain a main "
                       + "method with a sole String [] argument.", e ) ; 
           }
           catch ( ClassNotFoundException e )
           {
               throw new RepositoryException( "ClassLoader for repository Jar "
                       + "artifact " + m_descriptor + " could not find " 
  -                    + a_fqcn, e ) ;
  +                    + main, e ) ;
           }
       }
       
  @@ -165,37 +164,37 @@
        * Instantiates a runnable using a default constructor and calls its run
        * method.
        * 
  -     * @param a_fqcn the fully qualified class name of the Runnable 
  +     * @param main the fully qualified class name of the Runnable 
        * @throws RepositoryException on any class loading or invokation failures
        */
  -    public void run( String a_fqcn ) throws RepositoryException
  +    public void run( String main ) throws RepositoryException
       {
           Class l_clazz = null ;
           Runnable l_runnable = null ;
           
           try
           {
  -            l_clazz = m_loader.loadClass( a_fqcn ) ;
  +            l_clazz = m_loader.loadClass( main ) ;
               l_runnable = ( Runnable ) l_clazz.newInstance() ;
               l_runnable.run() ;
           }
           catch ( ClassCastException e )
           {
  -            throw new RepositoryException( a_fqcn + " is not a Runnable", e ) ;
  +            throw new RepositoryException( main + " is not a Runnable", e ) ;
           }
           catch ( InstantiationException e )
           {
  -            throw new RepositoryException( a_fqcn + " in " + m_descriptor
  +            throw new RepositoryException( main + " in " + m_descriptor
                       + " does not contain a default constructor" ) ;
           }
           catch ( IllegalAccessException e )
           {
               throw new RepositoryException( "Cannot invoke non-public run()"
  -                    + " in class " + a_fqcn ) ;
  +                    + " in class " + main ) ;
           }
           catch ( ClassNotFoundException e )
           {
  -            throw new RepositoryException( "Could not find " + a_fqcn 
  +            throw new RepositoryException( "Could not find " + main 
                       + " in repository jar artifact " + m_descriptor 
                       + " or in any one of its dependent Jars", e ) ;
           }
  @@ -205,17 +204,17 @@
       /**
        * Invokes a static method on a class.
        * 
  -     * @param a_fqcn the fully qualified class name
  +     * @param main the fully qualified class name
        * @param a_method the name of the method to invoke
        * @param a_types the fqcn of the parameters
        * @param a_args the arguments to the method
        * @return the methods return value if one exists, or null if method is void
        */
  -    public Object invoke( String a_fqcn, String a_method, String [] a_types, 
  +    public Object invoke( String main, String a_method, String [] a_types, 
                             Object [] a_args )
           throws RepositoryException
       {
  -        Class l_clazz = loadClass( a_fqcn ) ;
  +        Class l_clazz = loadClass( main ) ;
           Class [] l_types = new Class[a_types.length] ;
           
           /** Get all the argument classes */
  @@ -234,19 +233,19 @@
           {
               throw new RepositoryException( "Failed to invoke static target "
                       + toSignature( a_method, a_types ) + " in class " 
  -                    + a_fqcn ) ;
  +                    + main ) ;
           }
           catch ( IllegalAccessException e )
           {
               throw new RepositoryException( "Cannot access static method " 
                       + toSignature( a_method, a_types ) + " in class "
  -                    + a_fqcn, e ) ;
  +                    + main, e ) ;
           }
           catch ( NoSuchMethodException e )
           {
               throw new RepositoryException( "Could not find static method with"
                       + " matching signature " + toSignature( a_method, a_types ) 
  -                    + " for class " + a_fqcn, e ) ;
  +                    + " for class " + main, e ) ;
           }
       }
       
  @@ -254,13 +253,13 @@
       /**
        * Creates a new instance of a class using the default constructor.
        * 
  -     * @param a_fqcn the fully qualified name of the class to create an inst of
  +     * @param main the fully qualified name of the class to create an inst of
        * @return the instance created
        * @throws RepositoryException if the instantiation fails
        */
  -    public Object newInstance( String a_fqcn ) throws RepositoryException
  +    public Object newInstance( String main ) throws RepositoryException
       {
  -        Class l_clazz = loadClass( a_fqcn ) ;
  +        Class l_clazz = loadClass( main ) ;
           
           try
           {
  @@ -269,12 +268,12 @@
           catch ( IllegalAccessException e )
           {
               throw new RepositoryException( "A public accessible default "
  -                    + "constructor dies not exist for " + a_fqcn, e ) ;
  +                    + "constructor dies not exist for " + main, e ) ;
           }
           catch ( InstantiationException e )
           {
               throw new RepositoryException( "Failure while creating an"
  -                    + " instance of " + a_fqcn 
  +                    + " instance of " + main 
                       + " via the default constructor", e ) ;
           }
       }
  @@ -283,17 +282,17 @@
       /**
        * Creates a new instance of a class using a constructor taking arguments.
        * 
  -     * @param a_fqcn the fully qualified name of the class to create an inst of
  +     * @param main the fully qualified name of the class to create an inst of
        * @param a_types the fully qualified names of constructor parameter types
        * @param a_args the arguments to the constructor
        * @return the newly created instance 
        * @throws RepositoryException if the instantiation fails
        */
  -    public Object newInstance( String a_fqcn, String[] a_types, 
  +    public Object newInstance( String main, String[] a_types, 
                                  Object[] a_args )
           throws RepositoryException
       {
  -        Class l_clazz = loadClass( a_fqcn ) ;
  +        Class l_clazz = loadClass( main ) ;
           Class[] l_types = new Class[a_types.length] ;
           
           for ( int ii = 0; ii < a_types.length; ii++ )
  @@ -310,23 +309,23 @@
           {
               throw new RepositoryException( 
                       "Failed to instantiate with constructor "
  -                    + toSignature( getConstructor( a_fqcn ), a_types ), e ) ;
  +                    + toSignature( getConstructor( main ), a_types ), e ) ;
           }
           catch ( NoSuchMethodException e )
           {
               throw new RepositoryException( 
                       "Could not find a constructor with signature " 
  -                    + toSignature( getConstructor( a_fqcn ), a_types ), e ) ;
  +                    + toSignature( getConstructor( main ), a_types ), e ) ;
           }
           catch ( InvocationTargetException e )
           {
               throw new RepositoryException( "Failed to invoke constructor "
  -                    + toSignature( getConstructor( a_fqcn ), a_types ), e ) ;
  +                    + toSignature( getConstructor( main ), a_types ), e ) ;
           }
           catch ( IllegalAccessException e )
           {
               throw new RepositoryException( "Cannot access constructor " 
  -                    + toSignature( getConstructor( a_fqcn ), a_types ), e ) ;
  +                    + toSignature( getConstructor( main ), a_types ), e ) ;
           }
       }
       
  @@ -334,20 +333,20 @@
       /**
        * Gets the name of the constructor given the fully qualified class name.
        * 
  -     * @param a_fqcn the fully qualified class name
  +     * @param main the fully qualified class name
        * @return the name of the constructor
        */
  -    public String getConstructor( String a_fqcn )
  +    public String getConstructor( String main )
       {
  -        int l_lastDot = a_fqcn.indexOf( '.' ) ;
  +        int l_lastDot = main.indexOf( '.' ) ;
           
  -        // Class is in default package so a_fqcn = cn = constructor
  +        // Class is in default package so main = cn = constructor
           if ( -1 == l_lastDot )
           {
  -            return a_fqcn ;
  +            return main ;
           }
           
  -        return a_fqcn.substring( l_lastDot + 1 ) ;
  +        return main.substring( l_lastDot + 1 ) ;
       }
       
   
  @@ -403,19 +402,19 @@
       /**
        * Loads a class using the ClassLoader created by the Repository.
        * 
  -     * @param a_fqcn the fully qualified class name 
  +     * @param main the fully qualified class name 
        * @return the Class loaded using the Repository built ClassLoader
        * @throws RepositoryException if the class could not be found
        */
  -    public Class loadClass( String a_fqcn ) throws RepositoryException
  +    public Class loadClass( String main ) throws RepositoryException
       {
           try
           {
  -            return m_loader.loadClass( a_fqcn ) ;
  +            return m_loader.loadClass( main ) ;
           }
           catch ( ClassNotFoundException e )
           {
  -            throw new RepositoryException( "Could not find " + a_fqcn 
  +            throw new RepositoryException( "Could not find " + main 
                       + " in repository jar artifact " + m_descriptor 
                       + " or in any one of its dependent Jars", e ) ;
           }
  @@ -459,24 +458,25 @@
        */
       public static void main( String [] a_args )
       {
  -        String l_jarSpec = System.getProperty( "repository.application.jar" ) ;
  -        String l_fqcn = System.getProperty( "repository.application.class" ) ;
  -
  -        String[] l_repositories = DEFAULT_REPOSITORIES; // need to handle this via args
  -        
  -        if ( null == l_jarSpec )
  +        String l_jarSpec = getArtifactSpec( a_args );
  +        if( null == l_jarSpec )
           {
  -            System.err.println( "The repository application jar was not "
  -                    + "specified so we cannot continue ..." ) ;
  -            System.exit( -1 ) ;
  +            final String error = 
  +              "Cannot resolve an artifict target.";
  +              System.out.println( error );
  +            System.exit( -1 );
           }
  -        
  -        if ( null == l_fqcn )
  +
  +        String l_fqcn = getMainClassname( a_args );
  +        if( null == l_fqcn )
           {
  -            System.err.println( "The main application class was not "
  -                    + "specified so we cannot continue ..." ) ;
  -            System.exit( -1 ) ;
  +            final String error = 
  +              "Cannot resolve an main class.";
  +              System.out.println( error );
  +            System.exit( -1 );
           }
  +
  +        String[] l_repositories = DEFAULT_REPOSITORIES; // need to handle this via args
           
           Repository l_repository ;
           Artifact l_jar = MavenArtifactFactory.createJarArtifact( l_jarSpec ) ;
  @@ -489,6 +489,7 @@
               RepositoryContext l_context = l_factory.getDefaultContext() ;
               l_repository = l_factory.create( l_context ) ;
               l_bootstrapper = new Bootstrapper( l_repository, l_jar ) ;
  +
               l_bootstrapper.main( l_fqcn, a_args ) ;
           }
           catch ( RepositoryException e )
  @@ -496,5 +497,49 @@
               e.printStackTrace() ;
               System.exit( -1 ) ;
           }
  +    }
  +
  +    private static String getArtifactSpec( String[] args )
  +    {
  +        for( int i=0; i<args.length; i++ )
  +        {
  +            if( args[i].equals( "-artifact" ) )
  +            {
  +                if( args.length >= i+1 )
  +                {
  +                    return args[ i+1 ];
  +                }
  +                else
  +                {
  +                    final String error = 
  +                      "CLI Missing artifict argument.";
  +                    throw new IllegalArgumentException( error );
  +                }
  +            }
  +        }
  +        
  +        return System.getProperty( "repository.application.jar" ) ;
  +    }
  +
  +    private static String getMainClassname( String[] args )
  +    {
  +        for( int i=0; i<args.length; i++ )
  +        {
  +            if( args[i].equals( "-main" ) )
  +            {
  +                if( args.length >= i+1 )
  +                {
  +                    return args[ i+1 ];
  +                }
  +                else
  +                {
  +                    final String error = 
  +                      "CLI Missing main argument.";
  +                    throw new IllegalArgumentException( error );
  +                }
  +            }
  +        }
  +        
  +        return System.getProperty( "repository.application.main" ) ;
       }
   }
  
  
  

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


Mime
View raw message