lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kelv...@apache.org
Subject cvs commit: jakarta-lucene-sandbox/projects/appex/src/java/search Broker.java BrokerFactory.java NoSuchBrokerException.java ObjectNotFoundException.java SearchResultFactory.java TorqueBroker.java
Date Wed, 08 May 2002 15:53:49 GMT
kelvint     02/05/08 08:53:49

  Added:       projects/appex/src/java/search Broker.java
                        BrokerFactory.java NoSuchBrokerException.java
                        ObjectNotFoundException.java
                        SearchResultFactory.java TorqueBroker.java
  Log:
  Initial commit of a system of retrieving Torque-persisted objects from Document objects.
  
  Revision  Changes    Path
  1.1                  jakarta-lucene-sandbox/projects/appex/src/java/search/Broker.java
  
  Index: Broker.java
  ===================================================================
  package search;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.torque.pool.DBConnection;
  import org.apache.torque.util.Criteria;
  
  import java.util.List;
  
  /**
   * An interface to shield clients from knowledge of the underlying persistence
   * implementation.
   *
   * @author <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
   */
  public interface Broker
  {
      /**
       * Returns a list of objects given the Criteria.
       */
      List doSelect(Criteria crit) throws Exception;
  
      /**
       * Returns a list of objects given the Criteria.
       */
      List doSelect(Criteria crit, DBConnection dbCon) throws Exception;
  
      /**
       * Convenience method to obtain a single object via this broker.
       */
      Object getSingleObject(Criteria crit) throws ObjectNotFoundException, Exception;
  
      /**
       * Returns an object using it's primary keys.
       */
      Object retrieveByPK(String[] pk) throws Exception;
  
      /**
       * Returns an object using it's primary key.
       */
      Object retrieveByPK(String pk) throws Exception;
  }
  
  
  
  1.1                  jakarta-lucene-sandbox/projects/appex/src/java/search/BrokerFactory.java
  
  Index: BrokerFactory.java
  ===================================================================
  package search;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  /**
   * Factory class to obtain broker objects.
   *
   * @author <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
   */
  public abstract class BrokerFactory
  {
      public static Broker getBroker(Class bObj)
      {
          return new TorqueBroker(bObj);
      }
  }
  
  
  
  1.1                  jakarta-lucene-sandbox/projects/appex/src/java/search/NoSuchBrokerException.java
  
  Index: NoSuchBrokerException.java
  ===================================================================
  package search;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  /**
   * Thrown when a broker cannot be instantiated.
   *
   * @author <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
   */
  public class NoSuchBrokerException extends RuntimeException
  {
  
      /** Creates new GroupNotFoundException */
      public NoSuchBrokerException(String message)
      {
          super(message);
      }
  
  }
  
  
  
  1.1                  jakarta-lucene-sandbox/projects/appex/src/java/search/ObjectNotFoundException.java
  
  Index: ObjectNotFoundException.java
  ===================================================================
  package search;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.torque.util.Criteria;
  
  /**
   * Thrown when a Torque-persisted object can't be found when it is expected.
   *
   * @author <a href="mailto:soonping@relevanz.com">Phang Soon-Ping</a>
   */
  public class ObjectNotFoundException extends Exception
  {
      public ObjectNotFoundException(Class objectClass, String objectId)
      {
          super(generateMessage(objectClass, objectId));
      }
  
      public ObjectNotFoundException(Criteria crit)
      {
          super("Criteria :'" + crit.toString() + "' did not result in any object "
                + " when one was expected.");
      }
  
      private static String generateMessage(Class c, String id)
      {
          String cName = c.getName();
          int lastDot = cName.lastIndexOf('.');
          if (lastDot < 0)
          {
              return cName + " with id " + id + " not found.";
          }
          else
          {
              return cName.substring(lastDot + 1) + " with ID \"" + id + "\" not found.";
          }
      }
  }
  
  
  
  1.1                  jakarta-lucene-sandbox/projects/appex/src/java/search/SearchResultFactory.java
  
  Index: SearchResultFactory.java
  ===================================================================
  package search;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.log4j.Category;
  import org.apache.lucene.document.Document;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  
  /**
   * Factory class to materialize the objects which
   * are search results.
   */
  public abstract class SearchResultFactory
  {
      /**
       * Name of method for objects which don't have a broker, but which need to
       * be initialized somehow.
       */
      public static final String INIT_OBJECT_METHOD = "initializeObject";
  
      /**
       * Arguments for INIT_OBJECT_METHOD.
       */
      public static final Class[] INIT_OBJECT_METHOD_ARGS = new Class[]{String.class};
  
      public static final Class[] SEARCH_RESULT_WRAPPER_CTOR_ARGS
              = new Class[]{Object.class};
  
      private static Category cat = Category.getInstance(SearchResultFactory.class);
  
      /**
       * <p>
       * Materializes the object represented by the
       * {@link org.apache.lucene.document.Document}.
       * </p>
       * <p>
       * Objects which need to be returned via this method (essentially
       * any object which implements
       * {@link com.marketingbright.core.services.search.SearchResult},
       * must either have an associated broker, or a no-arg ctor and
       * INIT_OBJECT_METHOD with INIT_OBJECT_METHOD_ARGS.
       * </p>
       */
      public static Object getDocAsObject(Document doc) throws Exception
      {
          Class clazz = Class.forName(doc.get(DataSource.OBJECT_CLASS));
          String id = doc.get(DataSource.OBJECT_IDENTIFIER);
          Object o = null;
          try
          {
              Broker broker = BrokerFactory.getBroker(clazz);
              // assume it's a two-part compound pk if there's a comma
              int indexOfComma = id.indexOf(',');
              if (indexOfComma != -1)
              {
                  String[] pks = new String[2];
                  pks[0] = id.substring(0, indexOfComma);
                  pks[1] = id.substring(indexOfComma + 1);
                  o = broker.retrieveByPK(pks);
              }
              else
              {
                  o = broker.retrieveByPK(id);
              }
          }
          catch (NoSuchBrokerException nsbe)
          {
              /**
               * Some objects don't have brokers or peers, this offers an
               * alternative.
               */
              o = clazz.newInstance();
              invokeMethod(o, INIT_OBJECT_METHOD,
                           INIT_OBJECT_METHOD_ARGS, new Object[]{id});
          }
          String searchResultClassname = null;
          searchResultClassname = doc.get(DataSource.SEARCH_RESULT_CLASSNAME);
          return generateObject(searchResultClassname,
                                SEARCH_RESULT_WRAPPER_CTOR_ARGS,
                                new Object[]{o});
      }
  
      /**
       * Utility method to invoke an object's method.
       * @param o Object to invoke the method on.
       * @param methodname Name of the method to invoke.
       * @param parameter Method parameters.
       * @param args Arguments the method requires.
       * @return Object returned by the method.
       */
      private static Object invokeMethod(Object o, String methodname,
                                         Class[] parameter, Object[] args)
      {
          Class c = o.getClass();
          try
          {
              Method m = c.getMethod(methodname, parameter);
              return m.invoke(o, args);
          }
          catch (NoSuchMethodException nsme)
          {
              cat.error(nsme.getMessage() + " This method doesn't exist..", nsme);
          }
          catch (IllegalAccessException iae)
          {
              cat.error("No access to " + iae.getMessage() + ".", iae);
          }
          catch (InvocationTargetException ite)
          {
              cat.error("Trouble invoking " + ite.getMessage(), ite);
          }
          return null;
      }
  
      /**
       * Utility method to return an object based on its class name.
       *
       * @param type  Class name of object to be generated
       * @param clazz Class array of parameters.
       * @param args Object array of arguments.
       * @return Object
       */
      public static Object generateObject(String className,
                                          Class[] clazz,
                                          Object[] args)
      {
          Object o = null;
          try
          {
              Class c = Class.forName(className);
              Constructor con = c.getConstructor(clazz);
              if (con != null)
              {
                  o = con.newInstance(args);
              }
              else
                  throw new InstantiationException("Constructor with arguments:" + clazz.toString()
+ " non-existent.");
          }
          catch (ClassNotFoundException cnfe)
          {
              cat.error(cnfe.getMessage() + " No class named '" + className + "' was found.",
cnfe);
          }
          catch (InstantiationException ie)
          {
              cat.error(ie.getMessage() + " Class named '" + className + "' could not be 
instantiated.", ie);
          }
          catch (IllegalAccessException iae)
          {
              cat.error(iae.getMessage() + " No access to class named '" + className + "'.",
iae);
          }
          catch (NoSuchMethodException nsme)
          {
              cat.error(nsme.getMessage() + " No method in class named '" + className + "'.",
nsme);
          }
          catch (InvocationTargetException ite)
          {
              cat.error(ite.getMessage() + " in class named '" + className + "'.", ite);
          }
          return o;
      }
  }
  
  
  
  1.1                  jakarta-lucene-sandbox/projects/appex/src/java/search/TorqueBroker.java
  
  Index: TorqueBroker.java
  ===================================================================
  package search;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  import org.apache.log4j.Category;
  import org.apache.torque.om.ComboKey;
  import org.apache.torque.om.ObjectKey;
  import org.apache.torque.om.StringKey;
  import org.apache.torque.pool.DBConnection;
  import org.apache.torque.util.Criteria;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import java.util.Hashtable;
  import java.util.List;
  import java.util.Map;
  
  /**
   * Torque implementation of the Broker interface.
   *
   * @author <a href="mailto:soonping@relevanz.com">Phang Soon-Ping</a>
   */
  public class TorqueBroker implements Broker
  {
      protected static Map classMap = new Hashtable();
      private static Category cat = Category.getInstance(TorqueBroker.class);
      private static final String PEER_SUFFIX = "Peer";
  
      protected Object peer;
  
      public TorqueBroker(Class bObj) throws NoSuchBrokerException
      {
          String className = bObj.getName();
          peer = classMap.get(bObj);
          if (peer == null)
          {
              String peerClassName = className + PEER_SUFFIX;
              try
              {
                  peer = Class.forName(peerClassName).newInstance();
                  classMap.put(bObj, peer);
              }
              catch (Exception e)
              {
                  throw new NoSuchBrokerException("Unable to obtain a broker for "
                                                  + bObj.getName() + '.');
              }
          }
      }
  
      public synchronized List doSelect(Criteria crit) throws Exception
      {
          return (List) invokeMethod(
                  peer,
                  "doSelect",
                  new Class[]{Criteria.class},
                  new Object[]{crit}
          );
      }
  
      public synchronized List doSelect(Criteria crit, DBConnection dbCon)
              throws Exception
      {
          return (List) invokeMethod(
                  peer,
                  "doSelect",
                  new Class[]{Criteria.class, DBConnection.class},
                  new Object[]{crit, dbCon}
          );
      }
  
      public synchronized Object getSingleObject(Criteria crit)
              throws ObjectNotFoundException, Exception
      {
          List matchingObjects = doSelect(crit);
          if (!matchingObjects.isEmpty())
          {
              return matchingObjects.get(0);
          }
          else
          {
              throw new ObjectNotFoundException(crit);
          }
      }
  
      public synchronized Object retrieveByPK(String[] pk) throws Exception
      {
          ObjectKey oKey = null;
          if (pk.length > 1)
          {
              StringKey[] strKeys = new StringKey[pk.length];
              for (int i = 0; i < pk.length; i++)
              {
                  strKeys[i] = new StringKey(pk[i]);
              }
              oKey = new ComboKey(strKeys);
          }
          else
          {
              oKey = new StringKey(pk[0]);
          }
          return invokeMethod(
                  peer,
                  "retrieveByPK",
                  new Class[]{ObjectKey.class},
                  new Object[]{oKey});
      }
  
      public synchronized Object retrieveByPK(String pk) throws Exception
      {
          ObjectKey oKey = new StringKey(pk);
          return invokeMethod(
                  peer,
                  "retrieveByPK",
                  new Class[]{ObjectKey.class},
                  new Object[]{oKey});
      }
  
      /**
       * Utility method to invoke an object's method.
       * @param o Object to invoke the method on.
       * @param methodname Name of the method to invoke.
       * @param parameter Method parameters.
       * @param args Arguments the method requires.
       * @return Object returned by the method.
       */
      private static Object invokeMethod(Object o, String methodname,
                                         Class[] parameter, Object[] args)
      {
          Class c = o.getClass();
          try
          {
              Method m = c.getMethod(methodname, parameter);
              return m.invoke(o, args);
          }
          catch (NoSuchMethodException nsme)
          {
              cat.error(nsme.getMessage() + " This method doesn't exist..", nsme);
          }
          catch (IllegalAccessException iae)
          {
              cat.error("No access to " + iae.getMessage() + ".", iae);
          }
          catch (InvocationTargetException ite)
          {
              cat.error("Trouble invoking " + ite.getMessage(), ite);
          }
          return null;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>


Mime
View raw message