| 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.factories.PreviewFactory; 21 import com.dotmarketing.portlets.files.factories.FileFactory; 22 import com.dotmarketing.portlets.files.model.File; 23 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 24 import com.dotmarketing.util.Config; 25 import com.dotmarketing.util.Logger; 26 import com.dotmarketing.util.UtilMethods; 27 28 34 public class WorkingCache { 35 36 private static DotCache cache; 38 39 private static String providerEntry = "CACHE_PROVIDER"; 41 42 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 44 45 private static String regionName = "WorkingCache"; 47 48 static 49 { 50 init(); 51 } 52 53 public static void addToWorkingAssetToCache(WebAsset asset) 54 { 55 Identifier id = IdentifierFactory.getIdentifierByInode(asset); 57 String ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION"); 59 String uri = id.getURI(); 61 long inode = id.getHostInode(); 63 64 if (UtilMethods.isSet(uri)) 65 { 66 Logger.debug(WorkingCache.class, "Mapping Working: " + uri + " to " + uri); 67 if(uri.endsWith("." + ext)) 68 { 69 cache.put(inode + "-" + uri,uri); 72 73 if(id.getURI().endsWith("/index." + ext)) 75 { 76 Logger.debug(WorkingCache.class, "Mapping Working: " + uri.substring(0,uri.lastIndexOf("/index." + ext)) + " to " + uri); 77 cache.put(inode + "-" + uri.substring(0,uri.lastIndexOf("/index." + ext)),uri); 78 Logger.debug(WorkingCache.class, "Mapping Working: " + id.getURI().substring(0,id.getURI().lastIndexOf("index." + ext)) + " to " + uri); 79 cache.put(inode + "-" + uri.substring(0,uri.lastIndexOf("index." + ext)), uri); 80 } 81 } 82 else 83 { 84 String path = FileFactory.getRelativeAssetPath(asset); 86 Logger.debug(WorkingCache.class, "Mapping: " + uri + " to " + path); 88 cache.put(inode + "-" + uri, path); 89 } 90 } 91 PageNotFoundCache.removePageFromCache(uri); 92 93 } 94 95 public static String getPathFromCache(String URI, Host host) 96 { 97 return getPathFromCache (URI, host.getInode()); 98 } 99 100 public static String getPathFromCache(String URI, long hostId) 102 { 103 String _uri = (String ) cache.get(hostId + "-" + URI); 104 105 if(_uri != null) return _uri; 106 107 String ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION"); 108 109 if (URI.endsWith("/")) { 110 URI += "index." + ext; 112 } 113 114 Identifier id = IdentifierFactory.getIdentifierByURI(URI, hostId); 116 117 if(id.getInode() == 0) 118 { 119 URI += "/index." + ext; 121 id = IdentifierFactory.getIdentifierByURI(URI, hostId); 122 if(id.getInode() == 0) 123 { 124 return null; 125 } 126 } 127 128 129 WebAsset asset = null; 130 if(id.getURI().endsWith("." + ext)) 131 { 132 asset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, HTMLPage.class); 133 } 134 else 135 { 136 asset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, File.class); 137 } 138 if(asset.getInode() > 0) 140 { 141 Logger.debug(PreviewFactory.class, "Lazy Preview Mapping: " + id.getURI() + " to " + URI); 142 addToWorkingAssetToCache(asset); 143 } 144 145 return (String ) cache.get(hostId + "-" + URI); 146 } 147 148 public static void removeURIFromCache(String URI, Host host) 149 { 150 removeURIFromCache (URI, host.getInode()); 151 } 152 153 public static void removeURIFromCache(String URI, long hostId) 154 { 155 cache.remove(hostId + "-" + URI); 156 } 157 158 public static void removeAssetFromCache(WebAsset asset) 159 { 160 Inode inode = InodeFactory.getInode(String.valueOf(asset.getInode()),Inode.class); 161 Identifier identifier = IdentifierFactory.getIdentifierByInode(inode); 162 cache.remove(identifier.getHostInode() + "-" + identifier.getURI()); 163 } 164 165 public static void clearCache() 166 { 167 cache.clear(); 169 } 170 171 private static void init() 172 { 173 try 174 { 175 Properties WorkingProperties = new Properties (); 176 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 177 try { 178 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 179 if (UtilMethods.isSet(propertyFilePath)) { 180 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 181 WorkingProperties.load(fileInputStream); 182 } 183 } catch (FileNotFoundException ex) { 184 185 String propertyFileNotFound = "The property file has been no found \n"; 186 Logger.debug(WorkingCache.class, propertyFileNotFound + ex.getMessage()); 187 } catch (IOException ex) { 188 Logger.debug(WorkingCache.class, ex.getMessage()); 189 } 190 finally 191 { 192 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties); 193 } 194 } 195 catch(Exception ex) 196 { 197 Logger.error(WorkingCache.class,ex.toString()); 198 } 199 } 200 } 201 | Popular Tags |