You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/06/10 04:37:32 UTC

cvs commit: jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/util ObjectFactory.java

dgraham     2003/06/09 19:37:32

  Added:       mapper   LICENSE
               mapper/src/test/org/apache/commons/mapper/util
                        ObjectFactoryTest.java
               mapper/src/share/org/apache/commons/mapper
                        MapperAdapter.java
                        UniqueFieldAlreadyExistsException.java
                        MapperException.java ObjectNotFoundException.java
                        MapperFactory.java Mapper.java
               mapper/src/share/org/apache/commons/mapper/jdbc
                        JdbcMapperFactory.java JdbcHelper.java
                        ResultObjectFactory.java StatementPreparer.java
                        JdbcMapper.java
               mapper/src/share/org/apache/commons/mapper/util
                        ObjectFactory.java
  Log:
  Initial mapper drop.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/mapper/LICENSE
  
  Index: LICENSE
  ===================================================================
  /*
   * ====================================================================
   *
   * 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/>.
   *
   */
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/test/org/apache/commons/mapper/util/ObjectFactoryTest.java
  
  Index: ObjectFactoryTest.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.util;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import junit.framework.TestCase;
  
  import com.honeybearstudios.util.ObjectFactory;
  
  /**
   * Test the ObjectFactory class.
   *
   * @author David Graham
   */
  public class ObjectFactoryTest extends TestCase {
  
      /**
       * Constructor for ObjectFactoryTest.
       * @param arg0
       */
      public ObjectFactoryTest(String arg0) {
          super(arg0);
      }
  
      public void testConstruct() {
          Map m = (Map) ObjectFactory.construct("java.util.HashMap");
          assertTrue(m instanceof HashMap);
  
          try {
              ObjectFactory.construct("bad class name");
          } catch (IllegalArgumentException e) {
              // good
              return;
          }
          fail("Did not throw IllegalArgumentException.");
      }
  
      public void testCreate() {
          Map m = new HashMap();
          m.put("map", "java.util.HashMap");
          m.put("list", "java.util.ArrayList");
          ObjectFactory f = new ObjectFactory(m);
  
          List list = (List) f.create("list");
          assertTrue(list instanceof ArrayList);
  
          try {
              f.create("no name");
          } catch (IllegalArgumentException e) {
              // good
              return;
          }
          fail("Did not throw IllegalArgumentException.");
      }
  
      /**
       * Make sure the map returned from the factory is a copy.
       */
      public void testGetMap() {
          Map m1 = new HashMap();
          m1.put("name", "java.util.ArrayList");
  
          ObjectFactory f = new ObjectFactory(m1);
          Map m2 = f.getMap();
          assertEquals(m1, m2);
          m2.clear();
  
          assertTrue(f.getMap().size() == 1);
      }
  
      public void testEquals() {
          Map m = new HashMap();
          m.put("name", "java.util.ArrayList");
          ObjectFactory f = new ObjectFactory(m);
          ObjectFactory f2 = new ObjectFactory(m);
          ObjectFactory f3 = new ObjectFactory(new HashMap());
  
          assertTrue(f.equals(f2));
          assertTrue(f2.equals(f));
          assertTrue(!f.equals(f3));
  
      }
  
      public void testClone() {
          Map m = new HashMap();
          m.put("name", "java.util.ArrayList");
          ObjectFactory f = new ObjectFactory(m);
  
          ObjectFactory clone = (ObjectFactory) f.clone();
          assertEquals(f, clone);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperAdapter.java
  
  Index: MapperAdapter.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.List;
  
  /**
   * A default implementation of the Mapper interface that throws 
   * UnsupportedOperationExceptions for all methods.  This makes it easy to define
   * an anonymous inner class Mapper implementation.
   * 
   * @author David Graham
   */
  public class MapperAdapter implements Mapper {
  
      /**
       * Default constructor for MapperAdapter.
       */
      public MapperAdapter() {
          super();
      }
  
      /**
       * @see org.apache.commons.mapper.Mapper#create(java.lang.Object)
       */
      public Object create(Object object) throws MapperException {
          throw new UnsupportedOperationException("This mapper does not support create().");
      }
  
      /**
       * @see org.apache.commons.mapper.Mapper#findByUniqueId(java.lang.Object)
       */
      public Object findByUniqueId(Object id) throws MapperException {
          throw new UnsupportedOperationException("This mapper does not support findByUniqueID().");
      }
  
      /**
       * @see org.apache.commons.mapper.Mapper#delete(java.lang.Object)
       */
      public void delete(Object object) throws MapperException {
          throw new UnsupportedOperationException("This mapper does not support delete().");
  
      }
  
      /**
       * @see org.apache.commons.mapper.Mapper#update(java.lang.Object)
       */
      public void update(Object object) throws MapperException {
          throw new UnsupportedOperationException("This mapper does not support update().");
  
      }
  
      /**
       * @see org.apache.commons.mapper.Mapper#findAllObjects()
       */
      public List findAllObjects() throws MapperException {
          throw new UnsupportedOperationException("This mapper does not support findAllObjects().");
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/UniqueFieldAlreadyExistsException.java
  
  Index: UniqueFieldAlreadyExistsException.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;
  
  /**
   * Indicates that a record was unable to be created in the datastore because a 
   * unique field would have duplicates.  This could be a primary key or unique 
   * constraint in a database.
   * 
   * @author David Graham
   */
  public class UniqueFieldAlreadyExistsException extends MapperException {
  
      /**
       * Constructor for UniqueFieldAlreadyExistsException.
       */
      public UniqueFieldAlreadyExistsException() {
          super();
      }
  
      /**
       * Constructor for UniqueFieldAlreadyExistsException.
       * @param description
       */
      public UniqueFieldAlreadyExistsException(String description) {
          super(description);
      }
  
      /**
       * Constructor for UniqueFieldAlreadyExistsException.
       * @param description
       * @param cause
       */
      public UniqueFieldAlreadyExistsException(String description, Throwable cause) {
          super(description, cause);
      }
  
      /**
       * Constructor for UniqueFieldAlreadyExistsException.
       * @param cause
       */
      public UniqueFieldAlreadyExistsException(Throwable cause) {
          super(cause);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperException.java
  
  Index: MapperException.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;
  
  /**
   * MapperException encapsulates everything that could go wrong with mappers.
   * Often this will wrap another type of exception such as SQLException so that 
   * callers are insulated from implementation specific exceptions.  Callers can check 
   * the getCause() method for the wrapped exception.
   * 
   * @author David Graham
   */
  public class MapperException extends Exception {
  
      private Throwable cause = null;
  
      /**
       * Constructor for MapperException.
       */
      public MapperException() {
          super();
      }
  
      /**
       * Constructor for MapperException.
       * @param message
       */
      public MapperException(String message) {
          super(message);
      }
  
      /**
       * Constructor for MapperException.
       * @param message
       * @param cause
       */
      public MapperException(String message, Throwable cause) {
          super(message);
  
          this.cause = cause;
      }
  
      /**
       * Constructor for MapperException.
       * @param cause
       */
      public MapperException(Throwable cause) {
          this.cause = cause;
      }
  
      /**
       * Returns the cause.
       * @return Throwable
       */
      public Throwable getCause() {
          return this.cause;
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/ObjectNotFoundException.java
  
  Index: ObjectNotFoundException.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;
  
  /**
   * Indicates that the requested Object could not be found in the datastore.  Many 
   * times this is an acceptable condition and the Object can be set to null.  
   * However, if the client code expects a valid object, this exception can be thrown 
   * to indicate the error.
   * 
   * @author David Graham
   */
  public class ObjectNotFoundException extends MapperException {
  
      /**
       * Constructor for ObjectNotFoundException.
       */
      public ObjectNotFoundException() {
          super();
      }
  
      /**
       * Constructor for ObjectNotFoundException.
       * @param description
       */
      public ObjectNotFoundException(String description) {
          super(description);
      }
  
      /**
       * Constructor for ObjectNotFoundException.
       * @param description
       * @param cause
       */
      public ObjectNotFoundException(String description, Throwable cause) {
          super(description, cause);
      }
  
      /**
       * Constructor for ObjectNotFoundException.
       * @param cause
       */
      public ObjectNotFoundException(Throwable cause) {
          super(cause);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/MapperFactory.java
  
  Index: MapperFactory.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.HashMap;
  import java.util.Map;
  
  import com.honeybearstudios.util.ObjectFactory;
  
  /**
   * MapperFactory is responsible for creating mappers based on domain class names 
   * or any other String name.  This allows a domain class to be a value bean and 
   * delegate data storage responsibility to a specific Mapper subclass.
   * <p>
   * MapperFactory.getMapper() method uses caching to prevent creating mappers
   * multiple times.  This means that a Mapper returned from getMapper will not be a 
   * new instance which could cause problems in a multi-threaded environment 
   * (the servlet container environment is multi-threaded).  Mappers returned by the 
   * MapperFactory should not use instance variables to maintain state, rather, they 
   * should use local method variables so they are thread-safe.
   * <p>
   * Note that you do not need to subclass MapperFactory to specify the specific Mapper 
   * implementation classes to return from getMapper().  Because mapper 
   * 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.
   * 
   * @see Mapper
   * @author David Graham
   */
  public class MapperFactory {
  
      /** 
       * This factory actually creates the mappers. 
       */
      protected ObjectFactory factory = null;
  
      /** 
       * Holds mapper instances that have already been created.   The key is a String 
       * name which is often a domain class' name and the value is a Mapper instance.
       */
      protected Map mapperCache = new HashMap();
  
      /**
       * 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.
       * @return MapperFactory an initialized factory containing the mappings in the 
       * given Map.
       * @throws IllegalArgumentException if there is a problem finding the mapped 
       * classes.  
       */
      public MapperFactory(Map map) {
          super();
          this.factory = new ObjectFactory(map);
      }
  
      /**
       * Factory method for getting the mapper associated with the given class.
       * @param Class the class whose mapper should be returned
       * @return Mapper the mapper associated with the given class
       * @throws IllegalArgumentException if the given Class was not found in the map
       * or there was an error creating an instance of the Mapper.  This is usually caused
       * by a missing public no-arg constructor.
       */
      public Mapper getMapper(Class mappedClass) {
          return this.getMapper(mappedClass.getName());
      }
  
      /**
       * Factory method for getting the mapper associated with the given class.
       * @param name The name of the mapper to be returned.
       * @return Mapper the mapper associated with the given name.
       * @throws IllegalArgumentException if the given name was not found in the map
       * or there was an error creating an instance of the Mapper.  This is usually caused
       * by a missing public no-arg constructor.
       */
      public Mapper getMapper(String name) {
  
          synchronized (this.mapperCache) {
              Mapper m = (Mapper) this.mapperCache.get(name);
              if (m == null) {
                  m = (Mapper) this.factory.create(name);
                  this.initMapper(m);
                  this.mapperCache.put(name, m);
              }
  
              return m;
          }
      }
  
      /**
       * 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.
       */
      protected void initMapper(Mapper mapper) {
          // do nothing
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/Mapper.java
  
  Index: Mapper.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.List;
  
  /**
   * A Mapper is responsible for mapping domain objects to a persistent data store.
   * By removing this functionality from the domain objects, the backend
   * datastore implementation is allowed to change without affecting any domain 
   * objects.  Examples of Mapper implementations include JdbcMapper, EjbMapper, 
   * and JdoMapper which store data using their respective technologies.
   * <p>
   * Additionally, Mappers decouple your application from the underlying mapping 
   * technology.  You might use JdbcMappers for a small website and then migrate 
   * to EjbMappers as the site grows without your application code changing.
   * <p>
   * Mapper implementations should not use instance variables to maintain state 
   * because they will be run in a multi-threaded environment.  The MapperFactory 
   * will return the same instance of a Mapper every time it's requested.  Mappers 
   * should use local method variables and method parameters to maintain thread 
   * safety.
   * <p>
   * Mapper is equivalent to the concept of a data access object (DAO).
   * 
   * @see MapperFactory
   * @author David Graham
   */
  public interface Mapper {
  
      /**
       * Create the given object in a persistent data store (ie. an SQL insert).  The 
       * Mapper should document what type of Object is expected.
       * @return Object the created object's unique id.
       * @throws MapperException
       * @throws UnsupportedOperationException if this mapper doesn't support this 
       * method.
       */
      Object create(Object object) throws MapperException;
  
      /**
       * Find the given Object based on its unique identifier.  This could be a primary 
       * key for relational database backends or an ID field in an XML file for XML 
       * implementations.  The Mapper should document what type of object is 
       * expected and what type it returns.
       * @param id An Object that represents a unique ID or a container for 
       * composite key values.
       * @return Object a single object with the given id or null if none found
       * @throws MapperException
       * @throws UnsupportedOperationException if this mapper doesn't support this 
       * method.
       */
      Object findByUniqueId(Object id) throws MapperException;
  
      /**
       * Remove an object from the data store.
       * @throws MapperException
       * @throws UnsupportedOperationException if this mapper doesn't support this 
       * method.
       */
      void delete(Object object) throws MapperException;
  
      /**
       * Update the given object in the data store.
       * @throws MapperException
       * @throws UnsupportedOperationException if this mapper doesn't support this 
       * method.
       */
      void update(Object object) throws MapperException;
  
      /**
       * Returns a List of all objects for the given mapper.
       * @throws MapperException
       * @throws UnsupportedOperationException if this mapper doesn't support this 
       * method.
       */
      List findAllObjects() throws MapperException;
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcMapperFactory.java
  
  Index: JdbcMapperFactory.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.jdbc;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.mapper.Mapper;
  import org.apache.commons.mapper.MapperFactory;
  
  /**
   * JdbcMapperFactory controls the creation of JdbcMapper objects.  For details on 
   * how this class works see MapperFactory.  This factory allows you to specify the 
   * DataSource a mapper should use for queries.
   * 
   * @see MapperFactory
   * @see JdbcMapper
   * @author David Graham
   */
  public class JdbcMapperFactory extends MapperFactory {
  
      /** 
       * DataSource to give to mappers.
       */
      protected DataSource ds = null;
  
      /**
       * Helper to give to mappers.
       */
      protected JdbcHelper helper = null;
  
      /**
       * Query map to give mappers.
       */
      protected Map queries = null;
  
      /**
       * Create a new JdbcMapperFactory with the mappings contained in the given Map.
       * @param ds A DataSource object that the JdbcMappers returned from this factory
       * will use for queries.
       * @param mappers Any map containing domain class names as the keys and 
       * mapper class names as the values.
       * @return JdbcMapperFactory an initialized factory containing the mappings in 
       * the given Map.
       * @throws IllegalArgumentException if there is a problem finding the mapped classes.  
       */
      public JdbcMapperFactory(DataSource ds, Map mappers) {
          this(ds, mappers, null);
      }
  
      /**
       * Create a new JdbcMapperFactory with the mappings contained in the given Map.
       * @param ds A DataSource object that the JdbcMappers returned from this 
       * factory will use for queries.
       * @param mappers Any map containing domain class names as the keys and 
       * mapper class names as the values.
       * @param queries A map of query names to SQL queries to pass to the mappers.  
       * The keys in this map are case-insensitive.
       * @return JdbcMapperFactory an initialized factory containing the mappings in 
       * the given Map.
       * @throws IllegalArgumentException if there is a problem finding the mapped classes.  
       */
      public JdbcMapperFactory(DataSource ds, Map mappers, Map queries) {
  
          super(mappers);
          this.ds = ds;
          this.helper = new JdbcHelper(ds);
          this.queries = this.uppercaseKeys(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;
      }
  
      /**
       * Sets each JdbcMapper's datasource to the one given in the constructor before it's
       * placed in the cache and returned.
       * @see org.apache.commons.mapper.MapperFactory#initMapper(Mapper)
       */
      protected void initMapper(Mapper mapper) {
          JdbcMapper m = (JdbcMapper) mapper;
          m.setMapperFactory(this);
          m.setDataSource(this.ds);
          m.setHelper(this.helper);
          m.setQueries(this.queries);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcHelper.java
  
  Index: JdbcHelper.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.jdbc;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.mapper.MapperException;
  
  /**
   * JdbcHelper factors out mundane and error prone JDBC coding into easy to use 
   * helper methods.
   * 
   * @author David Graham
   */
  public class JdbcHelper {
  
      /**
       * An implementation of StatementPreparer that does nothing.  Useful when there
       * are no replacement parameters to be set on the PreparedStatement.
       */
      private static final StatementPreparer NULL_PREPARER = new StatementPreparer() {
          public void prepareStatement(PreparedStatement stmt, Object obj)
              throws SQLException {
              ; // do nothing
          }
      };
  
      /**
       * An implementation of StatementPreparer that fills a PreparedStatement 
       * with values from an Object[].
       */
      private static final StatementPreparer ARRAY_PREPARER =
          new StatementPreparer() {
          public void prepareStatement(PreparedStatement stmt, Object obj)
              throws SQLException {
                  
              Object[] args = (Object[]) obj;
              for (int i = 0; i < args.length; i++) {
                  stmt.setObject(i + 1, args[i]);
              }
          }
      };
  
      private DataSource ds = null;
  
      /**
       * Constructor for JdbcHelper.
       * @param ds The DataSource to get Connections from.
       */
      public JdbcHelper(DataSource ds) {
          super();
          this.ds = ds;
      }
  
      /**
       * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
       * statement is executed in it's own transaction that will be committed or rolled 
       * back depending on any SQLExceptions thrown.
       * @param sql The SQL statement to execute.
       * @param preparer Initializes the PreparedStatement's IN (ie. '?') parameters.
       * @param prepareObject An object to pass to the preparer to setup the 
       * PreparedStatement.
       * @throws MapperException
       * @return The number of rows updated.
       */
      public int executeUpdate(
          String sql,
          StatementPreparer preparer,
          Object prepareObject)
          throws MapperException {
  
          Connection conn = null;
          PreparedStatement stmt = null;
          boolean autoCommit = false;
          int rows = 0;
  
          try {
              conn = this.ds.getConnection();
              autoCommit = conn.getAutoCommit(); // save old value
              conn.setAutoCommit(false); // single transaction.
  
              stmt = conn.prepareStatement(sql);
              preparer.prepareStatement(stmt, prepareObject);
              rows = stmt.executeUpdate();
  
              conn.commit();
  
          } catch (SQLException e) {
              this.rollback(conn);
              throw new MapperException(e);
          } finally {
              this.setAutoCommit(autoCommit, conn);
              this.closeStatement(stmt);
              this.closeConnection(conn);
          }
  
          return rows;
      }
  
      /**
       * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
       * statement is executed in it's own transaction that will be committed or rolled 
       * back depending on any SQLExceptions thrown.
       * @param sql The SQL statement to execute.
       * @param params An array of values to fill the sql '?' markers with.
       * @throws MapperException
       * @return The number of rows updated.
       */
      public int executeUpdate(String sql, Object[] params) throws MapperException {
          return this.executeUpdate(sql, ARRAY_PREPARER, params);
      }
  
      /**
       * Executes the given SELECT SQL query and returns a List of results.
       * @param sql The SQL statement to execute.
       * @param preparer Initializes the PreparedStatement's IN parameters.
       * @param prepareObject An object to pass to the preparer to setup the 
       * PreparedStatement.
       * @param resultFactory The factory used to create the result objects from the 
       * ResultSet.
       * @return A list of objects generated by the resultFactory.
       * @throws MapperException
       */
      public List executeQuery(
          String sql,
          StatementPreparer preparer,
          Object prepareObject,
          ResultObjectFactory resultFactory)
          throws MapperException {
  
          Connection conn = null;
          PreparedStatement stmt = null;
          ResultSet rs = null;
          ArrayList found = new ArrayList();
  
          try {
              conn = this.ds.getConnection();
              stmt = conn.prepareStatement(sql);
  
              preparer.prepareStatement(stmt, prepareObject);
  
              rs = stmt.executeQuery();
              while (rs.next()) {
                  found.add(resultFactory.createResultObject(rs));
              }
  
          } catch (SQLException e) {
              throw new MapperException(e);
          } finally {
              this.closeResultSet(rs);
              this.closeStatement(stmt);
              this.closeConnection(conn);
          }
  
          return found;
      }
  
      /**
       * Executes the given SELECT SQL query and returns a List of results.
       * @param sql The SQL statement to execute.
       * @param params An array of values to fill the sql '?' markers with.
       * @param resultFactory
       * @return A list of objects generated by the resultFactory.
       * @throws MapperException
       * @see executeQuery(String, StatementPreparer, Object, ResultObjectFactory)
       */
      public List executeQuery(
          String sql,
          Object[] params,
          ResultObjectFactory resultFactory)
          throws MapperException {
          return this.executeQuery(sql, ARRAY_PREPARER, params, resultFactory);
      }
  
      /**
       * Executes a query that doesn't need parameter replacement.
       * @param sql The SQL statement to execute.
       * @param resultFactory
       * @return A list of objects generated by the resultFactory.
       * @throws MapperException
       * @see executeQuery(String, StatementPreparer, Object, ResultObjectFactory)
       */
      public List executeQuery(String sql, ResultObjectFactory resultFactory)
          throws MapperException {
          return this.executeQuery(sql, NULL_PREPARER, null, resultFactory);
      }
  
      /**
       * Performs just like the other getField method except that this one gets its own 
       * Connection.
       * @param query
       * @param column
       * @return
       * @throws MapperException
       */
      public String getField(String query, String column) throws MapperException {
          Connection conn = null;
          String field = null;
  
          try {
              conn = this.ds.getConnection();
              field = this.getField(query, column, conn);
  
          } catch (SQLException e) {
              throw new MapperException(e);
          } finally {
              this.closeConnection(conn);
          }
          return field;
      }
  
      /**
       * Executes the query and returns the column's value from the first returned row.
       * This is helpful when finding the last generated ID in a table but can be used to
       * return any column value.
       * @param query A valid SQL SELECT query.
       * @param column The column whose value should be returned.
       * @param conn A Connection to run the query on.
       * @throws SQLException if a database access error occurs or the given query
       * returned 0 rows.
       */
      public String getField(String query, String column, Connection conn)
          throws SQLException {
          PreparedStatement stmt = conn.prepareStatement(query);
          String field = null;
  
          try {
              ResultSet rs = stmt.executeQuery();
              if (!rs.next()) {
                  throw new SQLException(
                      "Query: " + query + " didn't return any records.");
              }
              field = rs.getString(column);
              rs.close();
          } finally {
              stmt.close();
          }
          return field;
      }
  
      /**
       * Rollback any changes made on the given connection.
       * @param conn The database Connection to rollback.  A null value is legal.
       * @throws MapperException if an SQLException occurred during the rollback.
       */
      public void rollback(Connection conn) throws MapperException {
          try {
              if (conn != null) {
                  conn.rollback();
              }
          } catch (SQLException e) {
              throw new MapperException(e);
          }
      }
  
      /**
       * Convenience method to reset autoCommit on a connection to its previous value
       * while wrapping any SQLException in a MapperException.  
       * We restore autoCommit after using the connection
       * for a create, delete, update, or find because other code might be using
       * the same DataSource object to get connections and might depend on the 
       * autoCommit flag being set a certain way.  We set it to false and then restore 
       * it when we're done with the connection.
       * @param autoCommit The autoCommit value to set on the Connection.
       * @param conn The Connection to set autoCommit on.
       * @throws MapperException if an SQLException occurred setting the autoCommit
       * status.
       */
      public void setAutoCommit(boolean autoCommit, Connection conn)
          throws MapperException {
          if (conn != null) {
              try {
                  conn.setAutoCommit(autoCommit);
              } catch (SQLException e) {
                  throw new MapperException(e);
              }
          }
      }
  
      /**
       * Attempts to close the given connection wrapping any SQLExceptions that occur
       * in a MapperException.  Typically, a mapper's methods throw MapperException 
       * rather than SQLException so this simplifies Connection cleanup.  Connection 
       * objects should <strong>always</strong> be closed in 
       * case the DataSource is performing Connection pooling so the Connection can 
       * be returned to the pool.
       * @param conn The Connection to close.  A null value for this argument is legal.
       * @throws MapperException if an SQLException is thrown closing the Connection.
       */
      public void closeConnection(Connection conn) throws MapperException {
          try {
              if (conn != null) {
                  conn.close();
              }
          } catch (SQLException e) {
              throw new MapperException(e);
          }
      }
  
      /**
       * Attempts to close the given Statement wrapping any SQLExceptions that occur
       * in a MapperException.  Typically, a mapper's methods throw MapperException 
       * rather than SQLException so this simplifies Statement cleanup.  Statement 
       * objects (especially PreparedStatements) should <strong>always</strong> be 
       * closed in case the DataSource is performing Statement pooling so the 
       * Statement can be returned to the pool.
       * @param stmt The Statment to close.  A null value for this argument is legal.
       * @throws MapperException if an SQLException is thrown closing the Statement.
       */
      public void closeStatement(Statement stmt) throws MapperException {
          try {
              if (stmt != null) {
                  stmt.close();
              }
          } catch (SQLException e) {
              throw new MapperException(e);
          }
      }
  
      /**
       * Attempts to close the given ResultSet wrapping any SQLExceptions that occur
       * in a MapperException.
       * @param rs The ResultSet to close.  A null value for this argument is legal.
       * @throws MapperException if an SQLException is thrown closing the ResultSet.
       */
      public void closeResultSet(ResultSet rs) throws MapperException {
          try {
              if (rs != null) {
                  rs.close();
              }
          } catch (SQLException e) {
              throw new MapperException(e);
          }
      }
  
      /**
       * Converts a boolean to an integer.  False is 0, true is 1.
       */
      public int toInt(boolean bool) {
          return bool ? 1 : 0;
      }
  
      /**
       * Converts an integer to a boolean.  0 is false, anything else is true.
       */
      public boolean toBoolean(int value) {
          return (value == 0) ? false : true;
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/ResultObjectFactory.java
  
  Index: ResultObjectFactory.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.jdbc;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  /**
   * <p>ResultObjectFactory converts a ResultSet row into an Object.  A typical 
   * implementation of this interface is an anonymous inner class.  For example,</p>
   * <code>
      private static final ResultObjectFactory makePerson = new ResultObjectFactory() {
          public Object createResultObject(ResultSet rs) throws SQLException {
              Person p = new Person()
              p.setName(rs.getString("name"));
              p.setEmail(rs.getString("email"));
              return p;
          }
      };
   * </code>
   * 
   * @author David Graham
   */
  public interface ResultObjectFactory {
  
      /**
       * Reads data from the ResultSet and stores it in an Object.  This method must
       * <strong>not</strong> alter the ResultSet or read any rows from it other than
       * the row it is currently positioned on.
       * @param rs The ResultSet containing the data for the object.
       * @return An object generated from the ResultSet.
       * @throws SQLException
       */
      public Object createResultObject(ResultSet rs) throws SQLException;
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/StatementPreparer.java
  
  Index: StatementPreparer.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.jdbc;
  
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  
  /**
   * <p>A StatementPreparer is responsible for setting up a PreparedStatement's 
   * replacement parameters.  The framework executes the prepareStatement() 
   * method immediately before executing the PreparedStatement.</p>
   * 
   * <p>Often, implementations of this interface will be anonymous inner classes.  
   * For example:</p>
   * <pre>
   * private StatementPreparer prepCreate = new StatementPreparer() {
   *      public void prepareStatement(PreparedStatement stmt, Object obj) {
   *          Person p = (Person) obj;
   *          stmt.setString(1, p.getEmail());
   *      }
   * }
   * </pre>
   * <p>The preceding preparer would be used when inserting Person objects into the
   * database.</p>
   * 
   * @author David Graham
   */
  public interface StatementPreparer {
  
      /**
       * Prepares the given statement for execution.
       * @param stmt The PreparedStatement to initialize.
       * @param obj An Object to prepare the statement with.
       * @throws SQLException
       */
      public void prepareStatement(PreparedStatement stmt, Object obj)
          throws SQLException;
          
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcMapper.java
  
  Index: JdbcMapper.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.jdbc;
  
  import java.util.List;
  import java.util.Map;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.mapper.Mapper;
  import org.apache.commons.mapper.MapperException;
  import org.apache.commons.mapper.MapperFactory;
  
  /**
   * JdbcMapper is the abstract parent for Mapper classes using JDBC to store and
   * retrieve data.  This implements the Mapper interface and throws 
   * UnsupportedOperationExceptions by default.
   * 
   * @see JdbcMapperFactory
   * @author David Graham
   */
  public abstract class JdbcMapper implements Mapper {
  
      /** 
       * DataSource to get connections from.  Making this protected allows child
       * Mappers to redefine the database they should use although most of the time 
       * they'll probably use the default database.
       */
      protected DataSource ds = null;
  
      protected JdbcHelper helper = null;
  
      /**
       * The MapperFactory that created this Mapper.  This is set by the 
       * JdbcMapperFactory when it creates a new JdbcMapper object.
       */
      protected MapperFactory mapperFactory = null;
  
      /**
       * Stores a map of query names to actual SQL queries.  This allows queries to be
       * externalized to support multiple database types.  This member is private to 
       * force subclasses to use the getQuery() method to retrieve queries with case 
       * insensitive names.
       */
      private Map queries = null;
  
      /**
       * Constructor for JdbcMapper.
       */
      public JdbcMapper() {
          super();
      }
  
      /**
       * @throws UnsupportedOperationException  
       * @see org.apache.commons.mapper.Mapper#create(Object)
       */
      public Object create(Object domainObject) throws MapperException {
          throw new UnsupportedOperationException("Create not supported.");
      }
  
      /**
       * @throws UnsupportedOperationException
       * @see org.apache.commons.mapper.Mapper#findByPrimaryKey(MappedObject)
       */
      public Object findByUniqueID(Object objectWithKeyValues)
          throws MapperException {
          throw new UnsupportedOperationException("Find by unique ID not supported.");
      }
  
      /**
       * @throws UnsupportedOperationException
       * @see org.apache.commons.mapper.Mapper#remove(MappedObject)
       */
      public void delete(Object domainObject) throws MapperException {
          throw new UnsupportedOperationException("Delete not supported.");
      }
  
      /**
       * @throws UnsupportedOperationException
       * @see org.apache.commons.mapper.Mapper#update(MappedObject)
       */
      public void update(Object domainObject) throws MapperException {
          throw new UnsupportedOperationException("Update not supported.");
      }
  
      /**
       * @throws UnsupportedOperationException
       * @see org.apache.commons.mapper.Mapper#findAllObjects()
       */
      public List findAllObjects() throws MapperException {
          throw new UnsupportedOperationException("Find all objects not supported.");
      }
  
      /**
       * Returns the SQL query for the given query name.  The name is case 
       * insensitive so a lookup on "PERSON" and "person" will return the same object.
       * @throws IllegalArgumentException if there is no mapping for the given name.
       */
      protected String getQuery(String queryName) {
          // uppercase key for case insensitive lookups
          String sqlQuery = (String) this.queries.get(queryName.toUpperCase());
          if (sqlQuery == null) {
              throw new IllegalArgumentException(
                  queryName + " is not a valid query name.");
          }
  
          return sqlQuery;
      }
  
      /**
       * Sets the DataSource.
       * @param ds The DataSource to use for queries.
       */
      public void setDataSource(DataSource ds) {
          this.ds = ds;
      }
  
      /**
       * Sets the mapperFactory.
       * @param mapperFactory The mapperFactory to set
       */
      public void setMapperFactory(MapperFactory mapperFactory) {
          this.mapperFactory = mapperFactory;
      }
  
      /**
       * Sets the queries.
       * @param queries The queries to set
       */
      public void setQueries(Map queries) {
          this.queries = queries;
      }
  
      /**
       * Sets the helper to use.
       * @param helper
       */
      public void setHelper(JdbcHelper helper) {
          this.helper = helper;
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/util/ObjectFactory.java
  
  Index: ObjectFactory.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.util;
  
  import java.io.Serializable;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * <p>
   * ObjectFactory maps string names to classes to be instantiated.  Given a name it 
   * will construct a new object of the mapped type using reflection.  Pass in a map 
   * with entries in the form "logical name"="fully qualified class name".  Both key and 
   * value must be Strings.
   * </p>
   * <p>
   * For quick instantiation from a String, use the class method construct(String).
   * </p>  
   * <p>
   * The classes to be constructed must have a public no-arg constructor.  An 
   * Exception thrown during the loading of a Class or creation of an Object is 
   * considered a programmer error and is converted to an IllegalArgumentException.  
   * This greatly simplifies client code and indicates a misconfiguration of the Map 
   * passed to this factory. This class is thread-safe.
   * </p>
   * <p>
   * Example:
   * <code>
   * Map map = new HashMap();
   * map.put("list", "java.util.ArrayList");
   * map.put("set", "java.util.HashSet");
   * 
   * ObjectFactory factory = new ObjectFactory(map);
   * Set mySet = (Set) factory.construct("set");
   * </code>
   * </p>
   * <p>
   * Using ObjectFactory without a map:
   * <code>Set mySet = (Set) ObjectFactory.construct("java.util.HashSet");</code>
   * </p>
   * <p>
   * This class is useful as a backing class for higher level factory classes.  The higher
   * level class can use ObjectFactory to do the mapping and creation work while it 
   * implements factory specific semantics and caching.
   * </p>
   * 
   * @author David Graham
   */
  public class ObjectFactory implements Cloneable, Serializable {
  
  	/** 
  	 * Stores "name"="qualified class name".
  	 */
  	protected Map nameMap = new HashMap();
  
  	/** 
  	 * Stores "name"=Class.  Caches Class objects for quick lookup.
  	 */
  	protected Map classMap = new HashMap();
  
  	/**
  	 * Create an ObjectFactory with the given mapping of names to fully qualified 
  	 * class names. 
  	 * @param map A map of logical names to fully qualified class names.
  	 */
  	public ObjectFactory(Map map) {
  		super();
  		this.nameMap = map;
  	}
  
  	/**
  	 * Convenience method that creates an object of the class given as a string.  
  	 * @param className The fully qualified class name of the object to be created.
  	 * @throws IllegalArgumentException if the Class for the given name could not be
  	 * found.
  	 */
  	public static Object construct(String className) {
  		return newInstance(loadClass(className));
  	}
  
  	/**
  	 * Returns an object of the class associated with the given command.
  	 * @throws IllegalArgumentException if given name was not found in the map, 
  	 * the Class for the name could not be found, or the Class is missing a no-arg
  	 * constructor.
  	 */
  	public Object create(String name) {
  		return newInstance(this.getNamedClass(name));
  	}
  
  	/**
  	 * Calls c.newInstance() and converts exceptions.
  	 * @throws IllegalArgumentException if there is an error instantiating an object 
       * of the class.  This usually indicates the class is missing a public no-arg 
       * constructor.
  	 */
  	protected static Object newInstance(Class c) {
  		try {
  			return c.newInstance();
  
  		} catch (InstantiationException e) {
  			throw new IllegalArgumentException(
  				c
  					+ " could not be created: "
  					+ e.getMessage()
  					+ ".  Check for public no-arg constructor.");
  		} catch (IllegalAccessException e) {
  			throw new IllegalArgumentException(
  				c
  					+ " could not be created: "
  					+ e.getMessage()
  					+ ".  Check for public no-arg constructor.");
  		}
  	}
  
  	/**
  	 * Returns the Class object mapped to the given name.  If not found in the 
  	 * cache, the Class object is stored there for quick retrieval.
  	 * @throws IllegalArgumentException if the given name is not mapped to a class
  	 * name or the Class could not be found.
  	 */
  	protected Class getNamedClass(String logicalName) {
  
  		Class c = (Class) this.classMap.get(logicalName);
  		if (c == null) {
  			String className = (String) this.nameMap.get(logicalName);
  
  			if (className == null) {
  				throw new IllegalArgumentException(
  					"Name not found in map: " + logicalName);
  			}
  
  			c = loadClass(className);
  
  			this.classMap.put(logicalName, c);
  		}
  
  		return c;
  	}
  
  	/**
  	 * Attempts to load the Class object for the given class name.
  	 * @param className The fully qualified class name to load.
  	 * @return The Class object for the class name.
  	 * @throws IllegalArgumentException if the Class is not found for the given 
  	 * name.
  	 */
  	protected static Class loadClass(String className) {
  		try {
  			return Class.forName(className);
  
  		} catch (ClassNotFoundException e) {
  			throw new IllegalArgumentException(
  				"Class '" + className + "' could not be found: " + e.getMessage());
  		}
  	}
  
  	/**
  	 * Gets a copy of the map this factory is using to construct instances of classes.  
  	 */
  	public Map getMap() {
  		return new HashMap(this.nameMap);
  	}
  
  	/**
  	 * Returns true if the ObjectFactory's maps are equal.
  	 */
  	public boolean equals(Object o) {
  		if (o == this) {
  			return true;
  		}
  
  		if (o instanceof ObjectFactory) {
  			ObjectFactory of = (ObjectFactory) o;
  			return of.nameMap.equals(this.nameMap);
  		}
  
  		return false;
  	}
  
  	/**
  	 * Returns a cloned instance of this ObjectFactory.
  	 */
  	public Object clone() {
  		ObjectFactory of = null;
  		try {
  			of = (ObjectFactory) super.clone();
  		} catch (CloneNotSupportedException e) {
  			// Object does support clone
  		}
  
  		of.classMap = new HashMap(this.classMap);
  		of.nameMap = new HashMap(this.nameMap);
  
  		return of;
  	}
  
  	/**
  	 * ObjectFactory's hashcode is based on its underlying Map's hashcodes.
  	 */
  	public int hashCode() {
  		return this.nameMap.hashCode() *176543;
  	}
  
  }
  
  

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