KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cache > BannerCache


1 /*
2  * Created on May 30, 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.dotmarketing.cache;
8
9 import java.io.FileInputStream JavaDoc;
10 import java.io.FileNotFoundException JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.util.Set JavaDoc;
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 /**
24  * @author David & Salvador
25  *
26  * TODO To change the template for this generated type comment go to
27  * Window - Preferences - Java - Code Style - Code Templates
28  */

29 public class BannerCache
30 {
31     //Cache to store the data
32
private static DotCache cache;
33
34     //Name of the variable with the provider name
35
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
36
37     //Name of the variable with the properties path
38
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
39
40     //region's name for the cache
41
private static String JavaDoc regionName = "BannerCache";
42     
43     static
44     {
45         init();
46     }
47     
48     public static void removeFromBannerCache(String JavaDoc path, String JavaDoc placement, Banner banner) {
49         //FInd the HashMap in the cache
50
HashMap JavaDoc bannerPlacements = (HashMap JavaDoc) cache.get(path);
51         if (bannerPlacements != null)
52         {
53             Set JavaDoc banners = (Set JavaDoc) bannerPlacements.get(placement);
54             if (banners != null) {
55                 banners.remove(banner);
56                 //TODO: validate this entries, in general you don't need to add the entry
57
//to the cache, but may be needed for distributed cache
58
//START
59
bannerPlacements.put(placement, banners);
60                 cache.put(path,bannerPlacements);
61                 //END
62
}
63         }
64     }
65     
66     public static void updateBannerCache(String JavaDoc previousPath, String JavaDoc previousPlacement,
67                                         String JavaDoc newPath, String JavaDoc newPlacement,
68                                         Banner banner)
69     {
70         cache.put(previousPath,null);
71     }
72
73     public static void addToBannerCache(String JavaDoc path, String JavaDoc placement, Banner banner)
74     {
75         //If the entry is null I create a new Instance of HashMap
76
HashMap JavaDoc bannerPlacements = (HashMap JavaDoc) cache.get(path);
77         if (bannerPlacements == null)
78         {
79             bannerPlacements = new HashMap JavaDoc();
80         }
81         
82         //If the entry is null I create a new instance of a HashSet to store the banner
83
Set JavaDoc banners = (Set JavaDoc) bannerPlacements.get(placement);
84         if (banners == null)
85         {
86             banners = new HashSet JavaDoc();
87         }
88         //Add the banner to the HashSet
89
banners.add(banner);
90         //Add the HashSet to the HashMap
91
bannerPlacements.put(placement,banners);
92         //Add the HashMap to the cache
93
cache.put(path,bannerPlacements);
94     }
95
96     public static ArrayList JavaDoc getBannersFromCache(String JavaDoc path, String JavaDoc placement)
97     {
98         //Find the HashMap
99
HashMap JavaDoc bannerPlacements = (HashMap JavaDoc) cache.get(path);
100         if (bannerPlacements==null)
101         {
102             bannerPlacements = new HashMap JavaDoc();
103         }
104         //Find the HashSet in the HashMap
105
ArrayList JavaDoc banners = (ArrayList JavaDoc) bannerPlacements.get(placement);
106         if (banners == null)
107         {
108             //If the Cache is empty, I populate the cache first
109
banners = new ArrayList JavaDoc();
110             //to do this lazily...
111
//List bannerList = BannerFactory.getBanners(path, placement);
112
List JavaDoc bannerList = BannerFactory.getBannersWithOrder(path, placement);
113             //Add the list to the HashSet
114
banners.addAll(bannerList);
115             //Add the HashSet to the HashMap
116
bannerPlacements.put(placement,banners);
117             //Add the HashMap to the cache
118
cache.put(path,bannerPlacements);
119         }
120         return banners;
121     }
122     
123     public static void clearCache()
124     {
125         //clear the cache
126
cache.clear();
127     }
128     
129     private static void init()
130     {
131         try
132         {
133             Properties JavaDoc BannerProperties = new Properties JavaDoc();
134             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
135             try
136             {
137                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
138                 if (UtilMethods.isSet(propertyFilePath)) {
139                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
140                     BannerProperties.load(fileInputStream);
141                 }
142             }
143             catch (FileNotFoundException JavaDoc ex)
144             {
145                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
146                 Logger.debug(BannerCache.class, propertyFileNotFound + ex.getMessage());
147             }
148             catch (IOException JavaDoc 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 JavaDoc ex)
158         {
159             Logger.error(BannerCache.class,ex.toString());
160         }
161     }
162 }
163
Popular Tags