KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JEntitySwitchRO


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JEntitySwitchRO.java,v 1.2 2005/04/28 16:52:59 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import javax.ejb.EJBException JavaDoc;
29 import javax.transaction.SystemException JavaDoc;
30 import javax.transaction.Transaction JavaDoc;
31
32 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
33
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 /**
37  * ReadOnly lock policy : Instance are never written to database.
38  * They are regularly read from database if bean is shared.
39  * @author Philippe Durieux
40  */

41 public class JEntitySwitchRO extends JEntitySwitch {
42
43     /**
44      * unique EntityContext
45      */

46     protected JEntityContext itContext = null;
47
48     /**
49      * empty constructor. Object is initialized via init() because it is
50      * implemented differently according to jorm mappers.
51      */

52     public JEntitySwitchRO() {
53         lockpolicy = EntityDesc.LOCK_READ_ONLY;
54         txUpdates = true; // never write anyway.
55
}
56
57     protected void initpolicy(JEntityFactory bf) {
58         lazyregister = true; // never write anyway.
59
}
60
61     protected JEntityContext getContext4Tx(Transaction JavaDoc tx) {
62         return itContext;
63     }
64
65     protected void setContext4Tx(Transaction JavaDoc tx, JEntityContext ctx) {
66         itContext = ctx;
67     }
68
69     protected void removeContext4Tx(Transaction JavaDoc tx) {
70         itContext = null;
71     }
72
73     public void waitmyturn(Transaction JavaDoc tx) {
74         // No synchro for this policy.
75
}
76
77     /**
78      * Map a context and its instance.
79      * @param tx - the Transaction object
80      * @param bctx - the JEntityContext to bind if not null
81      * @param forced - force to take this context. (case of create)
82      * @param holdit - increment count to hold it, a release will be called
83      * later.
84      * @return JEntityContext actually mapped
85      */

86     public synchronized JEntityContext mapICtx(Transaction JavaDoc tx, JEntityContext bctx, boolean forced, boolean holdit, boolean notused) {
87
88         waitmyturn(tx);
89
90         // Choose the context to use.
91
boolean newtrans = false;
92         JEntityContext jec = itContext;
93         if (forced) {
94             TraceEjb.context.log(BasicLevel.ERROR, ident + "create cannot be called on read only bean");
95             throw new EJBException JavaDoc("Read Only bean");
96         } else {
97             if (jec != null) {
98                 // Reuse the Context for this transaction.
99
// If a context was supplied, release it first.
100
if (bctx != null) {
101                     if (TraceEjb.isDebugContext())
102                             TraceEjb.context.log(BasicLevel.DEBUG, ident + "a context was supplied!");
103                     bf.releaseJContext(bctx);
104                 }
105                 newtrans = true;
106                 jec.reuseEntityContext(newtrans);
107             } else {
108                 if (bctx != null) {
109                     jec = bctx;
110                 } else {
111                     // no Context available : get one from the pool.
112
jec = (JEntityContext) bf.getJContext(this);
113                 }
114                 jec.initEntityContext(this);
115                 jec.activate(true);
116                 itContext = jec; // after activate
117
newtrans = true;
118             }
119         }
120
121         if (tx == null) {
122             if (holdit) {
123                 countIH++;
124                 if (TraceEjb.isDebugSynchro())
125                         TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IH count=" + countIH);
126                 if (shared && countIH == 1) {
127                     // reload state that could have been modified by
128
// transactions.
129
jec.activate(false);
130                 }
131             }
132         } else {
133             if (holdit) {
134                 countIT++;
135             }
136         }
137
138         return jec;
139     }
140
141     /**
142      * never called ?
143      */

144     public boolean passivateIH(boolean passivation) {
145         JEntityContext jec = getContext4Tx(null);
146         // If already passivated, look if we can destroy the objects
147
if (jec == null) {
148             if (inactivityTimeout > 0 &&
149                 System.currentTimeMillis() - timestamp > inactivityTimeout) {
150                 if (TraceEjb.isDebugContext()) {
151                     TraceEjb.context.log(BasicLevel.DEBUG, "discard object on timeout");
152                 }
153                 discardContext(null, true, false);
154             }
155             return true;
156         }
157         // passivate this instance if required.
158
if (passivation) {
159             if (TraceEjb.isDebugContext()) TraceEjb.context.log(BasicLevel.DEBUG, "TODO: passivate: " + jec);
160         }
161         return true;
162     }
163
164     public void endIH() {
165         return; // NEVER
166
}
167
168     /**
169      * This transaction has just modified this instance. (CMP2 only)
170      * @param tx transaction
171      */

172     public synchronized void notifyWriting(Transaction JavaDoc tx, JEntityContext bctx) {
173         TraceEjb.context.log(BasicLevel.ERROR, ident + "read only bean: cannot write");
174         throw new EJBException JavaDoc("Read Only bean");
175     }
176
177     /**
178      * @return State of this instance. State values are 0=in-tx, 1=out-tx, 2=idle,
179      * 3=passive, 4=removed. we don't synchronize this method to avoid
180      * jadmin blocks
181      */

182     public int getState() {
183         if (itContext != null) {
184             if (itContext.isMarkedRemoved()) {
185                 return 4;
186             } else {
187                 if (writingtx != null) {
188                     return 0;
189                 } else {
190                     return 1;
191                 }
192             }
193         }
194         return 3;
195     }
196
197 }
198
Popular Tags