KickJava   Java API By Example, From Geeks To Geeks.

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


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.portlets.structure.factories.StructureFactory;
9 import com.dotmarketing.portlets.structure.model.Structure;
10 import com.dotmarketing.util.Logger;
11 import com.dotmarketing.util.UtilMethods;
12
13 /**
14  * @author David
15  */

16 public class StructureCache {
17
18     //Cache to store the data
19
private static DotCache cache;
20
21     //Name of the variable with the provider name
22
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
23
24     //Name of the variable with the properties path
25
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
26
27     //region's name for the cache
28
private static String JavaDoc regionName = "StructureCache";
29
30     static
31     {
32         init();
33     }
34     
35     public static void addStructure(Structure st)
36     {
37         // we use the identifier uri for our mappings.
38
long inode = st.getInode();
39         String JavaDoc structureName = st.getName();
40         cache.put(inode, st);
41         cache.put(structureName, st);
42         
43     }
44     
45     public static Structure getStructureByInode(long inode)
46     {
47         Structure st = (Structure) cache.get(inode);
48         if (st == null) {
49             st = StructureFactory.getStructureByInode(inode);
50             addStructure(st);
51         }
52         return st;
53     }
54
55     public static Structure getStructureByInode(String JavaDoc inode)
56     {
57         Structure st = (Structure) cache.get(inode);
58         if (st == null) {
59             st = StructureFactory.getStructureByInode(inode);
60             addStructure(st);
61         }
62         return st;
63     }
64
65     /**
66      * This methods retrieves the structure from the cache based in the
67      * structure name.
68      *
69      * This methods tries to retrive the structure from the cache, if the
70      * structure were not found in the cache, it would try to find it in database
71      * and store it in cache.
72      *
73      * <b>NOTE:</b> This method runs the same code than getStructureByType
74      * the name and the type of a structure are synomyns
75      *
76      * @param name Name of the structure
77      * @return The structure from cache
78      */

79     public static Structure getStructureByName(String JavaDoc name)
80     {
81         Structure st = (Structure) cache.get(name);
82         if (st == null) {
83             st = StructureFactory.getStructureByType(name);
84             addStructure(st);
85         }
86         return st;
87     }
88
89     /**
90      * @see getStructureByName(String)
91      *
92      * @param type Type of the structure
93      * @return The structure from cache
94      */

95     public static Structure getStructureByType(String JavaDoc type)
96     {
97         return getStructureByName(type);
98     }
99
100     public boolean hasStructureByType (String JavaDoc name) {
101         return getStructureByType(name) != null;
102     }
103     
104     public boolean hasStructureByName (String JavaDoc name) {
105         return getStructureByName(name) != null;
106     }
107     
108     public boolean hasStructureByInode (String JavaDoc inode) {
109         return getStructureByInode(inode) != null;
110     }
111     
112     public boolean hasStructureByInode (long inode) {
113         return getStructureByInode(inode) != null;
114     }
115     
116     public static void removeStructure(Structure st)
117     {
118         long inode = st.getInode();
119         String JavaDoc structureName = st.getName();
120         cache.remove(inode);
121         cache.remove(structureName);
122     }
123
124     public static void clearCache()
125     {
126         //clear the cache
127
cache.clear();
128     }
129     
130     private static void init()
131     {
132         try
133         {
134             Properties JavaDoc WorkingProperties = new Properties JavaDoc();
135             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
136             try {
137                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
138                 if (UtilMethods.isSet(propertyFilePath)) {
139                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
140                     WorkingProperties.load(fileInputStream);
141                 }
142             } catch (FileNotFoundException JavaDoc ex) {
143                 
144                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
145                 Logger.debug(StructureCache.class, propertyFileNotFound + ex.getMessage());
146             } catch (IOException JavaDoc ex) {
147                 Logger.debug(StructureCache.class, ex.getMessage());
148             }
149             finally
150             {
151                 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties);
152             }
153         }
154         catch(Exception JavaDoc ex)
155         {
156             Logger.error(StructureCache.class,ex.toString());
157         }
158     }
159 }
160
Popular Tags