1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.lang.reflect.Field ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import java.util.StringTokenizer ; 38 import java.util.WeakHashMap ; 39 40 import javax.management.ObjectName ; 41 import javax.transaction.TransactionManager ; 42 43 import org.apache.catalina.Context; 44 import org.jboss.aspects.patterns.observable.Observer; 45 import org.jboss.aspects.patterns.observable.Subject; 46 import org.jboss.cache.Cache; 47 import org.jboss.cache.CacheException; 48 import org.jboss.cache.Fqn; 49 import org.jboss.cache.Node; 50 import org.jboss.cache.Region; 51 import org.jboss.cache.pojo.PojoCache; 52 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapperMBean; 53 import org.jboss.cache.buddyreplication.BuddyManager; 54 import org.jboss.cache.config.CacheLoaderConfig; 55 import org.jboss.cache.transaction.BatchModeTransactionManager; 56 import org.jboss.invocation.MarshalledValue; 57 import org.jboss.logging.Logger; 58 import org.jboss.mx.util.MBeanProxyExt; 59 import org.jboss.serial.io.MarshalledObject; 60 61 71 public class JBossCacheService 72 { 73 protected static Logger log_ = Logger.getLogger(JBossCacheService.class); 74 public static final String BUDDY_BACKUP = BuddyManager.BUDDY_BACKUP_SUBTREE; 75 public static final Fqn BUDDY_BACKUP_FQN = BuddyManager.BUDDY_BACKUP_SUBTREE_FQN; 76 public static final String SESSION = "JSESSION"; 77 public static final String ATTRIBUTE = "ATTRIBUTE"; 78 static final String VERSION_KEY = "VERSION"; 80 static final String FQN_DELIMITER = "/"; 81 82 private PojoCache pojoCache_; 83 private Cache plainCache_; 84 private ObjectName cacheServiceName_; 85 86 private String hostName_; 89 private String webAppPath_; 92 private TransactionManager tm; 93 94 private JBossCacheManager manager_; 95 private CacheListener cacheListener_; 96 private JBossCacheWrapper cacheWrapper_; 97 98 private boolean useTreeCacheMarshalling_ = false; 101 102 private WeakHashMap typeMap = new WeakHashMap (); 103 104 public JBossCacheService(String treeCacheObjectName) throws ClusteringNotSupportedException 105 { 106 try 108 { 109 cacheServiceName_ = new ObjectName (treeCacheObjectName); 110 PojoCacheJmxWrapperMBean mbean = (PojoCacheJmxWrapperMBean) MBeanProxyExt.create(PojoCacheJmxWrapperMBean.class, 112 cacheServiceName_); 113 if (mbean != null) 114 { 115 pojoCache_ = mbean.getPojoCache(); 116 } 117 } 118 catch (Throwable t) 119 { 120 121 String str = "Could not access TreeCache service " + 122 (cacheServiceName_ == null ? "<null>" : cacheServiceName_.toString()) + 123 " for Tomcat clustering"; 124 log_.debug(str); 125 throw new ClusteringNotSupportedException(str, t); 126 } 127 128 if (pojoCache_ == null) 129 { 130 String str = "Could not access TreeCache service " + 131 (cacheServiceName_ == null ? "<null>" : cacheServiceName_.toString()) + 132 " for Tomcat clustering"; 133 log_.debug(str); 134 throw new ClusteringNotSupportedException(str); 135 } 136 137 plainCache_ = pojoCache_.getCache(); 138 cacheWrapper_ = new JBossCacheWrapper(pojoCache_); 139 140 useTreeCacheMarshalling_ = plainCache_.getConfiguration().isUseRegionBasedMarshalling(); 141 } 142 143 public void start(ClassLoader tcl, JBossCacheManager manager) 144 { 145 manager_ = manager; 146 147 Context webapp = (Context) manager_.getContainer(); 148 String path = webapp.getName(); 149 if( path.length() == 0 || path.equals("/")) { 150 webAppPath_ = "ROOT"; 152 } else if ( path.startsWith("/") ) { 153 webAppPath_ = path.substring(1); 154 } else { 155 webAppPath_ = path; 156 } 157 log_.debug("Old and new web app path are: " +path + ", " +webAppPath_); 158 159 String host = webapp.getParent().getName(); 160 if( host == null || host.length() == 0) { 161 hostName_ = "localhost"; 162 }else { 163 hostName_ = host; 164 } 165 log_.debug("Old and new virtual host name are: " + host + ", " + hostName_); 166 167 168 cacheListener_ = new CacheListener(cacheWrapper_, manager_, hostName_, webAppPath_); 170 plainCache_.addCacheListener(cacheListener_); 171 172 Object [] objs = new Object []{SESSION, hostName_, webAppPath_}; 174 Fqn pathFqn = new Fqn( objs ); 175 try { 176 if(useTreeCacheMarshalling_) 177 { 178 log_.debug("UseMarshalling is true. We will register the fqn: " + 179 pathFqn + " with class loader" +tcl + 180 " and activate the webapp's Region"); 181 Region region = plainCache_.getRegion(pathFqn, true); 182 region.registerContextClassLoader(tcl); 183 region.activate(); 184 } 185 } catch (Exception ex) 186 { 187 throw new RuntimeException ("Can't register class loader", ex); 188 } 189 190 tm = plainCache_.getTransactionManager(); 192 if( ! (tm instanceof BatchModeTransactionManager) ) 193 { 194 throw new RuntimeException ("JBossCacheService.start(): JBossCacheAop transaction manager is not of type BatchModeTransactionManager." + 195 " Please check the tc6-cluster-service.xml TransactionManagerClassLookup field."); 196 } 197 198 if(isCachePassivationEnabled()) 199 { 200 log_.debug("JBossCache passivation is enabled"); 201 } 202 else 203 { 204 log_.debug("JBossCache passivation is disabled"); 205 } 206 } 207 208 public void stop() 209 { 210 plainCache_.removeCacheListener(cacheListener_); 211 212 Object [] objs = new Object []{SESSION, hostName_, webAppPath_}; 214 Fqn pathFqn = new Fqn( objs ); 215 216 if(useTreeCacheMarshalling_) 217 { 218 log_.debug("UseMarshalling is true. We will inactivate the fqn: " + 219 pathFqn + " and un-register its classloader"); 220 221 try { 222 Region region = plainCache_.getRegion(pathFqn, false); 223 if (region != null) 224 { 225 region.deactivate(); 226 region.unregisterContextClassLoader(); 227 } 228 } 229 catch (Exception e) 230 { 231 log_.error("Exception during inactivation of webapp region " + pathFqn + 232 " or un-registration of its class loader", e); 233 } 234 } 235 236 cacheWrapper_.removeLocalSubtree(pathFqn); 238 } 239 240 243 public TransactionManager getTransactionManager() 244 { 245 return tm; 246 } 247 248 251 public boolean isMarshallingAvailable() 252 { 253 return useTreeCacheMarshalling_; 254 } 255 256 264 public ClusteredSession loadSession(String realId, ClusteredSession toLoad) 265 { 266 Fqn fqn = getSessionFqn(realId); 267 Object sessionData = cacheWrapper_.get(fqn, realId, true); 268 269 if (sessionData == null) { 270 return null; 272 } 273 274 boolean firstLoad = (toLoad.getVersion() == 0); 275 276 byte[] sessionBytes = (byte[]) sessionData; 283 284 ClassLoader prevTCL = Thread.currentThread().getContextClassLoader(); 287 Thread.currentThread().setContextClassLoader(manager_.getWebappClassLoader()); 288 try 289 { 290 ObjectInputStream input = SessionSerializationFactory.createObjectInputStream(sessionBytes); 296 toLoad.readExternal(input); 297 input.close(); 298 } 299 catch (Exception e) 300 { 301 log_.error("loadSession(): id: " + realId + "exception occurred during serialization: " +e); 302 return null; 303 } 304 finally { 305 Thread.currentThread().setContextClassLoader(prevTCL); 306 } 307 309 if (firstLoad) 315 { 316 Integer ver = (Integer ) cacheWrapper_.get(fqn, VERSION_KEY); 317 if (ver != null) 318 toLoad.setVersion(ver.intValue()); 319 } 320 321 return toLoad; 322 } 323 324 public void putSession(String realId, ClusteredSession session) 325 { 326 Fqn fqn = getSessionFqn(realId); 327 328 if (session.getReplicateSessionBody()) 329 { 330 Map map = new HashMap (); 331 map.put(realId, externalizeSession(session)); 335 map.put(VERSION_KEY, new Integer (session.getVersion())); 337 cacheWrapper_.put(fqn, map); 338 } 339 else 340 { 341 cacheWrapper_.put(fqn, VERSION_KEY, new Integer (session.getVersion())); 343 } 344 } 345 346 public void removeSession(String realId) 347 { 348 Fqn fqn = getSessionFqn(realId); 349 if (log_.isDebugEnabled()) 350 { 351 log_.debug("Remove session from distributed store. Fqn: " + fqn); 352 } 353 cacheWrapper_.remove(fqn, realId); 355 cacheWrapper_.remove(fqn); 359 } 361 362 public void removeSessionLocal(String realId) 363 { 364 Fqn fqn = getSessionFqn(realId); 365 if (log_.isDebugEnabled()) 366 { 367 log_.debug("Remove session from my own distributed store only. Fqn: " + fqn); 368 } 369 cacheWrapper_.removeLocalSubtree(fqn); 370 } 371 372 public void removeSessionLocal(String realId, String dataOwner) 373 { 374 if (dataOwner == null) 375 { 376 removeSessionLocal(realId); 377 } 378 else 379 { 380 Fqn fqn = getSessionFqn(realId, dataOwner); 381 if (log_.isDebugEnabled()) 382 { 383 log_.debug("Remove session from my own distributed store only. Fqn: " + fqn); 384 } 385 cacheWrapper_.removeLocalSubtree(fqn); 386 } 387 } 388 389 390 public void evictSession(String realId) 391 { 392 Fqn fqn = getSessionFqn(realId); 393 if(log_.isDebugEnabled()) 394 { 395 log_.debug("evictSession(): evicting session from my distributed store. Fqn: " + fqn); 396 } 397 cacheWrapper_.evictSubtree(fqn); 398 399 } 400 401 public boolean exists(String realId) 402 { 403 Fqn fqn = getSessionFqn(realId); 404 return plainCache_.hasChild(fqn); 405 } 406 407 public Object getAttribute(String realId, String key) 408 { 409 Fqn fqn = getAttributeFqn(realId); 410 return getUnMarshalledValue(cacheWrapper_.get(fqn, key)); 411 } 412 413 public void putAttribute(String realId, String key, Object value) 414 { 415 Fqn fqn = getAttributeFqn(realId); 416 cacheWrapper_.put(fqn, key, getMarshalledValue(value)); 417 } 418 419 public void putAttribute(String realId, Map map) 420 { 421 Map marshalled = new HashMap (map.size()); 423 Set entries = map.entrySet(); 424 for (Iterator it = entries.iterator(); it.hasNext(); ) 425 { 426 Map.Entry entry = (Map.Entry ) it.next(); 427 marshalled.put(entry.getKey(), getMarshalledValue(entry.getValue())); 428 } 429 430 Fqn fqn = getAttributeFqn(realId); 431 cacheWrapper_.put(fqn, marshalled); 432 433 } 434 435 public void removeAttributes(String realId) 436 { 437 Fqn fqn = getAttributeFqn(realId); 438 cacheWrapper_.remove(fqn); 439 } 440 441 public Object removeAttribute(String realId, String key) 442 { 443 Fqn fqn = getAttributeFqn(realId); 444 if (log_.isTraceEnabled()) 445 { 446 log_.trace("Remove attribute from distributed store. Fqn: " + fqn + " key: " + key); 447 } 448 return getUnMarshalledValue(cacheWrapper_.remove(fqn, key)); 449 } 450 451 public void removeAttributesLocal(String realId) 452 { 453 Fqn fqn = getAttributeFqn(realId); 454 if (log_.isDebugEnabled()) 455 { 456 log_.debug("Remove attributes from my own distributed store only. Fqn: " + fqn); 457 } 458 cacheWrapper_.removeLocal(fqn); 459 } 460 461 465 public Set getAttributeKeys(String realId) 466 { 467 Set keys = null; 468 Fqn fqn = getAttributeFqn(realId); 469 try 470 { 471 Node node = plainCache_.getChild(fqn); 472 if (node != null) 473 keys = node.getKeys(); 474 } 475 catch (CacheException e) 476 { 477 log_.error("getAttributeKeys(): Exception getting keys for session " + realId, e); 478 } 479 480 return keys; 481 } 482 483 491 public Map getAttributes(String realId) 492 { 493 if (realId == null || realId.length() == 0) return new HashMap (); 494 495 Map map = new HashMap (); 496 Set set = getAttributeKeys(realId); 497 if(set != null) 498 { 499 for (Iterator it = set.iterator(); it.hasNext();) 500 { 501 String key = (String ) it.next(); 502 Object value = getAttribute(realId, key); 503 map.put(key, value); 504 } 505 } 506 return map; 507 } 508 509 516 public Map getSessionIds() throws CacheException 517 { 518 Map result = new HashMap (); 519 520 Node bbRoot = plainCache_.getChild(BUDDY_BACKUP_FQN); 521 if (bbRoot != null) 522 { 523 Set owners = bbRoot.getChildren(); 524 if (owners != null) 525 { 526 for (Iterator it = owners.iterator(); it.hasNext();) 527 { 528 Node owner = (Node) it.next(); 529 Node webRoot = owner.getChild(getWebappFqn()); 530 if (webRoot != null) 531 { 532 Set ids = webRoot.getChildrenNames(); 533 storeSessionOwners(ids, owner, result); 534 } 535 } 536 } 537 } 538 539 storeSessionOwners(getChildrenNames(getWebappFqn()), null, result); 540 541 return result; 542 } 543 544 private Set getChildrenNames(Fqn fqn) 545 { 546 Node node = plainCache_.getChild(fqn); 547 return (node == null ? null : node.getChildrenNames()); 548 } 549 550 private void storeSessionOwners(Set ids, Object owner, Map map) 551 { 552 if (ids != null) 553 { 554 for (Iterator it = ids.iterator(); it.hasNext();) 555 { 556 map.put(it.next(), owner); 557 } 558 } 559 } 560 561 569 public Object setPojo(String realId, String key, Object pojo) 570 { 571 if(log_.isTraceEnabled()) 572 { 573 log_.trace("setPojo(): session id: " + realId + " key: " + key + 574 " object: " + pojo.toString()); 575 } 576 Fqn fqn = getFieldFqn(realId, key); 578 try { 579 SessionReplicationContext.startCacheActivity(); 581 return pojoCache_.attach(fqn.toString(), pojo); 582 } catch (CacheException e) { 583 throw new RuntimeException ("JBossCacheService: exception occurred in cache setPojo ... ", e); 584 } 585 finally { 586 SessionReplicationContext.finishCacheActivity(); 587 } 588 } 589 590 596 public Object removePojo(String realId, String key) 597 { 598 if(log_.isTraceEnabled()) 599 { 600 log_.trace("removePojo(): session id: " +realId + " key: " +key); 601 } 602 Fqn fqn = getFieldFqn(realId, key); 604 try { 605 SessionReplicationContext.startCacheActivity(); 607 return pojoCache_.detach(fqn.toString()); 608 } catch (CacheException e) { 609 throw new RuntimeException ("JBossCacheService: exception occurred in cache removePojo ... ", e); 610 } 611 finally { 612 SessionReplicationContext.finishCacheActivity(); 613 } 614 } 615 616 622 public void removePojosLocal(String realId) 623 { 624 if(log_.isDebugEnabled()) 625 { 626 log_.debug("removePojoLocal(): session id: " +realId); 627 } 628 Fqn fqn = getAttributeFqn(realId); 630 try { 631 SessionReplicationContext.startCacheActivity(); 633 cacheWrapper_.removeLocalSubtree(fqn); 634 } 635 finally { 636 SessionReplicationContext.finishCacheActivity(); 637 } 638 } 639 640 646 public void removePojoLocal(String realId, String key) 647 { 648 if(log_.isTraceEnabled()) 649 { 650 log_.trace("removePojoLocal(): session id: " + realId + " key: " +key); 651 } 652 Fqn fqn = getFieldFqn(realId, key); 654 try { 655 SessionReplicationContext.startCacheActivity(); 657 cacheWrapper_.removeLocalSubtree(fqn); 658 } 659 finally { 660 SessionReplicationContext.finishCacheActivity(); 661 } 662 } 663 664 public Set getPojoKeys(String realId) 665 { 666 Set keys = null; 667 Fqn fqn = getAttributeFqn(realId); 668 try 669 { 670 keys = getChildrenNames(fqn); 671 } 672 catch (CacheException e) 673 { 674 log_.error("getPojoKeys(): Exception getting keys for session " + realId, e); 675 } 676 677 return keys; 678 } 679 680 681 687 public Object getPojo(String realId, String key) 688 { 689 if(log_.isTraceEnabled()) 690 { 691 log_.trace("getPojo(): session id: " +realId + " key: " +key); 692 } 693 Fqn fqn = getFieldFqn(realId, key); 695 696 try 697 { 698 return pojoCache_.find(fqn.toString()); 699 } 700 catch (CacheException e) 701 { 702 throw new RuntimeException ("JBossCacheService: exception occurred in cache getPojo ... ", e); 703 } 704 } 705 706 715 public void addObserver(Observer session, Object pojo) 716 { 717 addObserver(session, pojo, new HashSet ()); 718 } 719 720 private void addObserver(Observer session, Object pojo, Set processed) 721 { 722 if ( pojo instanceof Collection ) 723 { 724 Collection col = (Collection )pojo; 725 for (Iterator i = col.iterator(); i.hasNext();) { 726 addObserver(session, i.next(), processed); 728 } 729 } 730 else if (pojo instanceof Map ) 731 { 732 for (Iterator i = ((Map )pojo).entrySet().iterator(); i.hasNext();) 733 { 734 Map.Entry entry = (Map.Entry ) i.next(); 735 736 addObserver(session, entry.getKey(), processed); 738 addObserver(session, entry.getValue(), processed); 739 } 740 } 741 742 if(! (pojo instanceof Subject) ) 743 { 744 return; } 746 747 Subject subject = (Subject)pojo; 748 subject.addObserver(session); 749 750 if(log_.isTraceEnabled()) 751 { 752 log_.trace("addObserver(): session: " +session + " pojo name: " +pojo.getClass().getName()); 753 } 754 755 759 Class type = pojo.getClass(); 762 Set complexFields = (Set ) typeMap.get(type); 763 if (complexFields == null) 764 { 765 complexFields = Util.parseComplexFields(type); 766 typeMap.put(type, complexFields); 767 } 768 769 if (complexFields.size() == 0) 770 return; 771 772 processed.add(pojo); 774 775 for (Iterator iter = complexFields.iterator(); iter.hasNext();) 776 { 777 String fieldName = (String ) iter.next(); 778 Class curType = type; 779 while (curType != null) 780 { 781 try 782 { 783 Field field = curType.getDeclaredField(fieldName); 784 boolean accessible = field.isAccessible(); 785 Object value = null; 786 try 787 { 788 field.setAccessible(true); 789 790 value=field.get(pojo); 791 if (value != null && !processed.contains(value)) 793 addObserver(session, value, processed); 794 break; 795 } 796 catch(IllegalAccessException e) 797 { 798 throw new RuntimeException ("field access failed", e); 799 } 800 finally 801 { 802 field.setAccessible(accessible); 803 } 804 } 805 catch (NoSuchFieldException e) 806 { 807 curType = curType.getSuperclass(); 809 if (curType == null) 810 throw new RuntimeException ("Field " + fieldName + 811 " does not exist", e); 812 } 813 } 814 } 815 } 816 817 826 public void removeObserver(Observer session, Object pojo) 827 { 828 removeObserver(session, pojo, new HashSet ()); 829 } 830 831 private void removeObserver(Observer session, Object pojo, Set stack) 832 { 833 if ( pojo instanceof Collection ) 834 { 835 Collection col = (Collection )pojo; 836 for (Iterator i = col.iterator(); i.hasNext();) { 837 Object obj = i.next(); 838 removeObserver(session, obj, stack); 840 } 841 842 return; 843 } 844 else if (pojo instanceof Map ) 845 { 846 Map map = (Map )pojo; 847 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 848 Object key = i.next(); 849 Object value = map.get(key); 850 851 removeObserver(session, key, stack); 853 removeObserver(session, value, stack); 854 } 855 856 return; 857 } 858 if(! (pojo instanceof Subject) ) 861 { 862 return; } 864 865 Subject subject = (Subject)pojo; 866 subject.removeObserver(session); 867 if(log_.isTraceEnabled()) 868 { 869 log_.trace("removeObserver(): session: " +session + " pojo name: " +pojo.getClass().getName()); 870 } 871 872 876 Class type = pojo.getClass(); 879 Set complexFields = (Set ) typeMap.get(type); 880 if (complexFields == null) 881 { 882 complexFields = Util.parseComplexFields(type); 883 typeMap.put(type, complexFields); 884 } 885 886 if (complexFields.size() == 0) 887 return; 888 889 stack.add(pojo); 891 892 for (Iterator iter = complexFields.iterator(); iter.hasNext();) 893 { 894 String fieldName = (String ) iter.next(); 895 Class curType = type; 896 while (curType != null) 897 { 898 try 899 { 900 Field field = curType.getDeclaredField(fieldName); 901 boolean accessible = field.isAccessible(); 902 Object value = null; 903 try 904 { 905 field.setAccessible(true); 906 907 value=field.get(pojo); 908 if (value != null && !stack.contains(value)) 910 removeObserver(session, value, stack); 911 break; 912 } 913 catch(IllegalAccessException e) 914 { 915 throw new RuntimeException ("field access failed", e); 916 } 917 finally 918 { 919 field.setAccessible(accessible); 920 } 921 } 922 catch (NoSuchFieldException e) 923 { 924 curType = curType.getSuperclass(); 926 if (curType == null) 927 throw new RuntimeException ("Field " + fieldName + 928 " does not exist", e); 929 } 930 } 931 } 932 } 933 934 public boolean isCachePassivationEnabled() 935 { 936 CacheLoaderConfig clc = plainCache_.getConfiguration().getCacheLoaderConfig(); 937 if(clc != null) 938 { 939 return (clc.isPassivation() && !clc.isShared()); 940 } 941 else 942 { 943 return false; 944 } 945 } 946 947 private Fqn getFieldFqn(String id, String key) 948 { 949 List list = new ArrayList (6); 952 list.add(SESSION); 953 list.add(hostName_); 954 list.add(webAppPath_); 955 list.add(id); 956 list.add(ATTRIBUTE); 957 breakKeys(key, list); 958 return new Fqn(list); 959 } 960 961 private void breakKeys(String key, List list) 962 { 963 StringTokenizer token = new StringTokenizer (key, FQN_DELIMITER); 964 while(token.hasMoreTokens()) 965 { 966 list.add(token.nextToken()); 967 } 968 } 969 970 private Fqn getWebappFqn() 971 { 972 Object [] objs = new Object []{SESSION, hostName_, webAppPath_}; 974 return new Fqn(objs); 975 } 976 977 private Fqn getWebappFqn(Object dataOwner) 978 { 979 if (dataOwner == null) 980 return getWebappFqn(); 981 982 Object [] objs = new Object []{BUDDY_BACKUP, dataOwner, SESSION, hostName_, webAppPath_}; 984 return new Fqn(objs); 985 } 986 987 private Fqn getSessionFqn(String id) 988 { 989 Object [] objs = new Object []{SESSION, hostName_, webAppPath_, id}; 991 return new Fqn(objs); 992 } 993 994 private Fqn getSessionFqn(String id, String dataOwner) 995 { 996 Object [] objs = new Object []{BUDDY_BACKUP, dataOwner, SESSION, hostName_, webAppPath_, id}; 998 return new Fqn(objs); 999 } 1000 1001 private Fqn getAttributeFqn(String id) 1002 { 1003 Object [] objs = new Object []{SESSION, hostName_, webAppPath_, id, ATTRIBUTE}; 1005 return new Fqn(objs); 1006 } 1007 1008 private Object getMarshalledValue(Object value) 1009 { 1010 1017 try 1024 { 1025 if (SessionSerializationFactory.useJBossSerialization()) 1030 { 1031 MarshalledObject mo = SessionSerializationFactory.createMarshalledObject(value); 1032 if (log_.isTraceEnabled()) 1033 { 1034 log_.trace("JBoss Marshalled Object to size "); 1035 } 1036 return mo; 1037 1038 } 1039 else 1040 { 1041 MarshalledValue mv = SessionSerializationFactory.createMarshalledValue(value); 1042 if (log_.isTraceEnabled()) 1043 { 1044 log_.trace("marshalled object to size " + mv.size() + " bytes"); 1045 } 1046 return mv; 1047 } 1048 1049 } 1050 catch (IOException e) 1051 { 1052 log_.error("IOException occurred marshalling value ", e); 1053 return null; 1054 } 1055 } 1057 1058 private Object getUnMarshalledValue(Object mv) 1059 { 1060 if (mv == null) return null; 1071 ClassLoader prevTCL = Thread.currentThread().getContextClassLoader(); 1073 Thread.currentThread().setContextClassLoader(manager_.getWebappClassLoader()); 1074 try 1075 { 1076 1080 if (SessionSerializationFactory.useJBossSerialization()) 1081 { 1082 return ((MarshalledObject)mv).get(); 1083 } 1084 else 1085 { 1086 return ((MarshalledValue) mv).get(); 1087 } 1088 } 1089 catch (IOException e) 1090 { 1091 log_.error("IOException occurred unmarshalling value ", e); 1092 return null; 1093 } 1094 catch (ClassNotFoundException e) 1095 { 1096 log_.error("ClassNotFoundException occurred unmarshalling value ", e); 1097 return null; 1098 } 1099 finally 1100 { 1101 Thread.currentThread().setContextClassLoader(prevTCL); 1102 } 1103 } 1105 1106 private byte[] externalizeSession(ClusteredSession session) 1107 { 1108 try 1109 { 1110 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1112 1115 1119 1121 ObjectOutputStream oos = SessionSerializationFactory.createObjectOutputStream(baos); 1122 session.writeExternal(oos); 1123 oos.close(); 1125 byte[] bytes = baos.toByteArray(); 1126 1127 if (log_.isTraceEnabled()) 1128 { 1129 log_.trace("Serialized Object to size " + bytes.length + " bytes"); 1130 } 1131 1132 return bytes; 1133 } 1134 catch (Exception e) 1135 { 1136 log_.error("externalizeSession(): exception occurred externalizing session " + session, e); 1137 return null; 1138 } 1139 1140 } 1141 1142} 1143 | Popular Tags |