KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > ex3 > TestScreen


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestScreen.java,v 1.13 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.examples.ex3;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27
28 import org.enhydra.barracuda.core.event.*;
29 import org.apache.log4j.*;
30 import org.enhydra.barracuda.examples.ex3.events.*;
31 //import org.enhydra.barracuda.experimental.html.*;
32

33
34 /**
35  * This mini-app is just used for stress testing, to see what
36  * kind of performance level degradation we experience as the
37  * event heirarchy depth increases.
38  *
39  * The way this works is simple. If you invoke a Level1Event,
40  * the dispatcher will fire a RequestEvent, HttpRequestEvent,
41  * and then the actual Level1Event. If you invoke a Level10Event
42  * there will be 9 additional events generated because of the
43  * Polymorphic nature of the request hierarchy.
44  */

45 public class TestScreen extends DefaultEventGateway {
46
47     //public constants
48
protected static final Logger logger = Logger.getLogger(TestScreen.class.getName());
49
50     //this defines the various event handlers
51
private ListenerFactory levelFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new LevelHandler();} public String JavaDoc getListenerID() {return getID(LevelHandler.class);}};
52     private ListenerFactory renderFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderTestHandler();} public String JavaDoc getListenerID() {return getID(RenderTestHandler.class);}};
53
54     //private vars
55

56     /**
57      * Public constructor
58      */

59     public TestScreen() {
60         //specify who's interested in what
61
specifyLocalEventInterests(levelFactory, Level1Event.class);
62         specifyLocalEventInterests(levelFactory, Level2Event.class);
63         specifyLocalEventInterests(levelFactory, Level3Event.class);
64         specifyLocalEventInterests(levelFactory, Level4Event.class);
65         specifyLocalEventInterests(levelFactory, Level5Event.class);
66         specifyLocalEventInterests(levelFactory, Level6Event.class);
67         specifyLocalEventInterests(levelFactory, Level7Event.class);
68         specifyLocalEventInterests(levelFactory, Level8Event.class);
69         specifyLocalEventInterests(levelFactory, Level9Event.class);
70         specifyLocalEventInterests(levelFactory, Level10Event.class);
71         specifyLocalEventInterests(renderFactory, RenderTestScreenEvent.class);
72     }
73
74
75     //------------------------------------------------------------
76
// Model 2 - Controller Event Handlers
77
//------------------------------------------------------------
78
/**
79      * LevelHandlers - handle the request to get the test
80      * screen.
81      */

82     class LevelHandler extends DefaultBaseEventListener {
83         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
84             //unpack the necessary entities from the context
85
BaseEvent event = context.getEvent();
86             DispatchQueue queue = context.getQueue();
87
88             //in this case, we're not really doing anything special, so just
89
//redirect to the RenderLogin view
90
logger.debug("Handling event:"+event);
91             BaseEvent newEvent = new RenderTestScreenEvent();
92             newEvent.setSource(event);
93             queue.addEvent(newEvent);
94         }
95     }
96     
97
98     //------------------------------------------------------------
99
// Model 2 - View Event Handlers
100
//------------------------------------------------------------
101
/**
102      * RenderTestHandler - handle the request to render the test
103      * screen
104      */

105     class RenderTestHandler extends DefaultBaseEventListener {
106         public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException {
107             //unpack the necessary entities from the context
108
BaseEvent event = context.getEvent();
109             HttpServletResponse resp = context.getResponse();
110
111             //csc_061202.1 - added
112
//set the caching hdrs (allow this page to be cached by browser)
113
resp.setHeader("Cache-Control","max-age=0");
114             resp.setDateHeader("Last-Modified", System.currentTimeMillis());
115
116             //build the response
117
resp.setContentType("text/html");
118             PrintWriter out = resp.getWriter();
119             
120             //header
121
String JavaDoc title = "Simple Test Screen";
122             out.println ("<html>");
123             out.println (" <head>");
124             out.println (" <title>"+title+"</title>");
125             out.println (" </head>");
126             out.println (" <body>");
127             out.println (" <font face=\"Arial\">");
128
129             //body
130
out.println (" <p>"+title);
131             
132             //footer
133
out.println (" </font>");
134             out.println (" </body>");
135             out.println ("</html>");
136         }
137     }
138 }
139
140
Popular Tags