KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > ex1 > MainScreen


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

20 package org.enhydra.barracuda.examples.ex1;
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.apache.log4j.*;
29
30 import org.enhydra.barracuda.core.comp.*;
31 import org.enhydra.barracuda.core.event.*;
32 import org.enhydra.barracuda.core.event.helper.*;
33 import org.enhydra.barracuda.plankton.data.*;
34 import org.enhydra.barracuda.core.util.dom.*;
35 import org.enhydra.barracuda.core.util.http.*;
36 import org.enhydra.barracuda.examples.ex1.events.*;
37 import org.enhydra.barracuda.examples.ex1.xmlc.*;
38
39
40 /**
41  * Event handlers (both Controller and View) for the
42  * Main screen.
43  */

44 public class MainScreen extends DefaultEventGateway {
45
46     //public constants
47
protected static final Logger logger = Logger.getLogger(MainScreen.class.getName());
48
49     //this defines the various event handlers
50
private ListenerFactory getMainFactory = new EventForwardingFactory(new RenderMainScreen());
51     private ListenerFactory getMainSlowlyFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new GetMainScreenSlowlyHandler();} public String JavaDoc getListenerID() {return getID(GetMainScreenSlowlyHandler.class);}};
52     private ListenerFactory expireSessionFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new ExpireSessionHandler();} public String JavaDoc getListenerID() {return getID(ExpireSessionHandler.class);}};
53     private ListenerFactory renderMainFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderMainScreenHandler();} public String JavaDoc getListenerID() {return getID(RenderMainScreenHandler.class);}};
54
55     //private vars
56
private GetMainScreen lnkGetMainScreen; //added by TJ to show association
57
private ExpireSession lnkExpireSession; //added by TJ to show association
58
private RenderMainScreen lnkRenderMainScreen; //added by TJ to show association
59

60     /**
61      * Public constructor
62      */

63     public MainScreen() {
64         //specify who's interested in what
65
specifyLocalEventInterests(getMainFactory, GetMainScreen.class);
66         specifyLocalEventInterests(getMainSlowlyFactory, GetMainScreenSlowly.class);
67         specifyLocalEventInterests(expireSessionFactory, ExpireSession.class);
68         specifyLocalEventInterests(renderMainFactory, RenderMainScreen.class);
69     }
70
71
72     //------------------------------------------------------------
73
// Model 2 - Controller Event Handlers
74
//------------------------------------------------------------
75
/**
76      * GetMainScreenSlowlyHandler - get the main screen slowly
77      */

78     class GetMainScreenSlowlyHandler extends DefaultBaseEventListener {
79         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
80         
81             //get the LongRunning object so we can update it
82
// LongRunning lr = (LongRunning) context.getEvent();
83
// lr.setRedirectEvent(new GetMainScreen());
84
// lr.setETA(120);
85

86             //do something that takes a long time (2 minutes)
87
logger.info("GetMainScreenSlowly...");
88             for (int i=0; i<30; i++) {
89                 try {
90                     logger.debug("...iteration: "+i);
91                     Thread.currentThread().sleep(1000);
92                 } catch (InterruptedException JavaDoc e) {
93                     throw new IOException("Running thread was interrupted! Long running process will be terminated...");
94                 }
95             }
96             logger.info("done!");
97         
98             //forward control on to rendering even
99
context.getQueue().addEvent(new RenderMainScreen());
100         }
101     }
102     
103     /**
104      * ExpireSessionHandler - this is where we handle any ExpireSession
105      * events. Afterwards, redirect flow to the Main screen (which won't
106      * work because the session information is gone now, so instead you'll
107      * end up at the Login screen as you really should.)
108      */

