KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Lifecycle03


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26
27 /**
28  * Test for servlet lifecycle management. It's behavior is controlled by
29  * a request parameter <strong>step</strong>, which must be set to one of
30  * the following values:
31  * <ul>
32  * <li><em>1</em> - Throw an <code>UnavailableException</code> that indicates
33  * permanent unavailablility, which should cause this servlet instance
34  * to be destroyed and thrown away.</li>
35  * <li><em>2</em> - Check the lifecycle variables to ensure that the old
36  * instance was not reused.</li>
37  * </ul>
38  *
39  * @author Craig R. McClanahan
40  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
41  */

42
43 public class Lifecycle03 extends HttpServlet {
44
45     private static String JavaDoc staticTrail = "";
46
47     private String JavaDoc instanceTrail = "";
48
49     public void init() throws ServletException {
50         staticTrail += "I";
51         instanceTrail += "I";
52     }
53
54     public void destroy() {
55         staticTrail += "D";
56         instanceTrail += "D";
57     }
58
59     public void doGet(HttpServletRequest request, HttpServletResponse response)
60         throws IOException, ServletException {
61
62         staticTrail += "S";
63         instanceTrail += "S";
64
65         // For step=1, throw an exception
66
if ("1".equals(request.getParameter("step"))) {
67             staticTrail = "IS";
68             throw new UnavailableException("Lifecycle03 is permanently unavailable");
69         }
70
71         // For step=2, evaluate the results.
72
response.setContentType("text/plain");
73         PrintWriter writer = response.getWriter();
74         if (staticTrail.equals("ISDIS") && instanceTrail.equals("IS"))
75             writer.println("Lifecycle03 PASSED");
76         else
77             writer.println("Lifecycle03 FAILED - staticTrail=" + staticTrail +
78                            ", instanceTrail=" + instanceTrail);
79
80         while (true) {
81             String JavaDoc message = StaticLogger.read();
82             if (message == null)
83                 break;
84             writer.println(message);
85         }
86         StaticLogger.reset();
87
88     }
89
90
91 }
92
Popular Tags