KickJava   Java API By Example, From Geeks To Geeks.

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


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.IdentifierFactory;
19 import com.dotmarketing.factories.InodeFactory;
20 import com.dotmarketing.factories.PreviewFactory;
21 import com.dotmarketing.portlets.files.factories.FileFactory;
22 import com.dotmarketing.portlets.files.model.File;
23 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
24 import com.dotmarketing.util.Config;
25 import com.dotmarketing.util.Logger;
26 import com.dotmarketing.util.UtilMethods;
27
28 /**
29  * @author David
30  *
31  * TODO To change the template for this generated type comment go to
32  * Window - Preferences - Java - Code Style - Code Templates
33  */

34 public class WorkingCache {
35
36     //Cache to store the data
37
private static DotCache cache;
38
39     //Name of the variable with the provider name
40
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
41
42     //Name of the variable with the properties path
43
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
44
45     //region's name for the cache
46
private static String JavaDoc regionName = "WorkingCache";
47
48     static
49     {
50         init();
51     }
52     
53     public static void addToWorkingAssetToCache(WebAsset asset)
54     {
55         // we use the identifier uri for our mappings.
56
Identifier id = IdentifierFactory.getIdentifierByInode(asset);
57         //Velocity Page Extension
58
String JavaDoc ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
59         //Obtain the URI
60
String JavaDoc uri = id.getURI();
61         //Obtain the INODE
62
long inode = id.getHostInode();
63
64         if (UtilMethods.isSet(uri))
65         {
66             Logger.debug(WorkingCache.class, "Mapping Working: " + uri + " to " + uri);
67             if(uri.endsWith("." + ext))
68             {
69                 //add it to the cache
70
//for now we are adding the page URI
71
cache.put(inode + "-" + uri,uri);
72
73                 //if this is an index page, map its directories to it
74
if(id.getURI().endsWith("/index." + ext))
75                 {
76                     Logger.debug(WorkingCache.class, "Mapping Working: " + uri.substring(0,uri.lastIndexOf("/index." + ext)) + " to " + uri);
77                     cache.put(inode + "-" + uri.substring(0,uri.lastIndexOf("/index." + ext)),uri);
78                     Logger.debug(WorkingCache.class, "Mapping Working: " + id.getURI().substring(0,id.getURI().lastIndexOf("index." + ext)) + " to " + uri);
79                     cache.put(inode + "-" + uri.substring(0,uri.lastIndexOf("index." + ext)), uri);
80                 }
81             }
82             else
83             {
84                 //it's a file
85
String JavaDoc path = FileFactory.getRelativeAssetPath(asset);
86                 //add it to the cache
87
Logger.debug(WorkingCache.class, "Mapping: " + uri + " to " + path);
88                 cache.put(inode + "-" + uri, path);
89             }
90         }
91         PageNotFoundCache.removePageFromCache(uri);
92         
93     }
94     
95     public static String JavaDoc getPathFromCache(String JavaDoc URI, Host host)
96     {
97         return getPathFromCache (URI, host.getInode());
98     }
99
100         //Working cache methods
101
public static String JavaDoc getPathFromCache(String JavaDoc URI, long hostId)
102     {
103         String JavaDoc _uri = (String JavaDoc) cache.get(hostId + "-" + URI);
104
105         if(_uri != null) return _uri;
106         
107         String JavaDoc ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
108
109         if (URI.endsWith("/")) {
110             //it's a folder path, so I add index.html at the end
111
URI += "index." + ext;
112         }
113
114         // lets try to lazy get it.
115
Identifier id = IdentifierFactory.getIdentifierByURI(URI, hostId);
116
117         if(id.getInode() == 0)
118         {
119             //it's a folder path, so I add index.html at the end
120
URI += "/index." + ext;
121             id = IdentifierFactory.getIdentifierByURI(URI, hostId);
122             if(id.getInode() == 0)
123             {
124                 return null;
125             }
126         }
127
128
129         WebAsset asset = null;
130         if(id.getURI().endsWith("." + ext))
131         {
132             asset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, HTMLPage.class);
133         }
134         else
135         {
136             asset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, File.class);
137         }
138         // add to cache now
139
if(asset.getInode() > 0)
140         {
141             Logger.debug(PreviewFactory.class, "Lazy Preview Mapping: " + id.getURI() + " to " + URI);
142             addToWorkingAssetToCache(asset);
143         }
144
145         return (String JavaDoc) cache.get(hostId + "-" + URI);
146     }
147
148     public static void removeURIFromCache(String JavaDoc URI, Host host)
149     {
150         removeURIFromCache (URI, host.getInode());
151     }
152     
153     public static void removeURIFromCache(String JavaDoc URI, long hostId)
154     {
155             cache.remove(hostId + "-" + URI);
156     }
157
158     public static void removeAssetFromCache(WebAsset asset)
159     {
160         Inode inode = InodeFactory.getInode(String.valueOf(asset.getInode()),Inode.class);
161         Identifier identifier = IdentifierFactory.getIdentifierByInode(inode);
162         cache.remove(identifier.getHostInode() + "-" + 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 WorkingProperties = 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                     WorkingProperties.load(fileInputStream);
182                 }
183             } catch (FileNotFoundException JavaDoc ex) {
184                 
185                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
186                 Logger.debug(WorkingCache.class, propertyFileNotFound + ex.getMessage());
187             } catch (IOException JavaDoc ex) {
188                 Logger.debug(WorkingCache.class, ex.getMessage());
189             }
190             finally
191             {
192                 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties);
193             }
194         }
195         catch(Exception JavaDoc ex)
196         {
197             Logger.error(WorkingCache.class,ex.toString());
198         }
199     }
200 }
201
Popular Tags