KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cache > HostCache


1 /*
2  * Created on May 24, 2005
3  *
4  */

5 package com.dotmarketing.cache;
6
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.Properties JavaDoc;
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 /**
18  * @author maria & Salvador
19  *
20  */

21 public class HostCache
22 {
23     //Cache to store the data
24
private static DotCache cache;
25
26     //Name of the variable with the provider name
27
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
28
29     //Name of the variable with the properties path
30
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
31
32     //region's name for the cache
33
private static String JavaDoc 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 JavaDoc key,Host host)
49     {
50         //If the cache have the entry, first we remove the entry
51
if (cache.get(key) != null) {
52             cache.remove(key);
53         }
54         //Then add the entry to the cache
55
cache.put(key,host);
56         cache.put(host.getHostname(),host);
57     }
58     
59     public static void removeFromCache(Object JavaDoc key)
60     {
61         //Remove the entry from the cache
62
cache.remove(key);
63     }
64     
65     public static void clearCache()
66     {
67         //clear the cache
68
cache.clear();
69     }
70
71     public static Host getFromCache(String JavaDoc key)
72     {
73         //Found the host in the cache
74
Host host = (Host) cache.get(key);
75         //if the host is null or the inode is 0
76
//I populate the cache entry
77
if (host == null || host.getInode() == 0)
78         {
79             //if the host is not found we search on the DB by hostname and add it to the cache
80
host = HostFactory.getHostByHostName(key);
81
82             //if the host is not found by hostname, we search by alias
83
if (host == null || host.getInode() == 0)
84             {
85                 host = HostFactory.findHostByAliases(key);
86             }
87             
88             //if the host is not found by aliases, we get the default host
89
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             //if a host is found we add it to the cache
102
cache.put(key,host);
103         }
104         return host;
105     }
106     
107     private static void init()
108     {
109         try
110         {
111             Properties JavaDoc HostProperties = new Properties JavaDoc();
112             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
113             try {
114                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
115                 if (UtilMethods.isSet(propertyFilePath)) {
116                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
117                     HostProperties.load(fileInputStream);
118                 }
119             } catch (FileNotFoundException JavaDoc ex) {
120                 
121                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
122                 Logger.debug(HostCache.class, propertyFileNotFound + ex.getMessage());
123             } catch (IOException JavaDoc ex) {
124                 Logger.debug(HostCache.class, ex.getMessage());
125             }
126             finally
127             {
128                 cache = new DotCache(cacheProviderClassName, regionName,HostProperties);
129             }
130         }
131         catch(Exception JavaDoc ex)
132         {
133             Logger.error(HostCache.class,ex.toString());
134         }
135     }
136 }
137
Popular Tags