1 10 11 package org.mmbase.bridge.implementation; 12 13 import java.util.*; 14 import org.mmbase.bridge.*; 15 import org.mmbase.module.core.*; 16 import org.mmbase.util.logging.*; 17 18 26 public class BasicTransaction extends BasicCloud implements Transaction { 27 28 private static final Logger log = Logging.getLoggerInstance(BasicTransaction.class); 29 32 protected final String transactionContext; 33 34 private boolean canceled = false; 35 private boolean committed = false; 36 39 protected final String transactionName; 40 41 protected final BasicCloud parentCloud; 42 43 49 BasicTransaction(String transactionName, BasicCloud cloud) { 50 super(transactionName, cloud); 51 this.transactionName = transactionName; 52 this.parentCloud = cloud; 53 54 if (parentCloud instanceof BasicTransaction) { 59 transactionContext = ((BasicTransaction)parentCloud).transactionContext; 60 } else { 61 try { 62 transactionContext = account + "_" + transactionName; 66 BasicCloudContext.transactionManager.createTransaction(transactionContext); 67 } catch (TransactionManagerException e) { 68 throw new BridgeException(e.getMessage(), e); 69 } 70 } 71 } 72 73 74 public synchronized boolean commit() { 75 if (canceled) { 76 throw new BridgeException("Cannot commit transaction'" + name + "' (" + transactionContext +"), it was already canceled."); 77 } 78 if (committed) { 79 throw new BridgeException("Cannot commit transaction'" + name + "' (" + transactionContext +"), it was already committed."); 80 } 81 log.debug("Committing transaction " + transactionContext); 82 83 parentCloud.transactions.remove(transactionName); 85 if (parentCloud instanceof Transaction) { 88 } else { 90 try { 91 Collection col = BasicCloudContext.transactionManager.getTransaction(transactionContext); 92 BasicCloudContext.transactionManager.commit(userContext, transactionContext); 94 Iterator i = col.iterator(); 97 while (i.hasNext()) { 98 MMObjectNode n = (MMObjectNode) i.next(); 99 if (!TransactionManager.EXISTS_NOLONGER.equals(n.getStringValue("_exists"))) { 100 Node node = parentCloud.makeNode(n, "" + n.getNumber()); 101 node.commit(); 102 } 103 } 104 } catch (TransactionManagerException e) { 105 throw new BridgeException(e.getMessage(), e); 108 } 109 } 110 111 committed = true; 112 return true; 113 } 114 115 public synchronized void cancel() { 116 if (canceled) { 117 throw new BridgeException("Cannot cancel transaction'" + name + "' (" + transactionContext +"), it was already canceled."); 118 } 119 if (committed) { 120 throw new BridgeException("Cannot cancel transaction'" + name + "' (" + transactionContext +"), it was already committed."); 121 } 122 123 if (parentCloud instanceof Transaction) { 126 ((Transaction)parentCloud).cancel(); 127 } else { 128 try { 129 BasicCloudContext.transactionManager.cancel(userContext, transactionContext); 131 } catch (TransactionManagerException e) { 132 throw new BridgeException(e.getMessage(), e); 134 } 135 } 136 parentCloud.transactions.remove(transactionName); 138 canceled = true; 139 } 140 141 145 void add(String currentObjectContext) { 146 try { 147 BasicCloudContext.transactionManager.addNode(transactionContext, account, currentObjectContext); 148 } catch (TransactionManagerException e) { 149 throw new BridgeException(e.getMessage(), e); 150 } 151 } 152 153 157 void remove(String currentObjectContext) { 158 try { 159 BasicCloudContext.transactionManager.removeNode(transactionContext, account, currentObjectContext); 160 } catch (TransactionManagerException e) { 161 throw new BridgeException(e.getMessage(), e); 162 } 163 } 164 165 void delete(String currentObjectContext, MMObjectNode node) { 166 delete(currentObjectContext); 167 } 168 172 void delete(String currentObjectContext) { 173 try { 174 BasicCloudContext.transactionManager.deleteObject(transactionContext, account, currentObjectContext); 175 } catch (TransactionManagerException e) { 176 throw new BridgeException(e.getMessage(), e); 177 } 178 } 179 180 boolean contains(MMObjectNode node) { 181 if (transactionContext == null) { 183 return false; 184 } 185 try { 186 Collection transaction = BasicCloudContext.transactionManager.get(account, transactionContext); 187 return transaction.contains(node); 188 } catch (TransactionManagerException tme) { 189 throw new BridgeException(tme.getMessage(), tme); 190 } 191 } 192 193 201 protected void finalize() { 202 if ((transactionContext != null) && !(parentCloud instanceof Transaction)) { 203 cancel(); 204 } 205 } 206 207 public boolean isCanceled() { 208 return canceled; 209 } 210 public boolean isCommitted() { 211 return committed; 212 } 213 214 217 public String getCloudName() { 218 if (parentCloud instanceof Transaction) { 219 return ((Transaction) parentCloud).getCloudName(); 220 } 221 else { 222 return parentCloud.getName(); 223 } 224 } 225 } 226 227 | Popular Tags |