| 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.net.MalformedURLException; |
| 24 |
|
import java.net.ServerSocket; |
| 25 |
|
import java.net.Socket; |
| 26 |
|
import java.rmi.Naming; |
| 27 |
|
import java.rmi.NotBoundException; |
| 28 |
|
import java.rmi.registry.Registry; |
| 29 |
|
import java.rmi.server.RMISocketFactory; |
| 30 |
|
import java.util.Properties; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.logging.Log; |
| 33 |
|
import org.apache.commons.logging.LogFactory; |
| 34 |
|
import org.apache.jcs.auxiliary.remote.RemoteUtils; |
| 35 |
|
import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheConstants; |
| 36 |
|
import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheServiceAdmin; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
28 |
public class RemoteCacheServerFactory |
| 43 |
|
implements IRemoteCacheConstants |
| 44 |
|
{ |
| 45 |
21 |
private final static Log log = LogFactory.getLog( RemoteCacheServerFactory.class ); |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private static RemoteCacheServer remoteCacheServer; |
| 49 |
|
|
| 50 |
|
private static String serviceName; |
| 51 |
|
|
| 52 |
7 |
private static int DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MS = 10000; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private RemoteCacheServerFactory() |
| 56 |
|
{ |
| 57 |
0 |
super(); |
| 58 |
0 |
} |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
public static RemoteCacheServer getRemoteCacheServer() |
| 67 |
|
{ |
| 68 |
28 |
return remoteCacheServer; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
public static void startup( String host, int port, String propFile ) |
| 82 |
|
throws IOException |
| 83 |
|
{ |
| 84 |
28 |
if ( remoteCacheServer != null ) |
| 85 |
|
{ |
| 86 |
21 |
throw new IllegalArgumentException( "Server already started." ); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
7 |
synchronized ( RemoteCacheServer.class ) |
| 90 |
|
{ |
| 91 |
7 |
if ( remoteCacheServer != null ) |
| 92 |
|
{ |
| 93 |
0 |
return; |
| 94 |
|
} |
| 95 |
|
|
| 96 |
7 |
if ( log.isInfoEnabled() ) |
| 97 |
|
{ |
| 98 |
7 |
log.info( "ConfigFileName = [" + propFile + "]" ); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
try |
| 102 |
|
{ |
| 103 |
|
|
| 104 |
|
|
| 105 |
7 |
RMISocketFactory.setSocketFactory( new RMISocketFactory() |
| 106 |
|
{ |
| 107 |
|
public Socket createSocket( String host, int port ) |
| 108 |
|
throws IOException |
| 109 |
|
{ |
| 110 |
|
Socket socket = new Socket( host, port ); |
| 111 |
|
socket.setSoTimeout( DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MS ); |
| 112 |
|
socket.setSoLinger( false, 0 ); |
| 113 |
|
return socket; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
public ServerSocket createServerSocket( int port ) |
| 117 |
|
throws IOException |
| 118 |
|
{ |
| 119 |
|
return new ServerSocket( port ); |
| 120 |
|
} |
| 121 |
|
} ); |
| 122 |
|
} |
| 123 |
0 |
catch ( Exception e ) |
| 124 |
|
{ |
| 125 |
0 |
log.error( "Problem setting custom RMI Socket Factory.", e ); |
| 126 |
7 |
} |
| 127 |
|
|
| 128 |
|
|
| 129 |
7 |
RemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes(); |
| 130 |
7 |
rcsa.setConfigFileName( propFile ); |
| 131 |
|
|
| 132 |
7 |
Properties prop = RemoteUtils.loadProps( propFile ); |
| 133 |
|
|
| 134 |
|
|
| 135 |
7 |
String servicePortStr = prop.getProperty( REMOTE_CACHE_SERVICE_PORT ); |
| 136 |
7 |
int servicePort = -1; |
| 137 |
|
try |
| 138 |
|
{ |
| 139 |
7 |
servicePort = Integer.parseInt( servicePortStr ); |
| 140 |
|
|
| 141 |
7 |
rcsa.setServicePort( servicePort ); |
| 142 |
7 |
log.debug( "Remote cache service uses port number " + servicePort + "." ); |
| 143 |
|
} |
| 144 |
0 |
catch ( NumberFormatException ignore ) |
| 145 |
|
{ |
| 146 |
0 |
log.debug( "Remote cache service port property " + REMOTE_CACHE_SERVICE_PORT |
| 147 |
|
+ " not specified. An anonymous port will be used." ); |
| 148 |
7 |
} |
| 149 |
|
|
| 150 |
7 |
String lccStr = prop.getProperty( REMOTE_LOCAL_CLUSTER_CONSISTENCY ); |
| 151 |
7 |
if ( lccStr == null ) |
| 152 |
|
{ |
| 153 |
0 |
lccStr = "true"; |
| 154 |
|
} |
| 155 |
7 |
boolean lcc = Boolean.valueOf( lccStr ).class="keyword">booleanValue(); |
| 156 |
7 |
rcsa.setLocalClusterConsistency( lcc ); |
| 157 |
|
|
| 158 |
7 |
String acgStr = prop.getProperty( REMOTE_ALLOW_CLUSTER_GET ); |
| 159 |
7 |
if ( acgStr == null ) |
| 160 |
|
{ |
| 161 |
7 |
acgStr = "true"; |
| 162 |
|
} |
| 163 |
7 |
boolean acg = Boolean.valueOf( acgStr ).class="keyword">booleanValue(); |
| 164 |
7 |
rcsa.setAllowClusterGet( acg ); |
| 165 |
|
|
| 166 |
7 |
if ( log.isInfoEnabled() ) |
| 167 |
|
{ |
| 168 |
7 |
log.info( "Creating server with these attributes " + rcsa ); |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
7 |
remoteCacheServer = new RemoteCacheServer( rcsa ); |
| 173 |
|
|
| 174 |
7 |
if ( host == null ) |
| 175 |
|
{ |
| 176 |
0 |
host = ""; |
| 177 |
|
} |
| 178 |
|
|
| 179 |
7 |
serviceName = prop.getProperty( REMOTE_CACHE_SERVICE_NAME, REMOTE_CACHE_SERVICE_VAL ).trim(); |
| 180 |
|
|
| 181 |
7 |
if ( log.isInfoEnabled() ) |
| 182 |
|
{ |
| 183 |
7 |
log.info( "Binding server to " + host + ":" + port + " with the name " + serviceName ); |
| 184 |
|
} |
| 185 |
|
try |
| 186 |
|
{ |
| 187 |
7 |
Naming.rebind( "//" + host + ":" + port + "/" + serviceName, remoteCacheServer ); |
| 188 |
|
} |
| 189 |
0 |
catch ( MalformedURLException ex ) |
| 190 |
|
{ |
| 191 |
|
|
| 192 |
0 |
throw new IllegalArgumentException( ex.getMessage() + "; host=" + host + ", port=" + port ); |
| 193 |
7 |
} |
| 194 |
7 |
} |
| 195 |
7 |
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
static void shutdownImpl( String host, int port ) |
| 205 |
|
throws IOException |
| 206 |
|
{ |
| 207 |
0 |
if ( remoteCacheServer == null ) |
| 208 |
|
{ |
| 209 |
0 |
return; |
| 210 |
|
} |
| 211 |
0 |
synchronized ( RemoteCacheServer.class ) |
| 212 |
|
{ |
| 213 |
0 |
if ( remoteCacheServer == null ) |
| 214 |
|
{ |
| 215 |
0 |
return; |
| 216 |
|
} |
| 217 |
0 |
log.info( "Unbinding host=" + host + ", port=" + port + ", serviceName=" + serviceName ); |
| 218 |
|
try |
| 219 |
|
{ |
| 220 |
0 |
Naming.unbind( "//" + host + ":" + port + "/" + serviceName ); |
| 221 |
|
} |
| 222 |
0 |
catch ( MalformedURLException ex ) |
| 223 |
|
{ |
| 224 |
|
|
| 225 |
0 |
throw new IllegalArgumentException( ex.getMessage() + "; host=" + host + ", port=" + port |
| 226 |
|
+ ", serviceName=" + serviceName ); |
| 227 |
|
} |
| 228 |
0 |
catch ( NotBoundException ex ) |
| 229 |
|
{ |
| 230 |
|
|
| 231 |
0 |
} |
| 232 |
0 |
remoteCacheServer.release(); |
| 233 |
0 |
remoteCacheServer = null; |
| 234 |
|
|
| 235 |
|
try |
| 236 |
|
{ |
| 237 |
0 |
Thread.sleep( 2000 ); |
| 238 |
|
} |
| 239 |
0 |
catch ( InterruptedException ex ) |
| 240 |
|
{ |
| 241 |
|
|
| 242 |
0 |
} |
| 243 |
0 |
System.exit( 0 ); |
| 244 |
0 |
} |
| 245 |
0 |
} |
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
public static void main( String[] args ) |
| 255 |
|
throws Exception |
| 256 |
|
{ |
| 257 |
0 |
Properties prop = args.length > 0 ? RemoteUtils.loadProps( args[args.length - 1] ) : new Properties(); |
| 258 |
|
|
| 259 |
|
int port; |
| 260 |
|
try |
| 261 |
|
{ |
| 262 |
0 |
port = Integer.parseInt( prop.getProperty( "registry.port" ) ); |
| 263 |
|
} |
| 264 |
0 |
catch ( NumberFormatException ex ) |
| 265 |
|
{ |
| 266 |
0 |
port = Registry.REGISTRY_PORT; |
| 267 |
0 |
} |
| 268 |
|
|
| 269 |
|
|
| 270 |
0 |
if ( args.length > 0 && args[0].toLowerCase().indexOf( "-shutdown" ) != -1 ) |
| 271 |
|
{ |
| 272 |
0 |
String serviceName = prop.getProperty( REMOTE_CACHE_SERVICE_NAME, REMOTE_CACHE_SERVICE_VAL ).trim(); |
| 273 |
0 |
String registry = "//:" + port + "/" + serviceName; |
| 274 |
|
|
| 275 |
0 |
if ( log.isDebugEnabled() ) |
| 276 |
|
{ |
| 277 |
0 |
log.debug( "looking up server " + registry ); |
| 278 |
|
} |
| 279 |
0 |
Object obj = Naming.lookup( registry ); |
| 280 |
0 |
if ( log.isDebugEnabled() ) |
| 281 |
|
{ |
| 282 |
0 |
log.debug( "server found" ); |
| 283 |
|
} |
| 284 |
0 |
IRemoteCacheServiceAdmin admin = (IRemoteCacheServiceAdmin) obj; |
| 285 |
|
try |
| 286 |
|
{ |
| 287 |
0 |
admin.shutdown(); |
| 288 |
|
} |
| 289 |
0 |
catch ( Exception ex ) |
| 290 |
|
{ |
| 291 |
0 |
log.error( "Problem calling shutdown.", ex ); |
| 292 |
0 |
} |
| 293 |
0 |
log.debug( "done." ); |
| 294 |
0 |
System.exit( 0 ); |
| 295 |
|
} |
| 296 |
|
|
| 297 |
|
|
| 298 |
0 |
if ( args.length > 0 && args[0].toLowerCase().indexOf( "-stats" ) != -1 ) |
| 299 |
|
{ |
| 300 |
|
|
| 301 |
0 |
log.debug( "getting cache stats" ); |
| 302 |
|
|
| 303 |
|
try |
| 304 |
|
{ |
| 305 |
0 |
String serviceName = prop.getProperty( REMOTE_CACHE_SERVICE_NAME, REMOTE_CACHE_SERVICE_VAL ).trim(); |
| 306 |
0 |
String registry = "//:" + port + "/" + serviceName; |
| 307 |
0 |
log.debug( "looking up server " + registry ); |
| 308 |
0 |
Object obj = Naming.lookup( registry ); |
| 309 |
0 |
log.debug( "server found" ); |
| 310 |
|
|
| 311 |
0 |
log.debug( "obj = " + obj ); |
| 312 |
0 |
IRemoteCacheServiceAdmin admin = (IRemoteCacheServiceAdmin) obj; |
| 313 |
|
|
| 314 |
|
try |
| 315 |
|
{ |
| 316 |
0 |
System.out.println( admin.getStats().toString() ); |
| 317 |
0 |
log.debug( admin.getStats() ); |
| 318 |
|
} |
| 319 |
0 |
catch ( Exception es ) |
| 320 |
|
{ |
| 321 |
0 |
log.error( es ); |
| 322 |
0 |
} |
| 323 |
|
|
| 324 |
|
} |
| 325 |
0 |
catch ( Exception ex ) |
| 326 |
|
{ |
| 327 |
0 |
log.error( "Problem getting stats.", ex ); |
| 328 |
0 |
} |
| 329 |
0 |
log.debug( "done." ); |
| 330 |
0 |
System.exit( 0 ); |
| 331 |
|
} |
| 332 |
|
|
| 333 |
|
|
| 334 |
0 |
String host = prop.getProperty( "registry.host" ); |
| 335 |
|
|
| 336 |
0 |
if ( host == null || host.trim().equals( "" ) || host.trim().equals( "localhost" ) ) |
| 337 |
|
{ |
| 338 |
0 |
log.debug( "main> creating registry on the localhost" ); |
| 339 |
0 |
port = RemoteUtils.createRegistry( port ); |
| 340 |
|
} |
| 341 |
0 |
log.debug( "main> starting up RemoteCacheServer" ); |
| 342 |
0 |
RemoteCacheServerFactory.startup( host, port, args.length > 0 ? args[0] : null ); |
| 343 |
0 |
log.debug( "main> done" ); |
| 344 |
0 |
} |
| 345 |
|
} |