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.List ; 7 import java.util.Properties ; 8 9 import com.dotmarketing.portlets.structure.model.Field; 10 import com.dotmarketing.portlets.structure.model.Structure; 11 import com.dotmarketing.util.Logger; 12 import com.dotmarketing.util.UtilMethods; 13 14 17 public class FieldsCache { 18 19 private static DotCache cache; 21 22 private static String providerEntry = "CACHE_PROVIDER"; 24 25 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 27 28 private static String regionName = "FieldsCache"; 30 31 static 32 { 33 init(); 34 } 35 36 public static void addFields(Structure st, List <Field> fields) 37 { 38 long inode = st.getInode(); 40 String structureName = st.getName(); 41 cache.put(inode, fields); 42 cache.put(structureName, fields); 43 } 44 45 public static List <Field> getFieldsByStructureInode(long inode) 46 { 47 List <Field> fields = (List <Field>) cache.get(inode); 48 if (fields == null) { 49 Structure st = StructureCache.getStructureByInode(inode); 50 fields = st.getFields(); 51 addFields(st, fields); 52 } 53 return fields; 54 } 55 56 public static List <Field> getFieldsByStructureInode(String inode) 57 { 58 return getFieldsByStructureInode(Long.parseLong(inode)); 59 } 60 61 75 public static List <Field> getFieldsByStructureName(String name) 76 { 77 List <Field> fields = (List <Field>) cache.get(name); 78 if (fields == null) { 79 Structure st = StructureCache.getStructureByName(name); 80 addFields(st, st.getFields()); 81 } 82 return fields; 83 } 84 85 91 public static List <Field> getFieldsByStructureType(String type) 92 { 93 return getFieldsByStructureName(type); 94 } 95 96 public boolean hasFieldsByStructureType (String name) { 97 return getFieldsByStructureType(name) != null; 98 } 99 100 public boolean hasFieldsByStructureName (String name) { 101 return getFieldsByStructureName(name) != null; 102 } 103 104 public boolean hasFieldsByStructureInode (String inode) { 105 return getFieldsByStructureInode(inode) != null; 106 } 107 108 public boolean hasFieldsByStructureInode (long inode) { 109 return getFieldsByStructureInode(inode) != null; 110 } 111 112 public static void removeFields(Structure st) 113 { 114 long inode = st.getInode(); 115 String structureName = st.getName(); 116 cache.remove(inode); 117 cache.remove(structureName); 118 } 119 120 public static void clearCache() 121 { 122 cache.clear(); 124 } 125 126 private static void init() 127 { 128 try 129 { 130 Properties WorkingProperties = new Properties (); 131 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 132 try { 133 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 134 if (UtilMethods.isSet(propertyFilePath)) { 135 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 136 WorkingProperties.load(fileInputStream); 137 } 138 } catch (FileNotFoundException ex) { 139 140 String propertyFileNotFound = "The property file has been no found \n"; 141 Logger.debug(FieldsCache.class, propertyFileNotFound + ex.getMessage()); 142 } catch (IOException ex) { 143 Logger.debug(FieldsCache.class, ex.getMessage()); 144 } 145 finally 146 { 147 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties); 148 } 149 } 150 catch(Exception ex) 151 { 152 Logger.error(FieldsCache.class,ex.toString()); 153 } 154 } 155 } 156 | Popular Tags |