KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > logging > LoggerUtils


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: LoggerUtils.java,v 1.2 2005/07/12 08:16:52 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jonasadmin.test.logging;
27
28 import java.io.IOException JavaDoc;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32 import com.meterware.httpunit.SubmitButton;
33 import com.meterware.httpunit.WebConversation;
34 import com.meterware.httpunit.WebForm;
35 import com.meterware.httpunit.WebLink;
36 import com.meterware.httpunit.WebResponse;
37
38 /**
39  * Class to use logging pages
40  * @author Paul Kemler
41  *
42  */

43 public class LoggerUtils {
44
45     /**
46      * NAME of FRAME content
47      */

48     protected static final String JavaDoc FRAME_CONTENT = "content";
49
50     /**
51      * NAME of FRAME tree
52      */

53     protected static final String JavaDoc FRAME_TREE = "tree";
54
55     /**
56      * URL of the logger list
57      */

58     private static final String JavaDoc URL_JONASADMIN_LOGGING = "ListLoggers.do";
59
60     /**
61      * URL for creating a logger
62      */

63     private static final String JavaDoc URL_JONASADMIN_CREATE_LOGGER = "EditCatalinaAccessLogger.do?action=create";
64
65     /**
66      * URL for modifying an engine logger
67      */

68     private static final String JavaDoc URL_JONASADMIN_MODIFY_ENGINE_LOGGER = "EditCatalinaAccessLogger.do?select=jonas%3Atype%3DValve%2Cname%3DAccessLogValve";
69
70     /**
71      * URL for modifying a host logger
72      */

73     private static final String JavaDoc URL_JONASADMIN_MODIFY_HOST_LOGGER = "EditCatalinaAccessLogger.do?select=jonas%3Atype%3DValve%2Cname%3DAccessLogValve%2Chost";
74
75     /**
76      * Get logger page
77      * @param wc logged to jonasAdmin
78      * @return content frame of logger page
79      * @throws SAXException if an error occurs when link is matched or clicked
80      * @throws IOException if an error occurs when link is clicked
81      */

82     public static WebResponse getLoggerPage(WebConversation wc) throws SAXException JavaDoc, IOException JavaDoc {
83         // Go to logger page (ListLoggers.do)
84
WebResponse wr;
85         WebLink link;
86
87         wr = wc.getFrameContents(FRAME_TREE);
88
89         link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_LOGGING);
90         if (link != null) {
91             link.click();
92         } else {
93             throw new IllegalThreadStateException JavaDoc("The link " + URL_JONASADMIN_LOGGING + " is not found. ");
94         }
95
96         wr = wc.getFrameContents(FRAME_CONTENT);
97         return wr;
98     }
99
100     /**
101      * Create a new engine logger
102      * @param wc logged to jonasAdmin
103      * @throws Exception if an error occurs
104      */

105     public static void createNewEngineLogger(WebConversation wc) throws Exception JavaDoc {
106         createNewLogger(wc, null, null);
107     }
108
109     /**
110      * Create a new host logger: container type = Host and container name = localhost
111      * @param wc logged to jonasAdmin
112      * @throws Exception if an error occurs
113      */

114     public static void createNewHostLogger(WebConversation wc) throws Exception JavaDoc {
115         createNewLogger(wc, "Host", "localhost");
116     }
117
118     /**
119      * Create a new logger
120      * @param wc logged to jonasAdmin
121      * @throws Exception if an error occurs
122      */

123     public static void createNewLogger(WebConversation wc, String JavaDoc containerType, String JavaDoc containerName) throws Exception JavaDoc {
124
125         WebResponse wr = null;
126         WebLink link = null;
127         WebForm form = null;
128
129         String JavaDoc objectName;
130         String JavaDoc directory;
131         String JavaDoc action;
132         String JavaDoc save;
133         String JavaDoc resolveHosts;
134         String JavaDoc rotatable;
135         String JavaDoc suffix;
136         String JavaDoc prefix;
137         String JavaDoc pattern;
138
139         wr = getLoggerPage(wc);
140
141         // Verify if 'Access log valve engine' has yet been created
142
if (wr.getText().indexOf("AccessLogValve,seq") == -1) {
143             // Go to EditCatalinaAccessLogger.do?action=create
144
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_CREATE_LOGGER);
145             if (link != null) {
146                 link.click();
147             } else {
148                 throw new IllegalThreadStateException JavaDoc("The link " + URL_JONASADMIN_CREATE_LOGGER + " is not found. ");
149             }
150             wr = wc.getFrameContents(FRAME_CONTENT);
151
152             form = wr.getForms()[0];
153
154             objectName = form.getParameterValue("objectName");
155             directory = form.getParameterValue("directory");
156             action = form.getParameterValue("action");
157             save = form.getParameterValue("save");
158             resolveHosts = form.getParameterValue("resolveHosts");
159             rotatable = form.getParameterValue("rotatable");
160             suffix = form.getParameterValue("suffix");
161             prefix = form.getParameterValue("prefix");
162             pattern = form.getParameterValue("pattern");
163             if (containerType == null) {
164                 containerName = form.getParameterValue("containerName");
165                 containerType = form.getParameterValue("containerType");
166             }
167
168             createNewLogger(form, objectName, directory, action, save, resolveHosts, rotatable, suffix, prefix,
169                     pattern, containerName, containerType);
170
171         }
172     }
173
174     /**
175      * Create a new logger
176      * @param form Web Form to create logger
177      * @param objectName a string
178      * @param directory a string
179      * @param action a string
180      * @param save a string
181      * @param resolveHosts a string
182      * @param rotatable a string
183      * @param suffix a string
184      * @param prefix a string
185      * @param pattern a string
186      * @param containerName a string
187      * @param containerType "Engine" or "Host"
188      * @throws Exception if an error occurs when the button is clicked
189      */

