1 18 19 package org.apache.beehive.netui.tools.testrecorder.server.state; 20 21 import java.util.Calendar ; 22 import java.util.Date ; 23 24 import org.apache.beehive.netui.tools.testrecorder.shared.Logger; 25 import org.apache.beehive.netui.tools.testrecorder.shared.SessionBean; 26 27 28 33 abstract class SessionImpl implements Session { 34 35 private static final Logger log = Logger.getInstance( SessionImpl.class ); 36 37 public static final long LOOP_TIMEOUT = 250; 39 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 public static final int ERROR_END = 6; 50 51 private SessionBean sessionBean; 53 54 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 public boolean isSessionStarted() { 67 boolean rtnVal = false; 68 if ( getSessionState() > BEGIN || getSessionState() < END ) { 69 rtnVal = true; 70 } 71 return rtnVal; 72 } 73 74 public boolean isSessionFinished() { 76 boolean rtnVal = false; 77 if ( getSessionState() >= END ) { 78 rtnVal = true; 79 } 80 return rtnVal; 81 } 82 83 public abstract boolean sessionStart() throws SessionFailedException; 85 86 public synchronized boolean sessionEnd() throws SessionFailedException { 87 return sessionEnd( LOOP_TIMEOUT, HARD_TIMEOUT ); 88 } 89 90 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 ().getTime(); 97 long eTime = sTime; 98 while ( !isSessionFinished() ) { 99 if ( log.isDebugEnabled() ) { 100 log.debug( "sessionEnd(): cnt(" + cnt + "), starting loop" ); 101 } 102 if ( ( eTime - sTime ) > hardTimeout ) { 104 float milliseconds = ( eTime - sTime ); 105 String 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 ex ) { 119 } 121 } 122 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 ().getTime(); 133 } 134 return true; 135 } 136 137 142 public abstract int testCount(); 143 144 public String getStartDateString() { 145 return sessionBean.getStartDateString(); 146 } 147 148 public String getEndDateString() { 149 return sessionBean.getEndDateString(); 150 } 151 152 public String getSessionName() { 153 return sessionBean.getSessionName(); 154 } 155 156 public String getTestUser() { 157 return sessionBean.getTester(); 158 } 159 160 public String 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 protected synchronized void incrementInProgress() { 174 inProgress++; 175 if ( log.isDebugEnabled() ) { 176 log.debug( "inProgress( " + inProgress + " )" ); 177 } 178 } 179 180 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 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 notifyAll(); 220 try { 223 closeSession(); 224 } 225 catch ( Exception e ) { 226 String 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 startDate ) { 235 sessionBean.setStartDate( startDate ); 236 } 237 238 protected void setEndDate( Calendar endDate ) { 239 sessionBean.setEndDate( endDate ); 240 } 241 242 protected void setTestUser( String testUser ) { 243 sessionBean.setTester( testUser ); 244 } 245 246 protected void setDescription( String description ) { 247 sessionBean.setDescription( description ); 248 } 249 250 protected void setSessionState( int sessionState ) { 251 this.sessionState = sessionState; 252 } 253 254 protected void error( String msg ) throws SessionFailedException { 255 error( msg, null ); 257 } 258 259 protected void error( String msg, Exception e ) throws SessionFailedException { 260 error( msg, e, false ); 262 } 263 264 protected void error( String msg, Exception 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 toString() { 282 StringBuffer sb = new StringBuffer ( 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 |