1 package com.icesoft.util; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 6 import java.lang.reflect.Method ; 7 import java.lang.reflect.Field ; 8 import java.util.StringTokenizer ; 9 10 import javax.faces.event.PhaseListener; 11 import javax.faces.lifecycle.Lifecycle; 12 import javax.faces.context.FacesContext; 13 14 25 public class SeamUtilities { 26 27 private static final Log log = 28 LogFactory.getLog(SeamUtilities.class); 29 30 32 private static Class seamManagerClass; 33 34 private static Class [] seamClassArgs = new Class [0]; 35 private static Object [] seamInstanceArgs = new Object [0]; 36 private static Class [] seamGetEncodeMethodArgs = {String .class, String .class}; 37 private static Object [] seamEncodeMethodArgs = new Object [2]; 38 39 private static Object [] seamMethodNoArgs = new Object [0]; 40 41 private static Method seamConversationIdMethodInstance; 42 private static Method seamLongRunningMethodInstance; 43 private static Method seamAppendConversationMethodInstance; 44 private static Method seamInstanceMethod; 45 private static Method seamPageContextGetPrefixInstance; 46 47 private static Method seamConversationIdParameterMethod; 49 50 53 56 58 private static Object pageContextInstance; 59 60 private static String conversationIdParameter; 62 private static String conversationParentParameter = "parentConversationId"; 63 65 private static String seamVersion = "none"; 67 private static Method seamVersionMethod; 68 69 private static String SPRING_CLASS_NAME = 70 "org.springframework.webflow.executor.jsf.FlowVariableResolver"; 71 72 private static boolean isSpringLoaded; 73 74 static { 75 loadSeamEnvironment(); 76 loadSpringEnvironment(); 77 } 78 79 84 public static boolean isSeamEnvironment() { 85 return seamManagerClass != null; 86 } 87 88 94 public static boolean requiresSeamExpressionFactory(){ 95 return (seamVersion.startsWith("1.2.1")); 96 } 97 98 109 public static String encodeSeamConversationId(String uri, String viewId) { 110 111 if (! isSeamEnvironment() ) { 113 return uri; 114 } 115 116 String cleanedUrl = uri; 117 118 if (conversationIdParameter == null) { 119 getConversationIdParameterName(); 120 } 121 122 125 128 if (log.isTraceEnabled()) { 129 log.trace("SeamConversationURLParam: " + conversationIdParameter); 130 } 131 StringTokenizer st = new StringTokenizer ( uri, "?&"); 132 StringBuffer builder = new StringBuffer (); 133 134 String token; 135 boolean first = true; 136 while(st.hasMoreTokens() ){ 137 token = st.nextToken(); 138 if ( (token.indexOf(conversationIdParameter) > -1) || 139 (token.indexOf(conversationParentParameter) > -1) || 140 token.indexOf("rvn") > -1 ) { 142 continue; 143 } 144 builder.append(token); 145 146 if (st.hasMoreTokens() ) { 147 if (first) { 148 builder.append('?'); 149 first = false; 150 } else { 151 builder.append('&'); 152 } 153 } 154 } 155 156 if (builder.length() > 0) { 157 cleanedUrl = builder.toString(); 158 } 159 try { 162 163 Object seamManagerInstance = 165 seamInstanceMethod.invoke(null, seamInstanceArgs); 166 167 if (seamAppendConversationMethodInstance != null) { 168 seamEncodeMethodArgs[0] = cleanedUrl; 169 if (seamEncodeMethodArgs.length == 2) { 170 seamEncodeMethodArgs[1] = viewId; 171 } 172 173 176 177 cleanedUrl = (String ) seamAppendConversationMethodInstance 179 .invoke(seamManagerInstance, seamEncodeMethodArgs); 180 181 182 183 if (log.isDebugEnabled()) { 184 log.debug("Enabled redirect from: " + 185 uri + ", to: " + cleanedUrl); 186 } 187 } 188 } catch (Exception e) { 189 seamInstanceMethod = null; 190 seamManagerClass = null; 191 log.error("Exception encoding seam conversationId: ", e); 192 193 } 194 return cleanedUrl; 195 } 196 197 208 public static String getSeamConversationId() { 209 210 if ( !isSeamEnvironment()) { 211 return null; 212 } 213 214 String returnVal = null; 215 216 try { 217 Object seamManagerInstance = 220 seamInstanceMethod.invoke(null, seamMethodNoArgs); 221 222 if (seamConversationIdMethodInstance != null) { 223 224 String conversationId = 225 (String ) seamConversationIdMethodInstance.invoke( 226 seamManagerInstance, seamMethodNoArgs); 227 228 Boolean is = (Boolean ) seamLongRunningMethodInstance 229 .invoke(seamManagerInstance, 230 seamMethodNoArgs); 231 232 if (is.booleanValue()) { 233 returnVal = conversationId; 234 } 235 } 236 237 } catch (Exception e) { 238 seamInstanceMethod = null; 239 seamManagerClass = null; 240 log.error("Exception determining Seam ConversationId: ", e); 241 242 } 243 return returnVal; 244 } 245 246 257 public static String getPageContextKey() { 258 259 String returnVal = ""; 260 if (!isSeamEnvironment()) { 261 return returnVal; 262 } 263 264 try { 265 266 if (seamConversationIdMethodInstance != null) { 267 returnVal = (String ) seamPageContextGetPrefixInstance.invoke( 268 pageContextInstance, seamMethodNoArgs); 269 } 270 271 } catch (Exception e) { 272 log.error("Exception fetching Page from ScopeType: ", e); 273 274 } 275 return returnVal; 276 } 277 278 279 280 287 private static void loadSeamEnvironment() { 288 try { 289 290 seamManagerClass = Class.forName("org.jboss.seam.core.Manager"); 292 Class seamScopeTypeClass = Class.forName("org.jboss.seam.ScopeType"); 293 294 295 296 seamInstanceMethod = seamManagerClass.getMethod("instance", 298 seamClassArgs); 299 300 Field fieldInstance = seamScopeTypeClass.getField("PAGE"); 301 302 303 304 pageContextInstance = fieldInstance.get(seamScopeTypeClass); 305 306 307 seamPageContextGetPrefixInstance = seamScopeTypeClass.getMethod( 308 "getPrefix", seamClassArgs); 309 310 try { 312 Class seamClass = Class.forName("org.jboss.seam.Seam"); 313 seamVersionMethod = seamClass.getMethod("getVersion",null); 314 if (seamVersionMethod!=null){ 315 seamVersion = (String )seamVersionMethod.invoke(null,seamMethodNoArgs); 316 log.info("SeamUtilities: loadSeam.. seamVersion="+seamVersion); 317 } 318 } catch (NoSuchMethodException e){ 319 320 seamVersion="1.2.1.GA"; 321 log.info("\t -->>>> seamVersion is null"); 322 } 323 log.info("\t ->>> seamVersion="+seamVersion); 324 325 try { 326 seamAppendConversationMethodInstance = 327 seamManagerClass.getMethod("encodeConversationId", 328 seamGetEncodeMethodArgs); 329 } catch (NoSuchMethodException e) { 330 333 seamGetEncodeMethodArgs = new Class [] {String .class}; 334 seamEncodeMethodArgs = new Object [1]; 335 seamAppendConversationMethodInstance = 336 seamManagerClass.getMethod("encodeConversationId", 337 seamGetEncodeMethodArgs); 338 } 339 340 seamConversationIdMethodInstance = 341 seamManagerClass.getMethod("getCurrentConversationId", 342 seamClassArgs); 343 seamLongRunningMethodInstance = 344 seamManagerClass.getMethod("isLongRunningConversation", 345 seamClassArgs); 346 347 348 seamConversationIdParameterMethod = 349 seamManagerClass.getMethod("getConversationIdParameter", 350 seamClassArgs); 351 352 357 358 362 366 367 368 Class.forName("org.jboss.seam.util.Parameters"); 369 370 371 } catch (ClassNotFoundException cnf) { 372 } catch (Exception e) { 374 seamInstanceMethod = null; 375 seamManagerClass = null; 376 log.info("Exception loading seam environment: ", e); 377 } 378 379 if (seamManagerClass != null) { 380 log.info("Seam environment detected "); 381 } 382 } 383 384 385 402 public static String getConversationIdParameterName() { 403 if (!isSeamEnvironment()) { 404 return null; 405 } 406 if (conversationIdParameter != null ) { 407 return conversationIdParameter; 408 } 409 410 String returnVal = null; 411 try { 412 413 Object seamManagerInstance = 414 seamInstanceMethod.invoke(null, seamMethodNoArgs); 415 416 if (seamConversationIdParameterMethod != null) { 418 419 returnVal = (String ) seamConversationIdParameterMethod. 420 invoke(seamManagerInstance, seamMethodNoArgs); 421 conversationIdParameter = returnVal; 422 } 423 424 430 436 437 } catch (Exception e) { 438 log.error("Exception fetching conversationId Parameter name: ", e); 439 440 } 441 return returnVal; 442 } 443 444 451 public static void removeSeamDebugPhaseListener(Lifecycle lifecycle) { 452 PhaseListener[] phaseListeners = lifecycle.getPhaseListeners(); 453 for(int i = 0; i < phaseListeners.length; i++) { 456 if( phaseListeners[i].getClass().getName().equals( 458 "org.jboss.seam.debug.jsf.SeamDebugPhaseListener") ) 459 { 460 lifecycle.removePhaseListener(phaseListeners[i]); 461 seamDebugPhaseListenerClassLoader = phaseListeners[i].getClass().getClassLoader(); 463 } 465 } 466 } 467 468 public static ClassLoader getSeamDebugPhaseListenerClassLoader() { 469 return seamDebugPhaseListenerClassLoader; 470 } 471 472 private static ClassLoader seamDebugPhaseListenerClassLoader; 473 474 475 476 479 private static void loadSpringEnvironment() { 480 Class flowVariableResolver = null; 481 try { 482 flowVariableResolver = Class.forName(SPRING_CLASS_NAME); 483 } catch (Throwable t) { 484 if (log.isDebugEnabled()) { 485 log.debug("Spring webflow not detected: " + t); 486 } 487 } 488 if (null != flowVariableResolver) { 489 isSpringLoaded = true; 490 if (log.isDebugEnabled()) { 491 log.debug("Spring webflow detected: " + flowVariableResolver); 492 } 493 } 494 495 } 496 497 502 public static boolean isSpringEnvironment() { 503 return isSpringLoaded; 504 } 505 506 511 public static String getSpringFlowId() { 512 if ( !isSpringEnvironment()) { 513 return null; 514 } 515 FacesContext facesContext = FacesContext.getCurrentInstance(); 516 Object value = facesContext.getApplication() 518 .createValueBinding("#{flowExecutionKey}").getValue(facesContext); 519 if (null == value) { 520 return null; 521 } 522 523 return value.toString(); 524 } 525 530 public static String getFlowIdParameterName() { 531 return "_flowExecutionKey"; 532 } 533 534 } 535 | Popular Tags |