1 16 17 package org.apache.jetspeed.util; 18 19 import org.apache.turbine.util.RunData; 20 21 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 22 import org.apache.jetspeed.portal.Portlet; 23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 24 import org.apache.jetspeed.services.logging.JetspeedLogger; 25 import org.apache.jetspeed.services.persistence.PersistenceManager; 26 import org.apache.jetspeed.portal.PortletInstance; 27 28 35 public class PortletSessionState 36 { 37 40 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PortletSessionState.class.getName()); 41 42 45 public static final String CONFIG_CHANGED = "config_changed"; 46 47 50 public static Object getAttribute(RunData rundata, String attrName) 51 { 52 return rundata.getUser().getTemp(attrName); 53 } 54 55 public static Object getAttribute(RunData rundata, String attrName, Object defValue) 56 { 57 Object o = rundata.getUser().getTemp(attrName, defValue); 58 if (o instanceof java.lang.String && !(defValue instanceof java.lang.String )) 59 { 60 return defValue; 61 } 62 return o; 63 } 64 65 69 public static void setAttribute(RunData rundata, String attrName, Object attrValue) 70 { 71 rundata.getUser().setTemp(attrName, attrValue); 72 } 73 74 78 public static void clearAttribute(RunData rundata, String attrName) 79 { 80 rundata.getUser().removeTemp(attrName); 81 } 82 83 87 public static Object getAttribute(Portlet portlet, RunData rundata, String attrName) 88 { 89 return rundata.getUser().getTemp(generateKey(portlet, attrName)); 90 } 91 92 107 public static Object getAttributeWithFallback(Portlet portlet, RunData rundata, String attrName) 108 { 109 Object result = null; 110 111 if(isMyRequest(rundata, portlet)) 113 { 114 result = rundata.getParameters().getString(attrName); 115 if (result != null) 116 { 117 if (result.toString().trim().equalsIgnoreCase("")) 118 { 119 clearAttribute(portlet, rundata, attrName); 120 result = null; 121 } 122 else 123 { 124 setAttribute(portlet, rundata, attrName, result); 125 } 126 } 127 } 128 129 if (result == null) 131 { 132 result = getAttribute(portlet, rundata, attrName); 133 } 134 135 if (result == null) 137 { 138 result = portlet.getAttribute(attrName, null, rundata); 139 } 140 141 if (result == null) 143 { 144 result = portlet.getPortletConfig().getInitParameter(attrName); 145 } 146 147 return result; 148 149 } 150 151 159 public static boolean isMyRequest(RunData rundata, Portlet portlet) { 160 161 String requestPeid = rundata.getParameters().getString("js_peid"); 164 if (requestPeid == null || requestPeid.equalsIgnoreCase("")) 165 { 166 return true; 167 } 168 169 if (portlet == null || portlet.getID() == null) 172 { 173 return true; 174 } 175 176 String peId = null; 178 PortletInstance instance = PersistenceManager.getInstance(portlet, rundata); 179 if (instance != null) 180 { 181 peId = instance.getPortlet().getID(); 182 } 183 184 if (peId != null && peId.equals(requestPeid)) 186 { 187 return true; 188 } 189 else 190 { 191 return false; 192 } 193 } 194 195 199 public static void setAttribute(Portlet portlet, 200 RunData rundata, 201 String attrName, 202 Object attrValue) 203 { 204 rundata.getUser().setTemp(generateKey(portlet, attrName), attrValue); 205 } 206 207 211 public static void clearAttribute(Portlet portlet, RunData rundata, String attrName) 212 { 213 rundata.getUser().removeTemp(generateKey(portlet, attrName)); 214 } 215 216 219 protected static String generateKey(Portlet portlet, String name) 220 { 221 if (portlet != null) 222 { 223 return (portlet.getID()+"."+name); 224 } 225 else 226 { 227 logger.error("PortletSessionState: Passed null Velocity Portlet for name: " + name); 228 return name; 229 } 230 } 231 232 236 public static Object getAttribute(VelocityPortlet portlet, RunData rundata, String attrName) 237 { 238 return getAttribute((Portlet) portlet, rundata, attrName); 239 } 240 241 245 public static void setAttribute(VelocityPortlet portlet, 246 RunData rundata, 247 String attrName, 248 Object attrValue) 249 { 250 setAttribute((Portlet) portlet, rundata, attrName, attrValue); 251 } 252 253 257 public static void clearAttribute(VelocityPortlet portlet, RunData rundata, String attrName) 258 { 259 clearAttribute((Portlet) portlet, rundata, attrName); 260 } 261 262 269 public static void setPortletConfigChanged(Portlet portlet, RunData rundata) 270 { 271 setAttribute(portlet, rundata, CONFIG_CHANGED, "true"); 272 } 273 274 281 public static boolean getPortletConfigChanged(Portlet portlet, RunData rundata) 282 { 283 String state = (String ) getAttribute(portlet, rundata, CONFIG_CHANGED); 284 if (state != null) 285 { 286 clearAttribute(portlet, rundata, CONFIG_CHANGED); 287 } 288 289 return state != null; 290 } 291 292 } 293 | Popular Tags |