1 7 package com.dotmarketing.cache; 8 9 import java.io.FileInputStream ; 10 import java.io.FileNotFoundException ; 11 import java.io.IOException ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Properties ; 17 import java.util.Set ; 18 import com.dotmarketing.portlets.banners.factories.BannerFactory; 19 import com.dotmarketing.portlets.banners.model.Banner; 20 import com.dotmarketing.util.Logger; 21 import com.dotmarketing.util.UtilMethods; 22 23 29 public class BannerCache 30 { 31 private static DotCache cache; 33 34 private static String providerEntry = "CACHE_PROVIDER"; 36 37 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 39 40 private static String regionName = "BannerCache"; 42 43 static 44 { 45 init(); 46 } 47 48 public static void removeFromBannerCache(String path, String placement, Banner banner) { 49 HashMap bannerPlacements = (HashMap ) cache.get(path); 51 if (bannerPlacements != null) 52 { 53 Set banners = (Set ) bannerPlacements.get(placement); 54 if (banners != null) { 55 banners.remove(banner); 56 bannerPlacements.put(placement, banners); 60 cache.put(path,bannerPlacements); 61 } 63 } 64 } 65 66 public static void updateBannerCache(String previousPath, String previousPlacement, 67 String newPath, String newPlacement, 68 Banner banner) 69 { 70 cache.put(previousPath,null); 71 } 72 73 public static void addToBannerCache(String path, String placement, Banner banner) 74 { 75 HashMap bannerPlacements = (HashMap ) cache.get(path); 77 if (bannerPlacements == null) 78 { 79 bannerPlacements = new HashMap (); 80 } 81 82 Set banners = (Set ) bannerPlacements.get(placement); 84 if (banners == null) 85 { 86 banners = new HashSet (); 87 } 88 banners.add(banner); 90 bannerPlacements.put(placement,banners); 92 cache.put(path,bannerPlacements); 94 } 95 96 public static ArrayList getBannersFromCache(String path, String placement) 97 { 98 HashMap bannerPlacements = (HashMap ) cache.get(path); 100 if (bannerPlacements==null) 101 { 102 bannerPlacements = new HashMap (); 103 } 104 ArrayList banners = (ArrayList ) bannerPlacements.get(placement); 106 if (banners == null) 107 { 108 banners = new ArrayList (); 110 List bannerList = BannerFactory.getBannersWithOrder(path, placement); 113 banners.addAll(bannerList); 115 bannerPlacements.put(placement,banners); 117 cache.put(path,bannerPlacements); 119 } 120 return banners; 121 } 122 123 public static void clearCache() 124 { 125 cache.clear(); 127 } 128 129 private static void init() 130 { 131 try 132 { 133 Properties BannerProperties = new Properties (); 134 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 135 try 136 { 137 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 138 if (UtilMethods.isSet(propertyFilePath)) { 139 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 140 BannerProperties.load(fileInputStream); 141 } 142 } 143 catch (FileNotFoundException ex) 144 { 145 String propertyFileNotFound = "The property file has been no found \n"; 146 Logger.debug(BannerCache.class, propertyFileNotFound + ex.getMessage()); 147 } 148 catch (IOException ex) 149 { 150 Logger.debug(PageNotFoundCache.class, ex.getMessage()); 151 } 152 finally 153 { 154 cache = new DotCache(cacheProviderClassName, regionName,BannerProperties); 155 } 156 } 157 catch(Exception ex) 158 { 159 Logger.error(BannerCache.class,ex.toString()); 160 } 161 } 162 } 163 | Popular Tags |