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.HostFactory; 19 import com.dotmarketing.factories.IdentifierFactory; 20 import com.dotmarketing.factories.InodeFactory; 21 import com.dotmarketing.factories.PublishFactory; 22 import com.dotmarketing.portlets.files.factories.FileFactory; 23 import com.dotmarketing.portlets.files.model.File; 24 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 25 import com.dotmarketing.util.Config; 26 import com.dotmarketing.util.Logger; 27 import com.dotmarketing.util.UtilMethods; 28 29 35 public class LiveCache 36 { 37 private static DotCache cache; 39 40 private static String providerEntry = "CACHE_PROVIDER"; 42 43 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 45 46 private static String regionName = "LiveCache"; 48 49 static 50 { 51 init(); 52 } 53 54 public static void addToLiveAssetToCache(WebAsset asset) 55 { 56 String ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION"); 58 Identifier id = IdentifierFactory.getIdentifierByInode(asset); 60 Host host = HostFactory.getParentHost(asset); 62 String uri = id.getURI(); 64 long inode = host.getInode(); 66 67 if(uri.endsWith("." + ext)) 69 { 70 Logger.debug(LiveCache.class, "Mapping: " + uri + " to " + uri); 71 cache.put(inode + ":" + uri,uri); 73 74 if(uri.endsWith("/index." + ext)) 75 { 76 Logger.debug(LiveCache.class, "Mapping: " + uri.substring(0,uri.lastIndexOf("/index." + ext)) + " to " + uri); 78 cache.put(inode + ":" + uri.substring(0,uri.lastIndexOf("/index." + ext)),uri); 79 Logger.debug(LiveCache.class, "Mapping: " + uri.substring(0,uri.lastIndexOf("/index." + ext)) + " to " + uri); 81 cache.put(inode + ":" + uri.substring(0,uri.lastIndexOf("index." + ext)),uri); 82 } 83 } 84 else 85 { 86 String path = FileFactory.getRelativeAssetPath(asset); 87 Logger.debug(LiveCache.class, "Mapping: " + uri + " to " + path); 89 cache.put(inode + ":" + uri,path); 90 } 91 92 PageNotFoundCache.removePageFromCache(uri); 93 94 } 95 96 public static String getPathFromCache(String URI, Host host) 97 { 98 if (URI.equals("/")) { 99 String pointer = (String ) VirtualLinksCache.getPathFromCache(host.getHostname() + ":/cmsHomePage"); 100 if (!UtilMethods.isSet(pointer)) { 101 pointer = (String ) VirtualLinksCache.getPathFromCache("/cmsHomePage"); 102 } 103 if (UtilMethods.isSet(pointer)) 104 URI = pointer; 105 } 106 return getPathFromCache (URI, host.getInode()); 107 } 108 109 public static String getPathFromCache(String URI, long hostId) 110 { 111 String _uri = (String ) cache.get(hostId + ":" + URI); 112 113 if(_uri != null) 114 { 115 return _uri; 116 } 117 118 String ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION"); 119 120 if (URI.endsWith("/")) 121 { 122 URI += "index." + ext; 124 } 125 126 Identifier id = IdentifierFactory.getIdentifierByURI(URI, hostId); 128 129 if(id.getInode() == 0) 130 { 131 URI += "/index." + ext; 133 id = IdentifierFactory.getIdentifierByURI(URI, hostId); 134 if(id.getInode() == 0) 135 { 136 return null; 137 } 138 } 139 140 WebAsset asset = null; 141 if(id.getURI().endsWith("." + ext)) 142 { 143 asset = (WebAsset) IdentifierFactory.getLiveChildOfClass(id, HTMLPage.class); 144 } 145 else 146 { 147 asset = (WebAsset) IdentifierFactory.getLiveChildOfClass(id, File.class); 148 } 149 if(asset.getInode() > 0) 150 { 151 Logger.debug(PublishFactory.class, "Lazy Mapping: " + id.getURI() + " to " + URI); 152 addToLiveAssetToCache(asset); 153 } 154 return (String ) cache.get(hostId + ":" + URI); 155 } 156 157 public static void removeAssetFromCache(WebAsset asset) 158 { 159 long hostId = HostFactory.getParentHost(asset).getInode(); 160 Inode inode = InodeFactory.getInode(String.valueOf(asset.getInode()),Inode.class); 161 Identifier identifier = IdentifierFactory.getIdentifierByInode(inode); 162 cache.remove(hostId + ":" + 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 LiveProperties = 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 LiveProperties.load(fileInputStream); 182 } 183 } catch (FileNotFoundException ex) { 184 185 String propertyFileNotFound = "The property file has been no found \n"; 186 Logger.debug(LiveCache.class, propertyFileNotFound + ex.getMessage()); 187 } catch (IOException ex) { 188 Logger.debug(LiveCache.class, ex.getMessage()); 189 } 190 finally 191 { 192 cache = new DotCache(cacheProviderClassName, regionName,LiveProperties); 193 } 194 } 195 catch(Exception ex) 196 { 197 Logger.error(LiveCache.class,ex.toString()); 198 } 199 } 200 } | Popular Tags |