1 32 33 package com.knowgate.cache; 34 35 import java.rmi.RemoteException ; 36 37 import java.io.ByteArrayOutputStream ; 38 import java.io.IOException ; 39 40 import java.util.Date ; 41 import java.util.Properties ; 42 import java.util.Iterator ; 43 44 45 import java.net.URL ; 46 import java.net.MalformedURLException ; 47 48 import com.knowgate.debug.DebugFile; 49 import com.knowgate.misc.Environment; 50 import com.knowgate.dataobjs.DBSubset; 51 52 87 88 public final class DistributedCachePeer { 89 90 private Object oCtx; private Object oDCC; private Object oHome; 94 private LRUCachePolicy oCacheStore; 95 private Properties oEnvProps; 96 private int iProviderProtocol; 97 98 final private int PROTOCOL_NONE = -1; 99 final private int PROTOCOL_UNKNOWN = 0; 100 final private int PROTOCOL_HTTP = 1; 101 final private int PROTOCOL_HTTPS = 2; 102 final private int PROTOCOL_JNP = 3; 103 104 132 133 public DistributedCachePeer() 134 throws InstantiationException ,RemoteException { 135 136 long lServerTime, lClientTime; 137 String s3Tiers; 138 139 if (DebugFile.trace) { 140 DebugFile.writeln("Begin DistributedCachePeer()"); 141 DebugFile.incIdent(); 142 } 143 144 oCacheStore = new LRUCachePolicy(200, 400); 145 146 oEnvProps = Environment.getProfile("appserver"); 147 s3Tiers = oEnvProps.getProperty("threetiers", "disabled"); 148 149 158 159 if (DebugFile.trace) { 160 DebugFile.writeln ("java.naming.factory.initial=" + oEnvProps.getProperty("java.naming.factory.initial","")); 161 DebugFile.writeln ("java.naming.provider.url=" + oEnvProps.getProperty("java.naming.provider.url","")); 162 DebugFile.writeln ("java.naming.factory.url.pkgs=" + oEnvProps.getProperty("java.naming.factory.url.pkgs","")); 163 DebugFile.writeln ("jnp.socketFactory=" + oEnvProps.getProperty("jnp.socketFactory","")); 164 } 165 166 if (s3Tiers.equalsIgnoreCase("enabled") || s3Tiers.equalsIgnoreCase("yes") || s3Tiers.equalsIgnoreCase("true") || s3Tiers.equalsIgnoreCase("on") || s3Tiers.equals("1")) { 167 168 String sProviderURL = oEnvProps.getProperty("java.naming.provider.url","").toLowerCase(); 169 170 if (sProviderURL.startsWith("http://")) 171 iProviderProtocol = PROTOCOL_HTTP; 172 else if (sProviderURL.startsWith("https://")) 173 iProviderProtocol = PROTOCOL_HTTPS; 174 else if (sProviderURL.startsWith("jnp://")) 175 iProviderProtocol = PROTOCOL_JNP; 176 else 177 iProviderProtocol = PROTOCOL_UNKNOWN; 178 179 if (PROTOCOL_HTTP!=iProviderProtocol && PROTOCOL_HTTPS!=iProviderProtocol) { 180 181 try { 182 183 if (DebugFile.trace) DebugFile.writeln ("Context oCtx = new InitialContext(Properties)"); 184 185 oCtx = new javax.naming.InitialContext (oEnvProps); 186 187 if (DebugFile.trace) 188 DebugFile.writeln("oHome = (DistributedCacheCoordinatorHome) oCtx.lookup(\"DistributedCacheCoordinator\")"); 189 190 oHome = ( (javax.naming.Context ) oCtx).lookup("DistributedCacheCoordinator"); 191 192 if (DebugFile.trace) 193 DebugFile.writeln("DistributedCacheCoordinator = DistributedCacheCoordinatorHome.create()"); 194 195 oDCC = ( (com.knowgate.cache.server.DistributedCacheCoordinatorHome) oHome).create(); 196 197 lServerTime = ( (com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).now(); 199 200 lClientTime = new Date ().getTime(); 201 202 if (lClientTime < lServerTime) { 203 lServerTime = ( (com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).now() + 1000; 204 Environment.updateSystemTime(lServerTime); 205 } } 207 catch (javax.naming.NamingException ne) { 208 throw new java.lang.InstantiationException ("javax.naming.NamingException " + ne.getMessage() + " " + ne.getExplanation()); 209 } 210 catch (javax.ejb.CreateException ce) { 211 throw new java.lang.InstantiationException ("javax.ejb.CreateException " + ce.getMessage()); 212 } 213 } else 215 oDCC = null; 216 } else { 218 iProviderProtocol = PROTOCOL_NONE; 219 } 220 221 if (DebugFile.trace) { 222 DebugFile.decIdent(); 223 DebugFile.writeln("End new DistributedCachePeer()"); 224 } 225 226 } 228 230 private static String trim(String s) { 231 if (null==s) 232 return null; 233 else 234 return (s.replace('\n',' ').replace('\r',' ').replace('\t',' ')).trim(); 235 } 236 237 239 245 246 public Object get(String sTokenKey) 247 throws RemoteException ,NullPointerException { 248 249 long lLastMod; 250 long lLastLocal; 251 Object oRetSet = null; 252 253 if (DebugFile.trace) { 254 DebugFile.writeln("Begin DistributedCachePeer.get(" + sTokenKey + ")"); 255 DebugFile.incIdent(); 256 } 257 258 if (PROTOCOL_NONE==iProviderProtocol) 259 oRetSet = oCacheStore.get(sTokenKey); 262 else 263 { 264 oRetSet = oCacheStore.get(sTokenKey); 266 267 if (null!=oRetSet ) { 268 if (PROTOCOL_HTTP==iProviderProtocol || PROTOCOL_HTTPS==iProviderProtocol) { 270 271 String sProviderURL = oEnvProps.getProperty("java.naming.provider.url"); 272 273 if (null==sProviderURL) 274 throw new NullPointerException ("Property java.naming.provider.url not set at appserver.cnf"); 275 276 else { 277 String sLastMod = ""; 278 279 try { 280 if (DebugFile.trace) DebugFile.writeln("new javax.activation.DataHandler(new URL(" + sProviderURL + "?method=get&key=" + sTokenKey + "))"); 281 282 javax.activation.DataHandler oDataHndlr = new javax.activation.DataHandler (new URL (sProviderURL+"?method=get&key=" + sTokenKey)); 283 284 ByteArrayOutputStream oURLBytes = new ByteArrayOutputStream (128); 285 oDataHndlr.writeTo(oURLBytes); 286 sLastMod = trim(oURLBytes.toString()); 287 oURLBytes.close(); 288 oURLBytes = null; 289 290 if (DebugFile.trace) DebugFile.writeln("lLastMod=" + sLastMod); 291 292 lLastMod = Long.parseLong(sLastMod); 293 } 294 catch (MalformedURLException badurl) { 295 if (DebugFile.trace) DebugFile.writeln("MalformedURLException " + sProviderURL + "?method=get&key=" + sTokenKey); 296 297 throw new RemoteException ("MalformedURLException " + sProviderURL + "?method=get&key=" + sTokenKey); 298 } 299 catch (IOException badurl) { 300 if (DebugFile.trace) DebugFile.writeln("IOException " + sProviderURL + "?method=get&key=" + sTokenKey); 301 302 throw new RemoteException ("IOException " + sProviderURL + "?method=get&key=" + sTokenKey); 303 } 304 catch (NumberFormatException nume) { 305 if (DebugFile.trace) DebugFile.writeln("NumberFormatException " + sLastMod); 306 307 throw new RemoteException ("NumberFormatException " + sLastMod); 308 } 309 } 310 } 311 else { 312 if (DebugFile.trace) DebugFile.writeln("DistributedCacheCoordinator.lastModified(" + sTokenKey + ")"); 313 314 lLastMod = ((com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).lastModified(sTokenKey); 315 316 if (DebugFile.trace) DebugFile.writeln("lLastMod=" + String.valueOf(lLastMod)); 317 } 318 319 lLastLocal = oCacheStore.last(sTokenKey); 320 321 if (DebugFile.trace) { 322 if (lLastMod==0) 323 DebugFile.writeln(sTokenKey + " not found at distributed cache"); 324 else 325 DebugFile.writeln(sTokenKey + " has timestamp " + new Date (lLastMod).toString() + " at distributed cache"); 326 327 DebugFile.writeln(sTokenKey + " has timestamp " + new Date (lLastLocal).toString() + " at local cache"); 328 } 329 330 if (lLastLocal>=lLastMod) { 331 if (DebugFile.trace) DebugFile.writeln("cache hit for " + sTokenKey); 333 } 334 else { 335 if (DebugFile.trace) DebugFile.writeln("cache outdated for " + sTokenKey); 337 oCacheStore.remove(sTokenKey); 338 oRetSet = null; 339 } 340 } else { 342 if (DebugFile.trace) DebugFile.writeln("cache miss for " + sTokenKey); 343 } 344 } 346 if (DebugFile.trace) { 347 DebugFile.decIdent(); 348 DebugFile.writeln("End DistributedCachePeer.get()"); 349 } 350 351 return oRetSet; 352 } 354 356 360 public DBSubset getDBSubset(String sTokenKey) throws RemoteException ,ClassCastException { 361 Object oObj = get (sTokenKey); 362 363 if (null==oObj) 364 return null; 365 else 366 return (DBSubset) get(sTokenKey); 367 } 369 371 376 public String getString(String sTokenKey) throws RemoteException ,ClassCastException { 377 Object oObj = get (sTokenKey); 378 if (null==oObj) 379 return null; 380 else 381 return (String ) oObj; 382 } 384 386 390 public Boolean getBoolean(String sTokenKey) throws RemoteException , ClassCastException { 391 Object oObj = get (sTokenKey); 392 if (null==oObj) 393 return null; 394 else 395 return (Boolean ) oObj; 396 } 398 400 404 405 public java.util.Set keySet() { 406 407 return oCacheStore.keySet(); 408 } 409 410 412 420 public void put(String sTokenKey, Object oObj) 421 throws IllegalStateException , IllegalArgumentException , RemoteException { 422 long lDtServerModified; 423 424 if (DebugFile.trace) { 425 DebugFile.writeln("Begin DistributedCachePeer.put(" + sTokenKey + ", ...)"); 426 DebugFile.incIdent(); 427 } 428 429 if (PROTOCOL_NONE==iProviderProtocol) 430 431 lDtServerModified = System.currentTimeMillis(); 432 433 else if (PROTOCOL_HTTP==iProviderProtocol || PROTOCOL_HTTPS==iProviderProtocol) { 434 435 String sProviderURL = oEnvProps.getProperty("java.naming.provider.url"); 436 437 if (null == sProviderURL) 438 throw new NullPointerException ("Property java.naming.provider.url not set at appserver.cnf"); 439 440 else { 441 String sServerMod = ""; 442 443 try { 444 if (DebugFile.trace) DebugFile.writeln("new javax.activation.DataHandler(new URL(" + sProviderURL + "?method=put&key=" + sTokenKey + "))"); 445 446 javax.activation.DataHandler oDataHndlr = new javax.activation.DataHandler (new URL (sProviderURL + "?method=put&key=" + sTokenKey)); 447 448 ByteArrayOutputStream oURLBytes = new ByteArrayOutputStream (128); 449 oDataHndlr.writeTo(oURLBytes); 450 sServerMod = trim(oURLBytes.toString()); 451 oURLBytes.close(); 452 oURLBytes = null; 453 454 if (DebugFile.trace) DebugFile.writeln("lDtServerModified=" + sServerMod); 455 456 lDtServerModified = Long.parseLong(sServerMod); 457 } 458 catch (MalformedURLException badurl) { 459 if (DebugFile.trace) DebugFile.writeln("MalformedURLException " + sProviderURL + "?method=get&put=" + sTokenKey); 460 461 throw new RemoteException ("MalformedURLException " + sProviderURL + "?method=put&key=" + sTokenKey); 462 } 463 catch (IOException badurl) { 464 if (DebugFile.trace) DebugFile.writeln("IOException " + sProviderURL + "?method=get&put=" + sTokenKey); 465 466 throw new RemoteException ("IOException " + sProviderURL + "?method=get&put=" + sTokenKey); 467 } 468 catch (NumberFormatException nume) { 469 if (DebugFile.trace) DebugFile.writeln("NumberFormatException " + sServerMod); 470 471 throw new RemoteException ("NumberFormatException " + sServerMod); 472 } 473 } 474 } 475 else { 476 477 if (DebugFile.trace) DebugFile.writeln("DistributedCacheCoordinator.modify(" + sTokenKey + ")"); 478 479 lDtServerModified = ((com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).modify(sTokenKey); 480 481 if (DebugFile.trace) DebugFile.writeln("lDtServerModified=" + String.valueOf(lDtServerModified)); 482 483 } 484 485 if (DebugFile.trace) DebugFile.writeln("LRUCachePolicy.insert(" + sTokenKey + ", [Object], " + String.valueOf(lDtServerModified)); 486 487 oCacheStore.insert (sTokenKey, oObj, lDtServerModified); 488 489 if (DebugFile.trace) { 490 DebugFile.decIdent(); 491 DebugFile.writeln("End DistributedCachePeer.put()"); 492 } 493 } 495 497 505 public void putDBSubset(String sTableName, String sTokenKey, DBSubset oDBSS) throws RemoteException { 506 if (DebugFile.trace) { 507 DebugFile.writeln("Begin DistributedCachePeer.putDBSubset(" + sTokenKey + ", ...)"); 508 DebugFile.incIdent(); 509 } 510 511 put (sTokenKey, oDBSS); 512 513 if (DebugFile.trace) { 514 DebugFile.decIdent(); 515 DebugFile.writeln("End DistributedCachePeer.putDBSubset()"); 516 } 517 } 519 521 530 public void expire(String sTokenKey) throws IllegalArgumentException , RemoteException { 531 if (DebugFile.trace) { 532 DebugFile.writeln("Begin DistributedCachePeer.expire(" + sTokenKey + ")"); 533 DebugFile.incIdent(); 534 } 535 if (PROTOCOL_NONE!=iProviderProtocol) { 536 537 if (PROTOCOL_HTTP==iProviderProtocol || PROTOCOL_HTTPS==iProviderProtocol) { 538 539 String sProviderURL = oEnvProps.getProperty("java.naming.provider.url"); 540 541 if (null==sProviderURL) 542 throw new NullPointerException ("Property java.naming.provider.url not set at appserver.cnf"); 543 544 else { 545 546 try { 547 548 if (DebugFile.trace) DebugFile.writeln("new javax.activation.DataHandler(new URL(" + sProviderURL + "?method=expire&key=" + sTokenKey + "))"); 549 550 javax.activation.DataHandler oDataHndlr = new javax.activation.DataHandler (new URL (sProviderURL+"?method=expire&key=" + sTokenKey)); 551 552 ByteArrayOutputStream oURLBytes = new ByteArrayOutputStream (128); 553 oDataHndlr.writeTo(oURLBytes); 554 oURLBytes.close(); 555 oURLBytes = null; 556 557 } 558 catch (MalformedURLException badurl) { 559 throw new RemoteException ("MalformedURLException " + sProviderURL + "?method=expire&key=" + sTokenKey); 560 } 561 catch (IOException badurl) { 562 throw new RemoteException ("IOException " + sProviderURL + "?method=get&key=" + sTokenKey); 563 } 564 } } 566 else { 567 if (DebugFile.trace) DebugFile.writeln("DistributedCacheCoordinator.expire(" + sTokenKey + ")"); 568 569 ((com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).expire(sTokenKey); 570 } 571 } 572 oCacheStore.remove(sTokenKey); 573 574 if (DebugFile.trace) { 575 DebugFile.decIdent(); 576 DebugFile.writeln("End DistributedCachePeer.expire()"); 577 } 578 } 580 582 586 public void expireAll() throws RemoteException { 587 if (DebugFile.trace) { 588 DebugFile.writeln("Begin DistributedCachePeer.expireAll()"); 589 DebugFile.incIdent(); 590 } 591 Iterator oKeys; 592 593 if (PROTOCOL_NONE!=iProviderProtocol) { 594 595 oKeys = oCacheStore.m_map.keySet().iterator(); 596 597 while(oKeys.hasNext()) expire((String ) oKeys.next()); 598 599 } 601 oCacheStore.flush(); 602 603 if (DebugFile.trace) { 604 DebugFile.decIdent(); 605 DebugFile.writeln("End DistributedCachePeer.expireAll()"); 606 } 607 } 609 611 614 public int size() { 615 return oCacheStore.size(); 616 } 617 }
| Popular Tags
|