KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > JBossPortlet


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.portlet;
10
11 import org.jboss.portal.format.util.EntityTable;
12
13 import java.io.IOException JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.Portlet;
27 import javax.portlet.PortletConfig;
28 import javax.portlet.PortletContext;
29 import javax.portlet.PortletException;
30 import javax.portlet.PortletMode;
31 import javax.portlet.PortletSecurityException;
32 import javax.portlet.RenderRequest;
33 import javax.portlet.RenderResponse;
34 import javax.portlet.WindowState;
35 import javax.portlet.PortletPreferences;
36 import javax.portlet.PortletURL;
37
38 /**
39  * The JBossPortlet.
40  *
41  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
42  * @version $Revision: 1.4 $
43  */

44 public class JBossPortlet implements Portlet
45 {
46
47    private static final Class JavaDoc[] ACTION_LOOKUP = new Class JavaDoc[]{JBossActionRequest.class,JBossActionResponse.class};
48
49    /** .*/
50    private PortletConfig config;
51
52    public JBossPortlet()
53    {
54    }
55
56    /**
57     * Return the string <i>main</i>, it can be overriden to return another value by subclasses.
58     */

59    public String JavaDoc getDefaultOperation()
60    {
61       return "main";
62    }
63
64    /**
65     * Return the string <i>op</i>, it can be overriden to return another value by subclasses.
66     */

67    public String JavaDoc getOperationName()
68    {
69       return "op";
70    }
71
72    public void init() throws PortletException
73    {
74    }
75
76    public PortletConfig getPortletConfig()
77    {
78       return config;
79    }
80
81    public String JavaDoc getPortletName()
82    {
83       return config.getPortletName();
84    }
85
86    public PortletContext getPortletContext()
87    {
88       return config.getPortletContext();
89    }
90
91    /**
92     * Calls <code>doDispatch(JBossActionRequest,JBossActionResponse)</code>.
93     */

94    protected void processAction(JBossActionRequest req, JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException JavaDoc
95    {
96       doDispatch((JBossActionRequest)req,(JBossActionResponse)resp);
97    }
98
99    /**
100     * <p>This method looks up the method corresponding to the action. It uses the action parameter using the parameter name
101     * defines by the <code>operationName</code> field of this class. If not method is found it uses the method defined by the return of the method
102     * <code>getDefaultOperation()</code> of this class. In order to be found a method must use <code>JBossActionRequest</code> and
103     * <JBossActionResponse> in the signature.</p>
104     *
105     * <p>If not valid dispatcher is found it throws a PortletException, otherwise it invokes the method by reflection. The invoked
106     * method may declare exceptions in the throws clause of the method. Whenever an exception is raised during the invocation of
107     * the method, a decision is taken depending on the nature of the exception :</p>
108     *
109     * <ul>
110     * <li>If the exception is an instanceof <code>PortletException</code>, <code>IOException</code> then this exception
111     * is rethrown as is since this method declares them in its throws clause</li>
112     * <li>If the exception is an instance of <code>RuntimeException</code> or <code>Error>/code>, it is rethrown as is</li>
113     * <li>Otherwise a <code>PortletException</code> is created with the caught exception as cause and thrown</li>
114     * </ul>
115     */

116    protected void doDispatch(JBossActionRequest req, JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException JavaDoc
117    {
118       Method JavaDoc dispatcher = null;
119
120       // Get operation
121
String JavaDoc operation = req.getParameter(getOperationName());
122
123       // Try to locate specific operation
124
if (operation != null)
125       {
126          dispatcher = lookupMethod(operation, ACTION_LOOKUP);
127       }
128
129       // If it null try to getPortalObjectContext the default operation
130
if (dispatcher == null)
131       {
132          dispatcher = lookupMethod(getDefaultOperation(), ACTION_LOOKUP);
133       }
134
135       // Invoke the operation
136
if (dispatcher != null)
137       {
138          try
139          {
140             dispatcher.invoke(this, new Object JavaDoc[]{req,resp});
141          }
142          catch (IllegalAccessException JavaDoc e)
143          {
144             throw new PortletException(e);
145          }
146          catch (InvocationTargetException JavaDoc e)
147          {
148             Throwable JavaDoc t = e.getCause();
149             if (t instanceof PortletException)
150             {
151                throw (PortletException)t;
152             }
153             else if (t instanceof IOException JavaDoc)
154             {
155                throw (IOException JavaDoc)t;
156             }
157             else if (t instanceof RuntimeException JavaDoc)
158             {
159                throw (RuntimeException JavaDoc)t;
160             }
161             else if (t instanceof Error JavaDoc)
162             {
163                throw (Error JavaDoc)t;
164             }
165             else
166             {
167                throw new PortletException("Unexpected exception when dispatching the operation", e);
168             }
169          }
170       }
171       else
172       {
173          throw new PortletException("Nothing to invoke");
174       }
175    }
176
177    /**
178     * Default doEdit method that works in coordination with doEdit(JBossRenderRequest,JBossRenderResponse).
179     */

180    public void doEdit(JBossActionRequest req, JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException JavaDoc
181    {
182       PortletPreferences prefs = req.getPreferences();
183       Map JavaDoc map = prefs.getMap();
184       for (Iterator JavaDoc i = req.getParameterMap().entrySet().iterator();i.hasNext();)
185       {
186          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
187          String JavaDoc name = (String JavaDoc)entry.getKey();
188          String JavaDoc[] values = (String JavaDoc[])entry.getValue();
189          if (map.containsKey(name))
190          {
191             prefs.setValues(name, values);
192          }
193       }
194       prefs.store();
195    }
196
197
198    /**
199     * Calls <code>doDispatch(JBossRenderRequest,JBossRenderResponse)</code>.
200     */

201    protected void render(JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, PortletSecurityException, IOException JavaDoc
202    {
203       resp.setTitle(getTitle(req));
204       doDispatch((JBossRenderRequest)req, (JBossRenderResponse)resp);
205    }
206
207    /**
208     *
209     */

210    protected void doDispatch(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
211    {
212       if (!WindowState.MINIMIZED.equals(request.getWindowState()))
213       {
214          PortletMode portletMode = request.getPortletMode();
215          if (PortletMode.VIEW.equals(portletMode))
216          {
217             doView(request, response);
218          }
219          else if (PortletMode.HELP.equals(portletMode))
220          {
221             doHelp(request, response);
222          }
223          else if (PortletMode.EDIT.equals(portletMode))
224          {
225             doEdit(request, response);
226          }
227       }
228    }
229
230    /**
231     * Throw a <code>PortletException</code>.
232     */

233    protected void doView(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
234    {
235       throw new PortletException();
236    }
237
238    /**
239     * Throw a <code>PortletException</code>.
240     */

241    protected void doHelp(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
242    {
243       throw new PortletException();
244    }
245
246    /**
247     * Provide a default generic editor for preferences that produce HTML markup.
248     */

249    protected void doEdit(JBossRenderRequest request, JBossRenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
250    {
251       response.setContentType("text/html");
252       PrintWriter JavaDoc writer = response.getWriter();
253
254       //
255
PortletURL url = response.createActionURL();
256       url.setParameter(getDefaultOperation(), "doEdit");
257
258       //
259
writer.print("<table> " +
260          "<tr><td class=\"portlet-section-alternate\">" + "Name" +
261          "</td><td class=\"portlet-section-alternate\">" + "Value" +
262          "</td></tr>" +
263          "<form action=\"");
264       writer.print(url.toString());
265       writer.print("\" method=\"post\">");
266
267       //
268
PortletPreferences prefs = request.getPreferences();
269       for (Iterator JavaDoc i = prefs.getMap().entrySet().iterator();i.hasNext();)
270       {
271          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
272          String JavaDoc name = (String JavaDoc)entry.getKey();
273          String JavaDoc[] values = (String JavaDoc[])entry.getValue();
274
275          // Perform HTML entity replacement
276
name = EntityTable.FULL.convertEntities(name);
277          for (int j = 0; j < values.length; j++)
278          {
279             String JavaDoc value = values[j];
280             if (value != null)
281             {
282                values[j] = EntityTable.FULL.convertEntities(value);
283             }
284          }
285
286          //
287
writer.print("<tr><td class=\"portlet-section-body\">");
288          writer.print(name);
289          writer.print("</td><td class=\"portlet-section-body\">");
290          if (prefs.isReadOnly(name))
291          {
292             writer.print(name);
293          }
294          else
295          {
296             writer.print("<input class=\"portlet-form-input-field\" type=\"text\" name=\"");
297             writer.print(name);
298             writer.print("\" value=\"");
299             writer.print(values.length >= 1 ? values[0] : "");
300             writer.print("\"/>");
301          }
302          writer.print("</td></tr>");
303       }
304
305       writer.print("<tr><td colspan=\"2\" class=\"portlet-section-alternate\">" +
306          "<input class=\"portlet-form-button\" type=\"submit\" value=\"Save\"/>" +
307          "</td></tr>" +
308          "</form></table>");
309    }
310
311    public ResourceBundle JavaDoc getResourceBundle(Locale JavaDoc locale)
312    {
313       return getPortletConfig().getResourceBundle(locale);
314    }
315
316    protected String JavaDoc getTitle(RenderRequest request)
317    {
318       ResourceBundle JavaDoc bundle = getResourceBundle(request.getLocale());
319       return bundle.getString("javax.portlet.title");
320    }
321
322    public String JavaDoc getInitParameter(String JavaDoc name) throws IllegalArgumentException JavaDoc
323    {
324       return getPortletConfig().getInitParameter(name);
325    }
326
327    public Enumeration JavaDoc getInitParameterNames()
328    {
329       return getPortletConfig().getInitParameterNames();
330    }
331
332    // javax.portlet.Portlet implementation *****************************************************************************
333

334    public void init(PortletConfig config) throws PortletException
335    {
336       this.config = config;
337       init();
338    }
339
340    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
341    {
342       processAction((JBossActionRequest)request,(JBossActionResponse)response);
343    }
344
345    public void render(RenderRequest req, RenderResponse resp) throws PortletException, PortletSecurityException, IOException JavaDoc
346    {
347       render((JBossRenderRequest)req, (JBossRenderResponse)resp);
348    }
349
350    public void destroy()
351    {
352    }
353
354    // Private **********************************************************************************************************
355

356    /**
357     * Locate a method.
358     */

359    private Method JavaDoc lookupMethod(String JavaDoc operation, Class JavaDoc[] parameterTypes)
360    {
361       try
362       {
363          Method JavaDoc m = getClass().getMethod(operation, parameterTypes);
364          if (m.getReturnType() == void.class &&
365              Modifier.isPublic(m.getModifiers()))
366          {
367             return m;
368          }
369       }
370       catch (NoSuchMethodException JavaDoc e)
371       {
372          // Does not exist
373
}
374       return null;
375    }
376 }
377
Popular Tags