You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/05/24 23:27:45 UTC

cvs commit: jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components InterceptorAdapterFactory.java InterceptorAdapter.java

weaver      2004/05/24 14:27:44

  Modified:    components/cm/src/test/org/apache/jetspeed/components
                        TestGroovyComponentAdapter.java MockComponent.java
  Added:       components/cm/src/test/org/apache/jetspeed/components
                        BaseMockComponent.java MockDependentComponent.java
                        BaseMockComponent.groovy
                        TestInterceptorAdapter.java
               components/cm/src/java/org/apache/jetspeed/components/adapters
                        StandardDelegationStrategy.java
                        DelegationStrategy.java
                        InterceptorAdapterFactory.java
                        InterceptorAdapter.java
                        ThreadLocalDelegationStrategy.java
                        AbstractDelegationStrategy.java
  Removed:     components/cm/src/test/org/apache/jetspeed/components
                        MockComponent.groovy
               components/cm/src/java/org/apache/jetspeed/components
                        InterceptorAdapterFactory.java
                        InterceptorAdapter.java
  Log:
  see JS2-53 :<http://nagoya.apache.org/jira/browse/JS2-53>
  also, adapters have their own package now.
  
  Revision  Changes    Path
  1.2       +2 -2      jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/TestGroovyComponentAdapter.java
  
  Index: TestGroovyComponentAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/TestGroovyComponentAdapter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestGroovyComponentAdapter.java	15 Apr 2004 15:33:13 -0000	1.1
  +++ TestGroovyComponentAdapter.java	24 May 2004 21:27:44 -0000	1.2
  @@ -36,8 +36,8 @@
           MutablePicoContainer container = new DefaultPicoContainer();
           Parameter v1 = new ConstantParameter(new Integer(69));
           Parameter v2 = new ConstantParameter("Some Value");     
  -        ComponentAdapter adp1 = new GroovyComponentAdapter("multipleObjects", MockComponent.class, new Parameter[] {v1, v2}, false);
  -        ComponentAdapter adp2 = new GroovyComponentAdapter("singletonObject", MockComponent.class, new Parameter[] {v1, v2}, true);
  +        ComponentAdapter adp1 = new GroovyComponentAdapter("multipleObjects", BaseMockComponent.class, new Parameter[] {v1, v2}, false);
  +        ComponentAdapter adp2 = new GroovyComponentAdapter("singletonObject", BaseMockComponent.class, new Parameter[] {v1, v2}, true);
           
           container.registerComponent(adp1);
           container.registerComponent(adp2);
  
  
  
  1.2       +30 -31    jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/MockComponent.java
  
  Index: MockComponent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/MockComponent.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockComponent.java	15 Apr 2004 15:33:13 -0000	1.1
  +++ MockComponent.java	24 May 2004 21:27:44 -0000	1.2
  @@ -1,8 +1,17 @@
   /*
  - * Created on Apr 15, 2004
  - *
  - * TODO To change the template for this generated file go to
  - * Window - Preferences - Java - Code Generation - Code and Comments
  + * Copyright 2000-2001,2004 The Apache Software Foundation.
  + * 
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
  + * 
  + *      http://www.apache.org/licenses/LICENSE-2.0
  + * 
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   package org.apache.jetspeed.components;
   
  @@ -10,43 +19,33 @@
    * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver</a>
    *
    */
  -public class MockComponent
  +public interface MockComponent
   {
  -    private int fieldValue1;
  -    private String fieldValue2;
  -
  -    public MockComponent(int inValue1, String inValue2)
  -    {
  -        fieldValue1 = inValue1;
  -        fieldValue2 = inValue2;
  -    }
  -
       /**
        * @return Returns the value1.
        */
  -    protected int getValue1()
  -    {
  -        return fieldValue1;
  -    }
  +    int getValue1();
  +
       /**
        * @param value1 The value1 to set.
        */
  -    protected void setValue1( int value1 )
  -    {
  -        fieldValue1 = value1;
  -    }
  +    void setValue1( int value1 );
  +
       /**
        * @return Returns the value2.
        */
  -    protected String getValue2()
  -    {
  -        return fieldValue2;
  -    }
  +    String getValue2();
  +
       /**
        * @param value2 The value2 to set.
        */
  -    protected void setValue2( String value2 )
  -    {
  -        fieldValue2 = value2;
  -    }
  -}
  +    void setValue2( String value2 );
  +    
  +    /**
  +     * 
  +     * @return number of components of this type that have been instantiated.
  +     */
  +    int componentId();
  +    
  +    String getThreadName();
  +}
  \ No newline at end of file
  
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/BaseMockComponent.java
  
  Index: BaseMockComponent.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components;
  
  /**
   * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver</a>
   *
   */
  public class BaseMockComponent implements MockComponent
  {
      private int fieldValue1;
      private String fieldValue2;
    
      private int id;
      private String threadName;
      protected static int instanceCount;
  
      public BaseMockComponent(int inValue1, String inValue2)
      {
          id = instanceCount;
          instanceCount++;
          fieldValue1 = inValue1;
          fieldValue2 = inValue2;
          this.threadName = Thread.currentThread().getName();
          
      }
  
      /**
       * @return Returns the value1.
       */
      public int getValue1()
      {
          return fieldValue1;
      }
      /**
       * @param value1 The value1 to set.
       */
      public void setValue1( int value1 )
      {
          fieldValue1 = value1;
      }
      /**
       * @return Returns the value2.
       */
      public String getValue2()
      {
          return fieldValue2;
      }
      /**
       * @param value2 The value2 to set.
       */
      public void setValue2( String value2 )
      {
          fieldValue2 = value2;
      }
      /* (non-Javadoc)
       * @see org.apache.jetspeed.components.MockComponent#componentCount()
       */
      public int componentId()
      {        
          return id;
      }
      /* (non-Javadoc)
       * @see org.apache.jetspeed.components.MockComponent#getThreadName()
       */
      public String getThreadName()
      {        
          return threadName;
      }
  }
  
  
  
  1.2       +39 -0     jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/MockDependentComponent.java
  
  
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/BaseMockComponent.groovy
  
  Index: BaseMockComponent.groovy
  ===================================================================
  import org.apache.jetspeed.components.BaseMockComponent
  
  v1 = parameterReader.getValue(0, Integer).intValue() 
  v2 = parameterReader.getValue(1, String)
  
  return new BaseMockComponent(v1, v2)
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/TestInterceptorAdapter.java
  
  Index: TestInterceptorAdapter.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.apache.jetspeed.components.adapters.InterceptorAdapter;
  import org.apache.jetspeed.components.adapters.StandardDelegationStrategy;
  import org.apache.jetspeed.components.adapters.ThreadLocalDelegationStrategy;
  import org.picocontainer.Parameter;
  import org.picocontainer.PicoContainer;
  import org.picocontainer.defaults.CachingComponentAdapter;
  import org.picocontainer.defaults.ComponentParameter;
  import org.picocontainer.defaults.ConstantParameter;
  import org.picocontainer.defaults.ConstructorComponentAdapter;
  import org.picocontainer.defaults.DefaultPicoContainer;
  import org.picocontainer.defaults.InstanceComponentAdapter;
  import org.picocontainer.defaults.Swappable;
  
  /**
   * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver </a>
   *  
   */
  public class TestInterceptorAdapter extends TestCase
  {
  
      protected static final String SINGLETON_KEY = "singleton";
      static TestSuite suite;
  
      /**
       *  
       */
      public TestInterceptorAdapter()
      {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @param arg0
       */
      public TestInterceptorAdapter( String arg0 )
      {
          super(arg0);
          // TODO Auto-generated constructor stub
      }
  
      public static Test suite()
      {
          // All methods starting with "test" will be executed in the test suite.
          suite = new TestSuite(TestInterceptorAdapter.class);
          return suite;
      }
  
      public void testStandardDelegation() throws Exception
      {
          DefaultPicoContainer pico = new DefaultPicoContainer();
          MockComponent c1 = new BaseMockComponent(1, "c1");
          MockComponent c2 = new BaseMockComponent(2, "c2");
  
          pico.registerComponent(new InterceptorAdapter(
                  new InstanceComponentAdapter(MockComponent.class, c1),
                  StandardDelegationStrategy.class));
  
          MockComponent testComponent = (MockComponent) pico
                  .getComponentInstance(MockComponent.class);
          assertNotNull(testComponent);
  
          // Should not be the actual instance, but a dynamic proxy
          assertFalse(testComponent instanceof BaseMockComponent);
          
          assertTrue(testComponent.getValue1() == 1
                  && testComponent.getValue2().equals("c1"));
  
          // Are we Swappable???
          assertTrue(testComponent instanceof Swappable);
  
          // Now test our hot swapping
          ((Swappable) testComponent).__hotSwap(c2);
  
          assertTrue(testComponent.getValue1() == 2
                  && testComponent.getValue2().equals("c2"));
      }
  
      public void testThreadLocalDelegation() throws Exception
      {
          Thread.currentThread().setName("Thread 1");
          DefaultPicoContainer pico = new DefaultPicoContainer();
  
          ConstructorComponentAdapter cca = new ConstructorComponentAdapter(
                  MockComponent.class, BaseMockComponent.class, new Parameter[]{
                          new ConstantParameter(new Integer(1)),
                          new ConstantParameter("c1")});
  
          InterceptorAdapter ia = new InterceptorAdapter(cca,
                  ThreadLocalDelegationStrategy.class);
  
          CachingComponentAdapter cacheAdptr = new CachingComponentAdapter(ia);
  
          pico.registerComponent(cacheAdptr);
          pico.start();
  
          //Make sure that caching is working as expected
          assertTrue(pico.getComponentInstance(MockComponent.class) == pico
                  .getComponentInstance(MockComponent.class));
          MockComponent comp = (MockComponent) pico
                  .getComponentInstance(MockComponent.class);
          assertEquals("Thread 1", comp.getThreadName());
          ThreadTest th1 = new ThreadTest(pico);
          th1.setName("Thread 2");
          ThreadTest th2 = new ThreadTest(pico);
          th2.setName("Thread 3");
          th1.start();
          th2.start();
  
          // try to artificially increase the instance count
          for (int i = 0; i < 5; i++)
          {
              pico.getComponentInstance(MockComponent.class);
          }
  
          assertEquals(3, BaseMockComponent.instanceCount);
  
      }
  
      public void testDependencyWithThreadLocalAndHotSwapping() throws Exception
      {
          DefaultPicoContainer pico = new DefaultPicoContainer();
          
          pico.registerComponentImplementation(SINGLETON_KEY, MockDependentComponent.class,  new Parameter[]{new ComponentParameter(MockComponent.class)});
          
          ConstructorComponentAdapter cca = new ConstructorComponentAdapter(MockComponent.class, BaseMockComponent.class, new Parameter[] {new ConstantParameter(new Integer(1)), new ConstantParameter("c1")});
          
          pico.registerComponent(new InterceptorAdapter(cca, ThreadLocalDelegationStrategy.class));
          pico.start();
          
          ThreadTest2 tt = new ThreadTest2(pico);
          
          tt.start();
          
          MockDependentComponent mdc = (MockDependentComponent) pico.getComponentInstance(SINGLETON_KEY);
          assertNotNull(mdc);
          MockComponent testMc = mdc.getMockComponent();
          assertNotNull(testMc);
          assertEquals(testMc.getValue2(), "c1");
          
          tt.doVerify(SINGLETON_KEY, "c1", testMc);
          
          synchronized(tt)
          {
              tt.wait();
          }
          
          assertFalse(tt.failed());
          
          //Now change this thread without affecting the other thread
          MockComponent c2 = new BaseMockComponent(2, "c2");
          ((Swappable) testMc).__hotSwap(c2);
          
          assertEquals("c2", mdc.getMockComponent().getValue2());
          // verify that the value has not changed in the other thread
          tt.doVerify(SINGLETON_KEY, "c1", testMc);
          
          synchronized(tt)
          {
              tt.wait();
          }
          
          assertFalse(tt.failed());
          
          tt.shutdown();
          
      }
  
      class ThreadTest extends Thread
      {
          private PicoContainer pico;
  
          ThreadTest( PicoContainer pico )
          {
              this.pico = pico;
          }
  
          public void run()
          {
              // super.run();
  
              MockComponent mc = (MockComponent) pico
                      .getComponentInstance(MockComponent.class);
              assertEquals(Thread.currentThread().getName(), mc.getThreadName());
              // try to artificially increase the instance count
              for (int i = 0; i < 5; i++)
              {
                  pico.getComponentInstance(MockComponent.class);
              }
  
          }
      }
  
      class ThreadTest2 extends Thread
      {
          private PicoContainer pico;
          private boolean fieldVerify;
          private String fieldValue;
          private Object fieldKey;
          private boolean fieldRunning;
          private boolean failed=false;
          private Object otherThreadsInstance;
  
          ThreadTest2( PicoContainer pico )
          {
              this.pico = pico;
          }
  
          public void run()
          {
              // super.run();
              fieldRunning = true;
              while (fieldRunning)
              {
                  if (fieldVerify)
                  {
                      fieldVerify = false;
                      MockDependentComponent mdc = (MockDependentComponent) pico
                              .getComponentInstance(fieldKey);
                      try
                      {
                          assertNotNull(mdc);
                          assertNotNull(mdc.getMockComponent());
                          assertEquals(mdc.getMockComponent().getValue2(), fieldValue);
                          if(otherThreadsInstance != null)
                          {
                              // Verifies that component is the same in both Threads.
                              assertTrue(mdc.getMockComponent() == otherThreadsInstance);
                          }
                      }
                      catch (Error e)
                      {
                           System.err.println(e.getMessage());
                           failed = true;
                      }
                      
                      synchronized(this)
                      {
                          notifyAll();
                      }
                  }
              }
  
          }
  
          void doVerify( Object key, String value, Object otherThreadsInstance )
          {
              fieldKey = key;            
              fieldValue = value;
              this.otherThreadsInstance = otherThreadsInstance;
              fieldVerify = true;
          }       
         
          
          boolean failed()
          {
              return failed;
          }
          
          void shutdown()
          {
              fieldRunning = false;
          }
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see junit.framework.TestCase#tearDown()
       */
      protected void tearDown() throws Exception
      {
  
          super.tearDown();
      }
  }
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/adapters/StandardDelegationStrategy.java
  
  Index: StandardDelegationStrategy.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components.adapters;
  
  /**
   * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver</a>
   *
   */
  public class StandardDelegationStrategy extends AbstractDelegationStrategy
  {
  
      private Object delegatedInstance;
      
         protected Object getDelegatedInstance()
      {
           return delegatedInstance;
      }
  
  
      protected void setDelegatedInstance( Object instance )
      {
          delegatedInstance  = instance;
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/adapters/DelegationStrategy.java
  
  Index: DelegationStrategy.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components.adapters;
  
  import java.lang.reflect.InvocationHandler;
  
  import org.picocontainer.defaults.Swappable;
  
  /**
   * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver</a>
   *
   */
  public interface DelegationStrategy extends InvocationHandler, Swappable
  {
      void setAdapter(InterceptorAdapter adapter);
      
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/adapters/InterceptorAdapterFactory.java
  
  Index: InterceptorAdapterFactory.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   * License for the specific language governing permissions and limitations under
   * the License.
   */
  package org.apache.jetspeed.components.adapters;
  
  import org.picocontainer.defaults.DecoratingComponentAdapterFactory;
  import org.picocontainer.defaults.DefaultComponentAdapterFactory;
  import org.picocontainer.ComponentAdapter;
  import org.picocontainer.Parameter;
  import org.picocontainer.PicoIntrospectionException;
  import org.picocontainer.defaults.*;
  
  /**
   * InterceptorAdapterFactory
   * 
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
   * @version $Id: InterceptorAdapterFactory.java,v 1.2 2004/03/08 00:42:51 jford
   *                Exp $
   */
  public class InterceptorAdapterFactory
          extends
              DecoratingComponentAdapterFactory
  {
      private Class delegationStrategy;
      
      public InterceptorAdapterFactory()
      {
          this(new DefaultComponentAdapterFactory(), StandardDelegationStrategy.class);
      }
  
      public InterceptorAdapterFactory( ComponentAdapterFactory delegate,
              Class delegationStrategy )
      {
          super(delegate);
          this.delegationStrategy = delegationStrategy;
      }
  
      public ComponentAdapter createComponentAdapter( Object componentKey,
              Class componentImplementation, Parameter[] parameters )
              throws PicoIntrospectionException,
              AssignabilityRegistrationException,
              NotConcreteRegistrationException
      {
          return new InterceptorAdapter(super.createComponentAdapter(
                  componentKey, componentImplementation, parameters), delegationStrategy);
      }
  
  }
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/adapters/InterceptorAdapter.java
  
  Index: InterceptorAdapter.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   * License for the specific language governing permissions and limitations under
   * the License.
   */
  package org.apache.jetspeed.components.adapters;
  
  import java.lang.reflect.Proxy;
  
  import org.picocontainer.ComponentAdapter;
  import org.picocontainer.PicoInitializationException;
  import org.picocontainer.PicoIntrospectionException;
  import org.picocontainer.defaults.AssignabilityRegistrationException;
  import org.picocontainer.defaults.ImplementationHidingComponentAdapter;
  import org.picocontainer.defaults.InterfaceFinder;
  import org.picocontainer.defaults.NotConcreteRegistrationException;
  import org.picocontainer.defaults.Swappable;
  
  /**
   * InterceptorAdaptor
   * 
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
   * @version $Id: InterceptorAdapter.java,v 1.1 2004/05/24 21:27:44 weaver Exp $
   */
  public class InterceptorAdapter extends ImplementationHidingComponentAdapter // DecoratingComponentAdapterFactory
  {
      private Class delegationStrategyClass;
      
      public InterceptorAdapter( ComponentAdapter delegate, Class delegationStrategyClass )
      {
          super(delegate);
          this.delegationStrategyClass = delegationStrategyClass;
      }
      private final InterfaceFinder interfaceFinder = new InterfaceFinder();
  
      public Object getComponentInstance() throws PicoInitializationException,
              PicoIntrospectionException, AssignabilityRegistrationException,
              NotConcreteRegistrationException
      {
          Class[] interfaces;
          if (getDelegate().getComponentKey() instanceof Class
                  && ((Class) getDelegate().getComponentKey()).isInterface())
          {
              // If the compo
              interfaces = new Class[]{(Class) getDelegate().getComponentKey()};
          }
          else
          {
              interfaces = interfaceFinder.getInterfaces(getDelegate()
                      .getComponentImplementation());
          }
          Class[] swappableAugmentedInterfaces = new Class[interfaces.length + 1];
          swappableAugmentedInterfaces[interfaces.length] = Swappable.class;
          System.arraycopy(interfaces, 0, swappableAugmentedInterfaces, 0,
                  interfaces.length);
          if (interfaces.length == 0)
          {
              throw new PicoIntrospectionException(
                      "Can't hide implementation for "
                              + getDelegate().getComponentImplementation()
                                      .getName()
                              + ". It doesn't implement any interfaces.");
          }
          
          final DelegationStrategy delegationStrategy;
          try
          {
              delegationStrategy = (DelegationStrategy) delegationStrategyClass.newInstance();
              delegationStrategy.setAdapter(this);
          }
          catch (Exception e)
          {
              throw new PicoInitializationException("Error while creating new DelegationStartegy instance: "+e.toString(), e);
          }
          
          return Proxy.newProxyInstance(getClass().getClassLoader(),
                  swappableAugmentedInterfaces, delegationStrategy);
      }
  
      Object getDelegatedComponentInstance()
      {
          return super.getComponentInstance();
      }
  }
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/adapters/ThreadLocalDelegationStrategy.java
  
  Index: ThreadLocalDelegationStrategy.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components.adapters;
  
  /**
   * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver</a>
   *
   */
  public class ThreadLocalDelegationStrategy extends AbstractDelegationStrategy
  {
  
      private ThreadLocal localInstance=new ThreadLocal();
      
      /* (non-Javadoc)
       * @see org.apache.jetspeed.components.AbstractDelegationStrategy#getDelegatedInstance()
       */
      protected Object getDelegatedInstance()
      {        
          return localInstance.get();
      }
  
      /* (non-Javadoc)
       * @see org.apache.jetspeed.components.AbstractDelegationStrategy#setDelegatedInstance(java.lang.Object)
       */
      protected void setDelegatedInstance( Object instance )
      {
          localInstance.set(instance);
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/adapters/AbstractDelegationStrategy.java
  
  Index: AbstractDelegationStrategy.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components.adapters;
  
  import java.lang.reflect.Method;
  
  import org.picocontainer.defaults.InterfaceFinder;
  import org.picocontainer.defaults.Swappable;
  
  public abstract class AbstractDelegationStrategy implements DelegationStrategy
  {
  
      private InterceptorAdapter adapter;
      
      public Object invoke( Object proxy, Method method, Object[] args )
              throws Throwable
      {
          if(adapter == null)
          {
              throw new IllegalStateException("InterceptorAdapter cannot be null.  You must call setAdapter() before calling invoke().");
          }
          
          //System.out.println("Invoking method: " + method);
          Class declaringClass = method.getDeclaringClass();
          if (declaringClass.equals(Object.class))
          {
              if (method.equals(InterfaceFinder.hashCode))
              {
                  // Return the hashCode of ourself, as Proxy.newProxyInstance()
                  // may
                  // return cached proxies. We want a unique hashCode for each
                  // created proxy!
                  return new Integer(System
                          .identityHashCode(AbstractDelegationStrategy.this));
              }
              if (method.equals(InterfaceFinder.equals))
              {
                  return new Boolean(proxy == args[0]);
              }
              // If it's any other method defined by Object, call on ourself.
              return method.invoke(AbstractDelegationStrategy.this, args);
          }
          else if (declaringClass.equals(Swappable.class))
          {
              return method.invoke(this, args);
          }
          else
          {
              if (getDelegatedInstance() == null)
              {
                  setDelegatedInstance(adapter.getDelegatedComponentInstance());
              }
              return method.invoke(getDelegatedInstance(), args);
          }
      }
  
      public Object __hotSwap( Object newSubject )
      {
          Object result = getDelegatedInstance();
          setDelegatedInstance(newSubject);
          return result;
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.apache.jetspeed.components.DelegationStrategy#setAdapter(org.picocontainer.ComponentAdapter)
       */
      public void setAdapter( InterceptorAdapter adapter )
      {
          this.adapter = adapter;
  
      }
  
      protected abstract Object getDelegatedInstance();
     
      protected abstract void setDelegatedInstance( Object instance );
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org