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.JDOHelper;
33 import javax.jdo.PersistenceManagerFactory;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
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:$ $Date:$
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 int propsSize = props.size();
94 Set set = props.keySet();
95 Iterator iter = set.iterator();
96 while (iter.hasNext()) {
97 String key = (String) iter.next();
98 String value = props.getProperty(key);
99 buildPersistenceManagerFactory(key, value);
100 }
101 } catch (Exception e) {
102 logger.error("JDO multi configuration initialization failed ", e);
103 }
104 }
105
106 /***
107 * Build PersistenceManagerFactory.
108 *
109 * @param key
110 * @param value
111 * @throws IOException
112 */
113 private void buildPersistenceManagerFactory(String key, String value)
114 throws IOException {
115 Properties prop = ResourceHelper.getResourceAsProperties(value);
116 PersistenceManagerFactory pmFactory = JDOHelper
117 .getPersistenceManagerFactory(prop);
118 hashMap.put(key, pmFactory);
119 }
120
121 }