KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.beehive.netui.tools.testrecorder.shared.Util;
22 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.io.File JavaDoc;
30
31
32 /**
33  * User: ozzy
34  * Date: Mar 6, 2003
35  * Time: 3:34:14 PM
36  */

37 public class State implements Serializable JavaDoc {
38
39     private static final Logger log = Logger.getInstance( State.class );
40
41     private boolean testMode;
42     // needs to be serialized
43
private RecordSession recordSession = null;
44     // needs to be serialized
45
// map of PlaybackSessions
46
private Map JavaDoc playbackMap;
47
48
49     public State() {
50         if ( log.isInfoEnabled() ) {
51             log.info( "Constructor" );
52         }
53         playbackMap = new HashMap JavaDoc();
54     }
55
56     public boolean isTestMode() {
57         return testMode;
58     }
59
60     private void setTestMode() {
61         setTestMode( true );
62     }
63
64     public void setTestMode( boolean testMode ) {
65         this.testMode = testMode;
66     }
67
68     public boolean isRecording() {
69         boolean rtnVal = false;
70         if ( getRecordingSession() != null ) {
71             rtnVal = true;
72         }
73         return rtnVal;
74     }
75
76     public RecordSession getRecordingSession() {
77         return recordSession;
78     }
79
80     // return true if a new session has been started.
81
// if already in record mode false will be returned.
82
// Use record stop before starting a new session.
83
public boolean recordStart( RecordSession session ) throws SessionFailedException {
84         boolean rtnVal = false;
85         if ( isRecording() == true ) {
86             rtnVal = false;
87         }
88         else if ( session == null ) {
89             String JavaDoc msg = "the RecordSession may not be null";
90             log.error( msg );
91             throw new SessionFailedException( msg );
92         }
93         else {
94             synchronized ( this ) {
95                 if ( isRecording() == true ) {
96                     rtnVal = false;
97                 }
98                 else {
99                     rtnVal = session.sessionStart();
100                     if ( rtnVal ) {
101                         setRecordingSession( session );
102                         setTestMode();
103                     }
104                 }
105             }
106         }
107         if ( log.isDebugEnabled() ) {
108             log.debug( "rtnVal( " + rtnVal + " )" );
109             log.debug( "recording( " + isRecording() + " )" );
110             log.debug( "recordSession( " + getRecordingSession() + " )" );
111         }
112         return rtnVal;
113     }
114
115     // return the session or null if no recording session was in progress
116
public RecordSession recordStop() throws SessionFailedException {
117         RecordSession rtnVal = null;
118         if ( isRecording() == false ) {
119             rtnVal = null;
120         }
121         else {
122             synchronized ( this ) {
123                 if ( isRecording() == false ) {
124                     rtnVal = null;
125                 }
126                 else {
127                     // may throw a RuntimeException if the session fails to end.
128
// this method will wait for a timeout specified in the RecordSession class.
129
try {
130                         rtnVal = getRecordingSession();
131                         rtnVal.sessionEnd();
132                     }
133                     catch ( Exception JavaDoc ex ) {
134                         String JavaDoc msg = "ERROR: Failed while stopping recording session( " +
135                                 getRecordingSession().getSessionName() + " )";
136                         log.error( msg, ex );
137                         if ( ex instanceof SessionFailedException ) {
138                             throw (SessionFailedException) ex;
139                         }
140                         throw new SessionFailedException( msg, ex );
141                     }
142                     finally {
143                         removeRecordingSession();
144                     }
145                 }
146             }
147         }
148         if ( log.isDebugEnabled() ) {
149             log.debug( "recording( " + isRecording() + " )" );
150             log.debug( "rtnVal( " + rtnVal + " )" );
151         }
152         return rtnVal;
153     }
154
155     public PlaybackSession getPlaybackSession( String JavaDoc testId ) {
156         return (PlaybackSession) getInternalPlaybackMap().get( testId );
157     }
158
159     public Map JavaDoc getPlaybackMap() {
160         if ( playbackMap.size() == 0 ) {
161             return null;
162         }
163         // playback sessions only modifiable by classes in testrecorder package.
164
// !!! don't modify them without understanding the consequences!!!
165
return Collections.unmodifiableMap( playbackMap );
166     }
167
168     public boolean playbackSessionExists( PlaybackSession session ) {
169         return playbackSessionExists( session.getStringUID() );
170     }
171
172     public boolean playbackSessionExists( String JavaDoc testId ) {
173         if ( getPlaybackMap() == null ) {
174             return false;
175         }
176         return getInternalPlaybackMap().containsKey( testId );
177     }
178
179     // return true if a new session has been started.
180
// if this session is already in playback mode, return false.
181
// Use playback stop before starting a session.
182
public boolean playbackStart( PlaybackSession session ) throws SessionFailedException {
183         boolean rtnVal = false;
184         if ( session == null ) {
185             String JavaDoc msg = "the PlaybackSession may not be null";
186             log.fatal( msg );
187             throw new SessionFailedException( msg );
188         }
189         else {
190             if ( log.isDebugEnabled() ) {
191                 log.debug( "session( " + session + " )" );
192             }
193             rtnVal = session.sessionStart();
194             if ( rtnVal ) {
195                 addPlaybackSession( session );
196                 setTestMode();
197             }
198         }
199         if ( log.isDebugEnabled() ) {
200             log.debug( "rtnVal( " + rtnVal + " )" );
201         }
202         return rtnVal;
203     }
204
205     // return the session or null if the playback session was NOT in progress
206
public PlaybackSession playbackStop( String JavaDoc testId ) throws SessionFailedException {
207         PlaybackSession rtnVal = getPlaybackSession( testId );
208         // may throw a RuntimeException if the session fails to end.
209
// this method will wait for a timeout specified in the PlaybackSession class.
210
if ( rtnVal != null ) {
211             try {
212                 if ( rtnVal.sessionEnd() == false ) {
213                     if ( log.isWarnEnabled() ) {
214                         log.warn( "session failed to end( " + rtnVal + " )" );
215                     }
216                 }
217             }
218             finally {
219                 removePlaybackSession( rtnVal );
220             }
221         }
222         if ( log.isDebugEnabled() ) {
223             log.debug( "rtnVal( " + rtnVal + " )" );
224         }
225         return rtnVal;
226     }
227
228     public synchronized void stopAll() {
229         if ( log.isDebugEnabled() ) {
230             log.debug( "stopAll(): START ..." );
231         }
232         if ( getRecordingSession() != null ) {
233             if ( log.isDebugEnabled() ) {
234                 log.debug( "stopAll(): stopping record session ..." );
235             }
236             try {
237                 recordStop();
238             }
239             catch ( Exception JavaDoc e ) {
240                 log.error( e );
241             }
242             if ( log.isDebugEnabled() ) {
243                 log.debug( "stopAll(): .... record session stopped" );
244             }
245         }
246         if ( getInternalPlaybackMap() != null ) {
247             if ( log.isDebugEnabled() ) {
248                 log.debug( "stopAll(): stopping playback sessions, session count( " +
249                         getInternalPlaybackMap().size() + " )" );
250             }
251             Iterator JavaDoc it = Util.getListOfKeys( getInternalPlaybackMap() ).iterator();
252             String JavaDoc key = null;
253             while ( it.hasNext() ) {
254                 key = (String JavaDoc) it.next();
255                 if ( log.isDebugEnabled() ) {
256                     log.debug( "stopAll(): stopping playback session with key( " + key + " ) ..." );
257                 }
258                 try {
259                     playbackStop( key );
260                 }
261                 catch ( Exception JavaDoc e ) {
262                     log.error( e );
263                 }
264                 if ( log.isDebugEnabled() ) {
265                     log.debug( "stopAll(): ... playback session stopped" );
266                 }
267             }
268         }
269         if ( log.isDebugEnabled() ) {
270             log.debug( "stopAll(): ... END" );
271         }
272     }
273
274     public static PlaybackSession createPlaybackSession( String JavaDoc sessionName,
275             File JavaDoc playbackFile, File JavaDoc recordFile, File JavaDoc diffFile )
276             throws SessionFailedException {
277         return new PlaybackSessionImpl( sessionName, playbackFile,
278                 recordFile, diffFile );
279     }
280
281     public static RecordSession createRecordSession( String JavaDoc sessionName,
282             File JavaDoc recordFile, boolean overwrite, String JavaDoc testUser,
283             String JavaDoc description ) throws SessionFailedException {
284         return new RecordSessionImpl( sessionName, recordFile,
285                 overwrite, testUser, description );
286     }
287
288     boolean addPlaybackSession( PlaybackSession session ) {
289         boolean rtnVal = false;
290         synchronized ( playbackMap ) {
291             if ( playbackSessionExists( session ) ) {
292                 rtnVal = false;
293             }
294             else {
295                 getInternalPlaybackMap().put( session.getStringUID(), session );
296                 rtnVal = true;
297             }
298         }
299         return rtnVal;
300     }
301
302     PlaybackSession removePlaybackSession( PlaybackSession session ) {
303         return removePlaybackSession( session.getStringUID() );
304     }
305
306     PlaybackSession removePlaybackSession( String JavaDoc testId ) {
307         PlaybackSession rtnVal;
308         synchronized ( playbackMap ) {
309             if ( playbackSessionExists( testId ) ) {
310                 // returns the value and removes the key.
311
rtnVal = (PlaybackSession) playbackMap.remove( testId );
312             }
313             else {
314                 rtnVal = null;
315             }
316         }
317         return rtnVal;
318     }
319
320     protected RecordSession removeRecordingSession() {
321         RecordSession session = getRecordingSession();
322         setRecordingSession( null );
323         return session;
324     }
325
326     protected void setRecordingSession( RecordSession recordSession ) {
327         this.recordSession = recordSession;
328     }
329
330     protected Map JavaDoc getInternalPlaybackMap() {
331         return playbackMap;
332     }
333
334     public String JavaDoc toString() {
335         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 128 );
336         sb.append( "[ " );
337         sb.append( "recording( " + isRecording() + " )" );
338         sb.append( ", recordSession( " + getRecordingSession() + " )" );
339         sb.append( ", playbackMap( " + Util.toString( playbackMap ) + " )" );
340         sb.append( " ]" );
341         return sb.toString();
342     }
343 }
344
Popular Tags