KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.dotmarketing.cache;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import com.dotmarketing.beans.Host;
9 import com.dotmarketing.factories.HostFactory;
10 import com.dotmarketing.portlets.folders.factories.FolderFactory;
11 import com.dotmarketing.portlets.folders.model.Folder;
12 import com.dotmarketing.util.Logger;
13 import com.dotmarketing.util.UtilMethods;
14
15 /**
16  * @author David
17  */

18 public class FolderCache {
19
20     //Cache to store the data
21
private static DotCache cache;
22
23     //Name of the variable with the provider name
24
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
25
26     //Name of the variable with the properties path
27
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
28
29     //region's name for the cache
30
private static String JavaDoc regionName = "FolderCache";
31
32     static
33     {
34         init();
35     }
36     
37     public static void addFolder(Folder f)
38     {
39         // we use the identifier uri for our mappings.
40
long inode = f.getInode();
41         String JavaDoc folderPath = f.getPath();
42         Host host = HostFactory.getParentHost(f);
43         folderPath = host.getHostname() + ":" + folderPath;
44         String JavaDoc folderPath2 = host.getInode() + ":" + f.getPath();
45         cache.put(inode, f);
46         cache.put(folderPath, f);
47         cache.put(folderPath2, f);
48         
49     }
50     
51     public static Folder getFolderByInode(long inode)
52     {
53         Folder f = (Folder) cache.get(inode);
54         if (f == null) {
55             f = FolderFactory.getFolderByInode(inode);
56             addFolder(f);
57         }
58         return f;
59     }
60
61     public static Folder getFolderByInode(String JavaDoc inode)
62     {
63         Folder f = (Folder) cache.get(inode);
64         if (f == null) {
65             f = FolderFactory.getFolderByInode(Long.parseLong(inode));
66             addFolder(f);
67         }
68         return f;
69     }
70
71     public static Folder getFolderByPath(String JavaDoc path, String JavaDoc hostName)
72     {
73         String JavaDoc folderPath = hostName + ":" + path;
74         Folder f = (Folder) cache.get(folderPath);
75         if (f == null) {
76             Host host = HostCache.getFromCache(hostName);
77             f = FolderFactory.getFolderByPath(path, host);
78             addFolder(f);
79         }
80         
81         return f;
82     }
83
84     public static Folder getFolderByPath(String JavaDoc path, long hostId)
85     {
86         String JavaDoc folderPath = hostId + ":" + path;
87         Folder f = (Folder) cache.get(folderPath);
88         if (f == null) {
89             Host host = HostFactory.getHost(hostId);
90             f = FolderFactory.getFolderByPath(path, host);
91             addFolder(f);
92         }
93         
94         return f;
95     }
96
97     public static void removeFolder(Folder f)
98     {
99         long inode = f.getInode();
100         String JavaDoc folderPath = f.getPath();
101         Host host = HostFactory.getParentHost(f);
102         folderPath = host.getHostname() + ":" + folderPath;
103         cache.remove(inode);
104         cache.remove(folderPath);
105     }
106
107     public static void clearCache()
108     {
109         //clear the cache
110
cache.clear();
111     }
112     
113     private static void init()
114     {
115         try
116         {
117             Properties JavaDoc WorkingProperties = new Properties JavaDoc();
118             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
119             try {
120                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
121                 if (UtilMethods.isSet(propertyFilePath)) {
122                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
123                     WorkingProperties.load(fileInputStream);
124                 }
125             } catch (FileNotFoundException JavaDoc ex) {
126                 
127                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
128                 Logger.debug(FolderCache.class, propertyFileNotFound + ex.getMessage());
129             } catch (IOException JavaDoc ex) {
130                 Logger.debug(FolderCache.class, ex.getMessage());
131             }
132             finally
133             {
134                 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties);
135             }
136         }
137         catch(Exception JavaDoc ex)
138         {
139             Logger.error(FolderCache.class,ex.toString());
140         }
141     }
142 }
143
Popular Tags