db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/core PersistenceBrokerFactoryBaseImpl.java PersistenceBrokerHandle.java PersistenceBrokerFactoryIF.java PersistenceBrokerFactoryFactory.java PersistenceBrokerFactoryDefaultImpl.java
Date Tue, 28 Oct 2003 21:18:48 GMT
arminw      2003/10/28 13:18:48

  Modified:    src/java/org/apache/ojb/broker PersistenceBrokerFactory.java
               src/java/org/apache/ojb/broker/metadata MetadataManager.java
               src/java/org/apache/ojb/broker/core
                        PersistenceBrokerHandle.java
                        PersistenceBrokerFactoryIF.java
                        PersistenceBrokerFactoryFactory.java
                        PersistenceBrokerFactoryDefaultImpl.java
  Added:       src/java/org/apache/ojb/broker/core
                        PersistenceBrokerFactoryBaseImpl.java
  Log:
  - add set/getDefaultKey method to MetadataManager
  - in PBF implementation forward set/getDefaultKey
  calls to MetadataManager
  
  - add base class PersistenceBrokerBaseImpl
  (do not pool PB instances)
  - PersistenceBrokerDefaultImpl now extends base class
  (pools PB instances)
  
  - PersistenceBrokerDefaultImpl add protected methods
  for better extensibility
  
  - improve/update javadocs
  
  Revision  Changes    Path
  1.21      +15 -22    db-ojb/src/java/org/apache/ojb/broker/PersistenceBrokerFactory.java
  
  Index: PersistenceBrokerFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/PersistenceBrokerFactory.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- PersistenceBrokerFactory.java	29 Aug 2003 09:35:43 -0000	1.20
  +++ PersistenceBrokerFactory.java	28 Oct 2003 21:18:47 -0000	1.21
  @@ -56,10 +56,14 @@
   
   import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
   import org.apache.ojb.broker.util.configuration.Configurator;
  +import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
  +import org.apache.ojb.broker.metadata.MetadataManager;
   
   /**
  - * Factory class that produces PersistenceBroker instances.
  + * Convenience factory class that produces {@link PersistenceBroker} instances.
    *
  + * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory
  + * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF
    * @author Thomas Mahler
    * @version $Id$
    */
  @@ -73,32 +77,27 @@
        */
       public static Configurator getConfigurator()
       {
  -        return PersistenceBrokerFactoryFactory.getConfigurator();
  +        return OjbConfigurator.getInstance();
       }
   
       /**
  -     * Set the default PersistenceBroker (e.g. used within OJB
  -     * to find OJB internal tables)
  +     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#setDefaultKey
        */
       public static void setDefaultKey(PBKey key)
       {
  -        PersistenceBrokerFactoryFactory.instance().setDefaultKey(key);
  +        MetadataManager.getInstance().setDefaultPBKey(key);
       }
   
       /**
  -     * Get the default PersistenceBroker (e.g. used within OJB
  -     * to find OJB internal tables)
  +     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#getDefaultKey
        */
       public static PBKey getDefaultKey()
       {
  -        return PersistenceBrokerFactoryFactory.instance().getDefaultKey();
  +        return MetadataManager.getInstance().getDefaultPBKey();
       }
   
       /**
  -     * Return a default broker instance, specified in configuration.
  -     *
  -     * Will cause the OJB.properties file to load via {@link org.apache.ojb.broker.util.configuration.impl.OjbConfiguration#load
OjbConfiguration.load()}.
  -     *
  +     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#defaultPersistenceBroker
        */
       public static PersistenceBroker defaultPersistenceBroker() throws PBFactoryException
       {
  @@ -107,10 +106,7 @@
       }
   
       /**
  -     * Return a ready for action {@link org.apache.ojb.broker.PersistenceBroker} instance.
  -     * @param jcdAlias An jcdAlias name specified in the <code>jdbc-connection-descriptor</code>'s
  -     * @param user user name
  -     * @param password valid password
  +     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#createPersistenceBroker(java.lang.String,
java.lang.String, java.lang.String)
        */
       public static PersistenceBroker createPersistenceBroker(String jcdAlias,
                                                               String user, String password)
  @@ -121,9 +117,7 @@
       }
   
       /**
  -     * Return {@link org.apache.ojb.broker.PersistenceBroker} instance for the given
  -     * {@link org.apache.ojb.broker.PBKey}.
  -     * @param key
  +     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#createPersistenceBroker(org.apache.ojb.broker.PBKey)
        */
       public static PersistenceBroker createPersistenceBroker(PBKey key)
               throws PBFactoryException
  @@ -132,8 +126,7 @@
       }
   
       /**
  -     * release all broker instances pooled by the factory.
  -     * each broker instance is closed before release.
  +     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#releaseAllInstances
        */
       public static void releaseAllInstances()
       {
  
  
  
  1.12      +60 -36    db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java
  
  Index: MetadataManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- MetadataManager.java	4 Aug 2003 11:19:32 -0000	1.11
  +++ MetadataManager.java	28 Oct 2003 21:18:47 -0000	1.12
  @@ -5,6 +5,7 @@
   import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.broker.PBKey;
   
   import java.io.InputStream;
   import java.util.Hashtable;
  @@ -112,6 +113,7 @@
       private DescriptorRepository globalRepository;
       private ConnectionRepository connectionRepository;
       private boolean enablePerThreadChanges;
  +    private PBKey defaultPBKey;
   
       // singleton
       private MetadataManager()
  @@ -142,6 +144,7 @@
               throw new MetadataException(
                       "Can not read jdbc-connection-descriptor data, using repository: "
+ repository, e);
           }
  +        this.defaultPBKey = buildDefaultKey();
       }
   
       /**
  @@ -159,7 +162,7 @@
        * <br>
        * When {@link #isEnablePerThreadChanges per thread descriptor handling}  is enabled
        * we first search for a specific {@link org.apache.ojb.broker.metadata.DescriptorRepository}
  -     * for the calling thread, if none could be found the global descriptor was returned.
  +     * for the calling thread, if none can be found the global descriptor was returned.
        *
        * @see MetadataManager#getGlobalRepository
        * @see MetadataManager#copyOfGlobalRepository
  @@ -173,6 +176,7 @@
               if (repository == null)
               {
                   repository = getGlobalRepository();
  +                log.info("Can't find DescriptorRepository for current thread, use default
one");
               }
               return repository;
           }
  @@ -244,9 +248,7 @@
        * in mind if running in <a href="#perThread">per thread mode</a>
        * merge maybe only takes effect on current thread.
        *
  -     * @see mergeDescriptorRepository(DescriptorRepository targetRepository, DescriptorRepository
sourceRepository, boolean deep)
  -     * @see #isEnablePerThreadChanges
  -     * @see #setEnablePerThreadChanges
  +     * @see #mergeDescriptorRepository(DescriptorRepository targetRepository, DescriptorRepository
sourceRepository, boolean deep)
        */
       public void mergeDescriptorRepository(DescriptorRepository repository)
       {
  @@ -261,6 +263,9 @@
        * Note: Using <tt>deep copy mode</tt> all descriptors will be serialized
        * by using the default class loader to resolve classes. This could be problematic
        * when classes load by a context class loader.
  +     *
  +     * @see #isEnablePerThreadChanges
  +     * @see #setEnablePerThreadChanges
        */
       public void mergeDescriptorRepository(
               DescriptorRepository targetRepository, DescriptorRepository sourceRepository,
boolean deep)
  @@ -480,35 +485,54 @@
           metadataProfiles.clear();
       }
   
  -//    /**
  -//     * Returns a new {@link ClassDescriptor} instance.
  -//     */
  -//    public ClassDescriptor newClassDescriptor(DescriptorRepository repository)
  -//    {
  -//        return MetadataFactory.instance().newClassDescriptor(repository);
  -//    }
  -//
  -//    /**
  -//     * Returns a new {@link FieldDescriptor} instance.
  -//     */
  -//    public FieldDescriptor newFieldDescriptor(ClassDescriptor cld, int id)
  -//    {
  -//        return MetadataFactory.instance().newFieldDescriptor(cld, new Integer(id));
  -//    }
  -//
  -//    /**
  -//     * Returns a new {@link CollectionDescriptor} instance.
  -//     */
  -//    public CollectionDescriptor newCollectionDescriptor(ClassDescriptor cld)
  -//    {
  -//        return MetadataFactory.instance().newCollectionDescriptor(cld);
  -//    }
  -//
  -//    /**
  -//     * Returns a new {@link ObjectReferenceDescriptor} instance.
  -//     */
  -//    public ObjectReferenceDescriptor newObjectReferenceDescriptor(ClassDescriptor cld)
  -//    {
  -//        return MetadataFactory.instance().newObjectReferenceDescriptor(cld);
  -//    }
  +    /**
  +     * Return the default {@link PBKey}.
  +     * @see #setDefaultPBKey
  +     */
  +    public PBKey getDefaultPBKey()
  +    {
  +        return defaultPBKey;
  +    }
  +
  +    /**
  +     * Set the {@link PBKey} used for convinience method
  +     * {@link org.apache.ojb.broker.PersistenceBrokerFactory#defaultPersistenceBroker}.
  +     * <br/>
  +     * Note: Only allowed to set once by declare a default connection
  +     * in configuration files, or using this method.
  +     * @throws MetadataException if key was set more than one time
  +     */
  +    public void setDefaultPBKey(PBKey defaultPBKey)
  +    {
  +        if(this.defaultPBKey != null)
  +        {
  +            throw new MetadataException("Default key is already set. Current key is " +
this.defaultPBKey);
  +        }
  +        this.defaultPBKey = defaultPBKey;
  +        log.info("Set default PBKey for convenience broker creation: " + defaultPBKey);
  +    }
  +
  +    /**
  +     * Try to build an default PBKey for convenience PB create method.
  +     * @return PBKey or <code>null</code> if default key was not declared in
  +     * metadata
  +     */
  +    private PBKey buildDefaultKey()
  +    {
  +        List descriptors = connectionRepository().getAllDescriptor();
  +        JdbcConnectionDescriptor descriptor;
  +        for (Iterator iterator = descriptors.iterator(); iterator.hasNext();)
  +        {
  +            descriptor = (JdbcConnectionDescriptor) iterator.next();
  +            if (descriptor.isDefaultConnection())
  +            {
  +                return new PBKey(descriptor.getJcdAlias(), descriptor.getUserName(), descriptor.getPassWord());
  +            }
  +        }
  +        log.info("No 'default-connection' attribute set in jdbc-connection-descriptors,"
+
  +                " thus it's currently not possible to use 'defaultPersistenceBroker()'
" +
  +                " convenience method to lookup PersistenceBroker instances. But it's possible"+
  +                " to enable this at runtime using 'setDefaultKey' method.");
  +        return null;
  +    }
   }
  
  
  
  1.8       +9 -2      db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerHandle.java
  
  Index: PersistenceBrokerHandle.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerHandle.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PersistenceBrokerHandle.java	9 Oct 2003 14:59:46 -0000	1.7
  +++ PersistenceBrokerHandle.java	28 Oct 2003 21:18:47 -0000	1.8
  @@ -4,13 +4,20 @@
   import org.apache.ojb.broker.PersistenceBrokerException;
   
   /**
  - * A handle wraps the real (pooled) PB instance.
  + * A handle wraps a real (pooled) PB instance.
  + * In addition this class does set itself as current
  + * broker instance in {@link PersistenceBrokerThreadMapping#setCurrentPersistenceBroker}
  + * when instantiate and unset on broker close call.
    *
    * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
    * @version $Id$
    */
   public final class PersistenceBrokerHandle extends DelegatingPersistenceBroker
   {
  +    /**
  +     * Constructor for the handle, set itself in
  +     * {@link PersistenceBrokerThreadMapping#setCurrentPersistenceBroker}
  +     */
       public PersistenceBrokerHandle(PersistenceBroker broker)
       {
           super(broker);
  
  
  
  1.2       +21 -9     db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryIF.java
  
  Index: PersistenceBrokerFactoryIF.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryIF.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PersistenceBrokerFactoryIF.java	26 Apr 2003 23:18:25 -0000	1.1
  +++ PersistenceBrokerFactoryIF.java	28 Oct 2003 21:18:47 -0000	1.2
  @@ -60,6 +60,7 @@
   import org.apache.ojb.broker.util.configuration.Configurable;
   
   /**
  + * Factory for {@link PersistenceBroker} instances.
    * Each implementation have to provide a default constructor.
    *
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
  @@ -68,14 +69,25 @@
   public interface PersistenceBrokerFactoryIF extends Configurable
   {
       /**
  -     * Set the default PersistenceBroker (e.g. used within OJB
  -     * to find OJB internal tables)
  +     * Set the {@link PBKey} used for convinience {@link PersistenceBroker}
  +     * lookup method {@link #defaultPersistenceBroker}.
  +     * <br/>
  +     * Note: It's only allowed to set the default {@link PBKey} once.
  +     * All further calls will cause an exception.
  +     * If a default {@link org.apache.ojb.broker.metadata.JdbcConnectionDescriptor}
  +     * was declared in configuration file, OJB will set the declared PBKey as default.
  +     * <br/>
  +     * This method is convenience for
  +     * {@link org.apache.ojb.broker.metadata.MetadataManager#setDefaultPBKey}.
        */
       public void setDefaultKey(PBKey key);
   
       /**
  -     * Get the default PersistenceBroker (e.g. used within OJB
  -     * to find OJB internal tables)
  +     * Get the default {@link PBKey}.
  +     * This method is convenience for
  +     * {@link org.apache.ojb.broker.metadata.MetadataManager#getDefaultPBKey}.
  +     *
  +     * @see #setDefaultKey
        */
       public PBKey getDefaultKey();
   
  @@ -88,11 +100,11 @@
       public PersistenceBroker createPersistenceBroker(PBKey key) throws PBFactoryException;
   
       /**
  -     * Return {@link org.apache.ojb.broker.PersistenceBroker} instance.
  +     * Return a ready for action {@link org.apache.ojb.broker.PersistenceBroker} instance.
        *
  -     * @param jcdAlias alias name set in connection descriptor
  -     * @param user
  -     * @param password
  +     * @param jcdAlias An jcdAlias name specified in a <tt>jdbc-connection-descriptor</tt>
  +     * @param user user name specified in a <tt>jdbc-connection-descriptor</tt>
  +     * @param password valid password specified in a <tt>jdbc-connection-descriptor</tt>
        */
       public PersistenceBroker createPersistenceBroker(String jcdAlias, String user, String
password)
               throws PBFactoryException;
  
  
  
  1.2       +22 -21    db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryFactory.java
  
  Index: PersistenceBrokerFactoryFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PersistenceBrokerFactoryFactory.java	26 Apr 2003 23:18:25 -0000	1.1
  +++ PersistenceBrokerFactoryFactory.java	28 Oct 2003 21:18:48 -0000	1.2
  @@ -54,6 +54,8 @@
    * <http://www.apache.org/>.
    */
   
  +import org.apache.ojb.broker.OJBRuntimeException;
  +import org.apache.ojb.broker.util.ClassHelper;
   import org.apache.ojb.broker.util.configuration.Configuration;
   import org.apache.ojb.broker.util.configuration.Configurator;
   import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
  @@ -68,19 +70,18 @@
   public class PersistenceBrokerFactoryFactory
   {
       private static Logger log = LoggerFactory.getBootLogger();
  +
  +    private static final String PBF_KEY = "PersistenceBrokerFactoryClass";
       private static PersistenceBrokerFactoryIF singleton = init();
   
       /**
  -     *
  +     * Returns an {@link PersistenceBrokerFactoryIF} instance.
        */
       public static PersistenceBrokerFactoryIF instance()
       {
           return singleton;
       }
   
  -    /**
  -     *
  -     */
       private static PersistenceBrokerFactoryIF init()
       {
           if (log.isDebugEnabled()) log.debug("Instantiate PersistenceBrokerFactory");
  @@ -89,27 +90,27 @@
           {
               Configurator configurator = OjbConfigurator.getInstance();
               Configuration config = configurator.getConfigurationFor(null);
  -            pbfClass = config.getClass("PersistenceBrokerFactoryClass",
  -                    PersistenceBrokerFactoryDefaultImpl.class);
  -            PersistenceBrokerFactoryIF result = (PersistenceBrokerFactoryIF)
  -                    pbfClass.newInstance();
  +            pbfClass = config.getClass(PBF_KEY, null);
  +            if(pbfClass == null)
  +            {
  +                log.error("Creation of PersistenceBrokerFactory (PBF) instance failed,
can't get PBF class object");
  +                throw new OJBRuntimeException("Property for key '" + PBF_KEY + "' can not
be found in properties file");
  +            }
  +            PersistenceBrokerFactoryIF result = (PersistenceBrokerFactoryIF) ClassHelper.newInstance(pbfClass);
               configurator.configure(result);
  -            if (log.isDebugEnabled()) log.debug("PersistencebrokerFactory class: " + pbfClass);
  +            log.info("PersistencebrokerFactory class instantiated: " + result);
               return result;
           }
           catch (Exception e)
           {
  -            log.error("Error in instantiation of PersistenceBrokerFactory class "+pbfClass+
  -                        ", try to use default PersistenceBrokerFactory implementation",
e);
  -            return new PersistenceBrokerFactoryDefaultImpl();
  +            if(e instanceof OJBRuntimeException)
  +            {
  +                throw (OJBRuntimeException) e;
  +            }
  +            else
  +            {
  +                throw new OJBRuntimeException("Error while instantiation of PersistenceBrokerFactory
class", e);
  +            }
           }
  -    }
  -
  -     /**
  -     *
  -     */
  -    public static Configurator getConfigurator()
  -    {
  -        return OjbConfigurator.getInstance();
       }
   }
  
  
  
  1.5       +83 -184   db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryDefaultImpl.java
  
  Index: PersistenceBrokerFactoryDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryDefaultImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PersistenceBrokerFactoryDefaultImpl.java	16 Jul 2003 15:53:03 -0000	1.4
  +++ PersistenceBrokerFactoryDefaultImpl.java	28 Oct 2003 21:18:48 -0000	1.5
  @@ -61,162 +61,67 @@
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PBState;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  -import org.apache.ojb.broker.metadata.MetadataManager;
   import org.apache.ojb.broker.util.BrokerHelper;
  -import org.apache.ojb.broker.util.ClassHelper;
  -import org.apache.ojb.broker.util.configuration.Configuration;
  -import org.apache.ojb.broker.util.configuration.ConfigurationException;
  -import org.apache.ojb.broker.util.configuration.Configurator;
  -import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration;
  -import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
  -import org.apache.ojb.broker.util.interceptor.InterceptorFactory;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.apache.ojb.broker.util.logging.LoggingHelper;
   
  -import java.util.Iterator;
  -import java.util.List;
   import java.util.Properties;
   
   /**
    * This is the default implementation of the {@link PersistenceBrokerFactoryIF}
    * interface.
  - * <br/>
  + * <p>
    * This implementation use a pool of {@link org.apache.ojb.broker.PersistenceBroker}
  - * instances [abbr. PB]. Each PB instance (implementation class was specified in OJB configuration
file)
  - * obtained from the pool is wrapped by {@link PoolablePersistenceBroker} handle interaction
  - * with the pool.
  - * <br/>
  - * When calling {@link #createPersistenceBroker} the pooled-PB instance (<tt>PoolablePersistenceBroker</tt>)
  - * on its part was wrapped again with {@link PersistenceBrokerHandle}. When a client do
a PB.close()
  - * call on the handle, the underlying <tt>PoolablePersistenceBroker</tt> was
closed too (returns PB instance
  - * back to pool) and all further method calls on the handle (except <tt>PB.isClosed()</tt>)
result in
  - * an exception.
  + * instances [abbr. PB]. Each pooled PB instance (the implementation class was specified
  + * in OJB configuration file) is wrapped by {@link PoolablePersistenceBroker} class
  + * before add to pool.
  + * </p>
  + * <p>
  + * When calling {@link #createPersistenceBroker} or {@link #defaultPersistenceBroker} the
pooled-PB
  + * instance (<tt>PoolablePersistenceBroker</tt>) on its part was wrapped with
{@link PersistenceBrokerHandle}
  + * handle.
  + * </p>
  + * <p>
  + * When a client do a PB.close() call on the handle the wrapped <tt>PoolablePersistenceBroker</tt>
will
  + * be closed and returned to pool. All further method calls on the handle
  + * (except <tt>PB.isClosed()</tt> and <tt>PB.isInTransaction()</tt>)
result in an exception.
  + * </p>
  + * Each different {@link org.apache.ojb.broker.PBKey} (based on <code>PBKey.equals(...)</code>
method)
  + * get its own PB-pool.
  + *
  + * @see PersistenceBrokerFactoryBaseImpl
    *
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
    * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
    * @version $Id$
    */
  -public class PersistenceBrokerFactoryDefaultImpl implements PersistenceBrokerFactoryIF
  +public class PersistenceBrokerFactoryDefaultImpl extends PersistenceBrokerFactoryBaseImpl
   {
       private static Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryDefaultImpl.class);
  -
  -    private Class implementationClass;
  -    private Configurator configurator;
  -    private PersistenceBroker lastServed;
  -    private long instanceCount;
       private GenericKeyedObjectPool brokerPool;
       private PBPoolInfo poolConfig;
  -    private PBKey defaultPBKey;
   
       public PersistenceBrokerFactoryDefaultImpl()
       {
  -        setConfigurator(OjbConfigurator.getInstance());
  +        super();
           // get PB-pool configuration properties from OJB.properties
           poolConfig = new PBPoolInfo();
           // setup pool for PB instances
           brokerPool = this.createPool();
           log.info("Create PersistenceBroker instance pool, pool configuration was " + getPoolConfiguration());
  -        setDefaultKey(buildDefaultKey());
  -    }
  -
  -    /**
  -     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#setDefaultKey
  -     */
  -    public void setDefaultKey(PBKey key)
  -    {
  -
  -        this.defaultPBKey = key;
  -        log.info("Set default PBKey: " + key);
  -        /*
  -        TODO
  -        should we change the default-connection entry
  -        in the jdbcConnectionDescriptor with default-connection=true
  -        when key change?
  -        */
  -    }
  -
  -    /**
  -     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#getDefaultKey()
  -     */
  -    public PBKey getDefaultKey()
  -    {
  -        return this.defaultPBKey;
  -    }
  -
  -    private PBKey buildDefaultKey()
  -    {
  -        List descriptors = MetadataManager.getInstance().connectionRepository().getAllDescriptor();
  -        JdbcConnectionDescriptor descriptor;
  -        for (Iterator iterator = descriptors.iterator(); iterator.hasNext();)
  -        {
  -            descriptor = (JdbcConnectionDescriptor) iterator.next();
  -            if (descriptor.isDefaultConnection())
  -            {
  -                return new PBKey(descriptor.getJcdAlias(), descriptor.getUserName(), descriptor.getPassWord());
  -            }
  -        }
  -        log.warn("### No 'default' jdbc-connection-descriptor defined," +
  -                " you could not use the 'defaultPersistenceBroker()' method ###");
  -        return null;
       }
   
       /**
  -     * could be used for monitoring
  -     * TODO: is this useful?
  +     * Return broker instance from pool. If given {@link PBKey} was not found in pool
  +     * a new pool for given
  +     * @param pbKey
  +     * @return
  +     * @throws PBFactoryException
        */
  -    public Properties getPoolConfiguration()
  -    {
  -        return poolConfig;
  -    }
  -
  -    /**
  -     * could be used for runtime configuration
  -     * TODO: is this useful?
  -     */
  -    public void setPoolConfiguration(Properties prop)
  -    {
  -        poolConfig = new PBPoolInfo(prop);
  -        log.info("Change pooling configuration properties: " + poolConfig.getKeyedObjectPoolConfig());
  -        brokerPool.setConfig(poolConfig.getKeyedObjectPoolConfig());
  -    }
  -
  -    /**
  -     * For internal use! This methos creates real new PB instances
  -     */
  -    protected PersistenceBroker createNewBrokerInstance(PBKey key) throws PBFactoryException
  -    {
  -        if (key == null) throw new PBFactoryException("Could not create new broker with
PBkey argument 'null'");
  -        if (log.isEnabledFor(Logger.INFO))
  -        {
  -            // only count created instances when INFO-Log-Level
  -            log.info("Create new PB instance for PBKey " + key +
  -                    ", already created persistence broker instances: " + instanceCount);
  -            // useful for testing
  -            ++this.instanceCount;
  -        }
  -
  -        PersistenceBroker instance = null;
  -        Class[] types = {PBKey.class, PersistenceBrokerFactoryIF.class};
  -        Object[] args = {key, this};
  -        try
  -        {
  -            instance = (PersistenceBroker) ClassHelper.newInstance(implementationClass,
types, args);
  -            getConfigurator().configure(instance);
  -            instance = (PersistenceBroker) InterceptorFactory.getInstance().createInterceptorFor(instance);
  -        }
  -        catch (Exception e)
  -        {
  -            log.error("Creation of a new PB instance failed", e);
  -            throw new PBFactoryException("Creation of a new PB instance failed", e);
  -        }
  -        return instance;
  -    }
  -
       public PersistenceBroker createPersistenceBroker(PBKey pbKey) throws PBFactoryException
       {
  -        if (log.isDebugEnabled()) log.debug("createPersistenceBroker(key) called");
  +        if (log.isDebugEnabled()) log.debug("Obtain broker from pool, used PBKey is " +
pbKey);
           PersistenceBroker broker;
   
           /*
  @@ -227,85 +132,61 @@
           try
           {
               /*
  -            we wrap the pooled PB instance with a special client-handle
  -            to avoid PB-instance corruption by the client.
  +            get a pooled PB instance, the pool is reponsible to create new
  +            PB instances if not found in pool
               */
  -            broker = new PersistenceBrokerHandle((PersistenceBroker) brokerPool.borrowObject(pbKey));
  +            broker = ((PersistenceBroker) brokerPool.borrowObject(pbKey));
  +            /*
  +            now warp pooled PB instance with a handle to avoid PB corruption
  +            of closed PB instances.
  +            */
  +            broker = wrapRequestedBrokerInstance(broker);
   
           }
           catch (Exception e)
           {
  -            throw new PBFactoryException("Borrow broker from pool failed", e);
  +            throw new PBFactoryException("Borrow broker from pool failed, using PBKey "
+ pbKey, e);
           }
           return broker;
       }
   
       /**
  -     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#createPersistenceBroker(
  -     * String jcdAlias, String user, String password)
  -     */
  -    public PersistenceBroker createPersistenceBroker(String jcdAlias, String user, String
password)
  -            throws PBFactoryException
  -    {
  -        return this.createPersistenceBroker(new PBKey(jcdAlias, user, password));
  -    }
  -
  -    /**
  -     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#createPersistenceBroker(PBKey
key)
  -     */
  -    public PersistenceBroker defaultPersistenceBroker() throws PBFactoryException
  -    {
  -        if (getDefaultKey() == null) throw new PBFactoryException("There was no default-PBKey
specified");
  -        return this.createPersistenceBroker(getDefaultKey());
  -    }
  -
  -    /*
  -     * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
  -     */
  -    public void configure(Configuration config) throws ConfigurationException
  -    {
  -        implementationClass = ((PersistenceBrokerConfiguration) config).getPersistenceBrokerClass();
  -    }
  -
  -    /**
  -     * Gets the configurator.
  -     * @return Returns a Configurator
  +     * Each real pooled {@link PersistenceBroker} instance was wrapped by a
  +     * pooling handle when a new instance was created.
  +     *
  +     * @see PoolablePersistenceBroker
  +     * @param broker real {@link PersistenceBroker} instance
  +     * @param pool use {@link KeyedObjectPool}
  +     * @return wrapped broker instance
        */
  -    public Configurator getConfigurator()
  +    protected PersistenceBroker wrapBrokerWithPoolingHandle(PersistenceBroker broker, KeyedObjectPool
pool)
       {
  -        return configurator;
  +        return new PoolablePersistenceBroker(broker, pool);
       }
   
       /**
  -     * Sets the configurator.
  -     * @param configurator The configurator to set
  +     * Wraps the requested pooled broker instance. The returned handle
  +     * warps a pooled broker instance to avoid corruption
  +     * of already closed broker instances.
  +     *
  +     * @see PersistenceBrokerHandle
  +     * @param broker
  +     * @return
        */
  -    public void setConfigurator(Configurator configurator)
  -    {
  -        this.configurator = configurator;
  -    }
  -
  -    public PersistenceBrokerConfiguration getPersistenceBrokerConfiguration()
  +    protected PersistenceBroker wrapRequestedBrokerInstance(PersistenceBroker broker)
       {
  -        try
  -        {
  -            return (PersistenceBrokerConfiguration) getConfigurator().getConfigurationFor(null);
  -        }
  -        catch (ConfigurationException e)
  -        {
  -            return new OjbConfiguration();
  -        }
  +        return new PersistenceBrokerHandle(broker);
       }
   
       /**
  -     * @see org.apache.ojb.broker.core.PersistenceBrokerFactoryIF#releaseAllInstances()
  +     * @see PersistenceBrokerFactoryIF#releaseAllInstances()
        */
       public synchronized void releaseAllInstances()
       {
           log.warn("Release all instances referenced by this object");
  +        super.releaseAllInstances();
           try
           {
  -            instanceCount = 0;
               brokerPool.clear();
               //release all resources regarding the connection pooling
           }
  @@ -321,8 +202,29 @@
       }
   
       /**
  +     * could be used for monitoring
  +     * TODO: is this useful?
  +     */
  +    public Properties getPoolConfiguration()
  +    {
  +        return poolConfig;
  +    }
  +
  +    /**
  +     * could be used for runtime configuration
  +     * TODO: is this useful?
  +     */
  +    public void setPoolConfiguration(Properties prop)
  +    {
  +        poolConfig = new PBPoolInfo(prop);
  +        log.info("Change pooling configuration properties: " + poolConfig.getKeyedObjectPoolConfig());
  +        brokerPool.setConfig(poolConfig.getKeyedObjectPoolConfig());
  +    }
  +
  +
  +    /**
        * Create the {@link org.apache.commons.pool.KeyedObjectPool}, pooling
  -     * the {@link org.apache.ojb.broker.PersistenceBroker} instances - override this method
to
  +     * the {@link PersistenceBroker} instances - override this method to
        * implement your own pool and {@link org.apache.commons.pool.KeyedPoolableObjectFactory}.
        */
       private GenericKeyedObjectPool createPool()
  @@ -332,7 +234,7 @@
               log.debug("PersistenceBroker pool will be setup with the following configuration
" +
                       LoggingHelper.traceObject(conf));
           GenericKeyedObjectPool pool = new GenericKeyedObjectPool(null, conf);
  -        pool.setFactory(new PBKeyedPoolableObjectFactory(this, pool));
  +        pool.setFactory(new PersistenceBrokerFactoryDefaultImpl.PBKeyedPoolableObjectFactory(this,
pool));
           return pool;
       }
   
  @@ -343,7 +245,7 @@
   
       /**
        * This is a {@link org.apache.commons.pool.KeyedPoolableObjectFactory} implementation,
  -     * manage the life-cycle of {@link org.apache.ojb.broker.PersistenceBroker} instances
  +     * manage the life-cycle of {@link PersistenceBroker} instances
        * hold in an {@link org.apache.commons.pool.KeyedObjectPool}.
        *
        * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  @@ -361,7 +263,7 @@
   
           public Object makeObject(Object key) throws Exception
           {
  -            return new PoolablePersistenceBroker(pbf.createNewBrokerInstance((PBKey) key),
pool);
  +            return wrapBrokerWithPoolingHandle(pbf.createNewBrokerInstance((PBKey) key),
pool);
           }
   
           /**
  @@ -381,7 +283,7 @@
            * Check if the given PersistenceBroker instance
            * was already in transaction.
            * Was called when
  -         * {@link org.apache.ojb.broker.core.PBPoolInfo#init}
  +         * {@link PBPoolInfo#init}
            * method does set <code>testOnBorrow(true)</code>.
            * (Default was false, thus this method wasn't called)
            * See documentation jakarta-connons-pool api.
  @@ -404,7 +306,6 @@
           public void activateObject(Object key, Object obj) throws Exception
           {
               ((PBState) obj).setClosed(false);
  -            // PersistenceBrokerThreadMapping.setCurrentPersistenceBroker((PBKey) key,
(PersistenceBroker) obj);
           }
   
           /**
  @@ -412,8 +313,6 @@
            */
           public void passivateObject(Object key, Object obj) throws Exception
           {
  -            // PersistenceBrokerThreadMapping.unsetCurrentPersistenceBroker((PBKey) key,
(PersistenceBroker) obj);
  -            // this maybe already done before
               ((PBState) obj).setClosed(true);
           }
       }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryBaseImpl.java
  
  Index: PersistenceBrokerFactoryBaseImpl.java
  ===================================================================
  package org.apache.ojb.broker.core;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.ojb.broker.PBFactoryException;
  import org.apache.ojb.broker.PBKey;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.metadata.MetadataManager;
  import org.apache.ojb.broker.util.BrokerHelper;
  import org.apache.ojb.broker.util.ClassHelper;
  import org.apache.ojb.broker.util.configuration.Configuration;
  import org.apache.ojb.broker.util.configuration.ConfigurationException;
  import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
  import org.apache.ojb.broker.util.interceptor.InterceptorFactory;
  import org.apache.ojb.broker.util.logging.Logger;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  
  /**
   * This is an base implementation of the {@link PersistenceBrokerFactoryIF}
   * interface. Each request ({@link PersistenceBrokerFactoryIF#createPersistenceBroker} or
   * {@link PersistenceBrokerFactoryIF#defaultPersistenceBroker} call) creates a new
   * {@link PersistenceBroker} instance. No pooling of broker instances is used.
   *
   * @see PersistenceBrokerFactoryDefaultImpl
   *
   * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: PersistenceBrokerFactoryBaseImpl.java,v 1.1 2003/10/28 21:18:48 arminw
Exp $
   */
  public class PersistenceBrokerFactoryBaseImpl implements PersistenceBrokerFactoryIF
  {
      private static Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryBaseImpl.class);
  
      private Class implementationClass;
      private long instanceCount;
  
      public PersistenceBrokerFactoryBaseImpl()
      {
          configure(OjbConfigurator.getInstance().getConfigurationFor(null));
      }
  
      /**
       * @see PersistenceBrokerFactoryIF#setDefaultKey
       */
      public void setDefaultKey(PBKey key)
      {
          try
          {
              MetadataManager.getInstance().setDefaultPBKey(key);
          }
          catch (Exception e)
          {
              throw new PBFactoryException(e);
          }
      }
  
      /**
       * @see PersistenceBrokerFactoryIF#getDefaultKey()
       */
      public PBKey getDefaultKey()
      {
          return MetadataManager.getInstance().getDefaultPBKey();
      }
  
      /**
       * For internal use! This method creates real new PB instances
       */
      protected PersistenceBroker createNewBrokerInstance(PBKey key) throws PBFactoryException
      {
          if (key == null) throw new PBFactoryException("Could not create new broker with
PBkey argument 'null'");
          // check if the given key really exists
          if (MetadataManager.getInstance().connectionRepository().getDescriptor(key) == null)
          {
              throw new PBFactoryException("Given PBKey " + key + " does not match in metadata
configuration");
          }
          if (log.isEnabledFor(Logger.INFO))
          {
              // only count created instances when INFO-Log-Level
              log.info("Create new PB instance for PBKey " + key +
                      ", already created persistence broker instances: " + instanceCount);
              // useful for testing
              ++this.instanceCount;
          }
  
          PersistenceBroker instance = null;
          Class[] types = {PBKey.class, PersistenceBrokerFactoryIF.class};
          Object[] args = {key, this};
          try
          {
              instance = (PersistenceBroker) ClassHelper.newInstance(implementationClass,
types, args);
              OjbConfigurator.getInstance().configure(instance);
              instance = (PersistenceBroker) InterceptorFactory.getInstance().createInterceptorFor(instance);
          }
          catch (Exception e)
          {
              log.error("Creation of a new PB instance failed", e);
              throw new PBFactoryException("Creation of a new PB instance failed", e);
          }
          return instance;
      }
  
      /**
       * Always return a new created {@link PersistenceBroker} instance
       *
       * @param pbKey
       * @return
       * @throws PBFactoryException
       */
      public PersistenceBroker createPersistenceBroker(PBKey pbKey) throws PBFactoryException
      {
          if (log.isDebugEnabled()) log.debug("Obtain broker from pool, used PBKey is " +
pbKey);
  
          /*
          try to find a valid PBKey, if given key does not full match
          */
          pbKey = BrokerHelper.crossCheckPBKey(pbKey);
  
          try
          {
              return createNewBrokerInstance(pbKey);
  
          }
          catch (Exception e)
          {
              throw new PBFactoryException("Borrow broker from pool failed, using PBKey "
+ pbKey, e);
          }
      }
  
      /**
       * @see PersistenceBrokerFactoryIF#createPersistenceBroker(
              * String jcdAlias, String user, String password)
       */
      public PersistenceBroker createPersistenceBroker(String jcdAlias, String user, String
password)
              throws PBFactoryException
      {
          return this.createPersistenceBroker(new PBKey(jcdAlias, user, password));
      }
  
      /**
       * @see PersistenceBrokerFactoryIF#createPersistenceBroker(PBKey key)
       */
      public PersistenceBroker defaultPersistenceBroker() throws PBFactoryException
      {
          if (getDefaultKey() == null) throw new PBFactoryException("There was no default-PBKey
specified");
          return this.createPersistenceBroker(getDefaultKey());
      }
  
      /*
       * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
       */
      public void configure(Configuration config) throws ConfigurationException
      {
          implementationClass = ((PersistenceBrokerConfiguration) config).getPersistenceBrokerClass();
      }
  
      /**
       * @see PersistenceBrokerFactoryIF#releaseAllInstances()
       */
      public synchronized void releaseAllInstances()
      {
          instanceCount = 0;
      }
  
      /**
       * Not implemented!
       *
       * @return always 0
       */
      public int activePersistenceBroker()
      {
          return 0;
      }
  }
  
  
  

---------------------------------------------------------------------
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