109     class ExpireSessionHandler extends DefaultBaseEventListener {
110         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
111             //unpack the necessary entities from the context
112
BaseEvent event = context.getEvent();
113             HttpServletRequest req = context.getRequest();
114             DispatchQueue queue = context.getQueue();
115             logger.debug("Expire session");
116
117             //expire the session info
118
SessionServices.getSession(req).invalidate();
119
120             //in this case, we're not really doing anything special, so just
121
//redirect to the RenderMain view (note, this won't actually happen,
122
//because the Utilities class is ensuring that without session info
123
//we always get redirected to the Login screen...but we shouldn't have
124
//to know about that detail here)
125
BaseEvent newEvent = new GetMainScreen();
126             newEvent.setSource(event);
127             queue.addEvent(newEvent);
128         }
129     }
130     
131     //------------------------------------------------------------
132
// Model 2 - View Event Handlers
133
//------------------------------------------------------------
134
/**
135      * RenderMainScreenHandler - this is where we handle any RenderMainScreen
136      * event and actually generate the view. We check the session info
137      * to determine how many times they've been here, as well as to see
138      * if they were automatically logged in.
139      */

140     class RenderMainScreenHandler extends BTemplateViewHandler {
141         public Object JavaDoc getTemplateModels() {return new LocalTemplateModel();}
142         public Class JavaDoc getTemplateClass() {return MainHTML.class;}
143     }
144     
145     
146     //------------------------------------------------------------
147
// Components - TemplateModel
148
//------------------------------------------------------------
149
/**
150      * LocalTemplateModel
151      */

152     class LocalTemplateModel extends AbstractTemplateModel {
153         boolean gotState = false;
154         int numberOfVisits = -1;
155         boolean autoLogin = false;
156     
157         //register the model by name
158
public String JavaDoc getName() {return "Ex1_Main";}
159
160         //provide items by key
161
public Object JavaDoc getItem(String JavaDoc key) {
162             ViewContext vc = getViewContext();
163             if (key.equals("Visits")) {
164                 checkState();
165                 return numberOfVisits+" time"+(numberOfVisits>1 ? "s" : "");
166             } else if (key.equals("AutoLogin")) {
167                 checkState();
168                 if (autoLogin) return new BComponent(); //visible (go ahead and show the node)
169
else return null; //null (skip it)
170
} else if (key.equals("SecretTip")) {
171                 checkState();
172                 if (numberOfVisits>3) return new BComponent(); //visible (go ahead and show the node)
173
else return null; //null (skip it)
174
} else if (key.equals("GetLoginLink")) {
175                 return new BAction(new GetLoginScreen());
176             } else if (key.equals("GetMainScreenSlowly")) {
177                 BAction baction = new BAction(new GetMainScreenSlowly());
178                 baction.setParam("key1","foo");
179                 baction.setParam("key2","bar");
180                 baction.setParam("key3","rama");
181                 return baction;
182             } else if (key.equals("ExpireSessionLink")) {
183                 return new BAction(new ExpireSession());
184             } else {
185                 return super.getItem(key);
186             }
187         }
188         
189         private void checkState() {
190             if (gotState) return;
191             ViewContext vc = getViewContext();
192             //...see how many times we've been here...
193
HttpSession session = SessionServices.getSession(vc);
194             numberOfVisits = 0;
195             try {numberOfVisits = Integer.parseInt(""+session.getAttribute(LoginServices.NUMBER_OF_VISITS).toString());}
196             catch (Exception JavaDoc e) {}
197             session.setAttribute(LoginServices.NUMBER_OF_VISITS, ""+(++numberOfVisits));
198             //..see if we auto-logged-in
199
//csc_082302.2_start
200
// autoLogin = new Boolean(""+session.getAttribute(LoginServices.AUTO_LOGIN)).booleanValue();
201
ObjectRepository lr = ObjectRepository.getLocalRepository();
202             Boolean JavaDoc b = (Boolean JavaDoc) (lr.getState(LoginServices.AUTO_LOGIN));
203             autoLogin = (b!=null && b.booleanValue());
204 //csc_082302.2_end
205
gotState = true;
206         }
207     }
208 }
209
210
Popular Tags