KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > components > DefaultTransactionManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.session.components;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
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 /**
35  * This is the default implementation for the transaction manager.
36  *
37  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
38  * @version CVS $Id: DefaultTransactionManager.java 124685 2005-01-08 22:20:56Z antonio $
39 */

40 public final class DefaultTransactionManager
41 extends AbstractLogEnabled
42 implements Component, ThreadSafe, TransactionManager, Contextualizable {
43
44     protected Context context;
45
46     /**
47      * Get the transaction states from the current session
48      */

49     private TransactionState getSessionContextsTransactionState(SessionContext context) {
50         final Request request = ContextHelper.getRequest(this.context);
51         final Session session = request.getSession(true);
52         Map JavaDoc transactionStates = (Map JavaDoc)session.getAttribute(this.getClass().getName());
53         if (transactionStates == null) {
54             transactionStates = new HashMap JavaDoc(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         /** number readers reading*/
67         public int nr=0;
68         /** number of readers total (reading or waiting to read)*/
69         public int nrtotal=0;
70         /** number writers writing, 0 or 1 */
71         public int nw=0;
72         /** number of writers total (writing or waiting to write)*/
73         public int nwtotal=0;
74     }
75
76     /**
77      * Reset the transaction management state.
78      */

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     /**
88      * Start a reading transaction.
89      * This call must always be matched with a stopReadingTransaction().
90      * Otherwise the session context is blocked.
91      */

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 JavaDoc local) {
100                 throw new ProcessingException("Interrupted", local);
101             }
102         }
103         ts.nr++;
104     }
105
106     /**
107      * Stop a reading transaction.
108      * This call must always be done for each startReadingTransaction().
109      * Otherwise the session context is blocked.
110      */

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     /**
119      * Start a writing transaction.
120      * This call must always be matched with a stopWritingTransaction().
121      * Otherwise the session context is blocked.
122      */

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 JavaDoc local) {
131                 throw new ProcessingException("Interrupted", local);
132             }
133         }
134         ts.nw=1;
135      }
136
137     /**
138      * Stop a writing transaction.
139      * This call must always be done for each startWritingTransaction().
140      * Otherwise the session context is blocked.
141      */

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     /* (non-Javadoc)
150      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
151      */

152     public void contextualize(Context context) throws ContextException {
153         this.context = context;
154     }
155
156 }
157
Popular Tags