View Javadoc

    1   /***
    2    * @(#)DAOFactoryImpl.java
    3    *
    4    * JFoxSOAF, Service-Oriented Application Framework
    5    *
    6    * Copyright(c) JFoxSOAF Team
    7    *
    8    * Licensed under the GNU LGPL, Version 2.1 (the "License");
    9    * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   * http://www.gnu.org/copyleft/lesser.html
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   * For more information, please visit:
    21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
    22   * http://www.huihoo.org/jfox/jfoxsoaf
    23   */
    24
    25  package org.huihoo.jfox.soaf.services.dao;
    26
    27  import java.util.Collections;
    28  import java.util.HashMap;
    29  import java.util.Map;
    30
    31  import org.huihoo.jfox.soaf.exception.DAOException;
    32
    33  /***
    34   * <p>
    35   * Alternatvie DAO Factory implementation with reflection.
    36   * </p>
    37   *
    38   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    39   * @version $Revision: 1.1 $ $Date: 2005/04/27 08:02:53 $
    40   * @version Revision: 1.0
    41   */
    42
    43  public class DAOFactoryImpl implements DAOFactory {
    44
    45      private Map daoCache = Collections.synchronizedMap(new HashMap());
    46
    47      /***
    48       * This method instantiates the DAO class.
    49       *
    50       * @param clazz
    51       * @throws DAOException
    52       */
    53      public DAO getDAO(Class clazz) throws DAOException {
    54
    55          try {
    56              if (daoCache.containsKey(clazz.getName())) {
    57                  return (DAO) daoCache.get(clazz.getName());
    58              } else {
    59                  Object obj = Class.forName(clazz.getName()).newInstance();
    60                  daoCache.put(clazz.getName(), obj);
    61                  return (DAO) obj;
    62              }
    63          } catch (InstantiationException e) {
    64              throw new DAOException("DAOFactoryImpl.getDAO(" + clazz.getName()
    65                      + "):  NamingException while getting DAO type : \n"
    66                      + e.getMessage());
    67          } catch (IllegalAccessException e) {
    68              throw new DAOException("DAOFactoryImpl.getDAO(" + clazz.getName()
    69                      + "):  NamingException while getting DAO type : \n"
    70                      + e.getMessage());
    71          } catch (ClassNotFoundException e) {
    72              throw new DAOException("DAOFactoryImpl.getDAO(" + clazz.getName()
    73                      + "):  NamingException while getting DAO type : \n"
    74                      + e.getMessage());
    75          }
    76      }
    77
    78  }