KickJava   Java API By Example, From Geeks To Geeks.

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


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.Properties JavaDoc;
13
14 import com.dotmarketing.beans.Host;
15 import com.dotmarketing.beans.Identifier;
16 import com.dotmarketing.beans.Inode;
17 import com.dotmarketing.beans.WebAsset;
18 import com.dotmarketing.factories.HostFactory;
19 import com.dotmarketing.factories.IdentifierFactory;
20 import com.dotmarketing.factories.InodeFactory;
21 import com.dotmarketing.factories.PublishFactory;
22 import com.dotmarketing.portlets.files.factories.FileFactory;
23 import com.dotmarketing.portlets.files.model.File;
24 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
25 import com.dotmarketing.util.Config;
26 import com.dotmarketing.util.Logger;
27 import com.dotmarketing.util.UtilMethods;
28
29 /**
30  * @author David
31  *
32  * TODO To change the template for this generated type comment go to
33  * Window - Preferences - Java - Code Style - Code Templates
34  */

35 public class LiveCache
36 {
37     //Cache to store the data
38
private static DotCache cache;
39
40     //Name of the variable with the provider name
41
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
42
43     //Name of the variable with the properties path
44
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
45
46     //region's name for the cache
47
private static String JavaDoc regionName = "LiveCache";
48
49     static
50     {
51         init();
52     }
53     
54     public static void addToLiveAssetToCache(WebAsset asset)
55     {
56         //The default value for velocity page extension
57
String JavaDoc ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
58         // we use the identifier uri for our mappings.
59
Identifier id = IdentifierFactory.getIdentifierByInode(asset);
60         //Obtain the host of the webassets
61
Host host = HostFactory.getParentHost(asset);
62         //Obtain the URI for future uses
63
String JavaDoc uri = id.getURI();
64         //Obtain the inode value of the host;
65
long inode = host.getInode();
66
67         //if this is an index page, map its directories to it
68
if(uri.endsWith("." + ext))
69         {
70             Logger.debug(LiveCache.class, "Mapping: " + uri + " to " + uri);
71             //Add the entry to the cache
72
cache.put(inode + ":" + uri,uri);
73
74             if(uri.endsWith("/index." + ext))
75             {
76                 //Add the entry to the cache
77
Logger.debug(LiveCache.class, "Mapping: " + uri.substring(0,uri.lastIndexOf("/index." + ext)) + " to " + uri);
78                 cache.put(inode + ":" + uri.substring(0,uri.lastIndexOf("/index." + ext)),uri);
79                 //Add the entry to the cache
80
Logger.debug(LiveCache.class, "Mapping: " + uri.substring(0,uri.lastIndexOf("/index." + ext)) + " to " + uri);
81                 cache.put(inode + ":" + uri.substring(0,uri.lastIndexOf("index." + ext)),uri);
82             }
83         }
84         else
85         {
86             String JavaDoc path = FileFactory.getRelativeAssetPath(asset);
87             //add the entry to the cache
88
Logger.debug(LiveCache.class, "Mapping: " + uri + " to " + path);
89             cache.put(inode + ":" + uri,path);
90         }
91         
92         PageNotFoundCache.removePageFromCache(uri);
93         
94     }
95
96     public static String JavaDoc getPathFromCache(String JavaDoc URI, Host host)
97     {
98         if (URI.equals("/")) {
99             String JavaDoc pointer = (String JavaDoc) VirtualLinksCache.getPathFromCache(host.getHostname() + ":/cmsHomePage");
100             if (!UtilMethods.isSet(pointer)) {
101                 pointer = (String JavaDoc) VirtualLinksCache.getPathFromCache("/cmsHomePage");
102             }
103             if (UtilMethods.isSet(pointer))
104                 URI = pointer;
105         }
106         return getPathFromCache (URI, host.getInode());
107     }
108     
109     public static String JavaDoc getPathFromCache(String JavaDoc URI, long hostId)
110     {
111         String JavaDoc _uri = (String JavaDoc) cache.get(hostId + ":" + URI);
112
113         if(_uri != null)
114         {
115             return _uri;
116         }
117         
118         String JavaDoc ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
119
120         if (URI.endsWith("/"))
121         {
122             //it's a folder path, so I add index.html at the end
123
URI += "index." + ext;
124         }
125
126         // lets try to lazy get it.
127
Identifier id = IdentifierFactory.getIdentifierByURI(URI, hostId);
128
129         if(id.getInode() == 0)
130         {
131             //it's a folder path, so I add index.html at the end
132
URI += "/index." + ext;
133             id = IdentifierFactory.getIdentifierByURI(URI, hostId);
134             if(id.getInode() == 0)
135             {
136                 return null;
137             }
138         }
139
140         WebAsset asset = null;
141         if(id.getURI().endsWith("." + ext))
142         {
143             asset = (WebAsset) IdentifierFactory.getLiveChildOfClass(id, HTMLPage.class);
144         }
145         else
146         {
147             asset = (WebAsset) IdentifierFactory.getLiveChildOfClass(id, File.class);
148         }
149         if(asset.getInode() > 0)
150         {
151             Logger.debug(PublishFactory.class, "Lazy Mapping: " + id.getURI() + " to " + URI);
152             addToLiveAssetToCache(asset);
153         }
154         return (String JavaDoc) cache.get(hostId + ":" + URI);
155     }
156
157     public static void removeAssetFromCache(WebAsset asset)
158     {
159         long hostId = HostFactory.getParentHost(asset).getInode();
160         Inode inode = InodeFactory.getInode(String.valueOf(asset.getInode()),Inode.class);
161         Identifier identifier = IdentifierFactory.getIdentifierByInode(inode);
162         cache.remove(hostId + ":" + identifier.getURI());
163     }
164     
165     public static void clearCache()
166     {
167         //clear the cache
168
cache.clear();
169     }
170     
171     private static void init()
172     {
173         try
174         {
175             Properties JavaDoc LiveProperties = new Properties JavaDoc();
176             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
177             try {
178                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
179                 if (UtilMethods.isSet(propertyFilePath)) {
180                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
181                     LiveProperties.load(fileInputStream);
182                 }
183             } catch (FileNotFoundException JavaDoc ex) {
184                 
185                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
186                 Logger.debug(LiveCache.class, propertyFileNotFound + ex.getMessage());
187             } catch (IOException JavaDoc ex) {
188                 Logger.debug(LiveCache.class, ex.getMessage());
189             }
190             finally
191             {
192                 cache = new DotCache(cacheProviderClassName, regionName,LiveProperties);
193             }
194         }
195         catch(Exception JavaDoc ex)
196         {
197             Logger.error(LiveCache.class,ex.toString());
198         }
199     }
200 }
Popular Tags