KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > GlobalUtilities


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: GlobalUtilities.java,v 1.11 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.examples;
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
30
31 /**
32  * Global Utilities. This class is primarily responsible for
33  * handling an HttpResponseEvent and generating a "Resource
34  * not found error"
35  */

36 public class GlobalUtilities extends DefaultEventGateway {
37
38     //public constants
39

40     //this defines the various event handlers
41
private ListenerFactory httpResponseFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new HttpResponseHandler();} public String JavaDoc getListenerID() {return getID(HttpResponseHandler.class);}};
42
43     //private vars
44
private HttpResponseEvent lnkHttpRequestEvent; //added by TJ to show association
45

46     /**
47      * Public constructor
48      */

49     public GlobalUtilities() {
50         //specify who's interested in what
51
specifyLocalEventInterests(httpResponseFactory, HttpResponseEvent.class);
52     }
53
54     //------------------------------------------------------------
55
// Model 2 - View Event Handlers
56
//------------------------------------------------------------
57
/**
58      * HttpResponseHandler - this is the global handler for HttpResponseEvents.
59      * If we get one of these events it means that something very unusual happened
60      * in the system (like somebody requested an invalid event or something). So,
61      * display a simple page indicating that the resource was not available.
62      */

63     class HttpResponseHandler extends DefaultBaseEventListener {
64         public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException {
65             //unpack the necessary entities from the context
66
BaseEvent event = context.getEvent();
67             HttpServletResponse resp = context.getResponse();
68
69             //csc_061202.1 - added
70
//set the caching hdrs (this will allow the page to be cached by the browser)
71
resp.setHeader("Cache-Control","max-age=0");
72             resp.setDateHeader("Last-Modified", System.currentTimeMillis());
73
74             //render the page
75
resp.setContentType("text/html");
76             PrintWriter out = resp.getWriter();
77             out.println("<html><head><title>Resource not Found</title></head>");
78             out.println("<body><font face=\"Arial\">");
79
80             //display the general error
81
String JavaDoc targetEventName = (String JavaDoc) context.getState(ApplicationGateway.TARGET_EVENT_NAME);
82             out.println("<p>Beep-beep-beep! We're sorry your call did not go through. Will you please hang up and try your call again...");
83             out.println("<p>If you received this message, it indicates that either <strong>"+targetEventName+"</strong> is not a valid event OR the system is misconfigured. If you feel there is a problem with the system please <a HREF=\"mailto:christianc@lutris.com\">contact the application administrator</a>... ");
84
85             //log the specific problem
86
BaseEvent be = DefaultBaseEvent.getRootEvent(event);
87             out.println("<p>The base event that triggered this action was:"+be+"<br>");
88             out.println("Source:"+be.getSource()+"<br>");
89             
90             //footer
91
out.println("</font></body></html>");
92         }
93     }
94 }
95
96
Popular Tags