1 package org.apache.commons.jcs.auxiliary.remote.http.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCacheFactory;
23 import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
24 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
25 import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWait;
26 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheClient;
27 import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient;
28 import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
29 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
30 import org.apache.commons.jcs.engine.behavior.IElementSerializer;
31 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
32 import org.apache.commons.jcs.utils.config.OptionConverter;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38
39
40
41
42 public class RemoteHttpCacheFactory
43 extends AbstractAuxiliaryCacheFactory
44 {
45
46 private static final Log log = LogFactory.getLog( RemoteHttpCacheFactory.class );
47
48
49 private RemoteHttpCacheMonitor monitor;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @Override
65 public <K, V> AuxiliaryCache<K, V> createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr,
66 ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer )
67 {
68 RemoteHttpCacheAttributes rca = (RemoteHttpCacheAttributes) iaca;
69
70
71 rca.setRemoteType( RemoteType.LOCAL );
72
73 RemoteHttpClientListener<K, V> listener = new RemoteHttpClientListener<K, V>( rca, cacheMgr, elementSerializer );
74
75 IRemoteHttpCacheClient<K, V> remoteService = createRemoteHttpCacheClientForAttributes(rca);
76
77 IRemoteCacheClient<K, V> remoteCacheClient =
78 new RemoteHttpCache<K, V>( rca, remoteService, listener, monitor );
79 remoteCacheClient.setCacheEventLogger( cacheEventLogger );
80 remoteCacheClient.setElementSerializer( elementSerializer );
81
82 RemoteCacheNoWait<K, V> remoteCacheNoWait = new RemoteCacheNoWait<K, V>( remoteCacheClient );
83 remoteCacheNoWait.setCacheEventLogger( cacheEventLogger );
84 remoteCacheNoWait.setElementSerializer( elementSerializer );
85
86 return remoteCacheNoWait;
87 }
88
89
90
91
92
93
94
95
96 protected <V, K> IRemoteHttpCacheClient<K, V> createRemoteHttpCacheClientForAttributes(RemoteHttpCacheAttributes cattr)
97 {
98 IRemoteHttpCacheClient<K, V> remoteService = OptionConverter.instantiateByClassName( cattr
99 .getRemoteHttpClientClassName(), null );
100
101 if ( remoteService == null )
102 {
103 if ( log.isInfoEnabled() )
104 {
105 log.info( "Creating the default client for " + cattr.getCacheName());
106 }
107 remoteService = new RemoteHttpCacheClient<K, V>( );
108 }
109
110 remoteService.initialize( cattr );
111 return remoteService;
112 }
113
114
115
116
117 @Override
118 public void initialize()
119 {
120 super.initialize();
121 monitor = new RemoteHttpCacheMonitor(this);
122 monitor.setDaemon(true);
123 monitor.start();
124 }
125
126
127
128
129 @Override
130 public void dispose()
131 {
132 if (monitor != null)
133 {
134 monitor.notifyShutdown();
135 try
136 {
137 monitor.join(5000);
138 }
139 catch (InterruptedException e)
140 {
141
142 }
143 monitor = null;
144 }
145
146 super.dispose();
147 }
148 }