KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > Component


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;
10
11 import org.jboss.portal.common.concurrent.Valve;
12 import org.jboss.portal.server.invocation.AttachmentKey;
13 import org.jboss.portal.server.invocation.Interceptor;
14 import org.jboss.portal.server.invocation.Invocation;
15 import org.jboss.portal.server.invocation.InvocationType;
16 import org.jboss.portal.server.kernel.Service;
17 import org.jboss.portal.server.kernel.ServiceImplementation;
18 import org.jboss.portal.server.output.UnavailableResult;
19 import org.jboss.portal.server.plugins.invocation.InvocationPlugin;
20 import org.jboss.portal.server.plugins.mode.ContentTypes;
21 import org.jboss.portal.server.plugins.mode.Mode;
22 import org.jboss.portal.server.plugins.windowstate.WindowState;
23 import org.jboss.portal.server.util.Parameters;
24 import org.jboss.portal.server.user.UserContext;
25
26 /**
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.5 $
29  */

30 public abstract class Component
31    extends ServerObject
32 {
33
34    /** The application this component belongs to. */
35    protected Application application;
36
37    /** If not null, specifiy the caching behaviour. */
38    protected Integer JavaDoc expirationCache;
39
40    /** The invocation valve. */
41    protected final Valve valve;
42
43    protected Component(String JavaDoc name)
44    {
45       super(name);
46       this.valve = new Valve();
47    }
48
49    public PortalServer getServer()
50    {
51       return application.getServer();
52    }
53
54    /**
55     * Return the expiration cache value in seconds if any.
56     */

57    public Integer JavaDoc getExpirationCache()
58    {
59       return expirationCache;
60    }
61
62    public ContentTypes getContentTypes()
63    {
64       return (ContentTypes)getPlugin(PortalConstants.PLUGIN_CONTENT_TYPES);
65    }
66
67    /**
68     * Invoke on this component.
69     */

70    public Object JavaDoc invoke(Invocation invocation)
71    {
72       try
73       {
74          if (valve.beforeInvocation())
75          {
76             InvocationType itype = (InvocationType)invocation.getAttachment(AttachmentKey.INVOCATION_TYPE);
77             PortalRequest preq = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
78             WindowContext wctx = (WindowContext)invocation.getAttachment(AttachmentKey.WINDOW_CONTEXT);
79
80             // Translate into the portlet invocation type
81
InvocationType pitype = null;
82             if (itype == InvocationType.PROCESS_TARGET)
83             {
84                // Set window state
85
String JavaDoc windowStateAsString = preq.getControlParameters().getParameter("windowstate");
86                WindowState windowState = windowStateAsString != null ? WindowState.create(windowStateAsString) : null;
87
88                // Update window state
89
if (windowState != null)
90                {
91                   // fixme : check auth with window.getInstance().isWindowStateAllowed()
92
wctx.setWindowState(windowState);
93                }
94
95                // Set portlet mode
96
String JavaDoc portletModeAsString = preq.getControlParameters().getParameter("mode");
97                Mode mode = portletModeAsString != null ? Mode.create(portletModeAsString) : null;
98
99                // Update portlet mode
100
if (mode != null)
101                {
102                   // Check the new mode is authorized
103
PortalResponse resp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE);
104                   Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW);
105                   Instance instance = window.getInstance();
106                   Component component = instance.getComponent();
107                   ContentTypes ct = component.getContentTypes();
108                   if (ct.isSupported(mode, resp.getContentType()))
109                   {
110                      wctx.setMode(mode);
111                   }
112                   else
113                   {
114                      wctx.setMode(Mode.VIEW);
115                   }
116                }
117
118                String JavaDoc pitypeAsString = preq.getControlParameters().getParameter("type");
119                if ("action".equals(pitypeAsString))
120                {
121                   pitype = InvocationType.ACTION;
122                   try
123                   {
124                      //
125
InvocationPlugin ip = (InvocationPlugin)getPlugin(PortalConstants.PLUGIN_INVOCATION);
126                      Interceptor[] stack = ip.getStack();
127
128                      // Then invoke
129
invocation.setAttachment(AttachmentKey.INVOCATION_TYPE, pitype);
130                      return invocation.invokeNext(stack);
131                   }
132                   finally
133                   {
134                      invocation.setAttachment(AttachmentKey.INVOCATION_TYPE, itype);
135                   }
136                }
137                else if ("render".equals(pitypeAsString))
138                {
139                   // Update parameters
140
UserContext userCtx = (UserContext)invocation.getAttachment(AttachmentKey.USER_CONTEXT);
141                   Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW);
142                   InstanceContext ictx = (InstanceContext)userCtx.getContext(window.getInstance());
143
144                   //
145
Parameters newParameters = preq.getTargetParameters();
146                   Parameters oldParameters = ictx.getParameters();
147                   oldParameters.setParameters(newParameters);
148
149                   //
150
return null;
151                }
152                else if ("nav".equals(pitypeAsString))
153                {
154                   //
155
return null;
156                }
157                else
158                {
159                   throw new IllegalArgumentException JavaDoc();
160                }
161             }
162             else if (itype == InvocationType.PROCESS_RENDER)
163             {
164                pitype = InvocationType.RENDER;
165                try
166                {
167                   //
168
InvocationPlugin ip = (InvocationPlugin)getPlugin(PortalConstants.PLUGIN_INVOCATION);
169                   Interceptor[] stack = ip.getStack();
170
171                   // Then invoke
172
invocation.setAttachment(AttachmentKey.INVOCATION_TYPE, pitype);
173                   return invocation.invokeNext(stack);
174                }
175                finally
176                {
177                   invocation.setAttachment(AttachmentKey.INVOCATION_TYPE, itype);
178                }
179             }
180             else
181             {
182                throw new IllegalArgumentException JavaDoc();
183             }
184          }
185          else
186          {
187             return new UnavailableResult((WindowContext)invocation.getAttachment(AttachmentKey.WINDOW_CONTEXT));
188          }
189       }
190       finally
191       {
192          valve.afterInvocation();
193       }
194    }
195
196    /**
197     * The invocation valve.
198     */

199    public Valve getValve()
200    {
201       return valve;
202    }
203
204    /**
205     * Returns the application that component belongs to.
206     */

207    public Application getApplication()
208    {
209       return application;
210    }
211
212    public void addIDependOn(ServiceImplementation implementation)
213    {
214       Service service = implementation.getService();
215       if (service instanceof Application)
216       {
217          application = (Application)service;
218       }
219       else
220       {
221          super.addIDependOn(implementation);
222       }
223    }
224
225    public void removeIDependOn(ServiceImplementation implementation)
226    {
227       Service service = implementation.getService();
228       if (service instanceof Application)
229       {
230          application = null;
231       }
232       else
233       {
234          super.removeIDependOn(implementation);
235       }
236    }
237 }
238
Popular Tags