View Javadoc

    1   /***
    2    * @(#)SessionFactoryServiceImpl.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://sourceforge.net/projects/jfox
    22   */
    23
    24  package org.huihoo.jfox.soaf.services.persistence;
    25
    26  import java.io.IOException;
    27  import java.util.HashMap;
    28  import java.util.Iterator;
    29  import java.util.Properties;
    30  import java.util.Set;
    31
    32  import net.sf.hibernate.HibernateException;
    33  import net.sf.hibernate.SessionFactory;
    34  import net.sf.hibernate.cfg.Configuration;
    35
    36  import org.apache.commons.logging.Log;
    37  import org.apache.commons.logging.LogFactory;
    38  import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
    39  import org.picocontainer.Startable;
    40
    41  /***
    42   * <p>
    43   * SessionFactory Service Implementation
    44   * </p>
    45   *
    46   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    47   * @version $Revision:$ $Date:$
    48   * @version Revision: 1.0
    49   */
    50
    51  public class SessionFactoryServiceImpl implements SessionFactoryService,
    52  		Startable {
    53
    54  	private final Log logger = LogFactory.getLog(getClass());
    55
    56  	private static String HIBERNATE_MULTI_CONFIG = "hibernate-multi.properties";
    57
    58  	private static String HIBERNATE_DEFAULT_CONFIG = "hibernate.default";
    59
    60  	private Properties props;
    61
    62  	private HashMap hashMap = new HashMap();
    63
    64  	/***
    65  	 * @see org.huihoo.jfox.soaf.services.persistence.SessionFactoryService#getSessionFactory(java.lang.String)
    66  	 */
    67  	public SessionFactory getSessionFactory(String key) {
    68  		if (key == null) {
    69  			return (SessionFactory) hashMap.get(HIBERNATE_DEFAULT_CONFIG);
    70  		} else {
    71  			return (SessionFactory) hashMap.get(key);
    72  		}
    73  	}
    74
    75  	/***
    76  	 * @see org.picocontainer.Startable#start()
    77  	 */
    78  	public void start() {
    79  		loadConfig();
    80  	}
    81
    82  	/***
    83  	 * Load configuation.
    84  	 */
    85  	private void loadConfig() {
    86  		try {
    87  			props = ResourceHelper
    88  					.getResourceAsProperties(HIBERNATE_MULTI_CONFIG);
    89  			int propsSize = props.size();
    90  			Set set = props.keySet();
    91  			Iterator iter = set.iterator();
    92  			while (iter.hasNext()) {
    93  				String key = (String) iter.next();
    94  				String value = props.getProperty(key);
    95  				buildFactory(key, value);
    96  			}
    97  		} catch (Exception e) {
    98  			logger.error("Hibernate multi configuration initialization failed ", e);
    99  		}
    100 	}
    101
    102 	/***
    103 	 * @param key
    104 	 * @param value
    105 	 * @throws HibernateException
    106 	 */
    107 	private void buildFactory(String key, String value)
    108 			throws HibernateException, IOException {
    109 		Configuration config = new Configuration();
    110 		config.configure(ResourceHelper.getResourceURL(value));
    111 		SessionFactory sessionFactory = config.buildSessionFactory();
    112 		hashMap.put(key, sessionFactory);
    113 	}
    114
    115 	/***
    116 	 * @see org.picocontainer.Startable#stop()
    117 	 */
    118 	public void stop() {
    119 	}
    120 }