1 package com.dotmarketing.cache; 2 3 import java.io.FileInputStream ; 4 import java.io.FileNotFoundException ; 5 import java.io.IOException ; 6 import java.util.Properties ; 7 8 import com.dotmarketing.beans.Host; 9 import com.dotmarketing.factories.HostFactory; 10 import com.dotmarketing.portlets.folders.factories.FolderFactory; 11 import com.dotmarketing.portlets.folders.model.Folder; 12 import com.dotmarketing.util.Logger; 13 import com.dotmarketing.util.UtilMethods; 14 15 18 public class FolderCache { 19 20 private static DotCache cache; 22 23 private static String providerEntry = "CACHE_PROVIDER"; 25 26 private static String propertiesEntry = "CACHE_PROPERTIES_FILE"; 28 29 private static String regionName = "FolderCache"; 31 32 static 33 { 34 init(); 35 } 36 37 public static void addFolder(Folder f) 38 { 39 long inode = f.getInode(); 41 String folderPath = f.getPath(); 42 Host host = HostFactory.getParentHost(f); 43 folderPath = host.getHostname() + ":" + folderPath; 44 String folderPath2 = host.getInode() + ":" + f.getPath(); 45 cache.put(inode, f); 46 cache.put(folderPath, f); 47 cache.put(folderPath2, f); 48 49 } 50 51 public static Folder getFolderByInode(long inode) 52 { 53 Folder f = (Folder) cache.get(inode); 54 if (f == null) { 55 f = FolderFactory.getFolderByInode(inode); 56 addFolder(f); 57 } 58 return f; 59 } 60 61 public static Folder getFolderByInode(String inode) 62 { 63 Folder f = (Folder) cache.get(inode); 64 if (f == null) { 65 f = FolderFactory.getFolderByInode(Long.parseLong(inode)); 66 addFolder(f); 67 } 68 return f; 69 } 70 71 public static Folder getFolderByPath(String path, String hostName) 72 { 73 String folderPath = hostName + ":" + path; 74 Folder f = (Folder) cache.get(folderPath); 75 if (f == null) { 76 Host host = HostCache.getFromCache(hostName); 77 f = FolderFactory.getFolderByPath(path, host); 78 addFolder(f); 79 } 80 81 return f; 82 } 83 84 public static Folder getFolderByPath(String path, long hostId) 85 { 86 String folderPath = hostId + ":" + path; 87 Folder f = (Folder) cache.get(folderPath); 88 if (f == null) { 89 Host host = HostFactory.getHost(hostId); 90 f = FolderFactory.getFolderByPath(path, host); 91 addFolder(f); 92 } 93 94 return f; 95 } 96 97 public static void removeFolder(Folder f) 98 { 99 long inode = f.getInode(); 100 String folderPath = f.getPath(); 101 Host host = HostFactory.getParentHost(f); 102 folderPath = host.getHostname() + ":" + folderPath; 103 cache.remove(inode); 104 cache.remove(folderPath); 105 } 106 107 public static void clearCache() 108 { 109 cache.clear(); 111 } 112 113 private static void init() 114 { 115 try 116 { 117 Properties WorkingProperties = new Properties (); 118 String cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry); 119 try { 120 String propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry); 121 if (UtilMethods.isSet(propertyFilePath)) { 122 FileInputStream fileInputStream = new FileInputStream (propertyFilePath); 123 WorkingProperties.load(fileInputStream); 124 } 125 } catch (FileNotFoundException ex) { 126 127 String propertyFileNotFound = "The property file has been no found \n"; 128 Logger.debug(FolderCache.class, propertyFileNotFound + ex.getMessage()); 129 } catch (IOException ex) { 130 Logger.debug(FolderCache.class, ex.getMessage()); 131 } 132 finally 133 { 134 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties); 135 } 136 } 137 catch(Exception ex) 138 { 139 Logger.error(FolderCache.class,ex.toString()); 140 } 141 } 142 } 143 | Popular Tags |