KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > util > JonasAdminFiles


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JonasAdminFiles.java,v 1.5 2005/07/22 07:25:14 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jonasadmin.test.util;
27
28 import java.io.BufferedReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * Class to use jonas files
36  * @author kemlerp
37  *
38  */

39 public class JonasAdminFiles {
40
41     /**
42      * Get "server.xml" created files by the test
43      * @return The table of created files
44      */

45     private static Vector JavaDoc getCreatedFiles(String JavaDoc beginTime, String JavaDoc endTime) {
46         Vector JavaDoc createdFiles = new Vector JavaDoc();
47
48         // Get JONAS_BASE path
49
String JavaDoc jonasBase = System.getProperty("jonas.base");
50
51         // Get JONAS_BASE/conf path
52
String JavaDoc conf = jonasBase + File.separator + "conf";
53
54         // Get JONAS_BASE/conf directory
55
File JavaDoc directory = new File JavaDoc(conf);
56
57         // Get JONAS_BASE conf files
58
File JavaDoc[] files = directory.listFiles();
59
60         // First name of file to compare
61
String JavaDoc beginName = "server.xml." + beginTime;
62
63         // End name of file to compare
64
String JavaDoc endName = "server.xml." + endTime;
65
66         // Get the created files by the test
67
for (int i = 0; i < files.length; i++) {
68             if (files[i].getName().compareTo(beginName) >= 0) {
69                 if (files[i].getName().compareTo(endName) <= 0) {
70                     createdFiles.add(files[i]);
71                 }
72             }
73         }
74         return createdFiles;
75     }
76
77     /**
78      * Delete files
79      * @param beginTime begin time of the test
80      * @param endTime end time of the test
81      */

82     public static void deleteCreatedFiles(String JavaDoc beginTime, String JavaDoc endTime) {
83         Vector JavaDoc files = getCreatedFiles(beginTime, endTime);
84
85         for (int i = 0; i < files.size(); i++) {
86             // Delete file
87
((File JavaDoc) files.get(i)).delete();
88         }
89     }
90
91     /**
92      * Replace server.xml by previous configuration file and remove created files
93      * @param beginTime begin time of the test
94      * @param endTime end time of the test
95      */

96     public static void recoverServerConf(String JavaDoc beginTime, String JavaDoc endTime) {
97         Vector JavaDoc files = getCreatedFiles(beginTime, endTime);
98
99         if (files.size() > 0) {
100             // Get server.xml file
101
File JavaDoc serverConf = getServerXmlFile();
102             // Get the older copied configuration file
103
File JavaDoc copiedFile = (File JavaDoc) files.get(0);
104
105             // Rename previous configuration file to "server.xml"
106
if (serverConf.canWrite()) {
107                 copiedFile.renameTo(serverConf);
108             }
109
110             // Delete other created files
111
for (int i = 0; i < files.size(); i++) {
112                 ((File JavaDoc) files.get(i)).delete();
113             }
114         }
115     }
116
117
118     /**
119      * Get server.xml file
120      * @return File server.xml
121      */

122     public static File JavaDoc getServerXmlFile() {
123         // Get JONAS_BASE path
124
String JavaDoc jonasBase = System.getProperty("jonas.base");
125
126         // Get JONAS_BASE/conf/server.xml path
127
String JavaDoc serverConfPath = jonasBase + File.separator + "conf" + File.separator + "server.xml";
128
129         // Get server.xml file
130
return new File JavaDoc(serverConfPath);
131     }
132
133     /**
134      * Get previous server.xml file
135      * @param beginTime begin time of the test
136      * @param endTime end time of the test
137      * @return File
138      */

139     public static File JavaDoc getPreviousServerXmlFile(String JavaDoc beginTime, String JavaDoc endTime) {
140         Vector JavaDoc files = getCreatedFiles(beginTime, endTime);
141         return (File JavaDoc) files.get(files.size() - 1);
142     }
143
144     /**
145      * Get local joram server port
146      * @param fileName name of the administration task file: joram-admin.cfg (in JONAS_BASE/conf directory)
147      * @return port number
148      * @throws IOException if an error occurs while file is read
149      */

150     public static String JavaDoc getJoramServerPort(String JavaDoc fileName) throws IOException JavaDoc {
151         String JavaDoc port = null;
152
153         // Get JONAS_BASE path
154
String JavaDoc jonasBase = System.getProperty("jonas.base");
155
156         // Get JONAS_BASE/conf/server.xml path
157
String JavaDoc serverConfPath = jonasBase + File.separator + "conf" + File.separator + fileName;
158
159         // Get server.xml file
160
File JavaDoc file = new File JavaDoc(serverConfPath);
161
162         // Transform file into string
163
BufferedReader JavaDoc read = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
164         String JavaDoc line = read.readLine();
165         while (line != null) {
166             if (line.startsWith("Port")) {
167                 // Get port
168
port = line.substring("Port\t".length());
169             }
170             line = read.readLine();
171         }
172         read.close();
173
174         return port;
175     }
176
177     /**
178      * Get local joram server host name
179      * @param fileName name of the administration task file: joram-admin.cfg (in JONAS_BASE/conf directory)
180      * @return host name
181      * @throws IOException if an error occurs while file is read
182      */

183     public static String JavaDoc getJoramHostName(String JavaDoc fileName) throws IOException JavaDoc {
184         String JavaDoc hostName = null;
185
186         // Get JONAS_BASE path
187
String JavaDoc jonasBase = System.getProperty("jonas.base");
188
189         // Get JONAS_BASE/conf/server.xml path
190
String JavaDoc serverConfPath = jonasBase + File.separator + "conf" + File.separator + fileName;
191
192         // Get server.xml file
193
File JavaDoc file = new File JavaDoc(serverConfPath);
194
195         // Transform file into string
196
BufferedReader JavaDoc read = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
197         String JavaDoc line = read.readLine();
198         while (line != null) {
199             if (line.startsWith("Host")) {
200                 // Get port
201
hostName = line.substring("Host\t".length());
202             }
203             line = read.readLine();
204         }
205         read.close();
206
207         return hostName;
208     }
209 }
210
Popular Tags