1 5 package com.dotmarketing.cache; 6 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.IOException ; 10 import java.util.Properties ; 11 12 import com.dotmarketing.beans.Host; 13 import com.dotmarketing.factories.HostFactory; 14 import com.dotmarketing.util.Logger; 15 import com.dotmarketing.util.UtilMethods; 16 17 21 public class HostCache 22 { 23 private static DotCache cache; 25 26 private static String providerEntry = "CACHE_PROVIDER"; 28 29 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 31 32 private static String regionName = "HostCache"; 34 35 private static Host defaultHost = null; 36 37 static 38 { 39 init(); 40 } 41 42 public static void setDefaultHost(Host host) { 43 if (host != null && host.getInode() == 0) 44 return; 45 defaultHost = host; 46 } 47 48 public static void addToCache(String key,Host host) 49 { 50 if (cache.get(key) != null) { 52 cache.remove(key); 53 } 54 cache.put(key,host); 56 cache.put(host.getHostname(),host); 57 } 58 59 public static void removeFromCache(Object key) 60 { 61 cache.remove(key); 63 } 64 65 public static void clearCache() 66 { 67 cache.clear(); 69 } 70 71 public static Host getFromCache(String key) 72 { 73 Host host = (Host) cache.get(key); 75 if (host == null || host.getInode() == 0) 78 { 79 host = HostFactory.getHostByHostName(key); 81 82 if (host == null || host.getInode() == 0) 84 { 85 host = HostFactory.findHostByAliases(key); 86 } 87 88 if (host == null || host.getInode() == 0) 90 { 91 if (defaultHost == null || defaultHost.getInode() == 0) 92 { 93 host = HostFactory.getDefaultHost(); 94 setDefaultHost(host); 95 } 96 else 97 { 98 host = defaultHost; 99 } 100 } 101 cache.put(key,host); 103 } 104 return host; 105 } 106 107 private static void init() 108 { 109 try 110 { 111 Properties HostProperties = new Properties (); 112 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 113 try { 114 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 115 if (UtilMethods.isSet(propertyFilePath)) { 116 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 117 HostProperties.load(fileInputStream); 118 } 119 } catch (FileNotFoundException ex) { 120 121 String propertyFileNotFound = "The property file has been no found \n"; 122 Logger.debug(HostCache.class, propertyFileNotFound + ex.getMessage()); 123 } catch (IOException ex) { 124 Logger.debug(HostCache.class, ex.getMessage()); 125 } 126 finally 127 { 128 cache = new DotCache(cacheProviderClassName, regionName,HostProperties); 129 } 130 } 131 catch(Exception ex) 132 { 133 Logger.error(HostCache.class,ex.toString()); 134 } 135 } 136 } 137 | Popular Tags |