KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > oa > poa > AOMEntry


1 /*
2  * @(#)AOMEntry.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.oa.poa ;
9
10 import org.omg.CORBA.INTERNAL JavaDoc ;
11
12 import com.sun.corba.se.spi.orb.ORB ;
13
14 import com.sun.corba.se.spi.orbutil.fsm.Action ;
15 import com.sun.corba.se.spi.orbutil.fsm.ActionBase ;
16 import com.sun.corba.se.spi.orbutil.fsm.Guard ;
17 import com.sun.corba.se.spi.orbutil.fsm.GuardBase ;
18 import com.sun.corba.se.spi.orbutil.fsm.State ;
19 import com.sun.corba.se.spi.orbutil.fsm.StateImpl ;
20 import com.sun.corba.se.spi.orbutil.fsm.Input ;
21 import com.sun.corba.se.spi.orbutil.fsm.InputImpl ;
22 import com.sun.corba.se.spi.orbutil.fsm.FSM ;
23 import com.sun.corba.se.spi.orbutil.fsm.FSMImpl ;
24 import com.sun.corba.se.spi.orbutil.fsm.StateEngine ;
25 import com.sun.corba.se.spi.orbutil.fsm.StateEngineFactory ;
26
27 import com.sun.corba.se.impl.orbutil.concurrent.Mutex ;
28 import com.sun.corba.se.impl.orbutil.concurrent.CondVar ;
29
30 /** AOMEntry represents a Servant or potential Servant in the ActiveObjectMap.
31 * It may be in several states to allow for long incarnate or etherealize operations.
32 * The methods on this class mostly represent input symbols to the state machine
33 * that controls the lifecycle of the entry. A library is used to build the state
34 * machine rather than the more usual state pattern so that the state machine
35 * transitions are explicitly visible.
36 */

