|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SessionFactoryServiceImpl.java | 75% | 88.9% | 80% | 85.2% |
|
||||||||||||||
| 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 | 5 |
public SessionFactory getSessionFactory(String key) {
|
| 68 | 5 |
if (key == null) { |
| 69 | 5 |
return (SessionFactory) hashMap.get(HIBERNATE_DEFAULT_CONFIG);
|
| 70 |
} else {
|
|
| 71 | 0 |
return (SessionFactory) hashMap.get(key);
|
| 72 |
} |
|
| 73 |
} |
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* @see org.picocontainer.Startable#start()
|
|
| 77 |
*/
|
|
| 78 | 10 |
public void start() { |
| 79 | 10 |
loadConfig(); |
| 80 |
} |
|
| 81 |
|
|
| 82 |
/**
|
|
| 83 |
* Load configuation.
|
|
| 84 |
*/
|
|
| 85 | 10 |
private void loadConfig() { |
| 86 | 10 |
try {
|
| 87 | 10 |
props = ResourceHelper |
| 88 |
.getResourceAsProperties(HIBERNATE_MULTI_CONFIG); |
|
| 89 | 10 |
int propsSize = props.size();
|
| 90 | 10 |
Set set = props.keySet(); |
| 91 | 10 |
Iterator iter = set.iterator(); |
| 92 | 10 |
while (iter.hasNext()) {
|
| 93 | 20 |
String key = (String) iter.next(); |
| 94 | 20 |
String value = props.getProperty(key); |
| 95 | 20 |
buildFactory(key, value); |
| 96 |
} |
|
| 97 |
} catch (Exception e) {
|
|
| 98 | 0 |
logger.error("Hibernate multi configuration initialization failed ", e);
|
| 99 |
} |
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
/**
|
|
| 103 |
* @param key
|
|
| 104 |
* @param value
|
|
| 105 |
* @throws HibernateException
|
|
| 106 |
*/
|
|
| 107 | 20 |
private void buildFactory(String key, String value) |
| 108 |
throws HibernateException, IOException {
|
|
| 109 | 20 |
Configuration config = new Configuration();
|
| 110 | 20 |
config.configure(ResourceHelper.getResourceURL(value)); |
| 111 | 20 |
SessionFactory sessionFactory = config.buildSessionFactory(); |
| 112 | 20 |
hashMap.put(key, sessionFactory); |
| 113 |
} |
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* @see org.picocontainer.Startable#stop()
|
|
| 117 |
*/
|
|
| 118 | 0 |
public void stop() { |
| 119 |
} |
|
| 120 |
} |
|
||||||||||