commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc JdbcMapperFactoryListener.java JdbcMapper.java
Date Sat, 21 Jun 2003 01:53:13 GMT
dgraham     2003/06/20 18:53:12

  Modified:    mapper/src/share/org/apache/commons/mapper
                        MapperFactory.java
               mapper/src/share/org/apache/commons/mapper/jdbc
                        JdbcMapper.java
  Added:       mapper/src/share/org/apache/commons/mapper
                        MapperFactoryEvent.java MapperFactoryListener.java
               mapper/src/share/org/apache/commons/mapper/jdbc
                        JdbcMapperFactoryListener.java
  Log:
  Replaced MapperFactory.initMapper() with a much more flexible event system.
  Now Mapper implementations can be mixed without the need for complicated
  factories to initialize all the various kinds of Mappers.  Clients create one 
  MapperFactory instance and register listeners to do the Mapper initialization.
  Listeners that don't know how to handle a type of Mapper implementation 
  will simply ignore them.
  
  Revision  Changes    Path
  1.5       +49 -10    jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperFactory.java
  
  Index: MapperFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperFactory.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MapperFactory.java	10 Jun 2003 03:34:41 -0000	1.4
  +++ MapperFactory.java	21 Jun 2003 01:53:07 -0000	1.5
  @@ -61,7 +61,10 @@
   
   package org.apache.commons.mapper;
   
  +import java.util.ArrayList;
   import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.Map;
   
   import org.apache.commons.mapper.util.ObjectFactory;
  @@ -83,9 +86,12 @@
    * implementation classes are defined in a Map, you can have any combination of 
    * Mapper types in use at the same time.  For example, you could have 
    * EjbPersonMapper using an EJB to persist Person objects and a JdbcOrderMapper 
  - * using JDBC calls to persist Order objects.
  + * using JDBC calls to persist Order objects.  You can register MapperFactoryListener
  + * implementations with the factory to initialize the various types of Mappers when
  + * they are created.
    * 
    * @see Mapper
  + * @see MapperFactoryListener
    * @author David Graham
    */
   public class MapperFactory {
  @@ -102,6 +108,11 @@
       protected Map mapperCache = new HashMap();
   
       /**
  +     * The List of listeners to notify when a Mapper is created.
  +     */
  +    private List listeners = new ArrayList();
  +
  +    /**
        * Create a new MapperFactory with the mappings contained in the given Map.
        * @param Map any map containing logical names (often domain class names) as 
        * the keys and mapper class names as the values.
  @@ -139,7 +150,7 @@
               Mapper m = (Mapper) this.mapperCache.get(name);
               if (m == null) {
                   m = (Mapper) this.factory.create(name);
  -                this.initMapper(m);
  +                this.fireMapperCreated(m);
                   this.mapperCache.put(name, m);
               }
   
  @@ -148,12 +159,40 @@
       }
   
       /**
  -     * Initialize the mapper before it's returned for the first time.  This allows 
  -     * subclasses to perform mapper specific initialization without duplicating the 
  -     * caching logic.  This implementation does nothing.
  +     * Register a listener with this MapperFactory to receive notifications of events.
  +     * @param listener
  +     */
  +    public void addMapperFactoryListener(MapperFactoryListener listener) {
  +        synchronized (this.listeners) {
  +            this.listeners.add(listener);
  +        }
  +    }
  +
  +    /**
  +     * Unregister a listener from this MapperFactory.
  +     * @param listener
        */
  -    protected void initMapper(Mapper mapper) {
  -        // do nothing
  +    public void removeMapperFactoryListener(MapperFactoryListener listener) {
  +        synchronized (this.listeners) {
  +            this.listeners.remove(listener);
  +        }
  +    }
  +
  +    /**
  +     * Call the mapperCreated() method of all registered listeners.
  +     * @param listener
  +     */
  +    private void fireMapperCreated(Mapper created) {
  +        MapperFactoryEvent event = new MapperFactoryEvent(this, created);
  +
  +        synchronized (this.listeners) {
  +            Iterator iter = this.listeners.iterator();
  +            while (iter.hasNext()) {
  +                MapperFactoryListener listener = (MapperFactoryListener) iter.next();
  +                listener.mapperCreated(event);
  +            }
  +        }
  +        
       }
   
   }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperFactoryEvent.java
  
  Index: MapperFactoryEvent.java
  ===================================================================
  /*
   * $Header$
   * $Revision$
   * $Date$
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" 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"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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/>.
   *
   */
  
  package org.apache.commons.mapper;
  
  import java.util.EventObject;
  
  /**
   * An event that MapperFactory generates.
   * 
   * @author David Graham
   */
  public class MapperFactoryEvent extends EventObject {
  
      /**
       * A Mapper related to this event. 
       */
      private Mapper mapper = null;
  
      /**
       * Constructor for MapperFactoryEvent.
       * @param source The MapperFactory that generated the event.
       */
      public MapperFactoryEvent(Object source) {
          super(source);
      }
      
      /**
       * Constructor for MapperFactoryEvent.
       * @param source The MapperFactory that generated the event.
       * @param mapper A Mapper related to this event. 
       */
      public MapperFactoryEvent(Object source, Mapper mapper) {
          super(source);
      }    
  
      /**
       * @return The Mapper that is related to this event.
       */
      public Mapper getMapper() {
          return this.mapper;
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperFactoryListener.java
  
  Index: MapperFactoryListener.java
  ===================================================================
  /*
   * $Header$
   * $Revision$
   * $Date$
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" 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"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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/>.
   *
   */
   
  package org.apache.commons.mapper;
  
  import java.util.EventListener;
  
  /**
   * Listeners are notified every time a MapperFactory <strong>creates</strong>
a 
   * new Mapper instance.  They are not notified when a previously created instance
   * is returned from the cache.
   * 
   * @author David Graham
   */
  public interface MapperFactoryListener extends EventListener {
  
      /**
       * Called whenever a Mapper is created.
       * @param event Retrieve the created Mapper from 
       * <code>event.getMapper()</code>.
       */
      void mapperCreated(MapperFactoryEvent event);
  
  }
  
  
  
  1.4       +5 -5      jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcMapper.java
  
  Index: JdbcMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcMapper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JdbcMapper.java	10 Jun 2003 03:34:41 -0000	1.3
  +++ JdbcMapper.java	21 Jun 2003 01:53:11 -0000	1.4
  @@ -75,7 +75,7 @@
    * retrieve data.  This implements the Mapper interface and throws 
    * UnsupportedOperationExceptions by default.
    * 
  - * @see JdbcMapperFactory
  + * @see JdbcMapperFactoryListener
    * @author David Graham
    */
   public abstract class JdbcMapper implements Mapper {
  @@ -91,7 +91,7 @@
   
       /**
        * The MapperFactory that created this Mapper.  This is set by the 
  -     * JdbcMapperFactory when it creates a new JdbcMapper object.
  +     * JdbcMapperFactoryListener when it initializes a new JdbcMapper object.
        */
       protected MapperFactory mapperFactory = null;
   
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcMapperFactoryListener.java
  
  Index: JdbcMapperFactoryListener.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcMapperFactory.java,v
1.3 2003/06/10 03:34:41 dgraham Exp $
   * $Revision: 1.3 $
   * $Date: 2003/06/10 03:34:41 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" 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"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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/>.
   *
   */
  
  package org.apache.commons.mapper.jdbc;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.mapper.MapperFactory;
  import org.apache.commons.mapper.MapperFactoryEvent;
  import org.apache.commons.mapper.MapperFactoryListener;
  
  /**
   * JdbcMapperFactoryListener initializes JdbcMapper objects after they've been 
   * created by a MapperFactory.  Instances of this class can be registered with a 
   * MapperFactory with the <code>MapperFactory.addMapperFactoryListener()</code>
   * method.
   * 
   * @see MapperFactory
   * @see JdbcMapper
   * @author David Graham
   */
  public class JdbcMapperFactoryListener implements MapperFactoryListener {
  
      /** 
       * DataSource to give to mappers.
       */
      private DataSource ds = null;
  
      /**
       * Helper to give to mappers.
       */
      private JdbcHelper helper = null;
  
      /**
       * Query map to give mappers.
       */
      private Map queries = null;
  
      /**
       * Create a new JdbcMapperFactoryListener.
       * @param ds A DataSource object that the JdbcMappers returned from this factory
       * will use for queries.  
       */
      public JdbcMapperFactoryListener(DataSource ds) {
          this(ds, null);
      }
  
      /**
       * Create a new JdbcMapperFactoryListener.
       * @param ds A DataSource object that JdbcMappers initialized from this 
       * listener will use for queries.
       * @param queries A map of query names to SQL queries to pass to the mappers.  
       * The keys in this map are case-insensitive.
       */
      public JdbcMapperFactoryListener(DataSource ds, Map queries) {
          super();
          this.ds = ds;
          this.helper = new JdbcHelper(ds);
  
          if (queries != null) {
              this.queries = this.uppercaseKeys(queries);
          }
      }
  
      /**
       * Sets each JdbcMapper's datasource to the instance given in the constructor 
       * before it's placed in the cache and returned.  This ignores any Mapper that is
       * not a sublcass of JdbcMapper.
       * @param event
       * @see org.apache.commons.mapper.MapperFactoryListener#mapperCreated(org.apache.commons.mapper.MapperFactoryEvent)
       */
      public void mapperCreated(MapperFactoryEvent event) {
          if (!(event.getMapper() instanceof JdbcMapper)) {
              return;
          }
  
          JdbcMapper m = (JdbcMapper) event.getMapper();
  
          m.setMapperFactory((MapperFactory) event.getSource());
          m.setDataSource(this.ds);
          m.setHelper(this.helper);
          m.setQueries(this.queries);
      }
  
      /**
       * Uppercases key names for case-insensitive lookups.
       * @return A Map with uppercased keys from the given Map.
       */
      private Map uppercaseKeys(Map map) {
          Map temp = new HashMap();
          Iterator i = map.keySet().iterator();
          // copy key value pairs into new map
          while (i.hasNext()) {
              String key = (String) i.next();
              String value = (String) map.get(key);
              temp.put(key.toUpperCase(), value);
          }
  
          return temp;
      }
  
  }
  
  
  

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


Mime
View raw message