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: LoggerUtils.java 7041 2005-07-12 08:16:52Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.logging;
025:
026: import java.io.IOException;
027:
028: import org.xml.sax.SAXException;
029:
030: import com.meterware.httpunit.SubmitButton;
031: import com.meterware.httpunit.WebConversation;
032: import com.meterware.httpunit.WebForm;
033: import com.meterware.httpunit.WebLink;
034: import com.meterware.httpunit.WebResponse;
035:
036: /**
037: * Class to use logging pages
038: * @author Paul Kemler
039: *
040: */
041: public class LoggerUtils {
042:
043: /**
044: * NAME of FRAME content
045: */
046: protected static final String FRAME_CONTENT = "content";
047:
048: /**
049: * NAME of FRAME tree
050: */
051: protected static final String FRAME_TREE = "tree";
052:
053: /**
054: * URL of the logger list
055: */
056: private static final String URL_JONASADMIN_LOGGING = "ListLoggers.do";
057:
058: /**
059: * URL for creating a logger
060: */
061: private static final String URL_JONASADMIN_CREATE_LOGGER = "EditCatalinaAccessLogger.do?action=create";
062:
063: /**
064: * URL for modifying an engine logger
065: */
066: private static final String URL_JONASADMIN_MODIFY_ENGINE_LOGGER = "EditCatalinaAccessLogger.do?select=jonas%3Atype%3DValve%2Cname%3DAccessLogValve";
067:
068: /**
069: * URL for modifying a host logger
070: */
071: private static final String URL_JONASADMIN_MODIFY_HOST_LOGGER = "EditCatalinaAccessLogger.do?select=jonas%3Atype%3DValve%2Cname%3DAccessLogValve%2Chost";
072:
073: /**
074: * Get logger page
075: * @param wc logged to jonasAdmin
076: * @return content frame of logger page
077: * @throws SAXException if an error occurs when link is matched or clicked
078: * @throws IOException if an error occurs when link is clicked
079: */
080: public static WebResponse getLoggerPage(WebConversation wc)
081: throws SAXException, IOException {
082: // Go to logger page (ListLoggers.do)
083: WebResponse wr;
084: WebLink link;
085:
086: wr = wc.getFrameContents(FRAME_TREE);
087:
088: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
089: URL_JONASADMIN_LOGGING);
090: if (link != null) {
091: link.click();
092: } else {
093: throw new IllegalThreadStateException("The link "
094: + URL_JONASADMIN_LOGGING + " is not found. ");
095: }
096:
097: wr = wc.getFrameContents(FRAME_CONTENT);
098: return wr;
099: }
100:
101: /**
102: * Create a new engine logger
103: * @param wc logged to jonasAdmin
104: * @throws Exception if an error occurs
105: */
106: public static void createNewEngineLogger(WebConversation wc)
107: throws Exception {
108: createNewLogger(wc, null, null);
109: }
110:
111: /**
112: * Create a new host logger: container type = Host and container name = localhost
113: * @param wc logged to jonasAdmin
114: * @throws Exception if an error occurs
115: */
116: public static void createNewHostLogger(WebConversation wc)
117: throws Exception {
118: createNewLogger(wc, "Host", "localhost");
119: }
120:
121: /**
122: * Create a new logger
123: * @param wc logged to jonasAdmin
124: * @throws Exception if an error occurs
125: */
126: public static void createNewLogger(WebConversation wc,
127: String containerType, String containerName)
128: throws Exception {
129:
130: WebResponse wr = null;
131: WebLink link = null;
132: WebForm form = null;
133:
134: String objectName;
135: String directory;
136: String action;
137: String save;
138: String resolveHosts;
139: String rotatable;
140: String suffix;
141: String prefix;
142: String pattern;
143:
144: wr = getLoggerPage(wc);
145:
146: // Verify if 'Access log valve engine' has yet been created
147: if (wr.getText().indexOf("AccessLogValve,seq") == -1) {
148: // Go to EditCatalinaAccessLogger.do?action=create
149: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
150: URL_JONASADMIN_CREATE_LOGGER);
151: if (link != null) {
152: link.click();
153: } else {
154: throw new IllegalThreadStateException("The link "
155: + URL_JONASADMIN_CREATE_LOGGER
156: + " is not found. ");
157: }
158: wr = wc.getFrameContents(FRAME_CONTENT);
159:
160: form = wr.getForms()[0];
161:
162: objectName = form.getParameterValue("objectName");
163: directory = form.getParameterValue("directory");
164: action = form.getParameterValue("action");
165: save = form.getParameterValue("save");
166: resolveHosts = form.getParameterValue("resolveHosts");
167: rotatable = form.getParameterValue("rotatable");
168: suffix = form.getParameterValue("suffix");
169: prefix = form.getParameterValue("prefix");
170: pattern = form.getParameterValue("pattern");
171: if (containerType == null) {
172: containerName = form.getParameterValue("containerName");
173: containerType = form.getParameterValue("containerType");
174: }
175:
176: createNewLogger(form, objectName, directory, action, save,
177: resolveHosts, rotatable, suffix, prefix, pattern,
178: containerName, containerType);
179:
180: }
181: }
182:
183: /**
184: * Create a new logger
185: * @param form Web Form to create logger
186: * @param objectName a string
187: * @param directory a string
188: * @param action a string
189: * @param save a string
190: * @param resolveHosts a string
191: * @param rotatable a string
192: * @param suffix a string
193: * @param prefix a string
194: * @param pattern a string
195: * @param containerName a string
196: * @param containerType "Engine" or "Host"
197: * @throws Exception if an error occurs when the button is clicked
198: */
199: public static void createNewLogger(WebForm form, String objectName,
200: String directory, String action, String save,
201: String resolveHosts, String rotatable, String suffix,
202: String prefix, String pattern, String containerName,
203: String containerType) throws Exception {
204:
205: SubmitButton button = null;
206:
207: form.setParameter("objectName", objectName);
208: form.setParameter("directory", directory);
209: form.setParameter("action", action);
210: form.setParameter("save", save);
211: form.setParameter("resolveHosts", resolveHosts);
212: form.setParameter("rotatable", rotatable);
213: form.setParameter("suffix", suffix);
214: form.setParameter("prefix", prefix);
215: form.setParameter("pattern", pattern);
216: form.setParameter("containerName", containerName);
217: form.setParameter("containerType", containerType);
218:
219: button = form.getSubmitButton("btn_apply");
220: button.click();
221: }
222:
223: /**
224: * Delete logger
225: * @param wc logged to jonasAdmin
226: * @param host
227: * @throws Exception if an error occurs
228: */
229: public static void deleteLogger(WebConversation wc, String host)
230: throws Exception {
231: WebResponse wr = null;
232: WebForm form = null;
233: WebLink link = null;
234: SubmitButton button = null;
235:
236: String action = "";
237:
238: wr = getLoggerPage(wc);
239:
240: // Select logger to remove
241: form = wr.getForms()[0];
242: action = form.getParameterValue("action");
243: if (host == null) {
244: form.setCheckbox("selectedItemsArray",
245: "jonas:type=Valve,name=AccessLogValve", true);
246: } else {
247: form
248: .setCheckbox("selectedItemsArray",
249: "jonas:type=Valve,name=AccessLogValve,host="
250: + host, true);
251: }
252:
253: // Choose remove action
254: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
255: "remove");
256: if (link != null) {
257: link.click();
258: } else {
259: throw new IllegalThreadStateException("The link "
260: + "remove" + " is not found. ");
261: }
262: wr = wc.getFrameContents(FRAME_CONTENT);
263:
264: // Confirm
265: form = wr.getForms()[0];
266: button = form.getSubmitButton("btnSubmit");
267: button.click();
268:
269: }
270:
271: /**
272: * Modify a logger
273: * @param wc logged to jonasAdmin
274: * @param host null if container type is "engine" else container name
275: * @param directory a string
276: * @param resolveHosts "true" or "false"
277: * @param rotatable "true" or "false"
278: * @param suffix a string
279: * @param prefix a string
280: * @param pattern a string
281: * @throws Exception if an error occurs
282: */
283: public static void modifyLogger(WebConversation wc, String host,
284: String directory, String resolveHosts, String rotatable,
285: String suffix, String prefix, String pattern)
286: throws Exception {
287:
288: WebResponse wr = null;
289: WebForm form = null;
290: WebLink link = null;
291: SubmitButton button = null;
292:
293: String objectName = null;
294: String action = null;
295: String save = null;
296:
297: wr = getLoggerPage(wc);
298:
299: // Get form
300: if (host == null) {
301: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
302: URL_JONASADMIN_MODIFY_ENGINE_LOGGER);
303: if (link.getURLString().endsWith(
304: URL_JONASADMIN_MODIFY_ENGINE_LOGGER)
305: || link.getURLString().indexOf("%2Cseq%3D") != -1) {
306: if (link != null) {
307: link.click();
308: } else {
309: throw new IllegalThreadStateException("The link "
310: + URL_JONASADMIN_MODIFY_ENGINE_LOGGER
311: + " is not found. ");
312: }
313: wr = wc.getFrameContents(FRAME_CONTENT);
314: } else {
315: throw new IllegalThreadStateException("There is not "
316: + URL_JONASADMIN_MODIFY_ENGINE_LOGGER
317: + " link. ");
318: }
319: } else {
320: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
321: URL_JONASADMIN_MODIFY_HOST_LOGGER);
322: if (link != null) {
323: link.click();
324: } else {
325: throw new IllegalThreadStateException("The link "
326: + URL_JONASADMIN_MODIFY_HOST_LOGGER
327: + " is not found. ");
328: }
329:
330: wr = wc.getFrameContents(FRAME_CONTENT);
331: }
332: form = wr.getForms()[0];
333:
334: // get hidden params
335: objectName = form.getParameterValue("objectName");
336: action = form.getParameterValue("action");
337: save = form.getParameterValue("save");
338:
339: form.setParameter("objectName", objectName);
340: form.setParameter("action", action);
341: form.setParameter("save", save);
342: form.setParameter("directory", directory);
343: form.setParameter("resolveHosts", resolveHosts);
344: form.setParameter("rotatable", rotatable);
345: form.setParameter("suffix", suffix);
346: form.setParameter("prefix", prefix);
347: form.setParameter("pattern", pattern);
348:
349: button = form.getSubmitButton("btn_apply");
350: button.click();
351: }
352:
353: }
|