001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JonasAdminFiles.java 7258 2005-08-18 09:19:03Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.util;
025:
026: import java.io.BufferedReader;
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.io.FileReader;
030: import java.io.IOException;
031: import java.util.Properties;
032: import java.util.Vector;
033:
034: import org.apache.tools.ant.BuildException;
035:
036: /**
037: * Class to use jonas files
038: * @author kemlerp
039: *
040: */
041: public class JonasAdminFiles {
042:
043: /**
044: * Get "server.xml" created files by the test
045: * @return The table of created files
046: */
047: private static Vector getCreatedFiles(String beginTime,
048: String endTime) {
049: Vector createdFiles = new Vector();
050:
051: // Get JONAS_BASE path
052: String jonasBase = System.getProperty("jonas.base");
053:
054: // Get JONAS_BASE/conf path
055: String conf = jonasBase + File.separator + "conf";
056:
057: // Get JONAS_BASE/conf directory
058: File directory = new File(conf);
059:
060: // Get JONAS_BASE conf files
061: File[] files = directory.listFiles();
062:
063: // First name of file to compare
064: String beginName = "server.xml." + beginTime;
065:
066: // End name of file to compare
067: String endName = "server.xml." + endTime;
068:
069: // Get the created files by the test
070: for (int i = 0; i < files.length; i++) {
071: if (files[i].getName().compareTo(beginName) >= 0) {
072: if (files[i].getName().compareTo(endName) <= 0) {
073: createdFiles.add(files[i]);
074: }
075: }
076: }
077: return createdFiles;
078: }
079:
080: /**
081: * Delete files
082: * @param beginTime begin time of the test
083: * @param endTime end time of the test
084: */
085: public static void deleteCreatedFiles(String beginTime,
086: String endTime) {
087: Vector files = getCreatedFiles(beginTime, endTime);
088:
089: for (int i = 0; i < files.size(); i++) {
090: // Delete file
091: ((File) files.get(i)).delete();
092: }
093: }
094:
095: /**
096: * Delete file in $JONAS_BASE/conf directory
097: * @param fileName name of the file
098: */
099: public static void deleteConfigFile(String fileName) {
100:
101: // Get JONAS_BASE path
102: String jonasBase = System.getProperty("jonas.base");
103:
104: // Get JONAS_BASE/conf/... path
105: String serverConfPath = jonasBase + File.separator + "conf"
106: + File.separator + fileName;
107:
108: // Get file
109: File file = new File(serverConfPath);
110:
111: // delete file
112: file.delete();
113: }
114:
115: /**
116: * Replace server.xml by previous configuration file and remove created files
117: * @param beginTime begin time of the test
118: * @param endTime end time of the test
119: */
120: public static void recoverServerConf(String beginTime,
121: String endTime) {
122: Vector files = getCreatedFiles(beginTime, endTime);
123:
124: if (files.size() > 0) {
125: // Get server.xml file
126: File serverConf = getServerXmlFile();
127: // Get the older copied configuration file
128: File copiedFile = (File) files.get(0);
129:
130: // Rename previous configuration file to "server.xml"
131: if (serverConf.canWrite()) {
132: copiedFile.renameTo(serverConf);
133: }
134:
135: // Delete other created files
136: for (int i = 0; i < files.size(); i++) {
137: ((File) files.get(i)).delete();
138: }
139: }
140: }
141:
142: /**
143: * Get server.xml file
144: * @return File server.xml
145: */
146: public static File getServerXmlFile() {
147: // Get JONAS_BASE path
148: String jonasBase = System.getProperty("jonas.base");
149:
150: // Get JONAS_BASE/conf/server.xml path
151: String serverConfPath = jonasBase + File.separator + "conf"
152: + File.separator + "server.xml";
153:
154: // Get server.xml file
155: return new File(serverConfPath);
156: }
157:
158: /**
159: * Get previous server.xml file
160: * @param beginTime begin time of the test
161: * @param endTime end time of the test
162: * @return File
163: */
164: public static File getPreviousServerXmlFile(String beginTime,
165: String endTime) {
166: Vector files = getCreatedFiles(beginTime, endTime);
167: return (File) files.get(files.size() - 1);
168: }
169:
170: /**
171: * Get local joram server port
172: * @param fileName name of the administration task file: joram-admin.cfg (in JONAS_BASE/conf directory)
173: * @return port number
174: * @throws IOException if an error occurs while file is read
175: */
176: public static String getJoramServerPort(String fileName)
177: throws IOException {
178: String port = null;
179:
180: // Get JONAS_BASE path
181: String jonasBase = System.getProperty("jonas.base");
182:
183: // Get JONAS_BASE/conf/server.xml path
184: String serverConfPath = jonasBase + File.separator + "conf"
185: + File.separator + fileName;
186:
187: // Get server.xml file
188: File file = new File(serverConfPath);
189:
190: // Transform file into string
191: BufferedReader read = new BufferedReader(new FileReader(file));
192: String line = read.readLine();
193: while (line != null) {
194: if (line.startsWith("Port")) {
195: // Get port
196: port = line.substring("Port\t".length());
197: }
198: line = read.readLine();
199: }
200: read.close();
201:
202: return port;
203: }
204:
205: /**
206: * Get local joram server host name
207: * @param fileName name of the administration task file: joram-admin.cfg (in JONAS_BASE/conf directory)
208: * @return host name
209: * @throws IOException if an error occurs while file is read
210: */
211: public static String getJoramHostName(String fileName)
212: throws IOException {
213: String hostName = null;
214:
215: // Get JONAS_BASE path
216: String jonasBase = System.getProperty("jonas.base");
217:
218: // Get JONAS_BASE/conf/server.xml path
219: String serverConfPath = jonasBase + File.separator + "conf"
220: + File.separator + fileName;
221:
222: // Get server.xml file
223: File file = new File(serverConfPath);
224:
225: // Transform file into string
226: BufferedReader read = new BufferedReader(new FileReader(file));
227: String line = read.readLine();
228: while (line != null) {
229: if (line.startsWith("Host")) {
230: // Get port
231: hostName = line.substring("Host\t".length());
232: }
233: line = read.readLine();
234: }
235: read.close();
236:
237: return hostName;
238: }
239:
240: public static void writeConfigFile(String fileName,
241: Properties properties) throws IOException {
242:
243: // Get JONAS_BASE path
244: String jonasBase = System.getProperty("jonas.base");
245:
246: // Get JONAS_BASE/conf/... path
247: String serverConfPath = jonasBase + File.separator + "conf"
248: + File.separator + fileName;
249:
250: // Get file
251: File file = new File(serverConfPath);
252:
253: // Write the properties file back out
254: FileOutputStream output = null;
255:
256: try {
257: output = new FileOutputStream(file);
258:
259: final String header = "File was created by jonasAdmin test suite.";
260:
261: properties.store(output, header);
262: } catch (final IOException ioe) {
263: final String message = "Error while writing " + file;
264: throw new BuildException(message, ioe);
265: }
266:
267: }
268: }
|