| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CachingProvider |
|
| 1.5714285714285714;1.571 |
| 1 | /* | |
| 2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted under the terms of either of the following | |
| 6 | * Open Source licenses: | |
| 7 | * | |
| 8 | * The GNU General Public License, version 2, or any later version, as | |
| 9 | * published by the Free Software Foundation | |
| 10 | * (http://www.fsf.org/copyleft/gpl.html); | |
| 11 | * | |
| 12 | * or | |
| 13 | * | |
| 14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
| 15 | * | |
| 16 | * This software is provided "as is", with NO WARRANTY, not even the | |
| 17 | * implied warranties of fitness to purpose, or merchantability. You | |
| 18 | * assume all risks and liabilities associated with its use. | |
| 19 | * | |
| 20 | * See www.webmacro.org for more information on the WebMacro project. | |
| 21 | */ | |
| 22 | ||
| 23 | ||
| 24 | package org.webmacro.resource; | |
| 25 | ||
| 26 | import org.slf4j.Logger; | |
| 27 | import org.slf4j.LoggerFactory; | |
| 28 | import org.webmacro.Broker; | |
| 29 | import org.webmacro.InitException; | |
| 30 | import org.webmacro.Provider; | |
| 31 | import org.webmacro.ResourceException; | |
| 32 | import org.webmacro.util.Settings; | |
| 33 | ||
| 34 | /** | |
| 35 | * CacheManager is an abstract base class for providers which wish to | |
| 36 | * implement caching functionality. By extending CachingProvider and | |
| 37 | * implementing the methods in ResourceLoader, a provider can | |
| 38 | * automatically support caching using any CacheManager. CachingProvider | |
| 39 | * looks in the properties file to find the desired cache manager. | |
| 40 | * @since 0.96 | |
| 41 | */ | |
| 42 | ||
| 43 | abstract public class CachingProvider implements Provider, | |
| 44 | ResourceLoader | |
| 45 | { | |
| 46 | ||
| 47 | 2 | static Logger _log = LoggerFactory.getLogger(CachingProvider.class); |
| 48 | ||
| 49 | private CacheManager _cache; | |
| 50 | protected boolean _cacheSupportsReload; | |
| 51 | ||
| 52 | public CachingProvider () | |
| 53 | 12 | { |
| 54 | 12 | } |
| 55 | ||
| 56 | /** | |
| 57 | * If you override this method be sure and call super.init(...) | |
| 58 | */ | |
| 59 | public void init (Broker b, Settings config) throws InitException | |
| 60 | { | |
| 61 | String cacheManager; | |
| 62 | ||
| 63 | 12 | cacheManager = b.getSetting("CachingProvider." + getType() |
| 64 | + ".CacheManager"); | |
| 65 | 12 | if (cacheManager == null) |
| 66 | 0 | cacheManager = b.getSetting("CachingProvider.*.CacheManager"); |
| 67 | 12 | if (cacheManager == null || cacheManager.equals("")) |
| 68 | { | |
| 69 | 0 | _log.info("CachingProvider: No cache manager specified for " |
| 70 | + getType() + ", using TrivialCacheManager"); | |
| 71 | 0 | _cache = new TrivialCacheManager(); |
| 72 | } | |
| 73 | else | |
| 74 | { | |
| 75 | try | |
| 76 | { | |
| 77 | 12 | _cache = (CacheManager) b.classForName(cacheManager).newInstance(); |
| 78 | } | |
| 79 | 0 | catch (Exception e) |
| 80 | { | |
| 81 | 0 | _log.warn("Unable to load cache manager " + cacheManager |
| 82 | + " for resource type " + getType() | |
| 83 | + ", using TrivialCacheManager. Reason:\n" + e); | |
| 84 | 0 | _cache = new TrivialCacheManager(); |
| 85 | 12 | } |
| 86 | } | |
| 87 | 12 | _cache.init(b, config, getType()); |
| 88 | 12 | _cacheSupportsReload = _cache.supportsReload(); |
| 89 | 12 | } |
| 90 | ||
| 91 | /** | |
| 92 | * Clear the cache. If you override this method be sure | |
| 93 | * and call super.flush(). | |
| 94 | */ | |
| 95 | public void flush () | |
| 96 | { | |
| 97 | 0 | _cache.flush(); |
| 98 | 0 | } |
| 99 | ||
| 100 | /** | |
| 101 | * Close down the provider. If you override this method be | |
| 102 | * sure and call super.destroy(). | |
| 103 | */ | |
| 104 | public void destroy () | |
| 105 | { | |
| 106 | 0 | _cache.destroy(); |
| 107 | 0 | } |
| 108 | ||
| 109 | /** | |
| 110 | * Get the object associated with the specific query, using the | |
| 111 | * specified cache manager. | |
| 112 | */ | |
| 113 | public Object get (String query) throws ResourceException | |
| 114 | { | |
| 115 | 1542 | return _cache.get(query, this); |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * Delegates to ResourceLoader implementers the load operation | |
| 120 | * by casting the query as a string and invoking the | |
| 121 | * implemented method. | |
| 122 | */ | |
| 123 | public Object load (Object query, CacheElement ce) | |
| 124 | throws ResourceException | |
| 125 | { | |
| 126 | 544 | return ((ResourceLoader) this).load((String) query, ce); |
| 127 | } | |
| 128 | ||
| 129 | public String toString () | |
| 130 | { | |
| 131 | 12 | return "CachingProvider(type = " + getType() + ")"; |
| 132 | } | |
| 133 | ||
| 134 | ||
| 135 | } |