jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util ConfigurationUtil.java,1.1,1.2 ReflectionUtil.java,1.1,1.2 Type1Util.java,1.1,1.2 Type2Util.java,1.1,1.2 Type3Util.java,1.1,1.2

[email protected]
Newsgroups gmane.comp.java.jicarilla.cvs
Message-ID <[email protected]>
Update of /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20232/platform/container/impl/src/java/org/jicarilla/container/util

Modified Files:
	ConfigurationUtil.java ReflectionUtil.java Type1Util.java 
	Type2Util.java Type3Util.java 
Log Message:
start serious testing of the utility magic (and add some docs at the same time)

Index: ConfigurationUtil.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util/ConfigurationUtil.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ConfigurationUtil.java	10 Jan 2004 10:14:11 -0000	1.1
+++ ConfigurationUtil.java	29 Feb 2004 18:08:10 -0000	1.2
@@ -28,7 +28,6 @@
 import com.thoughtworks.xstream.XStream;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.configuration.DefaultConfiguration;
 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
 import org.jicarilla.container.Resolver;
 import org.xml.sax.SAXException;
@@ -38,7 +37,8 @@
 import java.io.InputStream;
 
 /**
- * 
+ * Utility methods that encapsulate heuristics for dealing with
+ * Avalon-Framework Configuration objects. 
  *
  * @author <a href="mail at leosimons dot com">Leo Simons</a>
  * @version $Id$
@@ -54,11 +54,19 @@
         final Object obj = container.get( className + CONFIGURATION_POSTFIX );
 
         if(obj == null)
-            return new DefaultConfiguration( className );
+            throw new ConfigurationException(
+                    "No configuration can be found for '" +
+                    className + "'" );
 
         if(obj instanceof Configuration )
             return (Configuration)obj;
 
+        return beanToConfiguration( obj );
+    }
+
+    protected static Configuration beanToConfiguration( final Object obj )
+            throws SAXException, IOException, ConfigurationException
+    {
         final XStream xstream = new XStream();
         final String configString = xstream.toXML(obj);
 

Index: ReflectionUtil.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util/ReflectionUtil.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ReflectionUtil.java	10 Jan 2004 10:14:11 -0000	1.1
+++ ReflectionUtil.java	29 Feb 2004 18:08:10 -0000	1.2
@@ -31,7 +31,7 @@
 import java.util.List;
 
 /**
- * 
+ * Utility methods that encapsulate reflection magic. 
  *
  * @author <a href="mail at leosimons dot com">Leo Simons</a>
  * @version $Id$
@@ -41,6 +41,8 @@
     public static Class loadClass( final String name )
             throws ClassNotFoundException
     {
+        Assert.assertNotNull( "name argument may not be null", name );
+
         return Thread.currentThread().getContextClassLoader()
                 .loadClass( name );
     }
@@ -57,7 +59,7 @@
                 .toArray( new Class[0] );
     }
 
-    public static List getAllSuperInterfaces(
+    protected static List getAllSuperInterfaces(
             final Class clazz, final List alreadyFound )
     {
         // done

Index: Type1Util.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util/Type1Util.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Type1Util.java	10 Jan 2004 10:14:11 -0000	1.1
+++ Type1Util.java	29 Feb 2004 18:08:10 -0000	1.2
@@ -25,11 +25,13 @@
 ==================================================================== */
 package org.jicarilla.container.util;
 
+import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.logger.ConsoleLogger;
 import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.parameters.Parameterizable;
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.jicarilla.container.Resolver;
@@ -37,12 +39,13 @@
 import org.jicarilla.container.integration.avalon.JicarillaBasedAvalonServiceManager;
 
 /**
- * 
+ * Utility methods that encapsulate reflection magic and heuristics to deal
+ * with Avalon-Framework components.
  *
  * @author <a href="mail at leosimons dot com">Leo Simons</a>
  * @version $Id$
  */
