1 23 28 29 package com.sun.jdo.api.persistence.support; 30 31 import java.util.ArrayList ; 32 import java.util.List ; 33 import java.util.Hashtable ; 34 35 import javax.transaction.Synchronization ; 36 37 46 public class SynchronizationManager implements Synchronization { 47 48 52 public SynchronizationManager(int initialCapacity) { 53 synchronizations = new ArrayList (initialCapacity); 54 } 55 56 59 public SynchronizationManager() { 60 this(defaultCapacity); 61 } 62 63 67 public static void registerSynchronization(Synchronization instance, PersistenceManager pm) { 68 SynchronizationManager synchronizationManager = getSynchronizationManager(pm); 69 synchronizationManager.registerSynchronization(instance); 70 } 71 72 75 public static void setDefaultCapacity(int capacity) { 76 defaultCapacity = capacity; 77 } 78 79 81 protected static int defaultCapacity = 100; 82 83 88 protected final List synchronizations; 89 90 94 protected SynchronizationManager(PersistenceManager pm) { 95 this(); 96 Transaction tx = pm.currentTransaction(); 97 tx.setSynchronization((Synchronization )this); 98 } 99 100 107 protected static SynchronizationManager getSynchronizationManager(PersistenceManager pm) { 108 Transaction tx = pm.currentTransaction(); 109 Synchronization oldsync = tx.getSynchronization(); 110 if (oldsync instanceof SynchronizationManager) { 111 return (SynchronizationManager) oldsync; 113 } else { 114 SynchronizationManager newsync = new SynchronizationManager(pm); 117 if (oldsync != null) { 118 newsync.registerSynchronization(oldsync); 120 } 121 return newsync; 122 } 123 } 124 125 130 public void beforeCompletion() { 131 int size = synchronizations.size(); 132 for (int i = 0; i < size; ++i) { 133 Synchronization instance = (Synchronization ) synchronizations.get(i); 134 instance.beforeCompletion(); 135 } 136 } 137 138 147 public void afterCompletion(int status) { 148 int size = synchronizations.size(); 149 StringBuffer sb = null; 150 for (int i = 0; i < size; ++i) { 151 Synchronization instance = (Synchronization ) synchronizations.get(i); 152 try { 153 instance.afterCompletion(status); 154 } catch (Exception e) { 155 if (sb == null) { 156 sb = new StringBuffer (); 157 } 158 sb.append(e.getMessage()).append('\n'); } 160 } 161 synchronizations.clear(); 162 if (sb != null) { 163 throw new JDOUserException(sb.toString()); 164 } 165 } 166 167 175 protected void registerSynchronization(Synchronization instance) { 176 synchronizations.add(instance); 177 } 178 179 } 180 | Popular Tags |