KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > junit > ClientSession


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

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 JavaDoc;
23 import javax.management.InstanceNotFoundException JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.ReflectionException JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31
32 import junit.framework.AssertionFailedError;
33
34 /**
35  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
36  * @version $Revision: 1.3 $
37  */

38 public class ClientSession
39 {
40
41    private static final Logger log = Logger.getLogger(ClientSession.class);
42
43    private final File JavaDoc testRoot;
44    private final HostConfiguration host;
45    private final HttpClient client;
46    private final String JavaDoc id;
47    private final RMIAdaptor adaptor;
48
49    private String JavaDoc appName;
50    private int count;
51    private boolean closed;
52
53    ClientSession(File JavaDoc testRoot, HostConfiguration host, HttpClient client, String JavaDoc 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    /**
67     * @throws IOException
68     * @throws IllegalStateException if the session if closed
69     */

70    private ServerResponse doGet(String JavaDoc uri, Header[] headers) throws IllegalStateException JavaDoc, IOException JavaDoc
71    {
72       if (closed)
73       {
74          throw new IllegalStateException JavaDoc("Session already closed");
75       }
76
77       //
78
StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("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       //
88
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    /**
104     * @throws IOException
105     * @throws IllegalStateException if the session if closed
106     */

107    public ServerResponse doGet(String JavaDoc uri, String JavaDoc[] componentNames) throws IOException JavaDoc, IllegalStateException JavaDoc
108    {
109       if (closed)
110       {
111          throw new IllegalStateException JavaDoc("Session already closed");
112       }
113
114       //
115
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 JavaDoc componentName = componentNames[i];
120          headers[1 + i] = new Header("componentName", componentName);
121       }
122       return doGet(uri, headers);
123    }
124
125    /**
126     * @throws IOException
127     * @throws IllegalStateException if the session if closed
128     */

129    public ServerResponse doGet(String JavaDoc uri) throws IOException JavaDoc, IllegalStateException JavaDoc
130    {
131       return doGet(uri, new Header[0]);
132    }
133
134    /**
135     * Deploy a portlet application. This method blocks until the deployment is finished
136     *
137     * @throws DeploymentException if the deployment process failed
138     * @throws IllegalStateException if the session if closed
139     * @throws IllegalArgumentException if the application name does not give a valid deployment
140     * @throws IOException
141     */

142    public void deploy(String JavaDoc appName) throws DeploymentException, IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, IOException JavaDoc
143    {
144       if (closed)
145       {
146          throw new IllegalStateException JavaDoc("Session already closed");
147       }
148       if (appName == null)
149       {
150           throw new IllegalArgumentException JavaDoc("Does not accept null argument");
151       }
152
153
154       //
155
if (this.appName != null)
156       {
157          throw new IllegalStateException JavaDoc("Cannot make 2 deployment at the same time");
158       }
159
160       //
161
File JavaDoc f = new File JavaDoc(testRoot, appName + ".war");
162       if (!f.exists())
163       {
164          throw new IllegalArgumentException JavaDoc("No such file to deploy " + f.getAbsolutePath());
165       }
166       if (!f.isFile())
167       {
168          throw new IllegalArgumentException JavaDoc("File is not a plain file " + f.getAbsolutePath());
169       }
170
171       //
172
try
173       {
174          adaptor.invoke(
175                new ObjectName JavaDoc("portal:service=MainDeployerFacade"),
176                "deploy",
177                new Object JavaDoc[]{f.toURL()},
178                new String JavaDoc[]{URL JavaDoc.class.getName()});
179       }
180       catch (MBeanException JavaDoc e)
181       {
182          Exception JavaDoc 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 JavaDoc e)
195       {
196          AssertionFailedError error = new AssertionFailedError();
197          error.initCause(e);
198          throw error;
199       }
200       catch (ReflectionException JavaDoc e)
201       {
202          AssertionFailedError error = new AssertionFailedError();
203          error.initCause(e);
204          throw error;
205       }
206       catch (InstanceNotFoundException JavaDoc e)
207       {
208          AssertionFailedError error = new AssertionFailedError();
209          error.initCause(e);
210          throw error;
211       }
212       catch (MalformedObjectNameException JavaDoc e)
213       {
214          AssertionFailedError error = new AssertionFailedError();
215          error.initCause(e);
216          throw error;
217       }
218
219       //
220
this.appName = appName;
221    }
222
223    /**
224     * Undeploy a portlet application. This method blocks until the undeployment is finished.
225     *
226     * @throws IllegalStateException if the session if closed
227     */

228    public void undeploy() throws IllegalStateException JavaDoc
229    {
230       if (closed)
231       {
232          throw new IllegalStateException JavaDoc("Session already closed");
233       }
234
235       //
236
if (appName == null)
237       {
238          log.warn("Nothing to undeploy");
239       }
240
241       //
242
File JavaDoc f = new File JavaDoc(testRoot, appName + ".war");
243       this.appName = null;
244
245       //
246
try
247       {
248          adaptor.invoke(
249                new ObjectName JavaDoc("portal:service=MainDeployerFacade"),
250                "undeploy",
251                new Object JavaDoc[]{f.toURL()},
252                new String JavaDoc[]{URL JavaDoc.class.getName()});
253       }
254       catch (Exception JavaDoc e)
255       {
256          log.warn("Unexpected exception when undeploying " + e);
257       }
258    }
259
260    /**
261     * @throws IllegalStateException if the session is closed
262     */

263    public void close() throws IllegalStateException JavaDoc
264    {
265       if (closed)
266       {
267          throw new IllegalStateException JavaDoc("Session already closed");
268       }
269
270       //
271
if (appName != null)
272       {
273          try
274          {
275             undeploy();
276          }
277          catch (Exception JavaDoc 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