1 /***
2 * @(#)CacheServiceManagerImpl.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://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.cache;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Properties;
30
31 import org.huihoo.jfox.soaf.exception.CacheServiceException;
32 import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
33
34 /***
35 * <p>
36 * CachingServiceManager is medator of pluggable service provider.
37 * </p>
38 *
39 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
40 * @version $Revision: 1.1 $ $Date: 2005/05/22 06:48:52 $
41 * @version Revision: 1.0
42 */
43
44 public class CacheServiceManagerImpl implements CacheServiceManager {
45
46 public CacheServiceManagerImpl() {
47 }
48
49 /***
50 * @see org.huihoo.jfox.soaf.services.cache.CacheServiceManager#getCacheService()
51 */
52 public CacheService getCacheService(String regionName, Properties prop)
53 throws CacheServiceException {
54 CacheServiceProvider cacheServiceProvider = null;
55 Properties props = null;
56 try {
57 InputStream stream = ResourceHelper
58 .getResourceAsStream(CacheConstant.CACHE_PROPERTIES);
59
60 if (stream != null) {
61 props = new Properties();
62 props.load(stream);
63 stream.close();
64 }
65 } catch (IOException e) {
66 throw new CacheServiceException(
67 "Load Cache configuration IO exception " + e);
68 } catch (SecurityException e) {
69 throw new CacheServiceException("Cache access security exception "
70 + e);
71 }
72
73 String providerClass = props.getProperty(CacheConstant.CACHE_PROVIDER);
74 if (providerClass != null && !providerClass.equals("")) {
75 cacheServiceProvider = getCacheServiceProvider(providerClass);
76 } else {
77 cacheServiceProvider = new HashMapCacheServiceProvider();
78 }
79 return cacheServiceProvider.buildCacheService(regionName, prop);
80
81 }
82
83 /***
84 * Load cache service provider.
85 *
86 * @param providerClass
87 * @return CacheServiceProvider
88 * @throws CacheServiceException
89 */
90 private CacheServiceProvider getCacheServiceProvider(String providerClass)
91 throws CacheServiceException {
92 try {
93 return (CacheServiceProvider) Class.forName(providerClass)
94 .newInstance();
95 } catch (Exception e) {
96 throw new CacheServiceException("Getting cahce service provider ",
97 e);
98 }
99 }
100
101 }