KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > WcfController


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.controller;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.servlet.http.HttpSession JavaDoc;
21
22 import org.apache.log4j.Logger;
23 /**
24  * WcfController is a {@link Controller} that is global for all
25  * components. WcfController just remembers RequestListeners and
26  * views, the actual work is done by {@link RequestFilter}. It works
27  * in JavaServer Faces context as well as stand-alone.
28  */

29 public class WcfController extends Controller {
30   private static Logger logger = Logger.getLogger(WcfController.class);
31   private List JavaDoc requestListeners = new LinkedList JavaDoc();
32
33   WcfController() {
34   }
35
36   private static final String JavaDoc WEBKEY = WcfController.class.getName() + ".dispatcher";
37
38   public static Controller instance(HttpSession JavaDoc session) {
39     WcfController ctrl = (WcfController) session.getAttribute(WEBKEY);
40     if (ctrl == null) {
41       ctrl = new WcfController();
42       session.setAttribute(WEBKEY, ctrl);
43     }
44     return ctrl;
45   }
46
47   public void addRequestListener(RequestListener l) {
48     requestListeners.add(l);
49   }
50
51   public void removeRequestListener(RequestListener l) {
52     requestListeners.remove(l);
53   }
54
55   public void setNextView(String JavaDoc uri) {
56     RequestContext context = RequestContext.instance();
57     context.getRequest().setAttribute(RequestFilter.NEXTVIEW, uri);
58   }
59   
60   public String JavaDoc getNextView() {
61     RequestContext context = RequestContext.instance();
62     return (String JavaDoc)context.getRequest().getAttribute(RequestFilter.NEXTVIEW);
63   }
64
65   public void request(RequestContext context) throws Exception JavaDoc {
66     // avoid ConcurrentModificationException - when a Component
67
// registers child components while dispatching the request
68
ArrayList JavaDoc list = new ArrayList JavaDoc(requestListeners);
69     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
70       RequestListener l = (RequestListener) it.next();
71       l.request(context);
72     }
73   }
74
75   public List JavaDoc getRootListeners() {
76     return Collections.unmodifiableList(requestListeners);
77   }
78 }
79
Popular Tags