You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2002/08/27 21:57:25 UTC

cvs commit: jakarta-avalon-excalibur/container/src/java/org/apache/excalibur/container/legacy ComponentProxyGenerator.java

bloritsch    2002/08/27 12:57:25

  Added:       container/src/java/org/apache/excalibur/container/legacy
                        ComponentProxyGenerator.java
  Log:
  Adding the ProxyGenerator
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/container/src/java/org/apache/excalibur/container/legacy/ComponentProxyGenerator.java
  
  Index: ComponentProxyGenerator.java
  ===================================================================
  /*
  * 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.
  */
  
  package org.apache.excalibur.container.legacy;
  
  import org.apache.avalon.framework.component.Component;
  import java.lang.reflect.Method;
  import java.lang.reflect.Proxy;
  import java.lang.reflect.InvocationHandler;
  
  /**
   * Create a Component proxy.  Requires JDK 1.3+
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public final class ComponentProxyGenerator
  {
      private final ClassLoader m_classLoader;
  
      /**
       * Initialize the ComponentProxyGenerator with the default classloader.
       * The default classloader is the Thread context classloader.
       */
      public ComponentProxyGenerator()
      {
          this( Thread.currentThread().getContextClassLoader() );
      }
  
      /**
       * Initialize the ComponentProxyGenerator with the supplied classloader.
       * If the supplied class loader is null, we use the Thread context class
       * loader.  If that is null, we use this class's classloader.
       */
      public ComponentProxyGenerator( final ClassLoader parentClassLoader )
      {
          m_classLoader = ( null == parentClassLoader ) ?
                  ( ( null == Thread.currentThread().getContextClassLoader() ) ?
                          getClass().getClassLoader()
                          : Thread.currentThread().getContextClassLoader() )
                  : parentClassLoader;
      }
  
      /**
       * Get the Component wrapped in the proxy.  The role must be the service
       * interface's fully qualified classname to work.
       */
      public Component getProxy( String role, Object service ) throws Exception
      {
          Class serviceInterface = m_classLoader.loadClass( role );
  
          return (Component) Proxy.newProxyInstance( m_classLoader,
                  new Class[] { Component.class, serviceInterface },
                  new ComponentInvocationHandler(service) );
      }
  
      /**
       * Internal class to handle the wrapping with Component
       */
      private final static class ComponentInvocationHandler implements InvocationHandler
      {
          private final Object m_delagate;
  
          public ComponentInvocationHandler( final Object proxy )
          {
              m_delagate = proxy;
          }
  
          public Object invoke(Object proxy, Method meth, Object[] args)
          throws Throwable
          {
              return meth.invoke( m_delagate, args );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>