1 package com.dotmarketing.cache; 2 3 import java.io.FileInputStream ; 4 import java.io.FileNotFoundException ; 5 import java.io.IOException ; 6 import java.util.Properties ; 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 16 public class StructureCache { 17 18 private static DotCache cache; 20 21 private static String providerEntry = "CACHE_PROVIDER"; 23 24 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 26 27 private static String regionName = "StructureCache"; 29 30 static 31 { 32 init(); 33 } 34 35 public static void addStructure(Structure st) 36 { 37 long inode = st.getInode(); 39 String 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 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 79 public static Structure getStructureByName(String 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 95 public static Structure getStructureByType(String type) 96 { 97 return getStructureByName(type); 98 } 99 100 public boolean hasStructureByType (String name) { 101 return getStructureByType(name) != null; 102 } 103 104 public boolean hasStructureByName (String name) { 105 return getStructureByName(name) != null; 106 } 107 108 public boolean hasStructureByInode (String 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 structureName = st.getName(); 120 cache.remove(inode); 121 cache.remove(structureName); 122 } 123 124 public static void clearCache() 125 { 126 cache.clear(); 128 } 129 130 private static void init() 131 { 132 try 133 { 134 Properties WorkingProperties = new Properties (); 135 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 136 try { 137 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 138 if (UtilMethods.isSet(propertyFilePath)) { 139 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 140 WorkingProperties.load(fileInputStream); 141 } 142 } catch (FileNotFoundException ex) { 143 144 String propertyFileNotFound = "The property file has been no found \n"; 145 Logger.debug(StructureCache.class, propertyFileNotFound + ex.getMessage()); 146 } catch (IOException ex) { 147 Logger.debug(StructureCache.class, ex.getMessage()); 148 } 149 finally 150 { 151 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties); 152 } 153 } 154 catch(Exception ex) 155 { 156 Logger.error(StructureCache.class,ex.toString()); 157 } 158 } 159 } 160 | Popular Tags |