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: 1.1 $ $Date: 2006/02/15 08:45:45 $
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 Set set = props.keySet();
90 Iterator iter = set.iterator();
91 while (iter.hasNext()) {
92 String key = (String) iter.next();
93 String value = props.getProperty(key);
94 buildFactory(key, value);
95 }
96 } catch (Exception e) {
97 logger.error("Hibernate multi configuration initialization failed ", e);
98 }
99 }
100
101 /***
102 * @param key
103 * @param value
104 * @throws HibernateException
105 */
106 private void buildFactory(String key, String value)
107 throws HibernateException, IOException {
108 Configuration config = new Configuration();
109 config.configure(ResourceHelper.getResourceURL(value));
110 SessionFactory sessionFactory = config.buildSessionFactory();
111 hashMap.put(key, sessionFactory);
112 }
113
114 /***
115 * @see org.picocontainer.Startable#stop()
116 */
117 public void stop() {
118 }
119 }