37 public class AOMEntry extends FSMImpl {
38     private final Thread JavaDoc[] etherealizer ; // The actual etherealize operation
39
// for this entry. It is
40
// represented as a Thread because
41
// the POA.deactivate_object never
42
// waits for the completion.
43
private final int[] counter ; // single element holder for counter
44
// accessed in actions
45
private final CondVar wait ; // accessed in actions
46

47     final POAImpl poa ;
48
49     public static final State INVALID = new StateImpl( "Invalid" ) ;
50     public static final State INCARN = new StateImpl( "Incarnating" ) {
51     public void postAction( FSM fsm ) {
52         AOMEntry entry = (AOMEntry)fsm ;
53         entry.wait.broadcast() ;
54     }
55     };
56     public static final State VALID = new StateImpl( "Valid" ) ;
57     public static final State ETHP = new StateImpl( "EtherealizePending" ) ;
58     public static final State ETH = new StateImpl( "Etherealizing" ) {
59     public void preAction( FSM fsm ) {
60         AOMEntry entry = (AOMEntry)fsm ;
61         Thread JavaDoc etherealizer = entry.etherealizer[0] ;
62         if (etherealizer != null)
63         etherealizer.start() ;
64     }
65
66     public void postAction( FSM fsm ) {
67         AOMEntry entry = (AOMEntry)fsm ;
68         entry.wait.broadcast() ;
69     }
70     };
71     public static final State DESTROYED = new StateImpl( "Destroyed" ) ;
72
73     static final Input START_ETH = new InputImpl( "startEtherealize" ) ;
74     static final Input ETH_DONE = new InputImpl( "etherealizeDone" ) ;
75     static final Input INC_DONE = new InputImpl( "incarnateDone" ) ;
76     static final Input INC_FAIL = new InputImpl( "incarnateFailure" ) ;
77     static final Input ACTIVATE = new InputImpl( "activateObject" ) ;
78     static final Input ENTER = new InputImpl( "enter" ) ;
79     static final Input EXIT = new InputImpl( "exit" ) ;
80
81     private static Action incrementAction = new ActionBase( "increment" ) {
82     public void doIt( FSM fsm, Input in ) {
83         AOMEntry entry = (AOMEntry)fsm ;
84         entry.counter[0]++ ;
85     }
86     } ;
87
88     private static Action decrementAction = new ActionBase( "decrement" ) {
89     public void doIt( FSM fsm, Input in ) {
90         AOMEntry entry = (AOMEntry)fsm ;
91         if (entry.counter[0] > 0)
92         entry.counter[0]-- ;
93         else
94         throw entry.poa.lifecycleWrapper().aomEntryDecZero() ;
95     }
96     } ;
97
98     private static Action throwIllegalStateExceptionAction = new ActionBase(
99     "throwIllegalStateException" ) {
100     public void doIt( FSM fsm, Input in ) {
101         throw new IllegalStateException JavaDoc(
102         "No transitions allowed from the DESTROYED state" ) ;
103     }
104     } ;
105
106     private static Guard waitGuard = new GuardBase( "wait" ) {
107     public Guard.Result evaluate( FSM fsm, Input in ) {
108         AOMEntry entry = (AOMEntry)fsm ;
109         try {
110         entry.wait.await() ;
111         } catch (InterruptedException JavaDoc exc) {
112         // XXX Log this
113
// NO-OP
114
}
115
116         return Guard.Result.DEFERED ;
117     }
118     } ;
119
120
121     private static class CounterGuard extends GuardBase {
122     private int value ;
123
124     public CounterGuard( int value )
125     {
126         super( "counter>" + value ) ;
127         this.value = value ;
128     }
129
130     public Guard.Result evaluate( FSM fsm, Input in )
131     {
132         AOMEntry entry = (AOMEntry)fsm ;
133         return Guard.Result.convert( entry.counter[0] > value ) ;
134     }
135     } ;
136
137     private static GuardBase greaterZeroGuard = new CounterGuard( 0 ) ;
138     private static Guard zeroGuard = new Guard.Complement( greaterZeroGuard ) ;
139     private static GuardBase greaterOneGuard = new CounterGuard( 1 ) ;
140     private static Guard oneGuard = new Guard.Complement( greaterOneGuard ) ;
141
142     private static StateEngine engine ;
143
144     static {
145     engine = StateEngineFactory.create() ;
146
147     // State, Input, Guard, Action, new State
148

149     engine.add( INVALID, ENTER, incrementAction, INCARN ) ;
150     engine.add( INVALID, ACTIVATE, null, VALID ) ;
151     engine.setDefault( INVALID ) ;
152
153     engine.add( INCARN, ENTER, waitGuard, null, INCARN ) ;
154     engine.add( INCARN, EXIT, null, INCARN ) ;
155     engine.add( INCARN, START_ETH, waitGuard, null, INCARN ) ;
156     engine.add( INCARN, INC_DONE, null, VALID ) ;
157     engine.add( INCARN, INC_FAIL, decrementAction, INVALID ) ;
158
159     engine.add( VALID, ENTER, incrementAction, VALID ) ;
160     engine.add( VALID, EXIT, decrementAction, VALID ) ;
161     engine.add( VALID, START_ETH, greaterZeroGuard, null, ETHP ) ;
162     engine.add( VALID, START_ETH, zeroGuard, null, ETH ) ;
163
164     engine.add( ETHP, ENTER, waitGuard, null, ETHP ) ;
165     engine.add( ETHP, START_ETH, null, ETHP ) ;
166     engine.add( ETHP, EXIT, greaterOneGuard, decrementAction, ETHP ) ;
167     engine.add( ETHP, EXIT, oneGuard, decrementAction, ETH ) ;
168
169     engine.add( ETH, START_ETH, null, ETH ) ;
170     engine.add( ETH, ETH_DONE, null, DESTROYED ) ;
171     engine.add( ETH, ENTER, waitGuard, null, ETH ) ;
172     
173     engine.setDefault( DESTROYED, throwIllegalStateExceptionAction, DESTROYED ) ;
174
175     engine.done() ;
176     }
177
178     public AOMEntry( POAImpl poa )
179     {
180     super( engine, INVALID, ((ORB)poa.getORB()).poaFSMDebugFlag ) ;
181     this.poa = poa ;
182     etherealizer = new Thread JavaDoc[1] ;
183     etherealizer[0] = null ;
184     counter = new int[1] ;
185     counter[0] = 0 ;
186     wait = new CondVar( poa.poaMutex,
187         ((ORB)poa.getORB()).poaConcurrencyDebugFlag ) ;
188     }
189
190     // Methods that drive the FSM: the real interface to this class
191
// Most just call the doIt method, but startEtherealize needs
192
// the etherealizer.
193
public void startEtherealize( Thread JavaDoc etherealizer )
194     {
195     this.etherealizer[0] = etherealizer ;
196     doIt( START_ETH ) ;
197     }
198
199     public void etherealizeComplete() { doIt( ETH_DONE ) ; }
200     public void incarnateComplete() { doIt( INC_DONE ) ; }
201     public void incarnateFailure() { doIt( INC_FAIL ) ; }
202     public void activateObject() { doIt( ACTIVATE ) ; }
203     public void enter() { doIt( ENTER ) ; }
204     public void exit() { doIt( EXIT ) ; }
205 }
206
Popular Tags