1 25 26 package org.objectweb.jonas.jonasadmin.test.logging; 27 28 import java.io.IOException ; 29 30 import org.xml.sax.SAXException ; 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 43 public class LoggerUtils { 44 45 48 protected static final String FRAME_CONTENT = "content"; 49 50 53 protected static final String FRAME_TREE = "tree"; 54 55 58 private static final String URL_JONASADMIN_LOGGING = "ListLoggers.do"; 59 60 63 private static final String URL_JONASADMIN_CREATE_LOGGER = "EditCatalinaAccessLogger.do?action=create"; 64 65 68 private static final String URL_JONASADMIN_MODIFY_ENGINE_LOGGER = "EditCatalinaAccessLogger.do?select=jonas%3Atype%3DValve%2Cname%3DAccessLogValve"; 69 70 73 private static final String URL_JONASADMIN_MODIFY_HOST_LOGGER = "EditCatalinaAccessLogger.do?select=jonas%3Atype%3DValve%2Cname%3DAccessLogValve%2Chost"; 74 75 82 public static WebResponse getLoggerPage(WebConversation wc) throws SAXException , IOException { 83 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 ("The link " + URL_JONASADMIN_LOGGING + " is not found. "); 94 } 95 96 wr = wc.getFrameContents(FRAME_CONTENT); 97 return wr; 98 } 99 100 105 public static void createNewEngineLogger(WebConversation wc) throws Exception { 106 createNewLogger(wc, null, null); 107 } 108 109 114 public static void createNewHostLogger(WebConversation wc) throws Exception { 115 createNewLogger(wc, "Host", "localhost"); 116 } 117 118 123 public static void createNewLogger(WebConversation wc, String containerType, String containerName) throws Exception { 124 125 WebResponse wr = null; 126 WebLink link = null; 127 WebForm form = null; 128 129 String objectName; 130 String directory; 131 String action; 132 String save; 133 String resolveHosts; 134 String rotatable; 135 String suffix; 136 String prefix; 137 String pattern; 138 139 wr = getLoggerPage(wc); 140 141 if (wr.getText().indexOf("AccessLogValve,seq") == -1) { 143 link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_CREATE_LOGGER); 145 if (link != null) { 146 link.click(); 147 } else { 148 throw new IllegalThreadStateException ("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 190 public static void createNewLogger(WebForm form, String objectName, String directory, String action, String save, 191 String resolveHosts, String rotatable, String suffix, String prefix, String pattern, String containerName, 192 String containerType) throws Exception { 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 218 public static void deleteLogger(WebConversation wc, String host) throws Exception { 219 WebResponse wr = null; 220 WebForm form = null; 221 WebLink link = null; 222 SubmitButton button = null; 223 224 String action = ""; 225 226 wr = getLoggerPage(wc); 227 228 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 link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "remove"); 239 if (link != null) { 240 link.click(); 241 } else { 242 throw new IllegalThreadStateException ("The link " + "remove" + " is not found. "); 243 } 244 wr = wc.getFrameContents(FRAME_CONTENT); 245 246 form = wr.getForms()[0]; 248 button = form.getSubmitButton("btnSubmit"); 249 button.click(); 250 251 } 252 253 265 public static void modifyLogger(WebConversation wc, String host, String directory, String resolveHosts, 266 String rotatable, String suffix, String prefix, String pattern) throws Exception { 267 268 WebResponse wr = null; 269 WebForm form = null; 270 WebLink link = null; 271 SubmitButton button = null; 272 273 String objectName = null; 274 String action = null; 275 String save = null; 276 277 wr = getLoggerPage(wc); 278 279 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 ("The link " + URL_JONASADMIN_MODIFY_ENGINE_LOGGER 288 + " is not found. "); 289 } 290 wr = wc.getFrameContents(FRAME_CONTENT); 291 } else { 292 throw new IllegalThreadStateException ("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 ("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 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 |