KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.InvocationHandler JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14
15 import javax.portlet.PreferencesValidator;
16
17 import org.jboss.portal.common.util.ProxyInfo;
18 import org.jboss.portal.common.value.BooleanValues;
19 import org.jboss.portal.common.value.ConversionException;
20 import org.jboss.portal.common.value.IntegerValues;
21 import org.jboss.portal.common.value.StringValues;
22 import org.jboss.portal.common.value.Value;
23 import org.jboss.portal.portlet.impl.PortletPreferencesImpl;
24 import org.jboss.portal.server.plugins.preferences.Preference;
25 import org.jboss.portal.server.plugins.preferences.PreferenceSet;
26
27 /**
28  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
29  * @version $Revision: 1.4 $
30  */

31 public class JBossPortletPreferences
32    extends PortletPreferencesImpl
33    implements InvocationHandler JavaDoc
34 {
35
36    private ProxyInfo proxyInfo;
37
38    public JBossPortletPreferences(
39          PreferenceSet system,
40          PreferenceSet user,
41          PreferencesValidator validator,
42          int mode,
43          ProxyInfo proxyInfo)
44    {
45       super(system, user, validator, mode);
46       this.proxyInfo = proxyInfo;
47    }
48
49    public Object JavaDoc getProxy()
50    {
51       if (proxy == null)
52       {
53          if (proxyInfo != null)
54          {
55             try
56             {
57                proxy = proxyInfo.instantiate(this);
58             }
59             catch (InstantiationException JavaDoc e)
60             {
61                log.error("Cannot instantiate proxy", e);
62             }
63             catch (IllegalAccessException JavaDoc e)
64             {
65                log.error("Cannot instantiate proxy", e);
66             }
67             catch (InvocationTargetException JavaDoc e)
68             {
69                log.error("Cannot instantiate proxy", e);
70             }
71          }
72       }
73       return proxy;
74    }
75
76    private Object JavaDoc proxy;
77
78    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
79    {
80       // First handle the Object methods
81
if (proxyInfo.getToString().equals(method))
82       {
83          return "proxy";
84       }
85       else if (proxyInfo.getHashCode().equals(method))
86       {
87          return new Integer JavaDoc(System.identityHashCode(proxy));
88       }
89       else if (proxyInfo.getEquals().equals(method))
90       {
91          return Boolean.valueOf(proxy == args[0]);
92       }
93
94       // The preference name
95
String JavaDoc key = method.getName().substring(3);
96
97       if (method.getName().startsWith("get"))
98       {
99          // Get return type
100
Class JavaDoc returnType = method.getReturnType();
101
102          Preference preference = strategy.getPreference(sets1, key);
103          if (preference != null)
104          {
105             Value value = preference.getValue();
106             if (String JavaDoc.class.equals(returnType))
107             {
108                return value.asString();
109             }
110             else if (String JavaDoc[].class.equals(returnType))
111             {
112                return value.asStringArray();
113             }
114             else if (int[].class.equals(returnType))
115             {
116                try
117                {
118                   return value.asIntArray();
119                }
120                catch (ConversionException e)
121                {
122                   return args[0];
123                }
124             }
125             else if (boolean[].class.equals(returnType))
126             {
127                try
128                {
129                   return value.asBooleanArray();
130                }
131                catch (ConversionException e)
132                {
133                   return args[0];
134                }
135             }
136             else if (returnType.isPrimitive())
137             {
138                try
139                {
140                   if (boolean.class.equals(returnType))
141                   {
142                      return Boolean.valueOf(value.asBoolean());
143                   }
144                   else if (boolean[].class.equals(returnType))
145                   {
146                      return value.asBooleanArray();
147                   }
148                   else if (int.class.equals(returnType))
149                   {
150                      return new Integer JavaDoc(value.asInt());
151                   }
152                   else if (int[].class.equals(returnType))
153                   {
154                      return value.asIntArray();
155                   }
156                   else
157                   {
158                      return args[0];
159                   }
160                }
161                catch (ConversionException e)
162                {
163                   return args[0];
164                }
165             }
166             else
167             {
168                return args[0];
169             }
170          }
171          else
172
173          {
174             // Return the default argument
175
return args[0];
176          }
177       }
178       else
179       {
180          // this is a set
181
Class JavaDoc clazz = method.getParameterTypes()[0];
182
183          // The value
184
Value value = null;
185
186          // If it is not null
187
if (args[0] != null)
188          {
189             // Get the value from the invocation
190
if (String JavaDoc.class.equals(clazz))
191             {
192                value = new StringValues((String JavaDoc)args[0]);
193             }
194             else if (String JavaDoc[].class.equals(clazz))
195             {
196                value = new StringValues((String JavaDoc[])args[0]);
197             }
198             else if (int.class.equals(clazz))
199             {
200                value = new IntegerValues((Integer JavaDoc)args[0]);
201             }
202             else if (int[].class.equals(clazz))
203             {
204                value = new IntegerValues((int[])args[0]);
205             }
206             else if (boolean.class.equals(clazz))
207             {
208                value = new BooleanValues((Boolean JavaDoc)args[0]);
209             }
210             else if (boolean[].class.equals(clazz))
211             {
212                value = new BooleanValues((boolean[])args[0]);
213             }
214             else
215             {
216                throw new IllegalArgumentException JavaDoc("bad type");
217             }
218          }
219
220          // Finally set the property
221
updates.put(key, new Update(value));
222
223          // This is a setter we don't care what we return
224
return null;
225       }
226    }
227 }
228
Popular Tags