-public class Type1Util extends ConfigurationUtil
+public class Type1Util
 {
     public final static ConsoleLogger FallbackLogger = new ConsoleLogger(
             ConsoleLogger.LEVEL_WARN );
@@ -57,11 +60,16 @@
             ContainerUtil.contextualize( instance, getContext( container ) );
             ContainerUtil.service( instance, getServiceManager( container ) );
 
-            final Configuration configuration = getConfiguration( container,
-                    instance.getClass().getName() );
-            ContainerUtil.configure( instance, configuration  );
-            ContainerUtil.parameterize( instance, Parameters.fromConfiguration(
-                    configuration ) );
+            if( instance instanceof Configurable ||
+                instance instanceof Parameterizable )
+            {
+                final Configuration configuration =
+                        ConfigurationUtil.getConfiguration( container,
+                                instance.getClass().getName() );
+                ContainerUtil.configure( instance, configuration  );
+                ContainerUtil.parameterize( instance, Parameters.fromConfiguration(
+                        configuration ) );
+            }
 
             ContainerUtil.initialize( instance );
             ContainerUtil.start( instance );

Index: Type2Util.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util/Type2Util.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Type2Util.java	10 Jan 2004 10:14:11 -0000	1.1
+++ Type2Util.java	29 Feb 2004 18:08:10 -0000	1.2
@@ -37,13 +37,28 @@
 import java.util.Set;
 
 /**
- * 
+ * Utility methods that encapsulate reflection magic and heuristics to deal
+ * with type 2 components, especially type 2 components that implement
+ * awareness interfaces.
  *
  * @author <a href="mail at leosimons dot com">Leo Simons</a>
  * @version $Id$
  */
 public class Type2Util
 {
+    /**
+     * The postfix on interfaces that determines whether they are
+     * considered an "awareness" interface, ie, the string "Aware".
+     */
+    public final static String AWARENESS_INTERFACE_POSTFIX = "Aware";
+
+    /**
+     * Retrieve all the methods of a class that are considered
+     * "awareness methods".
+     *
+     * @param clazz
+     * @return
+     */
     public static Method[] getAwarenessMethods( final Class clazz )
     {
         final Class[] interfaces = ReflectionUtil.getAllInterfaces( clazz );
@@ -53,37 +68,26 @@
         for( int i = 0; i < interfaces.length; i++ )
         {
             final Class interf = interfaces[i];
-            if( !interf.getName().endsWith("Aware") )
+            if( !interf.getName().endsWith( AWARENESS_INTERFACE_POSTFIX ) )
                 continue;
 
-            final Method[] methods = interf.getMethods();
-
-            Assert.assertTrue( interf.getName() +
-                    " may have but a single public method",
-                    methods.length == 1 );
-
-            final Method method = methods[0];
-            /* redundant! boolean accessible =
-            Modifier.isPublic(method.getModifiers());
-            if(     !accessible )
-                continue;*/
-
-            final Object returnType = method.getReturnType();
-            Assert.assertTrue( "The " + interf.getName() +
-                    " awareness method does not have a void return type",
-                    Void.TYPE.equals( returnType ) );
-
-            final Class[] parameterTypes = method.getParameterTypes();
-            Assert.assertTrue( "The " + interf.getName() + "" +
-                    "awareness method does not have but a single parameter",
-                    parameterTypes.length == 1 );
-
-            methodList.add( method );
+            getAwarenessMethod( interf, methodList );
         }
 
         return (Method[])methodList.toArray( new Method[methodList.size()] );
     }
 
+    /**
+     * Figures out all the awareness methods of an instance, and calls each and
+     * everyone one of them using instances retrieved from the provided
+     * resolver. The keys fed to that resolver are the classes of the
+     * parameters of the awareness interfaces.
+     *
+     * @param instance
+     * @param container
+     * @throws IllegalAccessException
+     * @throws InvocationTargetException
+     */
     public static void callAwarenessMethods(
             final Object instance, final Resolver container )
             throws IllegalAccessException, InvocationTargetException
@@ -104,8 +108,18 @@
         }
     }
 
+    /**
+     * Retrieves the arguments to an array of awareness methods. If the
+     * provided array contains methods that are not awareness methods, the
+     * result is undefined.
+     *
+     * @param methods
+     * @return
+     */
     public static Class[] getType2DependencyClasses( final Method[] methods )
     {
+        Assert.assertNotNull( "methods argument may not be null", methods );
+
         final List allKeys = new ArrayList();
         for( int i = 0; i < methods.length; i++ )
         {
@@ -117,6 +131,15 @@
         return (Class[])allKeys.toArray( new Class[allKeys.size()] );
     }
 
+    /**
+     * Determines whether the provided resolver contains all of the neccessary
+     * components to be able to call all of the "type 2 methods" (they don't
+     * need to be awareness methods) that are provided.
+     *
+     * @param clazz
+     * @param type2Methods
+     * @param dependencyProvider
+     */
     public static void checkDepencenciesAreSatisfiable(
             final Class clazz, final Method[] type2Methods,
             final Resolver dependencyProvider )
@@ -135,4 +158,39 @@
             throw new UnsatisfiableDependencyException(
                     clazz, problematicKeys );
     }
+
+    /**
+     * Retrieves the "awareness method" of an awareness interface and adds it
+     * to the provided list.
+     *
+     * @param interf
+     * @param methodList
+     */
+    protected static void getAwarenessMethod( final Class interf,
+            final List methodList )
+    {
+        final Method[] methods = interf.getMethods();
+
+        Assert.assertTrue( interf.getName() +
+                " must have a single public method, not " + methods.length,
+                methods.length == 1 );
+
+        final Method method = methods[0];
+        /* redundant! boolean accessible =
+        Modifier.isPublic(method.getModifiers());
+        if(     !accessible )
+            continue;*/
+
+        final Object returnType = method.getReturnType();
+        Assert.assertTrue( "The " + interf.getName() +
+                " awareness method does not have a void return type",
+                Void.TYPE.equals( returnType ) );
+
+        final Class[] parameterTypes = method.getParameterTypes();
+        Assert.assertTrue( "The " + interf.getName() + "" +
+                "awareness method does not have but a single parameter",
+                parameterTypes.length == 1 );
+
+        methodList.add( method );
+    }
 }

Index: Type3Util.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/util/Type3Util.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Type3Util.java	10 Jan 2004 10:14:11 -0000	1.1
+++ Type3Util.java	29 Feb 2004 18:08:10 -0000	1.2
@@ -39,7 +39,8 @@
 import java.util.Set;
 
 /**
- * 
+ * Utility methods that encapsulate reflection magic and heuristics to deal
+ * with type 3 components.
  *
  * @author <a href="mail at leosimons dot com">Leo Simons</a>
  * @version $Id$



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.