1 11 package org.jboss.portal.junit; 12 13 import org.apache.commons.httpclient.HostConfiguration; 14 import org.apache.commons.httpclient.HttpClient; 15 import org.apache.commons.httpclient.HttpState; 16 import org.apache.commons.httpclient.Header; 17 import org.apache.commons.httpclient.methods.GetMethod; 18 import org.apache.log4j.Logger; 19 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 20 import org.jboss.deployment.DeploymentException; 21 22 import javax.management.ObjectName ; 23 import javax.management.InstanceNotFoundException ; 24 import javax.management.MBeanException ; 25 import javax.management.ReflectionException ; 26 import javax.management.MalformedObjectNameException ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.net.MalformedURLException ; 31 32 import junit.framework.AssertionFailedError; 33 34 38 public class ClientSession 39 { 40 41 private static final Logger log = Logger.getLogger(ClientSession.class); 42 43 private final File testRoot; 44 private final HostConfiguration host; 45 private final HttpClient client; 46 private final String id; 47 private final RMIAdaptor adaptor; 48 49 private String appName; 50 private int count; 51 private boolean closed; 52 53 ClientSession(File testRoot, HostConfiguration host, HttpClient client, String id, RMIAdaptor adaptor) 54 { 55 this.testRoot = testRoot; 56 this.host = host; 57 this.client = client; 58 this.id = id; 59 this.adaptor = adaptor; 60 61 this.appName = null; 62 this.count = 0; 63 this.closed = false; 64 } 65 66 70 private ServerResponse doGet(String uri, Header[] headers) throws IllegalStateException , IOException 71 { 72 if (closed) 73 { 74 throw new IllegalStateException ("Session already closed"); 75 } 76 77 StringBuffer tmp = new StringBuffer ("GET[").append(uri); 79 for (int i = 0; i < headers.length; i++) 80 { 81 Header header = headers[i]; 82 tmp.append(',').append("header(").append(header.getName()).append('=').append(header.getValue()).append(')'); 83 } 84 tmp.append(']'); 85 log.info("Performing query : " + tmp); 86 87 GetMethod get = new GetMethod(uri); 89 90 for (int i = 0; i < headers.length; i++) 91 { 92 Header header = headers[i]; 93 get.addRequestHeader(header); 94 } 95 get.addRequestHeader(HeaderNames.REQUEST_COUNT, "" + count++); 96 get.addRequestHeader(HeaderNames.TEST_ID, id); 97 client.executeMethod(host, get); 98 ServerResponse resp = ServerResponseFactory.create(get); 99 get.releaseConnection(); 100 return resp; 101 } 102 103 107 public ServerResponse doGet(String uri, String [] componentNames) throws IOException , IllegalStateException 108 { 109 if (closed) 110 { 111 throw new IllegalStateException ("Session already closed"); 112 } 113 114 Header[] headers = new Header[1 + componentNames.length]; 116 headers[0] = new Header("appName", appName); 117 for (int i = 0; i < componentNames.length; i++) 118 { 119 String componentName = componentNames[i]; 120 headers[1 + i] = new Header("componentName", componentName); 121 } 122 return doGet(uri, headers); 123 } 124 125 129 public ServerResponse doGet(String uri) throws IOException , IllegalStateException 130 { 131 return doGet(uri, new Header[0]); 132 } 133 134 142 public void deploy(String appName) throws DeploymentException, IllegalArgumentException , IllegalStateException , IOException 143 { 144 if (closed) 145 { 146 throw new IllegalStateException ("Session already closed"); 147 } 148 if (appName == null) 149 { 150 throw new IllegalArgumentException ("Does not accept null argument"); 151 } 152 153 154 if (this.appName != null) 156 { 157 throw new IllegalStateException ("Cannot make 2 deployment at the same time"); 158 } 159 160 File f = new File (testRoot, appName + ".war"); 162 if (!f.exists()) 163 { 164 throw new IllegalArgumentException ("No such file to deploy " + f.getAbsolutePath()); 165 } 166 if (!f.isFile()) 167 { 168 throw new IllegalArgumentException ("File is not a plain file " + f.getAbsolutePath()); 169 } 170 171 try 173 { 174 adaptor.invoke( 175 new ObjectName ("portal:service=MainDeployerFacade"), 176 "deploy", 177 new Object []{f.toURL()}, 178 new String []{URL .class.getName()}); 179 } 180 catch (MBeanException e) 181 { 182 Exception te = e.getTargetException(); 183 if (te instanceof DeploymentException) 184 { 185 throw (DeploymentException)te; 186 } 187 else 188 { 189 AssertionFailedError error = new AssertionFailedError(); 190 error.initCause(e); 191 throw error; 192 } 193 } 194 catch (MalformedURLException e) 195 { 196 AssertionFailedError error = new AssertionFailedError(); 197 error.initCause(e); 198 throw error; 199 } 200 catch (ReflectionException e) 201 { 202 AssertionFailedError error = new AssertionFailedError(); 203 error.initCause(e); 204 throw error; 205 } 206 catch (InstanceNotFoundException e) 207 { 208 AssertionFailedError error = new AssertionFailedError(); 209 error.initCause(e); 210 throw error; 211 } 212 catch (MalformedObjectNameException e) 213 { 214 AssertionFailedError error = new AssertionFailedError(); 215 error.initCause(e); 216 throw error; 217 } 218 219 this.appName = appName; 221 } 222 223 228 public void undeploy() throws IllegalStateException 229 { 230 if (closed) 231 { 232 throw new IllegalStateException ("Session already closed"); 233 } 234 235 if (appName == null) 237 { 238 log.warn("Nothing to undeploy"); 239 } 240 241 File f = new File (testRoot, appName + ".war"); 243 this.appName = null; 244 245 try 247 { 248 adaptor.invoke( 249 new ObjectName ("portal:service=MainDeployerFacade"), 250 "undeploy", 251 new Object []{f.toURL()}, 252 new String []{URL .class.getName()}); 253 } 254 catch (Exception e) 255 { 256 log.warn("Unexpected exception when undeploying " + e); 257 } 258 } 259 260 263 public void close() throws IllegalStateException 264 { 265 if (closed) 266 { 267 throw new IllegalStateException ("Session already closed"); 268 } 269 270 if (appName != null) 272 { 273 try 274 { 275 undeploy(); 276 } 277 catch (Exception e) 278 { 279 log.warn("", e); 280 } 281 } 282 283 HttpState state = client.getState(); 284 state.clear(); 285 closed = true; 286 } 287 288 public boolean isClosed() 289 { 290 return closed; 291 } 292 } 293 | Popular Tags |