View Javadoc

1   /***
2    * @(#)PersistenceManagerFactoryServiceImpl.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 javax.jdo.PersistenceManagerFactory;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
37  import org.jpox.PersistenceManagerFactoryImpl;
38  import org.picocontainer.Startable;
39  
40  /***
41   * <p>
42   * PersistenceManagerFactory Service Implementation
43   * </p>
44   * 
45   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
46   * @version $Revision: 1.1 $ $Date: 2006/02/15 08:45:45 $
47   * @version Revision: 1.0
48   */
49  
50  public class PersistenceManagerFactoryServiceImpl implements
51  		PersistenceManagerFactoryService, Startable {
52  
53  	private final Log logger = LogFactory.getLog(getClass());
54  
55  	private static String JDO_MULTI_CONFIG = "jdo-multi.properties";
56  
57  	private static String JDO_DEFAULT_CONFIG = "jdo.default";
58  
59  	private Properties props;
60  
61  	private HashMap hashMap = new HashMap();
62  
63  	/***
64  	 * @see org.huihoo.jfox.soaf.services.persistence.PersistenceManagerFactoryService#getPersistenceManagerFactory(java.lang.String)
65  	 */
66  	public PersistenceManagerFactory getPersistenceManagerFactory(String key) {
67  		if (key == null) {
68  			return (PersistenceManagerFactory) hashMap.get(JDO_DEFAULT_CONFIG);
69  		} else {
70  			return (PersistenceManagerFactory) hashMap.get(key);
71  		}
72  	}
73  
74  	/***
75  	 * @see org.picocontainer.Startable#start()
76  	 */
77  	public void start() {
78  		loadConfig();
79  	}
80  
81  	/***
82  	 * @see org.picocontainer.Startable#stop()
83  	 */
84  	public void stop() {		
85  	}
86  
87  	/***
88  	 * Load configuation.
89  	 */
90  	private void loadConfig() {
91  		try {
92  			props = ResourceHelper.getResourceAsProperties(JDO_MULTI_CONFIG);
93  			Set set = props.keySet();
94  			Iterator iter = set.iterator();
95  			while (iter.hasNext()) {
96  				String key = (String) iter.next();
97  				String value = props.getProperty(key);
98  				buildPersistenceManagerFactory(key, value);
99  			}
100 		} catch (Exception e) {
101 			logger.error("JDO multi configuration initialization failed ", e);
102 		}
103 	}
104 
105 	/***
106 	 * Build PersistenceManagerFactory.
107 	 * 
108 	 * @param key
109 	 * @param value
110 	 * @throws IOException
111 	 */
112 	private void buildPersistenceManagerFactory(String key, String value)
113 			throws IOException {
114 		Properties prop = ResourceHelper.getResourceAsProperties(value);
115 		PersistenceManagerFactory pmFactory = PersistenceManagerFactoryImpl.getPersistenceManagerFactory(prop);	
116 		hashMap.put(key, pmFactory);
117 	}
118 	
119 }