1 9 package org.jboss.portlet; 10 11 import java.lang.reflect.InvocationHandler ; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.lang.reflect.Method ; 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 31 public class JBossPortletPreferences 32 extends PortletPreferencesImpl 33 implements InvocationHandler 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 getProxy() 50 { 51 if (proxy == null) 52 { 53 if (proxyInfo != null) 54 { 55 try 56 { 57 proxy = proxyInfo.instantiate(this); 58 } 59 catch (InstantiationException e) 60 { 61 log.error("Cannot instantiate proxy", e); 62 } 63 catch (IllegalAccessException e) 64 { 65 log.error("Cannot instantiate proxy", e); 66 } 67 catch (InvocationTargetException e) 68 { 69 log.error("Cannot instantiate proxy", e); 70 } 71 } 72 } 73 return proxy; 74 } 75 76 private Object proxy; 77 78 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 79 { 80 if (proxyInfo.getToString().equals(method)) 82 { 83 return "proxy"; 84 } 85 else if (proxyInfo.getHashCode().equals(method)) 86 { 87 return new Integer (System.identityHashCode(proxy)); 88 } 89 else if (proxyInfo.getEquals().equals(method)) 90 { 91 return Boolean.valueOf(proxy == args[0]); 92 } 93 94 String key = method.getName().substring(3); 96 97 if (method.getName().startsWith("get")) 98 { 99 Class returnType = method.getReturnType(); 101 102 Preference preference = strategy.getPreference(sets1, key); 103 if (preference != null) 104 { 105 Value value = preference.getValue(); 106 if (String .class.equals(returnType)) 107 { 108 return value.asString(); 109 } 110 else if (String [].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 (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 args[0]; 176 } 177 } 178 else 179 { 180 Class clazz = method.getParameterTypes()[0]; 182 183 Value value = null; 185 186 if (args[0] != null) 188 { 189 if (String .class.equals(clazz)) 191 { 192 value = new StringValues((String )args[0]); 193 } 194 else if (String [].class.equals(clazz)) 195 { 196 value = new StringValues((String [])args[0]); 197 } 198 else if (int.class.equals(clazz)) 199 { 200 value = new IntegerValues((Integer )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 )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 ("bad type"); 217 } 218 } 219 220 updates.put(key, new Update(value)); 222 223 return null; 225 } 226 } 227 } 228 | Popular Tags |