1 18 19 package org.apache.beehive.netui.tools.testrecorder.server.state; 20 21 import java.io.File ; 22 23 import org.apache.beehive.netui.tools.testrecorder.shared.RequestData; 24 import org.apache.beehive.netui.tools.testrecorder.shared.ResponseData; 25 import org.apache.beehive.netui.tools.testrecorder.shared.Logger; 26 import org.apache.beehive.netui.tools.testrecorder.shared.RecordSessionBean; 27 import org.apache.beehive.netui.tools.testrecorder.shared.util.DateHelper; 28 import org.apache.beehive.netui.tools.testrecorder.shared.xmlbeans.XMLHelper; 29 30 31 class RecordSessionImpl extends SessionImpl implements RecordSession { 32 33 private static final Logger log = Logger.getInstance( RecordSessionImpl.class ); 34 35 private File recordFile; 36 private RecordSessionBean sessionBean; 37 38 RecordSessionImpl( String sessionName, File recordFile, boolean overwrite, 39 String testUser, String description ) throws SessionFailedException { 40 super( new RecordSessionBean( sessionName ) ); 41 sessionBean = (RecordSessionBean) getSessionBean(); 42 setTestUser( testUser ); 43 setDescription( description ); 44 this.recordFile = recordFile; 45 if ( getSessionName() == null || getSessionName().trim().length() <= 0 ) { 46 String msg = "the session name supplied( " + 47 getSessionName() + " ) is invalid, it must be a non-null string of non-zero length"; 48 error( msg ); 49 } 50 if ( recordFile == null ) { 51 String msg = "the record file may not be null"; 52 error( msg ); 53 } 54 if ( recordFile.exists() ) { 55 if ( overwrite == false ) { 56 String msg = "ERROR: failed to create recording session, file( " + 57 recordFile.getAbsolutePath() + " ) exists and overwrite is false"; 58 error( msg ); 59 } 60 else if ( !recordFile.canWrite() ) { 61 String msg = "ERROR: failed to create recording session, file( " + 63 recordFile.getAbsolutePath() + " ) exists but is not writable"; 64 error( msg ); 65 } 66 else { 67 try { 69 recordFile.delete(); 70 } 71 catch ( Exception ex ) { 72 String msg = "unable to delete file( " + recordFile.getAbsolutePath() + " )"; 73 error( msg, ex ); 74 } 75 } 76 } 77 try { 79 if ( !recordFile.createNewFile() ) { 80 String msg = "unable to create new record file( " + recordFile.getAbsolutePath() + 81 " ) for session( " + getSessionName() + " )"; 82 error( msg ); 83 } 84 } 85 catch ( Exception ex ) { 86 String msg = "unable to create new record file( " + 87 recordFile.getAbsolutePath() + " ) for session( " + getSessionName() + " )"; 88 error( msg, ex ); 89 } 90 setSessionState( BEGIN ); 91 } 92 93 public synchronized boolean sessionStart() throws SessionFailedException { 94 boolean rtnVal = false; 95 if ( getSessionState() != BEGIN ) { 96 rtnVal = false; 97 } 98 else { 99 setStartDate( DateHelper.getCalendarInstance() ); 100 setSessionState( RECORD ); 101 rtnVal = true; 102 } 103 return rtnVal; 104 } 105 106 public synchronized boolean startTest() throws SessionFailedException { 107 boolean rtnVal = false; 108 if ( getSessionState() == RECORD ) { 109 incrementInProgress(); 110 rtnVal = true; 111 } 112 else if ( getSessionState() == BEGIN ) { 113 String msg = "Unable to start test, session must be started before starting a test"; 114 error( msg ); 115 } 116 else if ( getSessionState() == END ) { 117 String msg = "Unable to start test, session is finished, no new tests may be started"; 118 error( msg ); 119 } 120 else if ( getSessionState() >= ERROR ) { 121 String msg = "Unable to start test, Session has encountered a previous unrecoverable error"; 122 error( msg ); 123 } 124 else { 125 } 127 return rtnVal; 128 } 129 130 public synchronized int endTest( RequestData request, ResponseData response ) throws SessionFailedException { 132 if ( getSessionState() >= ERROR ) { 133 String msg = "Unable to end test, Session has encountered a previous unrecoverable error"; 134 error( msg, null, true ); 135 } 136 if ( getSessionState() == BEGIN ) { 137 String msg = "Session must be started before using test operations"; 138 error( msg ); 139 } 140 else if ( getSessionState() == END ) { 141 String msg = "Session is finished, no test operations may be performed"; 142 error( msg ); 143 } 144 if ( inProgressCnt() == 0 ) { 145 String msg = "No session in progress to end"; 147 error( msg ); 148 } 149 else if ( inProgressCnt() != 1 ) { 150 String msg = "inProgress( " + inProgressCnt() + " ) is invalid"; 152 error( msg, null, true ); 153 } 154 if ( request == null ) { 156 String msg = "request data may not be null, test count(" + ( testCount() + 1 ) + ")"; 158 error( msg, null, true ); 159 } 160 else if ( response == null ) { 161 String msg = "response data may not be null, test count(" + ( testCount() + 1 ) + ")"; 163 error( msg, null, true ); 164 } 165 decrementInProgress(); 166 sessionBean.addRequestResponseData( request, response ); 167 checkSessionComplete(); 169 if ( log.isDebugEnabled() ) { 170 log.debug( "testCount( " + testCount() + " )" ); 171 } 172 return testCount(); 173 } 174 175 protected synchronized void closeSessionInternal() throws SessionFailedException { 176 try { 177 setEndDate( DateHelper.getCalendarInstance() ); 178 XMLHelper.createRecordFile( getRecordFile(), sessionBean ); 179 } 180 catch ( Exception e ) { 181 setSessionState( ERROR_END ); 182 String msg = "ERROR: failed to create record XML file( " + getRecordFile().getAbsolutePath() + 183 " ), exception( " + e.getMessage() + " )"; 184 log.error( msg, e ); 185 throw new SessionFailedException( msg, e ); 186 } 187 } 188 189 public File getRecordFile() { 190 return recordFile; 191 } 192 193 public int testCount() { 194 return sessionBean.getTestCount(); 195 } 196 197 public String toString() { 198 StringBuffer sb = new StringBuffer ( 128 ); 199 sb.append( "[ " ); 200 sb.append( super.toString() ); 201 sb.append( ", recordFile( " + recordFile + " )" ); 202 sb.append( " ]" ); 203 return sb.toString(); 204 } 205 206 } 207 | Popular Tags |