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 spring-core.jar picocontainer-1.0.jar
Date Wed, 27 Oct 2004 20:34:27 GMT
tomdz       2004/10/27 13:34:27

  Modified:    .        .classpath
  Added:       src/java/org/apache/ojb/broker/core/configuration
                        PicoComponentContainer.java
                        ObjectCreationException.java
                        SpringComponentContainer.java
                        ObjectConfigurationException.java
                        ContainerInitializationException.java
                        AdaptedBeanFactory.java ComponentContainer.java
                        ComponentContainerBase.java
               lib      spring-core.jar picocontainer-1.0.jar
  Log:
  Initial checkin of the ComponentContainer interface and two implementations using PicoContainer
and Spring.
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/PicoComponentContainer.java
  
  Index: PicoComponentContainer.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.util.HashMap;
  
  import org.picocontainer.ComponentAdapter;
  import org.picocontainer.MutablePicoContainer;
  import org.picocontainer.PicoException;
  import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
  import org.picocontainer.defaults.DefaultPicoContainer;
  
  /**
   * A PicoContainer {@link org.apache.ojb.broker.core.configuration.ComponentContainer}
   * implementation.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class PicoComponentContainer extends ComponentContainerBase
  {
      /** The container */
      private MutablePicoContainer _pico;
      /** The singleton instances */
      private HashMap _singletons = new HashMap();
  
      /**
       * Creates a new PicoContainer component container.
       */
      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.registerComponentInstance(this);
      }
      
      /**
       * Creates a new child component container.
       * 
       * @param parent The parent container
       */
      private PicoComponentContainer(PicoComponentContainer parent)
      {
          super(parent);
          _pico = new DefaultPicoContainer(parent._pico);
          _pico.registerComponentInstance(this);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getChildFactory()
       */
      public ComponentContainer createChildContainer()
      {
          return new PicoComponentContainer(this);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getImplementationClass(java.lang.Class)
       */
      public Class getImplementationClass(Class type)
      {
          ComponentAdapter adapter = _pico.getComponentAdapter(type);
  
          return adapter == null ? null : adapter.getComponentImplementation();
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class)
       */
      public void setImplementationClass(Class baseClass, Class implClass)
      {
          _pico.unregisterComponent(baseClass);
          _pico.registerComponentImplementation(baseClass, implClass);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setSingletonInstance(java.lang.Class,
java.lang.Object)
       */
      public void setSingletonInstance(Class clazz, Object obj)
      {
          _pico.unregisterComponent(clazz);
          _pico.registerComponentInstance(clazz, obj);
          _singletons.put(clazz, obj);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getInstance(java.lang.Class)
       */
      public Object getInstance(Class clazz) throws ObjectCreationException, ObjectConfigurationException
      {
          if (_pico.getComponentAdapter(clazz) == null)
          {
              throw new ObjectCreationException("No implementation type configured for "+clazz.getName());
          }
  
          Object obj = null;
  
          try
          {
              obj = _pico.getComponentInstance(clazz);
          }
          catch (PicoException ex)
          {
              throw new ObjectCreationException("Could not create instance for "+clazz.getName(),
ex);
          }
          if (_singletons.get(clazz) != obj)
          {
              configure(obj);
          }
          return obj;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/ObjectCreationException.java
  
  Index: ObjectCreationException.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.apache.ojb.broker.OJBRuntimeException;
  
  /**
   * This exception is thrown by the {@link org.apache.ojb.broker.core.configuration.ComponentContainer}
   * if an object could not be created.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class ObjectCreationException extends OJBRuntimeException
  {
      /**
       * Creates a new exception object.
       */
      public ObjectCreationException()
      {
          super();
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param message The exception message
       */
      public ObjectCreationException(String message)
      {
          super(message);
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param cause The original exception
       */
      public ObjectCreationException(Throwable cause)
      {
          super(cause);
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param message The message
       * @param cause   The original exception
       */
      public ObjectCreationException(String message, Throwable cause)
      {
          super(message, cause);
      }
  
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/SpringComponentContainer.java
  
  Index: SpringComponentContainer.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.springframework.beans.BeansException;
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  import org.springframework.beans.factory.config.BeanDefinition;
  import org.springframework.beans.factory.support.RootBeanDefinition;
  
  /**
   * An {@link org.apache.ojb.broker.core.configuration.ComponentContainer} implementation
   * that uses Spring's <code>BeanFactory</code>. Note that we do not use the
   * <code>BeanFactory</code> to set the bean properties at the created objects
though
   * they can be used independently.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class SpringComponentContainer extends ComponentContainerBase
  {
      /** Spring's bean factory */
      private AdaptedBeanFactory _beanFactory;
  
      /**
       * Creates a new component container.
       */
      public SpringComponentContainer()
      {
          super();
          _beanFactory = new AdaptedBeanFactory();
          _beanFactory.setAllowBeanDefinitionOverriding(true);
      }
      
      /**
       * Creates a new component container that uses a given bean factory. Please note that
       * the this container will create a bean factory of its own that is a child of the given
       * bean factory. 
       * 
       * @param beanFactory The bean factory to use
       */
      public SpringComponentContainer(BeanFactory beanFactory)
      {
          super();
          _beanFactory = new AdaptedBeanFactory(beanFactory);
          _beanFactory.setAllowBeanDefinitionOverriding(true);
      }
  
      /**
       * Creates a new child component container.
       * 
       * @param parent The parent container
       */
      private SpringComponentContainer(SpringComponentContainer parent)
      {
          super(parent);
          _beanFactory = new AdaptedBeanFactory(parent._beanFactory);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#createChildFactory()
       */
      public ComponentContainer createChildContainer()
      {
          return new SpringComponentContainer(this);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getImplementationClass(java.lang.Class)
       */
      public Class getImplementationClass(Class type)
      {
          try
          {
              BeanDefinition beanDef = _beanFactory.getBeanDefinition(type.getName());
  
              if (beanDef == null)
              {
                  return getParentContainer() != null ? getParentContainer().getImplementationClass(type)
: null;
              }
              else
              {
                  return beanDef.getBeanClass();
              }
          }
          catch (NoSuchBeanDefinitionException ex)
          {
              return null;
          }
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setImplementationClass(java.lang.Class,
java.lang.Class)
       */
      public void setImplementationClass(Class baseClass, Class implClass)
      {
          BeanDefinition     curBeanDef = null;
          RootBeanDefinition newBeanDef = null;
  
          try
          {
              curBeanDef = _beanFactory.getBeanDefinition(baseClass.getName());
          }
          catch (NoSuchBeanDefinitionException ex)
          {}
  
          if ((curBeanDef != null) && (curBeanDef instanceof RootBeanDefinition))
          {
              newBeanDef = (RootBeanDefinition)curBeanDef;
          }
          else
          {
              newBeanDef = new RootBeanDefinition(implClass, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
              if (curBeanDef != null)
              {
                  newBeanDef.setPropertyValues(curBeanDef.getPropertyValues());
              }
          }
  
          newBeanDef.setBeanClass(implClass);
          newBeanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
          newBeanDef.setSingleton(false);
          newBeanDef.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_NONE);
          _beanFactory.registerBeanDefinition(baseClass.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)
      {
          _beanFactory.registerSingleton(clazz.getName(), obj);
          setImplementationClass(clazz, obj.getClass());
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getInstance(java.lang.Class)
       */
      public Object getInstance(Class clazz) throws ObjectCreationException, ObjectConfigurationException
      {
          Object obj = null;
  
          try
          {
              obj = _beanFactory.getBean(clazz.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);
          }
          catch (BeansException ex)
          {
              throw new ObjectCreationException("Could not create instance for "+clazz.getName(),
ex);
          }
  
          if (!_beanFactory.isSingleton(clazz.getName()))
          {
              configure(obj);
          }
          return obj;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/ObjectConfigurationException.java
  
  Index: ObjectConfigurationException.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.apache.ojb.broker.OJBRuntimeException;
  
  /**
   * This exception is thrown by the {@link org.apache.ojb.broker.core.configuration.ComponentContainer}
   * if an object could not be configured.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class ObjectConfigurationException extends OJBRuntimeException
  {
      /**
       * Creates a new exception object.
       */
      public ObjectConfigurationException()
      {
          super();
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param message The exception message
       */
      public ObjectConfigurationException(String message)
      {
          super(message);
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param cause The original exception
       */
      public ObjectConfigurationException(Throwable cause)
      {
          super(cause);
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param message The message
       * @param cause   The original exception
       */
      public ObjectConfigurationException(String message, Throwable cause)
      {
          super(message, cause);
      }
  
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/ContainerInitializationException.java
  
  Index: ContainerInitializationException.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.apache.ojb.broker.OJBRuntimeException;
  
  /**
   * This exception is thrown by the {@link org.apache.ojb.broker.core.configuration.ComponentContainer}
   * when an error during initialization occurs.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class ContainerInitializationException extends OJBRuntimeException
  {
      /**
       * Creates a new exception object.
       */
      public ContainerInitializationException()
      {
          super();
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param message The exception message
       */
      public ContainerInitializationException(String message)
      {
          super(message);
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param cause The original exception
       */
      public ContainerInitializationException(Throwable cause)
      {
          super(cause);
      }
  
      /**
       * Creates a new exception object.
       * 
       * @param message The message
       * @param cause   The original exception
       */
      public ContainerInitializationException(String message, Throwable cause)
      {
          super(message, cause);
      }
  
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/AdaptedBeanFactory.java
  
  Index: AdaptedBeanFactory.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.springframework.beans.factory.BeanDefinitionStoreException;
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  import org.springframework.beans.factory.support.SimpleInstantiationStrategy;
  
  /**
   * Adapted spring bean factory. Is overriden mainly for the access to protected methods.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public class AdaptedBeanFactory extends DefaultListableBeanFactory
  {
  
      /**
       * Creates a new bean factory.
       */
      public AdaptedBeanFactory()
      {
          super();
          setInstantiationStrategy(new SimpleInstantiationStrategy());
      }
  
      /**
       * Creates a new child bean factory
       * @param parent
       */
      public AdaptedBeanFactory(BeanFactory parent)
      {
          super(parent);
          setInstantiationStrategy(new SimpleInstantiationStrategy());
      }
  
      /* (non-Javadoc)
       * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#registerSingleton(java.lang.String,
java.lang.Object)
       */
      public void registerSingleton(String beanName, Object singletonObject) throws BeanDefinitionStoreException
      {
          removeSingleton(beanName);
          super.registerSingleton(beanName, singletonObject);
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/ComponentContainer.java
  
  Index: ComponentContainer.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.util.HashMap;
  import java.util.Properties;
  
  /**
   * Defines a factory and container for creating and maintaining configurable components.
   * The component objects are initialized via the IoC principle. Containers are
   * hierarchical where the children containers look at their parents if they cannot
   * handle a request themselves.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public interface ComponentContainer
  {
      /**
       * Creates a child container.
       * 
       * @return The new child container
       */
      public ComponentContainer createChildContainer();
  
      /**
       * Sets the parent container.
       * 
       * @param parent The parent container
       */
      public void setParentContainer(ComponentContainer parent);
  
      /**
       * Returns the parent container if any.
       * 
       * @return The parent container
       */
      public ComponentContainer getParentContainer();
  
      /**
       * Returns the registered implementation class for the given type.
       * 
       * @param type The type to register the implementation for
       * @return The registered class
       */
      public Class getImplementationClass(Class type);
  
      /**
       * Registers an implementation class for the given type.
       * 
       * @param type      The type to register the implementation for
       * @param implClass The implementation class to register
       */
      public void setImplementationClass(Class type, Class implClass);
  
      /**
       * 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
       */
      public void setSingletonInstance(Class clazz, 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.
       * 
       * @param clazz 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;
  
      /**
       * Returns the given property.
       * 
       * @param clazz 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);
  
      /**
       * 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
       * @return The properties or <code>null</code> if no properties are defined
for this type
       */
      public HashMap getProperties(Class clazz);
  
      /**
       * Sets the given property.
       * 
       * @param clazz 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); 
  
      /**
       * Sets the properties. The names of the properties are evaulated as
       * fully qualified property names, e.g. package.Class.propertyName.
       * 
       * @param props The properties
       * @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 setProperties(Properties props, boolean force) throws ContainerInitializationException;
  
      /**
       * Configures the given object, i.e. sets all properties that
       * are defined for the types of the object.
       * 
       * @param obj The object
       */
      public void configure(Object obj) throws ObjectConfigurationException;
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/configuration/ComponentContainerBase.java
  
  Index: ComponentContainerBase.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.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Map.Entry;
  
  import org.apache.commons.beanutils.BeanUtils;
  
  /**
   * Convenience base class for component containers.
   * 
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak<a>
   */
  public abstract class ComponentContainerBase implements ComponentContainer
  {
      /** The base container */
      private ComponentContainer _parentContainer;
      /** Contains the properties keyed by the class */
      private HashMap _propsPerClass = new HashMap();
  
      /**
       * For creating a container without a parent.
       */
      public ComponentContainerBase()
      {
          _parentContainer = null;
      }
      
      /**
       * For creating a container with a parent.
       * 
       * @param parent The parent container
       */
      public ComponentContainerBase(ComponentContainer parent)
      {
          _parentContainer = parent;
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setParentFactory(org.apache.ojb.broker.core.configuration.ComponentContainer)
       */
      public void setParentContainer(ComponentContainer parent)
      {
          _parentContainer = parent;
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getParentFactory()
       */
      public ComponentContainer getParentContainer()
      {
          return _parentContainer;
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getProperty(java.lang.Class,
java.lang.String)
       */
      public Object getProperty(Class clazz, String name)
      {
          if ("class".equals(name))
          {
              Class implClass = getImplementationClass(clazz);
  
              return implClass == null ? null : implClass.getName();
          }
          else
          {
              return getBeanProperty(clazz, name);
          }
      }
  
      /**
       * Returns the given bean property.
       * 
       * @param clazz 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)
      {
          HashMap props = getProperties(clazz);
  
          return props == null ? null : props.get(name);
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#getProperties(java.lang.Class)
       */
      public HashMap getProperties(Class clazz)
      {
          if (clazz == null)
          {
              throw new NullPointerException("No class specified");
          }
          return (HashMap)_propsPerClass.get(clazz);
      }
  
      /* (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)
      {
          // "class" is a special property that corresponds to registerImplementation
          if ("class".equals(name))
          {
              if (value instanceof Class)
              {
                  setImplementationClass(clazz, (Class)value);
              }
              else if (value instanceof String)
              {
                  try
                  {
                      setImplementationClass(clazz, getClassByName((String)value));
                  }
                  catch (ClassNotFoundException ex)
                  {
                      throw new ContainerInitializationException(ex);
                  }
              }
              else
              {
                  throw new ContainerInitializationException("Illegal property type of value
for property "+name);
              }
          }
          else
          {
              setBeanProperty(clazz, name, value, force);
          }
      }
  
      /**
       * Sets the given bean property.
       * 
       * @param clazz 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)
      {
          HashMap props = getProperties(clazz);
  
          if (props == null)
          {
              props = new HashMap();
              _propsPerClass.put(clazz, props);
          }
          if (force || !props.containsKey(name))
          {
              if (value == null)
              {
                  props.remove(name);
              }
              else
              {
                  props.put(name, value);
              }
          }
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#setProperties(java.util.Properties,
boolean)
       */
      public void setProperties(Properties props, boolean force) throws ContainerInitializationException
      {
          for (Iterator entryIt = props.entrySet().iterator(); entryIt.hasNext();)
          {
              Entry  entry  = (Entry)entryIt.next();
              String name   = (String)entry.getKey();
              int    dotPos = name.lastIndexOf('.');
  
              if (dotPos <= 0)
              {
                  throw new ContainerInitializationException("Illegal unqualified property
"+name);
              }
  
              try
              {
                  setProperty(getClassByName(name.substring(0, dotPos)),
                              name.substring(dotPos + 1),
                              entry.getValue(),
                              force);
              }
              catch (Exception ex)
              {
                  throw new ContainerInitializationException(ex);
              }
          }
      }
  
      /* (non-Javadoc)
       * @see org.apache.ojb.broker.core.configuration.ComponentContainer#configure(java.lang.Object)
       */
      public void configure(Object obj) throws ObjectConfigurationException
      {
          ArrayList containers = flattenContainerHierarchy();
          
          try
          {
              for (Iterator typeIt = getAllTypes(obj).iterator(); typeIt.hasNext();)
              {
                  Class clazz = (Class)typeIt.next();
  
                  for (Iterator containerIt = containers.iterator(); containerIt.hasNext();)
                  {
                      HashMap props = ((ComponentContainer)containerIt.next()).getProperties(clazz);
                      
                      if (props != null)
                      {
                          for (Iterator propIt = props.entrySet().iterator(); propIt.hasNext();)
                          {
                              Map.Entry entry = (Map.Entry)propIt.next();
          
                              BeanUtils.setProperty(obj, (String)entry.getKey(), entry.getValue());
                          }
                      }
                  }
              }
          }
          catch (Exception ex)
          {
              throw new ObjectConfigurationException(ex);
          }
      }
  
      /**
       * Returns the class object for the indicated class.
       * 
       * @param name The class name
       * @return The class object
       * @throws ClassNotFoundException If there is no such class
       */
      protected Class getClassByName(String name) throws ClassNotFoundException
      {
          // TODO: Use OJB's ClassHelper once this is part of OJB
          return Class.forName(name);
      }
      
      /**
       * Returns a list of this and all its parent factories, starting with the most base
       * factory.
       * 
       * @return The list of factories
       */
      protected ArrayList flattenContainerHierarchy()
      {
          ArrayList          containers = new ArrayList();
          ComponentContainer parent     = _parentContainer;
  
          containers.add(this);
          while (parent != null)
          {
              containers.add(0, parent);
              parent = parent.getParentContainer();
          }
          return containers;
      }
  
      /**
       * Returns all (base) types of the given object. If the object is a class, then
       * itself and all its super types are used, otherwise the types of the object.
       * 
       * @param obj The object or class
       * @return A list of all types of the object/class
       */
      protected ArrayList getAllTypes(Object obj)
      {
          ArrayList types = new ArrayList();
          ArrayList queue = new ArrayList();
          Class     type  = obj instanceof Class ? (Class)obj : obj.getClass();
  
          queue.add(type);
          while (!queue.isEmpty())
          {
              type = (Class)queue.get(0);
              queue.remove(0);
              types.add(0, type);
  
              if (!type.isInterface())
              {
                  Class baseClass = type.getSuperclass();
  
                  if (baseClass != null)
                  {
                      queue.add(baseClass);
                  }
              }
  
              Class[] interfaces = type.getInterfaces();
  
              if (interfaces != null)
              {
                  for (int idx = 0; idx < interfaces.length; idx++)
                  {
                      queue.add(interfaces[idx]);
                  }
              }
          }
          return types;
      }
  }
  
  
  
  1.41      +3 -1      db-ojb/.classpath
  
  Index: .classpath
  ===================================================================
  RCS file: /home/cvs/db-ojb/.classpath,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- .classpath	15 Aug 2004 21:51:38 -0000	1.40
  +++ .classpath	27 Oct 2004 20:34:26 -0000	1.41
  @@ -41,5 +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/spring-core.jar"/>
   	<classpathentry kind="output" path="target/classes/prod"/>
   </classpath>
  
  
  
  1.1                  db-ojb/lib/spring-core.jar
  
  	<<Binary file>>
  
  
  1.1                  db-ojb/lib/picocontainer-1.0.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