db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/lib picocontainer-1.1.jar picocontainer-1.0.jar
Date Fri, 05 Nov 2004 19:43:22 GMT
tomdz       2004/11/05 11:43:22

  Modified:    src/java/org/apache/ojb/broker/core/configuration
                        PicoComponentContainer.java
                        SpringComponentContainer.java
                        ComponentContainer.java ComponentContainerBase.java
               .        .classpath
               src/test/org/apache/ojb/broker/core/configuration
                        ComponentContainerTestBase.java
  Added:       src/java/org/apache/ojb/broker/core/configuration
                        AdaptedComponentAdapterFactory.java
                        NeedsInitialization.java
                        AdaptedComponentAdapter.java
               lib      picocontainer-1.1.jar
  Removed:     lib      picocontainer-1.0.jar
  Log:
  Changed singleton instance handling for the PicoContainer ComponentContainer implementation
to use the facilities provided by PicoContainer
  Updated to PicoContainer 1.1
  
  Revision  Changes    Path
  1.2       +50 -24    db-ojb/src/java/org/apache/ojb/broker/core/configuration/PicoComponentContainer.java
  
  Index: PicoComponentContainer.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/configuration/PicoComponentContainer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PicoComponentContainer.java	27 Oct 2004 20:34:26 -0000	1.1
  +++ PicoComponentContainer.java	5 Nov 2004 19:43:22 -0000	1.2
  @@ -15,13 +15,11 @@
    * limitations under the License.
    */
   
  -
  -import java.util.HashMap;
  +import java.lang.reflect.Modifier;
   
   import org.picocontainer.ComponentAdapter;
   import org.picocontainer.MutablePicoContainer;
   import org.picocontainer.PicoException;
  -import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
   import org.picocontainer.defaults.DefaultPicoContainer;
   
   /**
  @@ -34,8 +32,6 @@
   {
       /** The container */
       private MutablePicoContainer _pico;
  -    /** The singleton instances */
  -    private HashMap _singletons = new HashMap();
   
       /**
        * Creates a new PicoContainer component container.
  @@ -43,10 +39,7 @@
       public PicoComponentContainer()
       {
           super();
  -        // we don't use the default component adapter factory because
  -        // it will create component adapters that cache the instances
  -        // which is not what we want
  -        _pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
  +        _pico = new DefaultPicoContainer(new AdaptedComponentAdapterFactory());
           _pico.registerComponentInstance(this);
       }
       
  @@ -58,7 +51,7 @@
       private PicoComponentContainer(PicoComponentContainer parent)
       {
           super(parent);
  -        _pico = new DefaultPicoContainer(parent._pico);
  +        _pico = new DefaultPicoContainer(new AdaptedComponentAdapterFactory(), parent._pico);
           _pico.registerComponentInstance(this);
       }
   
  @@ -81,48 +74,81 @@
       }
   
       /* (non-Javadoc)
  -     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class)
  +     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class, boolean)
        */
  -    public void setImplementationClass(Class baseClass, Class implClass)
  +    public void setImplementationClass(Class type, Class implClass, boolean isSingleton)
       {
  -        _pico.unregisterComponent(baseClass);
  -        _pico.registerComponentImplementation(baseClass, implClass);
  +        _pico.unregisterComponent(type);
  +
  +        AdaptedComponentAdapter adapter = (AdaptedComponentAdapter)_pico.registerComponentImplementation(type,
implClass);
  +
  +        adapter.setCaching(isSingleton);
       }
   
       /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setSingletonInstance(java.lang.Class,
java.lang.Object)
        */
  -    public void setSingletonInstance(Class clazz, Object obj)
  +    public void setSingletonInstance(Class type, Object obj)
       {
  -        _pico.unregisterComponent(clazz);
  -        _pico.registerComponentInstance(clazz, obj);
  -        _singletons.put(clazz, obj);
  +        _pico.unregisterComponent(type);
  +
  +        // we're not using PicoContainer#registerComponentInstance because that
  +        // does not use our adapter but instead a InstanceComponentAdapter
  +        AdaptedComponentAdapter adapter = (AdaptedComponentAdapter)_pico.registerComponentImplementation(type,
obj.getClass());
  +
  +        adapter.setCaching(true);
  +        adapter.setComponentInstance(obj);
       }
   
       /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getInstance(java.lang.Class)
        */
  -    public Object getInstance(Class clazz) throws ObjectCreationException, ObjectConfigurationException
  +    public Object getInstance(Class type) throws ObjectCreationException, ObjectConfigurationException
       {
  -        if (_pico.getComponentAdapter(clazz) == null)
  +        AdaptedComponentAdapter adapter = (AdaptedComponentAdapter)_pico.getComponentAdapter(type);
  +
  +        if (adapter == null)
           {
  -            throw new ObjectCreationException("No implementation type configured for "+clazz.getName());
  +            if (!type.isInterface() && !Modifier.isAbstract(type.getModifiers()))
  +            {
  +                // we're auto-registering the class because it is concrete
  +                setImplementationClass(type, type);
  +                adapter = (AdaptedComponentAdapter)_pico.getComponentAdapter(type);
  +            }
  +            else
  +            {
  +                throw new ObjectCreationException("No implementation type configured for
"+type.getName());
  +            }
           }
   
           Object obj = null;
   
           try
           {
  -            obj = _pico.getComponentInstance(clazz);
  +            obj = adapter.getComponentInstance(_pico);
           }
           catch (PicoException ex)
           {
  -            throw new ObjectCreationException("Could not create instance for "+clazz.getName(),
ex);
  +            throw new ObjectCreationException("Could not create instance for "+type.getName(),
ex);
           }
  -        if (_singletons.get(clazz) != obj)
  +        if (!adapter.isCaching())
           {
               configure(obj);
  +            if (obj instanceof NeedsInitialization)
  +            {
  +                ((NeedsInitialization)obj).initialize();
  +            }
           }
           return obj;
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#isSingletonInstance(java.lang.Class,
java.lang.Object)
  +     */
  +    public boolean isSingletonInstance(Class type, Object obj)
  +    {
  +        AdaptedComponentAdapter adapter = (AdaptedComponentAdapter)_pico.getComponentAdapter(type);
  +
  +        return adapter.isCaching() && adapter.isInstance(obj);
       }
   }
  
  
  
  1.2       +52 -13    db-ojb/src/java/org/apache/ojb/broker/core/configuration/SpringComponentContainer.java
  
  Index: SpringComponentContainer.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/configuration/SpringComponentContainer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SpringComponentContainer.java	27 Oct 2004 20:34:26 -0000	1.1
  +++ SpringComponentContainer.java	5 Nov 2004 19:43:22 -0000	1.2
  @@ -15,6 +15,8 @@
    * limitations under the License.
    */
   
  +import java.lang.reflect.Modifier;
  +
   import org.springframework.beans.BeansException;
   import org.springframework.beans.factory.BeanFactory;
   import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  @@ -102,16 +104,16 @@
       }
   
       /* (non-Javadoc)
  -     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class)
  +     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class, boolean)
        */
  -    public void setImplementationClass(Class baseClass, Class implClass)
  +    public void setImplementationClass(Class type, Class implClass, boolean isSingleton)
       {
           BeanDefinition     curBeanDef = null;
           RootBeanDefinition newBeanDef = null;
   
           try
           {
  -            curBeanDef = _beanFactory.getBeanDefinition(baseClass.getName());
  +            curBeanDef = _beanFactory.getBeanDefinition(type.getName());
           }
           catch (NoSuchBeanDefinitionException ex)
           {}
  @@ -131,45 +133,82 @@
   
           newBeanDef.setBeanClass(implClass);
           newBeanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
  -        newBeanDef.setSingleton(false);
  +        newBeanDef.setSingleton(isSingleton);
           newBeanDef.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_NONE);
  -        _beanFactory.registerBeanDefinition(baseClass.getName(), newBeanDef);
  +        _beanFactory.registerBeanDefinition(type.getName(), newBeanDef);
       }
   
       /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setSingletonInstance(java.lang.Class,
java.lang.Object)
        */
  -    public void setSingletonInstance(Class clazz, Object obj)
  +    public void setSingletonInstance(Class type, Object obj)
       {
  -        _beanFactory.registerSingleton(clazz.getName(), obj);
  -        setImplementationClass(clazz, obj.getClass());
  +        _beanFactory.registerSingleton(type.getName(), obj);
  +        setImplementationClass(type, obj.getClass());
       }
   
       /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getInstance(java.lang.Class)
        */
  -    public Object getInstance(Class clazz) throws ObjectCreationException, ObjectConfigurationException
  +    public Object getInstance(Class type) throws ObjectCreationException, ObjectConfigurationException
       {
           Object obj = null;
   
           try
           {
  -            obj = _beanFactory.getBean(clazz.getName());
  +            obj = _beanFactory.getBean(type.getName());
           }
           catch (NoSuchBeanDefinitionException ex)
           {
               // is thrown by Spring if there is the type is not registered
  -            throw new ObjectCreationException("No implementation type configured for "+clazz.getName(),
ex);
  +            if (!type.isInterface() && !Modifier.isAbstract(type.getModifiers()))
  +            {
  +                // we're auto-registering the class because it is concrete
  +                setImplementationClass(type, type);
  +                try
  +                {
  +                    obj = _beanFactory.getBean(type.getName());
  +                }
  +                catch (NoSuchBeanDefinitionException ex2)
  +                {
  +                    throw new ObjectCreationException("No implementation type configured
for "+type.getName(), ex2);
  +                }
  +            }
  +            else
  +            {
  +                throw new ObjectCreationException("No implementation type configured for
"+type.getName(), ex);
  +            }
           }
           catch (BeansException ex)
           {
  -            throw new ObjectCreationException("Could not create instance for "+clazz.getName(),
ex);
  +            throw new ObjectCreationException("Could not create instance for "+type.getName(),
ex);
           }
   
  -        if (!_beanFactory.isSingleton(clazz.getName()))
  +        if (!_beanFactory.isSingleton(type.getName()))
           {
               configure(obj);
  +            if (obj instanceof NeedsInitialization)
  +            {
  +                ((NeedsInitialization)obj).initialize();
  +            }
           }
           return obj;
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#isSingletonInstance(java.lang.Class,
java.lang.Object)
  +     */
  +    public boolean isSingletonInstance(Class type, Object obj)
  +    {
  +        if (_beanFactory.isSingleton(type.getName()))
  +        {
  +            try
  +            {
  +                return obj == _beanFactory.getBean(type.getName());
  +            }
  +            catch (NoSuchBeanDefinitionException ex)
  +            {}
  +        }
  +        return false;
       }
   }
  
  
  
  1.2       +55 -12    db-ojb/src/java/org/apache/ojb/broker/core/configuration/ComponentContainer.java
  
  Index: ComponentContainer.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/configuration/ComponentContainer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComponentContainer.java	27 Oct 2004 20:34:26 -0000	1.1
  +++ ComponentContainer.java	5 Nov 2004 19:43:22 -0000	1.2
  @@ -28,6 +28,10 @@
    */
   public interface ComponentContainer
   {
  +    // TODO: In the 1.0 version, there exists the possibility to define an interceptor
  +    //       (property InterceptorClass) which acts as a wrapper around components
  +    //       This could be easily provided by the ComponentContainer, too
  +
       /**
        * Creates a child container.
        * 
  @@ -66,56 +70,95 @@
       public void setImplementationClass(Class type, Class implClass);
   
       /**
  +     * Registers an implementation class for the given type.
  +     * 
  +     * @param type        The type to register the implementation for
  +     * @param implClass   The implementation class to register
  +     * @param isSingleton Whether exactly one instance of this type
  +     *                    shall be created
  +     */
  +    public void setImplementationClass(Class type, Class implClass, boolean isSingleton);
  +
  +    /**
        * Registers the given object as the singleton instance for the given type. Note that
        * singleton instances are assumed to be already configured.
        * 
  -     * @param clazz The class to register the object for
  -     * @param obj   The object
  +     * @param type The class to register the object for
  +     * @param obj  The object
        */
  -    public void setSingletonInstance(Class clazz, Object obj);
  +    public void setSingletonInstance(Class type, Object obj);
   
       /**
        * Return a new instance of the implementation for the given class.
        * If a singleton instance is registered for this type, then this instance
        * is returned. The instance will be configured except if it is a singleton
  -     * instance.
  +     * instance. Furthermore, if the implementation class implements the
  +     * {@link NeedsInitialization} interface, then the
  +     * {@link NeedsInitialization#initialize()} method will be called as the last
  +     * step.<br>
  +     * If there is no implementation class is defined, but the given class is concrete
  +     * then it will be instantiated instead. Note however that the same is (currently)
  +     * not possible for arguments. I.e. there must be at least one constructor where
  +     * for all arguments there are implementation classes or singleton instances
  +     * defined. 
        * 
  -     * @param clazz The class
  +     * @param type The class
        * @return The instance
        * @exception ObjectCreationException      If no implementation is configured for this
type
        * @exception ObjectConfigurationException If the instance could not be configured
        */
  -    public Object getInstance(Class clazz) throws ObjectCreationException, ObjectConfigurationException;
  +    public Object getInstance(Class type) throws ObjectCreationException, ObjectConfigurationException;
  +
  +    /**
  +     * Return the singleton instance of the implementation for the given class.
  +     * If no singleton is yet set for this class, then a new object will be created
  +     * using {@link #getInstance(Class)} and set as the singleton.
  +     * 
  +     * @param type The class
  +     * @return The instance
  +     * @exception ObjectCreationException      If no implementation is configured for this
type
  +     * @exception ObjectConfigurationException If the instance could not be configured
  +     */
  +    public Object getSingletonInstance(Class type) throws ObjectCreationException, ObjectConfigurationException;
  +
  +    /**
  +     * Determines whether the given object is the singleton instance of the given class.
  +     * 
  +     * @param type The class
  +     * @param obj   The object
  +     * @return <code>true</code> if the object is the singleton instance
  +     */
  +    public boolean isSingletonInstance(Class type, Object obj);
   
       /**
        * Returns the given property.
        * 
  -     * @param clazz The type that the property is set for
  +     * @param type The type that the property is set for
        * @param name  The property name
        * @return The value or <code>null</code> if there is no such property
        */
  -    public Object getProperty(Class clazz, String name);
  +    public Object getProperty(Class type, String name);
   
       /**
        * Returns all properties specifically defined for the specified type. Note that the
"class"
        * property if set will be not be part of the hashmap because it is directly used with
the
        * {@link #setImplementationClass(Class, Class)} method.
        * 
  -     * @param clazz The type
  +     * @param type The type
        * @return The properties or <code>null</code> if no properties are defined
for this type
        */
  -    public HashMap getProperties(Class clazz);
  +    public HashMap getProperties(Class type);
   
       /**
        * Sets the given property.
        * 
  -     * @param clazz The type to set the property for
  +     * @param type The type to set the property for
        * @param name  The property name
        * @param value The value; use <code>null</code> to unset the property
        * @param force If <code>true</code> then an existing property will be
overriden, otherwise
        *              it will only be set if not yet defined
        */
  -    public void setProperty(Class clazz, String name, Object value, boolean force); 
  +    public void setProperty(Class type, String name, Object value, boolean force); 
   
       /**
        * Sets the properties. The names of the properties are evaulated as
  
  
  
  1.2       +40 -18    db-ojb/src/java/org/apache/ojb/broker/core/configuration/ComponentContainerBase.java
  
  Index: ComponentContainerBase.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/configuration/ComponentContainerBase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComponentContainerBase.java	27 Oct 2004 20:34:26 -0000	1.1
  +++ ComponentContainerBase.java	5 Nov 2004 19:43:22 -0000	1.2
  @@ -71,32 +71,54 @@
       }
   
       /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class)
  +     */
  +    public void setImplementationClass(Class type, Class implClass)
  +    {
  +        setImplementationClass(type, implClass, false);
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getSingletonInstance(java.lang.Class)
  +     */
  +    public Object getSingletonInstance(Class type) throws ObjectCreationException, ObjectConfigurationException
  +    {
  +        Object obj = getInstance(type);
  +
  +        if (!isSingletonInstance(type, obj))
  +        {
  +            setSingletonInstance(type, obj);
  +        }
  +        return obj;
  +    }
  +    
  +    /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getProperty(java.lang.Class,
java.lang.String)
        */
  -    public Object getProperty(Class clazz, String name)
  +    public Object getProperty(Class type, String name)
       {
           if ("class".equals(name))
           {
  -            Class implClass = getImplementationClass(clazz);
  +            Class implClass = getImplementationClass(type);
   
               return implClass == null ? null : implClass.getName();
           }
           else
           {
  -            return getBeanProperty(clazz, name);
  +            return getBeanProperty(type, name);
           }
       }
   
       /**
        * Returns the given bean property.
        * 
  -     * @param clazz The type that the property is set for
  -     * @param name  The property name
  +     * @param type The type that the property is set for
  +     * @param name The property name
        * @return The value or <code>null</code> if there is no such property
        */
  -    protected Object getBeanProperty(Class clazz, String name)
  +    protected Object getBeanProperty(Class type, String name)
       {
  -        HashMap props = getProperties(clazz);
  +        HashMap props = getProperties(type);
   
           return props == null ? null : props.get(name);
       }
  @@ -104,32 +126,32 @@
       /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getProperties(java.lang.Class)
        */
  -    public HashMap getProperties(Class clazz)
  +    public HashMap getProperties(Class type)
       {
  -        if (clazz == null)
  +        if (type == null)
           {
               throw new NullPointerException("No class specified");
           }
  -        return (HashMap)_propsPerClass.get(clazz);
  +        return (HashMap)_propsPerClass.get(type);
       }
   
       /* (non-Javadoc)
        * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setProperty(java.lang.Class,
java.lang.String, java.lang.Object, boolean)
        */
  -    public void setProperty(Class clazz, String name, Object value, boolean force)
  +    public void setProperty(Class type, String name, Object value, boolean force)
       {
           // "class" is a special property that corresponds to registerImplementation
           if ("class".equals(name))
           {
               if (value instanceof Class)
               {
  -                setImplementationClass(clazz, (Class)value);
  +                setImplementationClass(type, (Class)value);
               }
               else if (value instanceof String)
               {
                   try
                   {
  -                    setImplementationClass(clazz, getClassByName((String)value));
  +                    setImplementationClass(type, getClassByName((String)value));
                   }
                   catch (ClassNotFoundException ex)
                   {
  @@ -143,27 +165,27 @@
           }
           else
           {
  -            setBeanProperty(clazz, name, value, force);
  +            setBeanProperty(type, name, value, force);
           }
       }
   
       /**
        * Sets the given bean property.
        * 
  -     * @param clazz The type to set the property for
  +     * @param type  The type to set the property for
        * @param name  The property name
        * @param value The value; use <code>null</code> to unset the property
        * @param force If <code>true</code> then an existing property will be
overriden, otherwise
        *              it will only be set if not yet defined
        */
  -    protected void setBeanProperty(Class clazz, String name, Object value, boolean force)
  +    protected void setBeanProperty(Class type, String name, Object value, boolean force)
       {
  -        HashMap props = getProperties(clazz);
  +        HashMap props = getProperties(type);
   
           if (props == null)
           {
               props = new HashMap();
  -            _propsPerClass.put(clazz, props);
  +            _propsPerClass.put(type, props);
           }
           if (force || !props.containsKey(name))
           {
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/AdaptedComponentAdapterFactory.java
  
  Index: AdaptedComponentAdapterFactory.java
  ===================================================================
  package org.apache.ojb.broker.core.configuration;
  
  /* Copyright 2002-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.
   */
  
  import java.io.Serializable;
  
  import org.picocontainer.ComponentAdapter;
  import org.picocontainer.Parameter;
  import org.picocontainer.PicoIntrospectionException;
  import org.picocontainer.defaults.AssignabilityRegistrationException;
  import org.picocontainer.defaults.ComponentAdapterFactory;
  import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
  import org.picocontainer.defaults.NotConcreteRegistrationException;
  
  /**
   * A <code>ComponentAdapterFactory</code> for creating {@link AdaptedComponentAdapter}
   * instances.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class AdaptedComponentAdapterFactory implements ComponentAdapterFactory, Serializable
  {
      public ComponentAdapter createComponentAdapter(Object      componentKey,
                                                     Class       componentImplementation,
                                                     Parameter[] parameters) throws PicoIntrospectionException,
                                                                                    AssignabilityRegistrationException,
                                                                                    NotConcreteRegistrationException
      {
          return new AdaptedComponentAdapter(new ConstructorInjectionComponentAdapter(componentKey,
componentImplementation, parameters));
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/NeedsInitialization.java
  
  Index: NeedsInitialization.java
  ===================================================================
  package org.apache.ojb.broker.core.configuration;
  
  /* Copyright 2002-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.
   */
  
  /**
   * This interface should be used for types managed by the
   * {@link org.apache.ojb.broker.core.configuration.ComponentContainer} that
   * require initialization after an instance is created and configured. In that
   * case, the {@link #initialize()} method is called.
   *
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
   */
  public interface NeedsInitialization
  {
      /**
       * Performs initialization.
       */
      public void initialize();
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/AdaptedComponentAdapter.java
  
  Index: AdaptedComponentAdapter.java
  ===================================================================
  package org.apache.ojb.broker.core.configuration;
  
  /* Copyright 2002-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.
   */
  
  import org.picocontainer.ComponentAdapter;
  import org.picocontainer.PicoContainer;
  import org.picocontainer.PicoInitializationException;
  import org.picocontainer.PicoIntrospectionException;
  import org.picocontainer.defaults.DecoratingComponentAdapter;
  import org.picocontainer.defaults.ObjectReference;
  import org.picocontainer.defaults.SimpleReference;
  
  /**
   * A <code>ComponentAdapter</code> that allows to selectively cache the created
   * instance thus allowing for singletons-per-class. As such it is a variation
   * of the <code>CachingComponentAdapter</code> that comes with PicoContainer.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class AdaptedComponentAdapter extends DecoratingComponentAdapter
  {
      /** The instance */
      private ObjectReference _reference;
      /** Whether we're caching the instance */
      private boolean _caching = false;
  
      /**
       * Creates a new adapter.
       * 
       * @param delegate The wrapped adapter
       */
      public AdaptedComponentAdapter(ComponentAdapter delegate)
      {
          this(delegate, new SimpleReference());
      }
  
      /**
       * Creates a new adapter.
       * 
       * @param delegate  The wrapped adapter
       * @param reference The reference to the instance
       */
      public AdaptedComponentAdapter(ComponentAdapter delegate, ObjectReference reference)
      {
          super(delegate);
          _reference = reference;
      }
  
      /**
       * Returns whether this adapter instance caches the instance.
       * 
       * @return <code>true</code> if this adapter caches the instance
       */
      public boolean isCaching()
      {
          return _caching;
      }
  
      /**
       * Specifies whether this adapter instance caches the instance.
       * 
       * @param caching <code>true</code> if this adapter caches the instance
       */
      public void setCaching(boolean caching)
      {
          _caching = caching;
      }
  
      /**
       * Determines whether this adapter contains the given object.
       * 
       * @param obj The object
       * @return <code>true</code> if it is the object currently contained in
this adapter
       */
      public boolean isInstance(Object obj)
      {
          return _reference.get() == obj;
      }
  
      /* (non-Javadoc)
       * @see org.picocontainer.ComponentAdapter#getComponentInstance(org.picocontainer.PicoContainer)
       */
      public Object getComponentInstance(PicoContainer pico) throws PicoInitializationException,
PicoIntrospectionException
      {
          if (!_caching || (_reference.get() == null))
          {
              _reference.set(super.getComponentInstance(pico));
          }
          return _reference.get();
      }
  
      /**
       * Sets the instance of this adapter.
       * 
       * @param obj The object
       */
      public void setComponentInstance(Object obj)
      {
          _reference.set(obj);
      }
  }
  
  
  
  1.42      +2 -2      db-ojb/.classpath
  
  Index: .classpath
  ===================================================================
  RCS file: /home/cvs/db-ojb/.classpath,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- .classpath	27 Oct 2004 20:34:26 -0000	1.41
  +++ .classpath	5 Nov 2004 19:43:22 -0000	1.42
  @@ -41,7 +41,7 @@
   	<classpathentry kind="lib" path="lib/commons-digester-1.5.jar"/>
   	<classpathentry kind="lib" path="lib/commons-grafolia-1.0.jar"/>
   	<classpathentry kind="lib" path="lib/commons-graph-0.8.1.jar"/>
  -	<classpathentry kind="lib" path="lib/picocontainer-1.0.jar"/>
  +	<classpathentry kind="lib" path="lib/picocontainer-1.1.jar"/>
   	<classpathentry kind="lib" path="lib/spring-core.jar"/>
   	<classpathentry kind="output" path="target/classes/prod"/>
   </classpath>
  
  
  
  1.2       +62 -0     db-ojb/src/test/org/apache/ojb/broker/core/configuration/ComponentContainerTestBase.java
  
  Index: ComponentContainerTestBase.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/core/configuration/ComponentContainerTestBase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComponentContainerTestBase.java	27 Oct 2004 20:47:25 -0000	1.1
  +++ ComponentContainerTestBase.java	5 Nov 2004 19:43:22 -0000	1.2
  @@ -235,6 +235,68 @@
           assertNotNull(obj2);
       }
   
  +    public void testCreation15()
  +    {
  +        _container.setImplementationClass(AImpl.class, AImpl.class);
  +
  +        AImpl obj = (AImpl)_container.getInstance(AImpl.class);
  +
  +        assertNotNull(obj);
  +    }
  +
  +    public void testCreation16()
  +    {
  +        // not type is configured but the class is concrete
  +        AImpl obj = (AImpl)_container.getInstance(AImpl.class);
  +
  +        assertNotNull(obj);
  +    }
  +
  +    public void testCreation17()
  +    {
  +        _container.setImplementationClass(A.class, AImpl.class);
  +        _container.setImplementationClass(C.class, CImpl.class);
  +
  +        C obj1 = (C)_container.getInstance(C.class);
  +        A obj2 = (A)_container.getInstance(A.class);
  +
  +        assertTrue(obj1 instanceof CImpl);
  +        assertTrue(obj2 instanceof AImpl);
  +
  +        // not the same objects because we're not creating singletons
  +        assertFalse(((CImpl)obj1).getSub() == obj2);
  +    }
  +
  +    public void testCreation18()
  +    {
  +        _container.setImplementationClass(A.class, AImpl.class, true);
  +        _container.setImplementationClass(C.class, CImpl.class);
  +
  +        C obj1 = (C)_container.getInstance(C.class);
  +        A obj2 = (A)_container.getInstance(A.class);
  +
  +        assertTrue(obj1 instanceof CImpl);
  +        assertTrue(obj2 instanceof AImpl);
  +
  +        // this time the same objects because we're creating singletons for A
  +        assertTrue(((CImpl)obj1).getSub() == obj2);
  +    }
  +
  +    public void testCreation19()
  +    {
  +        _container.setImplementationClass(A.class, AImpl.class, true);
  +        _container.setImplementationClass(C.class, CImpl.class);
  +
  +        C obj1 = (C)_container.getInstance(C.class);
  +        A obj2 = (A)_container.getInstance(A.class);
  +
  +        assertTrue(obj1 instanceof CImpl);
  +        assertTrue(obj2 instanceof AImpl);
  +
  +        // this time the same objects because we're creating singletons for A
  +        assertTrue(((CImpl)obj1).getSub() == obj2);
  +    }
  +
       public void testConfiguration1()
       {
           _container.setImplementationClass(A.class, AImpl.class);
  
  
  
  1.1                  db-ojb/lib/picocontainer-1.1.jar
  
  	<<Binary file>>
  
  

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


Mime
View raw message