1 package org.apache.jcs.auxiliary.remote.server;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.net.UnknownHostException;
25 import java.rmi.RemoteException;
26 import java.rmi.registry.LocateRegistry;
27 import java.util.Properties;
28
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.jcs.access.exception.CacheException;
37 import org.apache.jcs.engine.control.CompositeCacheManager;
38 import org.apache.jcs.utils.net.HostNameUtil;
39 import org.apache.jcs.utils.props.PropertyLoader;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class RemoteCacheStartupServlet
63 extends HttpServlet
64 {
65
66 private static final long serialVersionUID = 1L;
67
68
69 private final static Log log = LogFactory.getLog( RemoteCacheStartupServlet.class );
70
71
72 private static final int DEFAULT_REGISTRY_PORT = 1101;
73
74
75 private static final String DEFAULT_PROPS_FILE_NAME = "cache";
76
77
78 private static final String DEFAULT_PROPS_FILE_SUFFIX = "ccf";
79
80
81 private final String propsFileName = DEFAULT_PROPS_FILE_NAME;
82
83
84 private final String fullPropsFileName = DEFAULT_PROPS_FILE_NAME + "." + DEFAULT_PROPS_FILE_SUFFIX;
85
86
87
88
89
90
91
92
93
94 @Override
95 public void init()
96 throws ServletException
97 {
98 super.init();
99
100
101 int registryPort = DEFAULT_REGISTRY_PORT;
102
103 try
104 {
105 Properties props = PropertyLoader.loadProperties( propsFileName );
106 if ( props != null )
107 {
108 String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
109
110 try
111 {
112 registryPort = Integer.parseInt( portS );
113 }
114 catch ( NumberFormatException e )
115 {
116 log.error( "Problem converting port to an int.", e );
117 }
118 }
119 }
120 catch ( Exception e )
121 {
122 log.error( "Problem loading props.", e );
123 }
124 catch ( Throwable t )
125 {
126 log.error( "Problem loading props.", t );
127 }
128
129
130 String registryHost;
131 try
132 {
133 registryHost = HostNameUtil.getLocalHostAddress();
134
135 if ( log.isDebugEnabled() )
136 {
137 log.debug( "registryHost = [" + registryHost + "]" );
138 }
139
140 if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
141 {
142 log.warn( "The local address [" + registryHost
143 + "] is INVALID. Other machines must be able to use the address to reach this server." );
144 }
145
146 try
147 {
148 LocateRegistry.createRegistry( registryPort );
149 }
150 catch ( RemoteException e )
151 {
152 log.error( "Problem creating registry. It may already be started. " + e.getMessage() );
153 }
154 catch ( Throwable t )
155 {
156 log.error( "Problem creating registry.", t );
157 }
158
159 try
160 {
161 RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + fullPropsFileName );
162 if ( log.isInfoEnabled() )
163 {
164 log.info( "Remote JCS Server started with properties from " + fullPropsFileName );
165 }
166 }
167 catch ( IOException e )
168 {
169 log.error( "Problem starting remote cache server.", e );
170 }
171
172 catch ( Throwable t )
173 {
174 log.error( "Problem starting remote cache server.", t );
175 }
176 }
177 catch ( UnknownHostException e )
178 {
179 log.error( "Could not get local address to use for the registry!", e );
180 }
181 }
182
183
184
185
186
187
188
189
190
191 @Override
192 protected void service( HttpServletRequest request, HttpServletResponse response )
193 throws ServletException, IOException
194 {
195 String stats = "";
196
197 try
198 {
199 stats = CompositeCacheManager.getInstance().getStats();
200 }
201 catch (CacheException e)
202 {
203 throw new ServletException(e);
204 }
205
206 if ( log.isInfoEnabled() )
207 {
208 log.info( stats );
209 }
210
211 try
212 {
213 OutputStream os = response.getOutputStream();
214 os.write( stats.getBytes() );
215 os.close();
216 }
217 catch ( IOException e )
218 {
219 log.error( "Problem writing response.", e );
220 }
221 }
222
223
224
225
226 @Override
227 public void destroy()
228 {
229 super.destroy();
230
231 log.info( "Shutting down remote cache " );
232
233 try
234 {
235 CompositeCacheManager.getInstance().shutDown();
236 }
237 catch (CacheException e)
238 {
239 log.error("Could not retrieve cache manager instance", e);
240 }
241 }
242 }