190     public static void createNewLogger(WebForm form, String JavaDoc objectName, String JavaDoc directory, String JavaDoc action, String JavaDoc save,
191             String JavaDoc resolveHosts, String JavaDoc rotatable, String JavaDoc suffix, String JavaDoc prefix, String JavaDoc pattern, String JavaDoc containerName,
192             String JavaDoc containerType) throws Exception JavaDoc {
193
194         SubmitButton button = null;
195
196         form.setParameter("objectName", objectName);
197         form.setParameter("directory", directory);
198         form.setParameter("action", action);
199         form.setParameter("save", save);
200         form.setParameter("resolveHosts", resolveHosts);
201         form.setParameter("rotatable", rotatable);
202         form.setParameter("suffix", suffix);
203         form.setParameter("prefix", prefix);
204         form.setParameter("pattern", pattern);
205         form.setParameter("containerName", containerName);
206         form.setParameter("containerType", containerType);
207
208         button = form.getSubmitButton("btn_apply");
209         button.click();
210     }
211
212     /**
213      * Delete logger
214      * @param wc logged to jonasAdmin
215      * @param host
216      * @throws Exception if an error occurs
217      */

218     public static void deleteLogger(WebConversation wc, String JavaDoc host) throws Exception JavaDoc {
219         WebResponse wr = null;
220         WebForm form = null;
221         WebLink link = null;
222         SubmitButton button = null;
223
224         String JavaDoc action = "";
225
226         wr = getLoggerPage(wc);
227
228         // Select logger to remove
229
form = wr.getForms()[0];
230         action = form.getParameterValue("action");
231         if (host == null) {
232             form.setCheckbox("selectedItemsArray", "jonas:type=Valve,name=AccessLogValve", true);
233         } else {
234             form.setCheckbox("selectedItemsArray", "jonas:type=Valve,name=AccessLogValve,host=" + host, true);
235         }
236
237         // Choose remove action
238
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "remove");
239         if (link != null) {
240             link.click();
241         } else {
242             throw new IllegalThreadStateException JavaDoc("The link " + "remove" + " is not found. ");
243         }
244         wr = wc.getFrameContents(FRAME_CONTENT);
245
246         // Confirm
247
form = wr.getForms()[0];
248         button = form.getSubmitButton("btnSubmit");
249         button.click();
250
251     }
252
253     /**
254      * Modify a logger
255      * @param wc logged to jonasAdmin
256      * @param host null if container type is "engine" else container name
257      * @param directory a string
258      * @param resolveHosts "true" or "false"
259      * @param rotatable "true" or "false"
260      * @param suffix a string
261      * @param prefix a string
262      * @param pattern a string
263      * @throws Exception if an error occurs
264      */

265     public static void modifyLogger(WebConversation wc, String JavaDoc host, String JavaDoc directory, String JavaDoc resolveHosts,
266             String JavaDoc rotatable, String JavaDoc suffix, String JavaDoc prefix, String JavaDoc pattern) throws Exception JavaDoc {
267
268         WebResponse wr = null;
269         WebForm form = null;
270         WebLink link = null;
271         SubmitButton button = null;
272
273         String JavaDoc objectName = null;
274         String JavaDoc action = null;
275         String JavaDoc save = null;
276
277         wr = getLoggerPage(wc);
278
279         // Get form
280
if (host == null) {
281             link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_MODIFY_ENGINE_LOGGER);
282             if (link.getURLString().endsWith(URL_JONASADMIN_MODIFY_ENGINE_LOGGER)
283                     || link.getURLString().indexOf("%2Cseq%3D") != -1) {
284                 if (link != null) {
285                     link.click();
286                 } else {
287                     throw new IllegalThreadStateException JavaDoc("The link " + URL_JONASADMIN_MODIFY_ENGINE_LOGGER
288                             + " is not found. ");
289                 }
290                 wr = wc.getFrameContents(FRAME_CONTENT);
291             } else {
292                 throw new IllegalThreadStateException JavaDoc("There is not " + URL_JONASADMIN_MODIFY_ENGINE_LOGGER + " link. ");
293             }
294         } else {
295             link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_MODIFY_HOST_LOGGER);
296             if (link != null) {
297                 link.click();
298             } else {
299                 throw new IllegalThreadStateException JavaDoc("The link " + URL_JONASADMIN_MODIFY_HOST_LOGGER
300                         + " is not found. ");
301             }
302
303             wr = wc.getFrameContents(FRAME_CONTENT);
304         }
305         form = wr.getForms()[0];
306
307         // get hidden params
308
objectName = form.getParameterValue("objectName");
309         action = form.getParameterValue("action");
310         save = form.getParameterValue("save");
311
312         form.setParameter("objectName", objectName);
313         form.setParameter("action", action);
314         form.setParameter("save", save);
315         form.setParameter("directory", directory);
316         form.setParameter("resolveHosts", resolveHosts);
317         form.setParameter("rotatable", rotatable);
318         form.setParameter("suffix", suffix);
319         form.setParameter("prefix", prefix);
320         form.setParameter("pattern", pattern);
321
322         button = form.getSubmitButton("btn_apply");
323         button.click();
324     }
325
326 }
327
Popular Tags