1 7 package com.dotmarketing.cache; 8 9 import java.io.FileInputStream ; 10 import java.io.FileNotFoundException ; 11 import java.io.IOException ; 12 import java.util.Properties ; 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 29 public class IdentifierCache 30 { 31 private static DotCache cache; 33 34 private static String providerEntry = "CACHE_PROVIDER"; 36 37 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 39 40 private static String 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 String key = id.getHostInode() + "-" + id.getURI(); 54 55 cache.put(key,id); 57 cache.put(id.getInode(),id); 58 } 59 } 60 61 public static void addAssetToIdentifierCache(Inode webAsset){ 62 Identifier id = IdentifierFactory.getIdentifierByInode(webAsset); 64 addIdentifierToIdentifierCache(id); 65 cache.put(webAsset.getInode(),id); 66 } 67 68 public static Identifier getPathFromIdCache(String URI, Host host) 69 { 70 return getPathFromIdCache(URI,host.getInode()); 71 } 72 73 81 public static Identifier getPathFromIdCache(String URI, long hostId) { 82 83 84 Identifier value = (Identifier) cache.get(hostId + "-" + URI); 85 86 if(value == null) 88 { 89 Identifier id = IdentifierFactory.getIdentifierByURI(URI,hostId); 91 if(id.getInode() > 0) 92 { 93 IdentifierCache.addIdentifierToIdentifierCache(id); 95 value = (Identifier) cache.get(hostId + "-" + URI); 97 } 98 } 99 100 if (value == null) 101 value = new Identifier (); 102 103 return value; 105 } 106 107 public static void removeURIFromIdCache(String URI, Host host) { 108 removeURIFromIdCache (URI, host.getInode()); 109 } 110 111 public static void removeURIFromIdCache(String URI, long hostId) 112 { 113 String 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 String key = identifier.getHostInode() + "-" + identifier.getURI(); 125 cache.remove(key); 127 cache.remove(asset.getInode()); 128 cache.remove(identifier.getInode()); 129 } 130 } 131 132 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(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 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 key = inode; 165 cache.remove(key); 166 } 167 168 public static void clearCache() 169 { 170 cache.clear(); 172 } 173 174 private static void init() 175 { 176 try 177 { 178 Properties IdentifierProperties = new Properties (); 179 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 180 try { 181 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 182 if (UtilMethods.isSet(propertyFilePath)) { 183 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 184 IdentifierProperties.load(fileInputStream); 185 } 186 } catch (FileNotFoundException ex) { 187 188 String propertyFileNotFound = "The property file has been no found \n"; 189 Logger.debug(IdentifierCache.class, propertyFileNotFound + ex.getMessage()); 190 } catch (IOException ex) { 191 Logger.debug(IdentifierCache.class, ex.getMessage()); 192 } 193 finally 194 { 195 cache = new DotCache(cacheProviderClassName, regionName,IdentifierProperties); 196 } 197 } 198 catch(Exception 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 value = (Long ) cache.get(webAssetInode); 208 209 if(value != null) 211 { 212 returnValue = true; 213 } 214 return returnValue; 215 } 216 } 217 | Popular Tags |