KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > invocation > PreferencesInterceptor


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.portlet.invocation;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 import javax.portlet.PortletPreferences;
15 import javax.portlet.PreferencesValidator;
16
17 import org.jboss.portal.portlet.impl.PortletPreferencesImpl;
18 import org.jboss.portal.portlet.plugins.preferences.PortletPreferenceSetPlugin;
19 import org.jboss.portal.server.Component;
20 import org.jboss.portal.server.Instance;
21 import org.jboss.portal.server.PortalConstants;
22 import org.jboss.portal.server.Window;
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.plugins.preferences.MergeStrategy;
28 import org.jboss.portal.server.plugins.preferences.MergedPreferenceSet;
29 import org.jboss.portal.server.plugins.preferences.PreferenceSet;
30 import org.jboss.portal.server.plugins.preferences.PreferenceSetPlugin;
31 import org.jboss.portal.server.plugins.preferences.PreferenceStore;
32 import org.jboss.portal.server.plugins.preferences.SimpleMergeStrategy;
33 import org.jboss.portal.server.user.UserContext;
34
35 /**
36  * This interceptor is responsible for setting up the
37  * javax.portlet.PortletPreferences implementation
38  * instance in the invocation.
39  *
40  * todo : make the merge strategy configurable
41  *
42  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
43  * @version $Revision: 1.3 $
44  */

45 public class PreferencesInterceptor
46       implements Interceptor
47 {
48
49    public Object JavaDoc invoke(Invocation invocation)
50    {
51       InvocationType pitype = (InvocationType)invocation.getAttachment(AttachmentKey.INVOCATION_TYPE);
52
53       boolean isAction = pitype == InvocationType.ACTION;
54       boolean isRender = pitype == InvocationType.RENDER;
55
56       if (isAction || isRender)
57       {
58          UserContext userContext = (UserContext)invocation.getAttachment(AttachmentKey.USER_CONTEXT);
59          Window window = (Window)invocation.getAttachment(PortletKey.WINDOW);
60          Instance instance = window.getInstance();
61          Component component = instance.getComponent();
62
63          try
64          {
65             // The list of preferences set
66
List JavaDoc list = new ArrayList JavaDoc();
67
68             // Add portlet prefs
69
PortletPreferenceSetPlugin componentPrefs = (PortletPreferenceSetPlugin)component.getPlugin(PortalConstants.PLUGIN_PREFS);
70             PreferencesValidator validator = componentPrefs.getPreferencesValidator();
71             list.add(componentPrefs);
72
73             // Add instance prefs if any
74
PreferenceSetPlugin instancePrefs = (PreferenceSetPlugin)instance.getPlugin(PortalConstants.PLUGIN_PREFS);
75             if (instancePrefs != null)
76             {
77                list.add(instancePrefs);
78             }
79
80             // Add user prefs last which is the one modified
81
PreferenceStore store = userContext.getPreferenceStore();
82             PreferenceSet user = store.get(instance.getID());
83
84             //
85
PreferenceSet[] sets = (PreferenceSet[])list.toArray(new PreferenceSet[list.size()]);
86             MergeStrategy strategy = SimpleMergeStrategy.getSingleton();
87             PreferenceSet system = new MergedPreferenceSet(sets, strategy);
88             int mode = isAction ? PortletPreferencesImpl.ACTION : PortletPreferencesImpl.RENDER;
89             PortletPreferences prefs = new PortletPreferencesImpl(system, user, validator, mode);
90
91             //
92
invocation.setAttachment(PortletKey.PORTLET_PREFERENCES, prefs);
93             return invocation.invokeNext();
94          }
95          finally
96          {
97             invocation.removeAttachment(PortletKey.PORTLET_PREFERENCES);
98          }
99       }
100       else
101       {
102          return invocation.invokeNext();
103       }
104    }
105
106 }
107
Popular Tags