KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > invocation > portal > MainDispatcherInterceptor


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.invocation.portal;
10
11 import java.io.PrintWriter JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import org.jboss.portal.server.Component;
16 import org.jboss.portal.server.Instance;
17 import org.jboss.portal.server.PortalRequest;
18 import org.jboss.portal.server.PortalServer;
19 import org.jboss.portal.server.ServerObjectID;
20 import org.jboss.portal.server.Window;
21 import org.jboss.portal.server.WindowContext;
22 import org.jboss.portal.server.PortalResponse;
23 import org.jboss.portal.server.invocation.AttachmentKey;
24 import org.jboss.portal.server.invocation.Interceptor;
25 import org.jboss.portal.server.invocation.Invocation;
26 import org.jboss.portal.server.invocation.InvocationType;
27 import org.jboss.portal.server.output.ErrorResult;
28 import org.jboss.portal.server.output.FragmentResult;
29 import org.jboss.portal.server.output.Result;
30 import org.jboss.portal.server.output.UnavailableResult;
31 import org.jboss.portal.server.user.UserContext;
32 import org.jboss.portal.server.util.Properties;
33
34 /**
35  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
36  * @version $Revision: 1.7 $
37  */

38 public class MainDispatcherInterceptor implements Interceptor
39 {
40
41    public Object JavaDoc invoke(Invocation invocation)
42    {
43       PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
44       PortalResponse resp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE);
45       UserContext userContext = (UserContext)invocation.getAttachment(AttachmentKey.USER_CONTEXT);
46
47       // Get the windows that will be rendered
48
Collection JavaDoc windows = (Collection JavaDoc)invocation.getAttachment(AttachmentKey.VIEW);
49
50       // The result of the target processing
51
Result targetResult = resp.getTargetResult();
52
53       //
54
if (targetResult instanceof ErrorResult)
55       {
56          // fixme : should be configurable and better for outputting the error as HTML
57
ErrorResult errorResult = (ErrorResult)targetResult;
58
59          // Remove the bad window from the set
60
WindowContext producer = errorResult.getProducer();
61          ServerObjectID id = producer.getID();
62          for (Iterator JavaDoc i = windows.iterator();i.hasNext();)
63          {
64             Window window = (Window)i.next();
65             if (window.getID().equals(id))
66             {
67                i.remove();
68             }
69          }
70
71          FragmentResult fragment = new FragmentResult(errorResult.getProducer());
72          fragment.setContentType("text/html");
73          PrintWriter JavaDoc writer = fragment.getWriter();
74          writer.write("Error");
75          resp.addRenderResult(fragment);
76       }
77
78       // Render all the windows seen during this request
79
for (Iterator JavaDoc i = windows.iterator();i.hasNext();)
80       {
81          Window window = (Window)i.next();
82          Instance instance = window.getInstance();
83          Component component = instance.getComponent();
84          WindowContext windowContext = (WindowContext)userContext.getContext(window);
85
86          Result renderResult = null;
87          try
88          {
89             invocation.setAttachment(AttachmentKey.WINDOW, window);
90             invocation.setAttachment(AttachmentKey.WINDOW_CONTEXT, windowContext);
91             invocation.setAttachment(AttachmentKey.RESPONSE_PROPERTIES, new Properties());
92             invocation.setAttachment(AttachmentKey.INVOCATION_TYPE, InvocationType.PROCESS_RENDER);
93             renderResult = (Result)component.invoke(invocation);
94          }
95          finally
96          {
97             invocation.removeAttachment(AttachmentKey.WINDOW);
98             invocation.removeAttachment(AttachmentKey.WINDOW_CONTEXT);
99             invocation.removeAttachment(AttachmentKey.RESPONSE_PROPERTIES);
100          }
101
102          // Dispose the component
103
if (renderResult instanceof UnavailableResult)
104          {
105             PortalServer container = component.getServer();
106             container.stop(component.getID());
107          }
108          else
109          {
110             resp.addRenderResult(renderResult);
111          }
112       }
113
114       return null;
115    }
116 }
117
Popular Tags