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.Iterator ; 13 import java.util.Properties ; 14 15 import org.apache.commons.logging.LogFactory; 16 17 import com.dotmarketing.portlets.virtuallinks.factories.VirtualLinkFactory; 18 import com.dotmarketing.portlets.virtuallinks.model.VirtualLink; 19 import com.dotmarketing.util.Logger; 20 import com.dotmarketing.util.UtilMethods; 21 22 28 public class VirtualLinksCache 29 { 30 private static DotCache cache; 32 33 private static String providerEntry = "CACHE_PROVIDER"; 35 36 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 38 39 private static String regionName = "VirtualCache"; 41 42 static 43 { 44 init(); 45 } 46 47 public static String getPathFromCache(String url) 48 { 49 String realLink = (String ) cache.get(url); 50 return realLink; 51 } 52 53 public static void addPathToCache(VirtualLink vl) 54 { 55 if (vl != null && vl.getInode() > 0) 56 { 57 LogFactory.getLog(VirtualLinkFactory.class).info("Mapping: " + vl.getUrl() + " to: " + vl.getUri()); 58 cache.put(vl.getUrl(), vl.getUri()); 59 } 60 61 PageNotFoundCache.removePageFromCache(vl.getUrl()); 62 63 } 64 65 public static void removePathFromCache(String url) 66 { 67 cache.remove(url); 68 } 69 70 public static void clearCache() 71 { 72 cache.clear(); 73 try { 74 Thread.sleep(1000); 75 } catch (Exception e) { } 76 mapAllVirtualLinks(); 77 } 78 79 public static void mapAllVirtualLinks() { 80 Iterator iter = VirtualLinkFactory.getVirtualLinks().iterator(); 81 while (iter.hasNext()) { 82 VirtualLink vl = (VirtualLink) iter.next(); 83 addPathToCache(vl); 84 } 85 } 86 87 private static void init() 88 { 89 try 90 { 91 Properties VirtualLinksProperties = new Properties (); 92 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 93 try { 94 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 95 if (UtilMethods.isSet(propertyFilePath)) { 96 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 97 VirtualLinksProperties.load(fileInputStream); 98 } 99 } catch (FileNotFoundException ex) { 100 101 String propertyFileNotFound = "The property file has been no found \n"; 102 Logger.debug(VirtualLinksCache.class, propertyFileNotFound + ex.getMessage()); 103 } catch (IOException ex) { 104 Logger.debug(VirtualLinksCache.class, ex.getMessage()); 105 } 106 finally 107 { 108 cache = new DotCache(cacheProviderClassName, regionName,VirtualLinksProperties); 109 } 110 } 111 catch(Exception ex) 112 { 113 Logger.error(VirtualLinksCache.class,ex.toString()); 114 } 115 } 116 } 117 | Popular Tags |