KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > config > WebappConfig


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.shared.config;
20
21 import org.apache.beehive.netui.tools.testrecorder.shared.util.StringHelper;
22 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
23
24 import java.io.File JavaDoc;
25
26 /**
27  * User: ozzy
28  * Date: Apr 12, 2004
29  * Time: 5:35:51 PM
30  */

31 public class WebappConfig {
32
33     private static final Logger log = Logger.getInstance( WebappConfig.class );
34
35     private String JavaDoc name;
36     private String JavaDoc description;
37     private boolean testMode;
38     private ServerConfig server;
39     // includes leading slash
40
private String JavaDoc contextRoot;
41     private File JavaDoc testDirectory;
42     private File JavaDoc resultsDirectory;
43     private Config config;
44
45     public WebappConfig( String JavaDoc name, String JavaDoc description, ServerConfig server, boolean testMode,
46             String JavaDoc contextRoot, String JavaDoc testDirectory, Config config ) {
47         this.name = name;
48         this.description = description;
49         this.testMode = testMode;
50         this.server = server;
51         this.contextRoot = contextRoot;
52         this.testDirectory = new File JavaDoc( testDirectory );
53         this.resultsDirectory = new File JavaDoc( config.getBaseDirectory(), name + "-playback" );
54         this.config = config;
55     }
56
57     public String JavaDoc getName() {
58         return name;
59     }
60
61     public String JavaDoc getDescription() {
62         return description;
63     }
64
65     public ServerConfig getServer() {
66         return server;
67     }
68
69     public boolean isTestMode() {
70         return testMode;
71     }
72
73     public String JavaDoc getContextRoot() {
74         return contextRoot;
75     }
76
77     public String JavaDoc getServletURI() {
78         return getContextRoot() + "/" + getConfig().getServletURI();
79     }
80
81     public String JavaDoc getTestDirectory() {
82         return testDirectory.getAbsolutePath();
83     }
84
85     /**
86      * @return true if the directory exists or was created successfully.
87      */

88     public boolean createTestDirectory() {
89         if ( testDirectory.exists() ) {
90             return true;
91         }
92         return testDirectory.mkdirs();
93     }
94
95     public String JavaDoc getResultsDirectory() {
96         return resultsDirectory.getAbsolutePath();
97     }
98
99     /**
100      * returns true only if all files could be deleted from the results directory, false otherwise
101      *
102      * @return
103      */

104     public boolean deleteResults() {
105         return deleteResults( false );
106     }
107
108     /**
109      * return value is true only if all files could be deleted from the results directory,
110      * false otherwise.
111      *
112      * @return
113      */

114     public boolean deleteResults( boolean removeDir ) {
115         boolean rtnVal = true;
116         if ( resultsDirectory.exists() ) {
117             File JavaDoc[] files = resultsDirectory.listFiles();
118             File JavaDoc file = null;
119             for ( int i = 0; i < files.length; i++ ) {
120                 file = files[i];
121                 if ( !file.delete() ) {
122                     if ( log.isWarnEnabled() ) {
123                         log.warn( "unable to delete results file( " + file.getAbsolutePath() + " )" );
124                     }
125                     rtnVal = false;
126                 }
127             }
128             if ( rtnVal && removeDir ) {
129                 if ( !resultsDirectory.delete() ) {
130                     if ( log.isWarnEnabled() ) {
131                         log.warn( "unable to delete( " + resultsDirectory.getAbsolutePath() + " )" );
132                     }
133                     rtnVal = false;
134                 }
135             }
136         }
137         return rtnVal;
138     }
139
140     /**
141      * @return true if the results directory exists immediately prior to returning. therefore,
142      * if the directory already exists this method will return true.
143      */

144     public boolean createResultsDirectory() {
145         boolean rtnVal = false;
146         resultsDirectory.mkdirs();
147         if ( resultsDirectory.exists() ) {
148             rtnVal = true;
149         }
150         return rtnVal;
151     }
152
153     public boolean handleSuffix( String JavaDoc suffix ) {
154         return getConfig().handleSuffix( suffix );
155     }
156
157     public Config getConfig() {
158         return config;
159     }
160
161     public String JavaDoc toString() {
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
163         sb.append( "[ " );
164         sb.append( "name( " + getName() + " )" );
165         sb.append( ", description( " + getDescription() + " )" );
166         sb.append( ", testMode( " + isTestMode() + " )" );
167         sb.append( ", server( " + getServer() + " )" );
168         sb.append( ", contextRoot( " + getContextRoot() + " )" );
169         sb.append( ", servletURI( " + getServletURI() + " )" );
170         sb.append( ", testDir( " + getTestDirectory() + " )" );
171         sb.append( ", resultsDir( " + getResultsDirectory() + " )" );
172         sb.append( ", suffixList( " + StringHelper.toString( getConfig().getSuffixes().iterator(), "\n", "\n\t" ) + " )" );
173         sb.append( " ]" );
174         return sb.toString();
175     }
176
177 }
178
Popular Tags