KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.apache.commons.logging.LogFactory;
16
17 import com.dotmarketing.portlets.virtuallinks.factories.VirtualLinkFactory;
18 import com.dotmarketing.portlets.virtuallinks.model.VirtualLink;
19 import com.dotmarketing.util.Logger;
20 import com.dotmarketing.util.UtilMethods;
21
22 /**
23  * @author David & Salvador
24  *
25  * TODO To change the template for this generated type comment go to
26  * Window - Preferences - Java - Code Style - Code Templates
27  */

28 public class VirtualLinksCache
29 {
30     //Cache to store the data
31
private static DotCache cache;
32
33     //Name of the variable with the provider name
34
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
35
36     //Name of the variable with the properties path
37
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
38
39     //region's name for the cache
40
private static String JavaDoc regionName = "VirtualCache";
41
42     static
43     {
44         init();
45     }
46
47     public static String JavaDoc getPathFromCache(String JavaDoc url)
48     {
49         String JavaDoc realLink = (String JavaDoc) cache.get(url);
50         return realLink;
51     }
52     
53     public static void addPathToCache(VirtualLink vl)
54     {
55         if (vl != null && vl.getInode() > 0)
56         {
57             LogFactory.getLog(VirtualLinkFactory.class).info("Mapping: " + vl.getUrl() + " to: " + vl.getUri());
58             cache.put(vl.getUrl(), vl.getUri());
59         }
60         
61         PageNotFoundCache.removePageFromCache(vl.getUrl());
62         
63     }
64
65     public static void removePathFromCache(String JavaDoc url)
66     {
67         cache.remove(url);
68     }
69     
70     public static void clearCache()
71     {
72         cache.clear();
73         try {
74            Thread.sleep(1000);
75         } catch (Exception JavaDoc e) { }
76         mapAllVirtualLinks();
77     }
78     
79     public static void mapAllVirtualLinks() {
80         Iterator JavaDoc iter = VirtualLinkFactory.getVirtualLinks().iterator();
81         while (iter.hasNext()) {
82             VirtualLink vl = (VirtualLink) iter.next();
83             addPathToCache(vl);
84         }
85     }
86
87     private static void init()
88     {
89         try
90         {
91             Properties JavaDoc VirtualLinksProperties = new Properties JavaDoc();
92             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
93             try {
94                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
95                 if (UtilMethods.isSet(propertyFilePath)) {
96                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
97                     VirtualLinksProperties.load(fileInputStream);
98                 }
99             } catch (FileNotFoundException JavaDoc ex) {
100                 
101                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
102                 Logger.debug(VirtualLinksCache.class, propertyFileNotFound + ex.getMessage());
103             } catch (IOException JavaDoc ex) {
104                 Logger.debug(VirtualLinksCache.class, ex.getMessage());
105             }
106             finally
107             {
108                 cache = new DotCache(cacheProviderClassName, regionName,VirtualLinksProperties);
109             }
110         }
111         catch(Exception JavaDoc ex)
112         {
113             Logger.error(VirtualLinksCache.class,ex.toString());
114         }
115     }
116 }
117
Popular Tags