001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portlet;
023:
024: import org.jboss.portal.common.util.ProxyInfo;
025: import org.jboss.portal.portlet.impl.jsr168.api.PortletPreferencesImpl;
026: import org.jboss.portal.portlet.impl.jsr168.info.ContainerPreferencesInfo;
027: import org.jboss.portal.portlet.state.PropertyContext;
028:
029: import javax.portlet.PreferencesValidator;
030: import java.lang.reflect.InvocationHandler;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033:
034: /**
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8786 $
037: */
038: public class JBossPortletPreferences extends PortletPreferencesImpl
039: implements InvocationHandler {
040:
041: private ProxyInfo proxyInfo;
042:
043: public JBossPortletPreferences(PropertyContext manager,
044: ContainerPreferencesInfo containerPrefs,
045: PreferencesValidator validator, int mode,
046: ProxyInfo proxyInfo) {
047: super (manager, containerPrefs, validator, mode);
048: this .proxyInfo = proxyInfo;
049: }
050:
051: public Object getProxy() {
052: if (proxy == null) {
053: if (proxyInfo != null) {
054: try {
055: proxy = proxyInfo.instantiate(this );
056: } catch (InstantiationException e) {
057: log.error("Cannot instantiate proxy", e);
058: } catch (IllegalAccessException e) {
059: log.error("Cannot instantiate proxy", e);
060: } catch (InvocationTargetException e) {
061: log.error("Cannot instantiate proxy", e);
062: }
063: }
064: }
065: return proxy;
066: }
067:
068: private Object proxy;
069:
070: public Object invoke(Object proxy, Method method, Object[] args)
071: throws Throwable {
072: /*
073: // First handle the Object methods
074: if (proxyInfo.getToString().equals(method))
075: {
076: return "proxy";
077: }
078: else if (proxyInfo.getHashCode().equals(method))
079: {
080: return new Integer(System.identityHashCode(proxy));
081: }
082: else if (proxyInfo.getEquals().equals(method))
083: {
084: return Boolean.valueOf(proxy == args[0]);
085: }
086:
087: // The preference name
088: String key = method.getName().substring(3);
089:
090: if (method.getName().startsWith("get"))
091: {
092: // Get return type
093: Class returnType = method.getReturnType();
094:
095: Preference preference = strategy.getPreference(sets1, key);
096: if (preference != null)
097: {
098: Value value = preference.getValue();
099: if (String.class.equals(returnType))
100: {
101: return value.asString();
102: }
103: else if (String[].class.equals(returnType))
104: {
105: return value.asStringArray();
106: }
107: else if (int[].class.equals(returnType))
108: {
109: try
110: {
111: return value.asIntArray();
112: }
113: catch (ConversionException e)
114: {
115: return args[0];
116: }
117: }
118: else if (boolean[].class.equals(returnType))
119: {
120: try
121: {
122: return value.asBooleanArray();
123: }
124: catch (ConversionException e)
125: {
126: return args[0];
127: }
128: }
129: else if (returnType.isPrimitive())
130: {
131: try
132: {
133: if (boolean.class.equals(returnType))
134: {
135: return Boolean.valueOf(value.asBoolean());
136: }
137: else if (boolean[].class.equals(returnType))
138: {
139: return value.asBooleanArray();
140: }
141: else if (int.class.equals(returnType))
142: {
143: return new Integer(value.asInt());
144: }
145: else if (int[].class.equals(returnType))
146: {
147: return value.asIntArray();
148: }
149: else
150: {
151: return args[0];
152: }
153: }
154: catch (ConversionException e)
155: {
156: return args[0];
157: }
158: }
159: else
160: {
161: return args[0];
162: }
163: }
164: else
165:
166: {
167: // Return the default argument
168: return args[0];
169: }
170: }
171: else
172: {
173: // this is a set
174: Class clazz = method.getParameterTypes()[0];
175:
176: // The value
177: Value value = null;
178:
179: // If it is not null
180: if (args[0] != null)
181: {
182: // Get the value from the invocation
183: if (String.class.equals(clazz))
184: {
185: value = new StringValues((String)args[0]);
186: }
187: else if (String[].class.equals(clazz))
188: {
189: value = new StringValues((String[])args[0]);
190: }
191: else if (int.class.equals(clazz))
192: {
193: value = new IntegerValues((Integer)args[0]);
194: }
195: else if (int[].class.equals(clazz))
196: {
197: value = new IntegerValues((int[])args[0]);
198: }
199: else if (boolean.class.equals(clazz))
200: {
201: value = new BooleanValues((Boolean)args[0]);
202: }
203: else if (boolean[].class.equals(clazz))
204: {
205: value = new BooleanValues((boolean[])args[0]);
206: }
207: else
208: {
209: throw new IllegalArgumentException("bad type");
210: }
211: }
212:
213: // Finally set the property
214: updates.put(key, new Update(value));
215:
216: // This is a setter we don't care what we return
217: return null;
218: }
219: */
220: throw new Error();
221: }
222: }
|