1 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 44 public class MainScreen extends DefaultEventGateway { 45 46 protected static final Logger logger = Logger.getLogger(MainScreen.class.getName()); 48 49 private ListenerFactory getMainFactory = new EventForwardingFactory(new RenderMainScreen()); 51 private ListenerFactory getMainSlowlyFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new GetMainScreenSlowlyHandler();} public String getListenerID() {return getID(GetMainScreenSlowlyHandler.class);}}; 52 private ListenerFactory expireSessionFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new ExpireSessionHandler();} public String getListenerID() {return getID(ExpireSessionHandler.class);}}; 53 private ListenerFactory renderMainFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderMainScreenHandler();} public String getListenerID() {return getID(RenderMainScreenHandler.class);}}; 54 55 private GetMainScreen lnkGetMainScreen; private ExpireSession lnkExpireSession; private RenderMainScreen lnkRenderMainScreen; 60 63 public MainScreen() { 64 specifyLocalEventInterests(getMainFactory, GetMainScreen.class); 66 specifyLocalEventInterests(getMainSlowlyFactory, GetMainScreenSlowly.class); 67 specifyLocalEventInterests(expireSessionFactory, ExpireSession.class); 68 specifyLocalEventInterests(renderMainFactory, RenderMainScreen.class); 69 } 70 71 72 78 class GetMainScreenSlowlyHandler extends DefaultBaseEventListener { 79 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 80 81 86 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 e) { 93 throw new IOException("Running thread was interrupted! Long running process will be terminated..."); 94 } 95 } 96 logger.info("done!"); 97 98 context.getQueue().addEvent(new RenderMainScreen()); 100 } 101 } 102 103 109 class ExpireSessionHandler extends DefaultBaseEventListener { 110 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 111 BaseEvent event = context.getEvent(); 113 HttpServletRequest req = context.getRequest(); 114 DispatchQueue queue = context.getQueue(); 115 logger.debug("Expire session"); 116 117 SessionServices.getSession(req).invalidate(); 119 120 BaseEvent newEvent = new GetMainScreen(); 126 newEvent.setSource(event); 127 queue.addEvent(newEvent); 128 } 129 } 130 131 140 class RenderMainScreenHandler extends BTemplateViewHandler { 141 public Object getTemplateModels() {return new LocalTemplateModel();} 142 public Class getTemplateClass() {return MainHTML.class;} 143 } 144 145 146 152 class LocalTemplateModel extends AbstractTemplateModel { 153 boolean gotState = false; 154 int numberOfVisits = -1; 155 boolean autoLogin = false; 156 157 public String getName() {return "Ex1_Main";} 159 160 public Object getItem(String 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(); else return null; } else if (key.equals("SecretTip")) { 171 checkState(); 172 if (numberOfVisits>3) return new BComponent(); else return null; } 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 HttpSession session = SessionServices.getSession(vc); 194 numberOfVisits = 0; 195 try {numberOfVisits = Integer.parseInt(""+session.getAttribute(LoginServices.NUMBER_OF_VISITS).toString());} 196 catch (Exception e) {} 197 session.setAttribute(LoginServices.NUMBER_OF_VISITS, ""+(++numberOfVisits)); 198 ObjectRepository lr = ObjectRepository.getLocalRepository(); 202 Boolean b = (Boolean ) (lr.getState(LoginServices.AUTO_LOGIN)); 203 autoLogin = (b!=null && b.booleanValue()); 204 gotState = true; 206 } 207 } 208 } 209 210 | Popular Tags |