1 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 ; 25 import java.util.HashMap ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.io.Serializable ; 29 import java.io.File ; 30 31 32 37 public class State implements Serializable { 38 39 private static final Logger log = Logger.getInstance( State.class ); 40 41 private boolean testMode; 42 private RecordSession recordSession = null; 44 private Map playbackMap; 47 48 49 public State() { 50 if ( log.isInfoEnabled() ) { 51 log.info( "Constructor" ); 52 } 53 playbackMap = new HashMap (); 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 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 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 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 try { 130 rtnVal = getRecordingSession(); 131 rtnVal.sessionEnd(); 132 } 133 catch ( Exception ex ) { 134 String 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 testId ) { 156 return (PlaybackSession) getInternalPlaybackMap().get( testId ); 157 } 158 159 public Map getPlaybackMap() { 160 if ( playbackMap.size() == 0 ) { 161 return null; 162 } 163 return Collections.unmodifiableMap( playbackMap ); 166 } 167 168 public boolean playbackSessionExists( PlaybackSession session ) { 169 return playbackSessionExists( session.getStringUID() ); 170 } 171 172 public boolean playbackSessionExists( String testId ) { 173 if ( getPlaybackMap() == null ) { 174 return false; 175 } 176 return getInternalPlaybackMap().containsKey( testId ); 177 } 178 179 public boolean playbackStart( PlaybackSession session ) throws SessionFailedException { 183 boolean rtnVal = false; 184 if ( session == null ) { 185 String 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 public PlaybackSession playbackStop( String testId ) throws SessionFailedException { 207 PlaybackSession rtnVal = getPlaybackSession( testId ); 208 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 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 it = Util.getListOfKeys( getInternalPlaybackMap() ).iterator(); 252 String key = null; 253 while ( it.hasNext() ) { 254 key = (String ) 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 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 sessionName, 275 File playbackFile, File recordFile, File diffFile ) 276 throws SessionFailedException { 277 return new PlaybackSessionImpl( sessionName, playbackFile, 278 recordFile, diffFile ); 279 } 280 281 public static RecordSession createRecordSession( String sessionName, 282 File recordFile, boolean overwrite, String testUser, 283 String 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 testId ) { 307 PlaybackSession rtnVal; 308 synchronized ( playbackMap ) { 309 if ( playbackSessionExists( testId ) ) { 310 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 getInternalPlaybackMap() { 331 return playbackMap; 332 } 333 334 public String toString() { 335 StringBuffer sb = new StringBuffer ( 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 |