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.InetAddress;
25 import java.net.UnknownHostException;
26 import java.rmi.RemoteException;
27 import java.rmi.registry.LocateRegistry;
28 import java.util.Properties;
29
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.jcs.engine.control.CompositeCacheManager;
38 import org.apache.jcs.utils.props.PropertyLoader;
39
40 /***
41 * This servlet can be used to startup the JCS remote cache. It is easy to
42 * deploy the remote server in a tomcat base. This give you an easy way to
43 * monitor its activity.
44 * <p>
45 *
46 * <code>
47 * <servlet>
48 <servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
49 <servlet-class>
50 org.apache.jcs.auxiliary.remote.server.RemoteCacheStartupServlet
51 </servlet-class>
52 <load-on-startup>1</load-on-startup>
53 </servlet>
54
55
56 <servlet-mapping>
57 <servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
58 <url-pattern>/jcs</url-pattern>
59 </servlet-mapping>
60 * </code>
61 *
62 *
63 * @author Aaron Smuts
64 *
65 */
66 public class RemoteCacheStartupServlet
67 extends HttpServlet
68 {
69 private static final long serialVersionUID = 1L;
70
71 private final static Log log = LogFactory.getLog( RemoteCacheStartupServlet.class );
72
73 private static final int DEFAULT_REGISTRY_PORT = 1101;
74
75 private static final String DEFAULT_PROPS_FILE_NAME = "cache.ccf";
76
77 /***
78 * Starts the registry and then tries to bind to it.
79 * <p>
80 * Gets the port from a props file. Uses the local host name for the rgistry
81 * host. Tries to start the registry, ignoreing failure. Starts the server.
82 */
83 public void init()
84 throws ServletException
85 {
86 super.init();
87
88
89 int registryPort = DEFAULT_REGISTRY_PORT;
90
91 try
92 {
93 Properties props = PropertyLoader.loadProperties( DEFAULT_PROPS_FILE_NAME );
94 if ( props != null )
95 {
96 String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
97
98 try
99 {
100 registryPort = Integer.parseInt( portS );
101 }
102 catch ( NumberFormatException e )
103 {
104 log.error( "Problem converting port to an int.", e );
105 }
106 }
107 }
108 catch ( Exception e )
109 {
110 log.error( "Problem loading props.", e );
111 }
112 catch ( Throwable t )
113 {
114 log.error( "Problem loading props.", t );
115 }
116
117
118 String registryHost;
119 try
120 {
121 registryHost = InetAddress.getLocalHost().getHostAddress();
122
123 if ( log.isDebugEnabled() )
124 {
125 log.debug( "registryHost = [" + registryHost + "]" );
126 }
127
128 if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
129 {
130 log.warn( "The local address [" + registryHost
131 + "] is INVALID. Other machines must be able to use the address to reach this server." );
132 }
133
134 try
135 {
136 LocateRegistry.createRegistry( registryPort );
137 }
138 catch ( RemoteException e )
139 {
140 log.error( "Problem creating registry. It may already be started. " + e.getMessage() );
141 }
142 catch ( Throwable t )
143 {
144 log.error( "Problem creating registry.", t );
145 }
146
147 try
148 {
149 RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + DEFAULT_PROPS_FILE_NAME );
150 }
151 catch ( IOException e )
152 {
153 log.error( "Problem starting remote cache server.", e );
154 }
155
156 catch ( Throwable t )
157 {
158 log.error( "Problem starting remote cache server.", t );
159 }
160 }
161 catch ( UnknownHostException e )
162 {
163 log.error( "Could not get local address to use for the registry!", e );
164 }
165 }
166
167 /***
168 * It just dumps the stats.
169 */
170 protected void service( HttpServletRequest request, HttpServletResponse response )
171 throws ServletException, IOException
172 {
173
174 String stats = CompositeCacheManager.getInstance().getStats();
175 if ( log.isInfoEnabled() )
176 {
177 log.info( stats );
178 }
179
180 try
181 {
182 OutputStream os = response.getOutputStream();
183 os.write( stats.getBytes() );
184 os.close();
185 }
186 catch ( IOException e )
187 {
188 log.error( "Problem writing response.", e );
189 }
190 }
191
192
193
194
195
196
197 public void destroy()
198 {
199 super.destroy();
200
201 log.info( "Shutting down remote cache " );
202
203 CompositeCacheManager.getInstance().shutDown();
204 }
205 }