KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
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 JavaDoc recordFile;
36     private RecordSessionBean sessionBean;
37
38     RecordSessionImpl( String JavaDoc sessionName, File JavaDoc recordFile, boolean overwrite,
39             String JavaDoc testUser, String JavaDoc 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 JavaDoc 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 JavaDoc msg = "the record file may not be null";
52             error( msg );
53         }
54         if ( recordFile.exists() ) {
55             if ( overwrite == false ) {
56                 String JavaDoc 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                 // overwrite is true ... file is not writable.
62
String JavaDoc msg = "ERROR: failed to create recording session, file( " +
63                         recordFile.getAbsolutePath() + " ) exists but is not writable";
64                 error( msg );
65             }
66             else {
67                 // overwrite is true and file is writable.
68
try {
69                     recordFile.delete();
70                 }
71                 catch ( Exception JavaDoc ex ) {
72                     String JavaDoc msg = "unable to delete file( " + recordFile.getAbsolutePath() + " )";
73                     error( msg, ex );
74                 }
75             }
76         }
77         // file doesn't exist
78
try {
79             if ( !recordFile.createNewFile() ) {
80                 String JavaDoc msg = "unable to create new record file( " + recordFile.getAbsolutePath() +
81                         " ) for session( " + getSessionName() + " )";
82                 error( msg );
83             }
84         }
85         catch ( Exception JavaDoc ex ) {
86             String JavaDoc 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 JavaDoc msg = "Unable to start test, session must be started before starting a test";
114             error( msg );
115         }
116         else if ( getSessionState() == END ) {
117             String JavaDoc 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 JavaDoc msg = "Unable to start test, Session has encountered a previous unrecoverable error";
122             error( msg );
123         }
124         else {
125             // getSessionState() == STOP ... return false, no new tests
126
}
127         return rtnVal;
128     }
129
130     // returns the current test count
131
public synchronized int endTest( RequestData request, ResponseData response ) throws SessionFailedException {
132         if ( getSessionState() >= ERROR ) {
133             String JavaDoc msg = "Unable to end test, Session has encountered a previous unrecoverable error";
134             error( msg, null, true );
135         }
136         if ( getSessionState() == BEGIN ) {
137             String JavaDoc msg = "Session must be started before using test operations";
138             error( msg );
139         }
140         else if ( getSessionState() == END ) {
141             String JavaDoc msg = "Session is finished, no test operations may be performed";
142             error( msg );
143         }
144         if ( inProgressCnt() == 0 ) {
145             // no tests in progress to end.
146
String JavaDoc msg = "No session in progress to end";
147             error( msg );
148         }
149         else if ( inProgressCnt() != 1 ) {
150             // programming error, unrecoverable
151
String JavaDoc msg = "inProgress( " + inProgressCnt() + " ) is invalid";
152             error( msg, null, true );
153         }
154         // write to record file.
155
if ( request == null ) {
156             // unrecoverable, test data cannot be persisted
157
String JavaDoc msg = "request data may not be null, test count(" + ( testCount() + 1 ) + ")";
158             error( msg, null, true );
159         }
160         else if ( response == null ) {
161             // unrecoverable, test data cannot be persisted
162
String JavaDoc 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         // cleans up the session if getSessionState() == STOP and inProgressCnt() == 0
168
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 JavaDoc e ) {
181             setSessionState( ERROR_END );
182             String JavaDoc 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 JavaDoc getRecordFile() {
190         return recordFile;
191     }
192
193     public int testCount() {
194         return sessionBean.getTestCount();
195     }
196
197     public String JavaDoc toString() {
198         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 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