1 16 package org.apache.cocoon.webapps.session.components; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.context.Context; 23 import org.apache.avalon.framework.context.ContextException; 24 import org.apache.avalon.framework.context.Contextualizable; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 import org.apache.cocoon.ProcessingException; 28 import org.apache.cocoon.components.ContextHelper; 29 import org.apache.cocoon.environment.Request; 30 import org.apache.cocoon.environment.Session; 31 import org.apache.cocoon.webapps.session.TransactionManager; 32 import org.apache.cocoon.webapps.session.context.SessionContext; 33 34 40 public final class DefaultTransactionManager 41 extends AbstractLogEnabled 42 implements Component, ThreadSafe, TransactionManager, Contextualizable { 43 44 protected Context context; 45 46 49 private TransactionState getSessionContextsTransactionState(SessionContext context) { 50 final Request request = ContextHelper.getRequest(this.context); 51 final Session session = request.getSession(true); 52 Map transactionStates = (Map )session.getAttribute(this.getClass().getName()); 53 if (transactionStates == null) { 54 transactionStates = new HashMap (5, 3); 55 session.setAttribute(this.getClass().getName(), transactionStates); 56 } 57 TransactionState state = (TransactionState)transactionStates.get(context); 58 if ( state == null ) { 59 state = new TransactionState(); 60 transactionStates.put(context, state); 61 } 62 return state; 63 } 64 65 private static class TransactionState { 66 67 public int nr=0; 68 69 public int nrtotal=0; 70 71 public int nw=0; 72 73 public int nwtotal=0; 74 } 75 76 79 public void resetTransactions(SessionContext context) { 80 TransactionState ts = this.getSessionContextsTransactionState(context); 81 ts.nr=0; 82 ts.nrtotal=0; 83 ts.nw=0; 84 ts.nwtotal=0; 85 } 86 87 92 public synchronized void startReadingTransaction(SessionContext context) 93 throws ProcessingException { 94 TransactionState ts = this.getSessionContextsTransactionState(context); 95 ts.nrtotal++; 96 while (ts.nw!=0) { 97 try { 98 wait(); 99 } catch (InterruptedException local) { 100 throw new ProcessingException("Interrupted", local); 101 } 102 } 103 ts.nr++; 104 } 105 106 111 public synchronized void stopReadingTransaction(SessionContext context) { 112 TransactionState ts = this.getSessionContextsTransactionState(context); 113 ts.nr--; 114 ts.nrtotal--; 115 if (ts.nrtotal==0) notify(); 116 } 117 118 123 public synchronized void startWritingTransaction(SessionContext context) 124 throws ProcessingException { 125 TransactionState ts = this.getSessionContextsTransactionState(context); 126 ts.nwtotal++; 127 while (ts.nrtotal+ts.nw != 0) { 128 try { 129 wait(); 130 } catch (InterruptedException local) { 131 throw new ProcessingException("Interrupted", local); 132 } 133 } 134 ts.nw=1; 135 } 136 137 142 public synchronized void stopWritingTransaction(SessionContext context) { 143 TransactionState ts = this.getSessionContextsTransactionState(context); 144 ts.nw=0; 145 ts.nwtotal--; 146 notifyAll(); 147 } 148 149 152 public void contextualize(Context context) throws ContextException { 153 this.context = context; 154 } 155 156 } 157 | Popular Tags |