KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > cache > ConfigurationCacheFactory


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.config.cache;
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22
23 import org.sape.carbon.core.bootstrap.BootStrapper;
24 import org.sape.carbon.core.config.ConfigurationService;
25 import org.sape.carbon.core.exception.ExceptionUtility;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * Factory class that returns an instance of ConfigurationCache. By default,
32  * getInstance returns an instance of SynchronizedConfigurationCache. If
33  * a class that implements ConfigurationCache is specified within
34  * DeploymentProperties by the name carbon.config.CacheType, an instance of that
35  * class will be returned.
36  *
37  * Copyright 2002 Sapient
38  * @since carbon 1.1
39  * @author Douglas Voet, Oct 24, 2002
40  * @version $Revision: 1.8 $($Author: dvoet $ / $Date: 2003/05/05 21:21:16 $)
41  */

42 public class ConfigurationCacheFactory {
43
44     /** Provides a handle to Apache-commons logger */
45     private static Log log =
46         LogFactory.getLog(ConfigurationCacheFactory.class);
47
48     /** Holds teh deployment property name for the cache type. */
49     public static final String JavaDoc CACHE_TYPE_PROPERTY_NAME =
50         "carbon.config.CacheType";
51
52     /**
53      * Gets a new instance of the configured ConfigurationCache
54      * @param configService the ConfigurationService that the cache will use
55      * to retrieve configuration docuemnts
56      * @return ConfigurationCache
57      */

58     public static ConfigurationCache getInstance(
59         ConfigurationService configService) {
60
61         ConfigurationCache cache =
62             new SynchronizedConfigurationCache(configService);
63
64         String JavaDoc cacheTypeName =
65             BootStrapper.getInstance().getDeploymentProperty(
66                 ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME);
67
68         if (cacheTypeName != null) {
69             // a cache type was specified, try to use it
70
try {
71
72                 Class JavaDoc cacheType = Class.forName(cacheTypeName);
73                 Constructor JavaDoc cacheConstructor = cacheType.getConstructor(
74                     new Class JavaDoc[] {ConfigurationService.class});
75                 cache = (ConfigurationCache) cacheConstructor.newInstance(
76                     new Object JavaDoc[] {configService});
77
78             } catch (ClassCastException JavaDoc cce) {
79                 if (log.isWarnEnabled()) {
80                     log.warn("Specifed Configuration cache ["
81                         + cacheTypeName
82                         + "], was not of type "
83                         + ConfigurationCache.class.getName()
84                         + ", using default");
85                 }
86             } catch (NoSuchMethodException JavaDoc nsme) {
87                 if (log.isWarnEnabled()) {
88                     log.warn("Specifed Configuration cache ["
89                         + cacheTypeName
90                         + "], does not have a single parameter "
91                         + "constructor of type "
92                         + ConfigurationService.class.getName()
93                         + ", using default");
94                 }
95             } catch (ClassNotFoundException JavaDoc cnfe) {
96                 if (log.isWarnEnabled()) {
97                     log.warn("Specifed Configuration cache ["
98                         + cacheTypeName
99                         + "], was not found, using default");
100                 }
101             } catch (InstantiationException JavaDoc ie) {
102                 if (log.isWarnEnabled()) {
103                     log.warn("Caught InstantiationException "
104                         + "instantiating class ["
105                         + cacheTypeName
106                         + "], using default");
107                 }
108             } catch (IllegalAccessException JavaDoc iae) {
109                 if (log.isWarnEnabled()) {
110                     log.warn("Caught IllegalAccessException "
111                         + "instantiating class ["
112                         + cacheTypeName
113                         + "], using default");
114                 }
115             } catch (InvocationTargetException JavaDoc ite) {
116                 if (log.isWarnEnabled()) {
117                     log.warn("Caught exception instantiating class ["
118                         + cacheTypeName
119                         + "], using default: "
120                         + ExceptionUtility.printStackTracesToString(
121                             ite.getTargetException()));
122                 }
123             }
124         }
125
126         if (log.isInfoEnabled()) {
127             log.info("Using instance of " + cache.getClass().getName()
128                                         + " to cache fetched configurations ");
129         }
130
131         return cache;
132     }
133 }
134
Popular Tags