KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Logger;
21 import com.dotmarketing.util.UtilMethods;
22
23 /**
24  * @author David
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 IdentifierCache
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 = "IdentifierCache";
42
43     static
44     {
45         init();
46     }
47
48     public static void addIdentifierToIdentifierCache(Identifier id)
49     {
50         
51         if (id != null && id.getInode() > 0) {
52             //Obtain the key for the new entrance
53
String JavaDoc key = id.getHostInode() + "-" + id.getURI();
54             
55             //Add the new entry to the cache
56
cache.put(key,id);
57             cache.put(id.getInode(),id);
58         }
59     }
60
61     public static void addAssetToIdentifierCache(Inode webAsset){
62         //we use the identifier uri for our mappings.
63
Identifier id = IdentifierFactory.getIdentifierByInode(webAsset);
64         addIdentifierToIdentifierCache(id);
65         cache.put(webAsset.getInode(),id);
66     }
67     
68     public static Identifier getPathFromIdCache(String JavaDoc URI, Host host)
69     {
70         return getPathFromIdCache(URI,host.getInode());
71     }
72
73     /**
74      * This method find the identifier associated with the given URI
75      * this methods will try to find the identifier in memory but if it is not found in memory
76      * it'll be found in db and put in memory.
77      * @param URI uri of the identifier
78      * @param hostId host where the identifier belongs
79      * @return The identifier or an empty (inode = 0) identifier if it wasn't found in momory and db.
80      */

81     public static Identifier getPathFromIdCache(String JavaDoc URI, long hostId) {
82
83
84         Identifier value = (Identifier) cache.get(hostId + "-" + URI);
85         
86         //If not found
87
if(value == null)
88         {
89             //If the entry is not in the cache I will find it at DB
90
Identifier id = IdentifierFactory.getIdentifierByURI(URI,hostId);
91             if(id.getInode() > 0)
92             {
93                 //Add the entry to the cache
94
IdentifierCache.addIdentifierToIdentifierCache(id);
95                 //return the value
96
value = (Identifier) cache.get(hostId + "-" + URI);
97             }
98         }
99
100         if (value == null)
101             value = new Identifier ();
102         
103         //Return the entry
104
return value;
105     }
106
107     public static void removeURIFromIdCache(String JavaDoc URI, Host host) {
108         removeURIFromIdCache (URI, host.getInode());
109     }
110     
111     public static void removeURIFromIdCache(String JavaDoc URI, long hostId)
112     {
113         String JavaDoc key = hostId + "-" + URI;
114         cache.remove(key);
115     }
116
117     public static void removeAssetFromIdCache(WebAsset asset)
118     {
119         Inode inode = InodeFactory.getInode(String.valueOf(asset.getInode()),Inode.class);
120         Identifier identifier = IdentifierFactory.getIdentifierByInode(inode);
121         
122         if (identifier.getInode() > 0) {
123             //Obtain the key of the entry to delete
124
String JavaDoc key = identifier.getHostInode() + "-" + identifier.getURI();
125             //Remove the element from the cache
126
cache.remove(key);
127             cache.remove(asset.getInode());
128             cache.remove(identifier.getInode());
129         }
130     }
131     
132     //Methods to store identifiers using webassets inodes (not urls) as keys
133
public static Identifier getIdentifierByInodeFromCache(WebAsset asset)
134     {
135         return getIdentifierByInodeFromCache(asset.getInode());
136     }
137
138     public static Identifier getIdentifierByInodeFromCache(long webAssetInode) {
139
140         Identifier value = (Identifier) cache.get(webAssetInode);
141         
142         //If not found
143
if(value == null)
144         {
145             Inode inode = InodeFactory.getInode(webAssetInode, Inode.class);
146             addAssetToIdentifierCache(inode);
147             value= (Identifier) cache.get(webAssetInode);
148             
149         }
150         
151         if (value == null)
152             value = new Identifier ();
153         
154         //Return the entry
155
return value;
156     }
157
158     public static void removeFromIdCacheByInode(WebAsset inode) {
159         removeFromIdCacheByInode (inode.getInode());
160     }
161     
162     public static void removeFromIdCacheByInode(long inode)
163     {
164         Long JavaDoc key = inode;
165         cache.remove(key);
166     }
167     
168     public static void clearCache()
169     {
170         //clear the cache
171
cache.clear();
172     }
173     
174     private static void init()
175     {
176         try
177         {
178             Properties JavaDoc IdentifierProperties = new Properties JavaDoc();
179             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
180             try {
181                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
182                 if (UtilMethods.isSet(propertyFilePath)) {
183                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
184                     IdentifierProperties.load(fileInputStream);
185                 }
186             } catch (FileNotFoundException JavaDoc ex) {
187                 
188                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
189                 Logger.debug(IdentifierCache.class, propertyFileNotFound + ex.getMessage());
190             } catch (IOException JavaDoc ex) {
191                 Logger.debug(IdentifierCache.class, ex.getMessage());
192             }
193             finally
194             {
195                 cache = new DotCache(cacheProviderClassName, regionName,IdentifierProperties);
196             }
197         }
198         catch(Exception JavaDoc ex)
199         {
200             Logger.error(IdentifierCache.class,ex.toString());
201         }
202     }
203     
204     public static boolean hasIdentifier(long webAssetInode)
205     {
206         boolean returnValue = false;
207         Long JavaDoc value = (Long JavaDoc) cache.get(webAssetInode);
208         
209         //If found
210
if(value != null)
211         {
212             returnValue = true;
213         }
214         return returnValue;
215     }
216 }
217
Popular Tags