KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > server > state > SessionImpl


1 /*
2  * Copyright 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  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.server.state;
20
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23
24 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
25 import org.apache.beehive.netui.tools.testrecorder.shared.SessionBean;
26
27
28 /**
29  * User: ozzy
30  * Date: Mar 10, 2003
31  * Time: 10:33:11 AM
32  */

33 abstract class SessionImpl implements Session {
34
35     private static final Logger log = Logger.getInstance( SessionImpl.class );
36
37     // milliseconds
38
public static final long LOOP_TIMEOUT = 250;
39     // milliseconds ... 1 minute to you and me
40
public static final int HARD_TIMEOUT = 60 * 1 * 1000;
41
42     public static final int BEGIN = 0;
43     public static final int RECORD = 1;
44     public static final int PLAYBACK = 2;
45     public static final int STOP = 3;
46     public static final int END = 4;
47     public static final int ERROR = 5;
48     // ended with an error
49
public static final int ERROR_END = 6;
50
51     // static
52
private SessionBean sessionBean;
53
54     // dynamic
55
private int inProgress;
56     private int sessionState;
57     private boolean sessionClosed;
58
59     SessionImpl( SessionBean sessionBean ) {
60         this.sessionBean = sessionBean;
61         inProgress = 0;
62         sessionState = BEGIN;
63     }
64
65     // session has been started, new tests may or may not be startable
66
public boolean isSessionStarted() {
67         boolean rtnVal = false;
68         if ( getSessionState() > BEGIN || getSessionState() < END ) {
69             rtnVal = true;
70         }
71         return rtnVal;
72     }
73
74     // all tests have been persisted, no new tests may be started.
75
public boolean isSessionFinished() {
76         boolean rtnVal = false;
77         if ( getSessionState() >= END ) {
78             rtnVal = true;
79         }
80         return rtnVal;
81     }
82
83     // returns true if the session is successfully started, false otherwise.
84
public abstract boolean sessionStart() throws SessionFailedException;
85
86     public synchronized boolean sessionEnd() throws SessionFailedException {
87         return sessionEnd( LOOP_TIMEOUT, HARD_TIMEOUT );
88     }
89
90     // TODO get rid of boolean return value
91
public synchronized boolean sessionEnd( long loopTimeout, long hardTimeout ) throws SessionFailedException {
92         if ( getSessionState() < ERROR ) {
93             setSessionState( STOP );
94         }
95         int cnt = 0;
96         long sTime = new Date JavaDoc().getTime();
97         long eTime = sTime;
98         while ( !isSessionFinished() ) {
99             if ( log.isDebugEnabled() ) {
100                 log.debug( "sessionEnd(): cnt(" + cnt + "), starting loop" );
101             }
102             // we can only wait so long ...
103
if ( ( eTime - sTime ) > hardTimeout ) {
104                 float milliseconds = ( eTime - sTime );
105                 String JavaDoc msg = "ERROR: session end timed out, elapsedTime( " + milliseconds + "" +
106                         "milliseconds ), inProgress( " + inProgressCnt() + " )";
107                 log.error( msg );
108                 throw new SessionFailedException( msg );
109             }
110             cnt++;
111             if ( cnt != 1 ) {
112                 try {
113                     if ( log.isDebugEnabled() ) {
114                         log.debug( "sessionEnd(): cnt(" + cnt + "), waiting ..." );
115                     }
116                     wait( loopTimeout );
117                 }
118                 catch ( InterruptedException JavaDoc ex ) {
119                     // do nothing.
120
}
121             }
122             // cleans up the session if getSessionState() == STOP and inProgressCnt() == 0
123
if ( getSessionState() == STOP && inProgressCnt() == 0 || getSessionState() >= ERROR ) {
124                 if ( getSessionState() == ERROR ) {
125                     setSessionState( ERROR_END );
126                 }
127                 else {
128                     setSessionState( END );
129                 }
130                 closeSession();
131             }
132             eTime = new Date JavaDoc().getTime();
133         }
134         return true;
135     }
136
137     /**
138      * indicates the number of tests executed by this session.
139      *
140      * @return
141      */

142     public abstract int testCount();
143
144     public String JavaDoc getStartDateString() {
145         return sessionBean.getStartDateString();
146     }
147
148     public String JavaDoc getEndDateString() {
149         return sessionBean.getEndDateString();
150     }
151
152     public String JavaDoc getSessionName() {
153         return sessionBean.getSessionName();
154     }
155
156     public String JavaDoc getTestUser() {
157         return sessionBean.getTester();
158     }
159
160     public String JavaDoc getDescription() {
161         return sessionBean.getDescription();
162     }
163
164     protected int getSessionState() {
165         return sessionState;
166     }
167
168     protected int inProgressCnt() {
169         return inProgress;
170     }
171
172     // called by synchronized methods
173
protected synchronized void incrementInProgress() {
174         inProgress++;
175         if ( log.isDebugEnabled() ) {
176             log.debug( "inProgress( " + inProgress + " )" );
177         }
178     }
179
180     // called by synchronized methods
181
protected synchronized void decrementInProgress() {
182         inProgress--;
183         if ( log.isDebugEnabled() ) {
184             log.debug( "inProgress( " + inProgress + " )" );
185         }
186     }
187
188     protected SessionBean getSessionBean() {
189         return sessionBean;
190     }
191
192     protected synchronized void closeSession() throws SessionFailedException {
193         if ( log.isDebugEnabled() ) {
194             log.debug( "closing session( " + getSessionName() + " ), testCount( " + testCount() + " )" );
195         }
196         if ( sessionClosed == true ) {
197             return;
198         }
199         sessionClosed = true;
200         closeSessionInternal();
201     }
202
203     protected abstract void closeSessionInternal() throws SessionFailedException;
204
205     // called by synchronized methods
206
protected synchronized void checkSessionComplete() throws SessionFailedException {
207         if ( log.isDebugEnabled() ) {
208             log.debug( "checkSessionComplete(): inProgressCnt(" + inProgressCnt() + "), sessionState(" +
209                     getSessionState() + ")" );
210         }
211         if ( ( getSessionState() == STOP && inProgressCnt() == 0 ) || getSessionState() >= ERROR ) {
212             if ( getSessionState() == ERROR ) {
213                 setSessionState( ERROR_END );
214             }
215             else {
216                 setSessionState( END );
217             }
218             // a thread may be waiting for the session to complete
219
notifyAll();
220             // clean up the session
221
// may throw IOException
222
try {
223                 closeSession();
224             }
225             catch ( Exception JavaDoc e ) {
226                 String JavaDoc msg = "Failed to close session( " + getSessionName() + " ), exception( " +
227                         e.getMessage() + " )";
228                 log.error( msg, e );
229                 throw new SessionFailedException( msg, e );
230             }
231         }
232     }
233
234     protected void setStartDate( Calendar JavaDoc startDate ) {
235         sessionBean.setStartDate( startDate );
236     }
237
238     protected void setEndDate( Calendar JavaDoc endDate ) {
239         sessionBean.setEndDate( endDate );
240     }
241
242     protected void setTestUser( String JavaDoc testUser ) {
243         sessionBean.setTester( testUser );
244     }
245
246     protected void setDescription( String JavaDoc description ) {
247         sessionBean.setDescription( description );
248     }
249
250     protected void setSessionState( int sessionState ) {
251         this.sessionState = sessionState;
252     }
253
254     protected void error( String JavaDoc msg ) throws SessionFailedException {
255         // recoverable error
256
error( msg, null );
257     }
258
259     protected void error( String JavaDoc msg, Exception JavaDoc e ) throws SessionFailedException {
260         // recoverable error
261
error( msg, e, false );
262     }
263
264     protected void error( String JavaDoc msg, Exception JavaDoc e, boolean unrecoverable ) throws SessionFailedException {
265         if ( e == null ) {
266             log.error( msg );
267         }
268         else {
269             log.error( msg, e );
270         }
271         if ( unrecoverable ) {
272             setSessionState( ERROR_END );
273             closeSession();
274         }
275         if ( e == null ) {
276             throw new SessionFailedException( msg );
277         }
278         throw new SessionFailedException( msg, e );
279     }
280
281     public String JavaDoc toString() {
282         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 128 );
283         sb.append( "[ " );
284         sb.append( "sessionName( " + getSessionName() + " )" );
285         sb.append( ", startDate( " + sessionBean.getStartDateString() + " )" );
286         sb.append( ", endDate( " + sessionBean.getEndDateString() + " )" );
287         sb.append( ", testUser( " + getTestUser() + " )" );
288         sb.append( ", description( " + getDescription() + " )" );
289         sb.append( ", state( " + getSessionState() + " )" );
290         sb.append( ", testCount( " + testCount() + " )" );
291         sb.append( ", inProgress( " + inProgressCnt() + " )" );
292         sb.append( " ]" );
293         return sb.toString();
294     }
295
296 }
297
Popular Tags