1 81 package com.ivata.groupware.web.tag.container; 82 83 import org.apache.log4j.Logger; 84 85 import javax.servlet.http.HttpServletRequest ; 86 import javax.servlet.http.HttpSession ; 87 import javax.servlet.jsp.JspException ; 88 import javax.servlet.jsp.PageContext ; 89 import javax.servlet.jsp.tagext.BodyTagSupport ; 90 91 import org.apache.struts.taglib.TagUtils; 92 import org.picocontainer.PicoContainer; 93 94 import com.ivata.groupware.admin.security.server.SecuritySession; 95 import com.ivata.groupware.container.PicoContainerFactory; 96 import com.ivata.mask.util.StringHandling; 97 import com.ivata.mask.util.SystemException; 98 99 100 147 public class ContainerBeanTag extends BodyTagSupport { 148 151 private static final Logger logger = Logger 152 .getLogger(ContainerBeanTag.class); 153 154 private final static String [] SCOPES = new String [] { 155 "application", 156 "session", 157 "request", 158 "page" 159 }; 160 161 167 private String body; 168 173 private String id = null; 174 175 181 private String name; 182 183 189 private String property; 190 195 private String scope; 196 197 203 private String toScope; 204 205 209 private String type = null; 210 211 217 private Object value; 218 219 236 private Object beanLookup(final PageContext pageContext, 237 final String beanName, 238 final String property, 239 final String beanScope) 240 throws JspException { 241 TagUtils tagUtils = TagUtils.getInstance(); 242 Object thisValue = tagUtils.lookup(pageContext, beanName, beanScope); 243 if ((thisValue != null) 247 && (property != null)) { 248 thisValue = tagUtils.lookup(pageContext, beanName, property, beanScope); 249 } 250 return thisValue; 251 } 252 253 258 public int doAfterBody() throws JspException { 259 String thisBody; 260 if ((bodyContent != null) 261 && ((thisBody = bodyContent.getString()) != null) 262 && ((thisBody = thisBody.trim()).length() > 0)) { 263 body = thisBody; 264 } 265 return SKIP_BODY; 266 267 } 268 269 public int doEndTag() throws JspException { 270 HttpSession session = pageContext.getSession(); 272 HttpServletRequest request = (HttpServletRequest ) 273 pageContext.getRequest(); 274 275 Object thisValue = null; 276 String actualType = this.type; 277 if (actualType == null) { 278 if ((name == null) || (value != null)) { 281 actualType = "java.lang.String"; 282 } else { 283 actualType = "java.lang.Object"; 284 } 285 } 286 287 if (body != null) { 288 if (value != null) { 290 throw new JspException ("ERROR in ContainerBeanTag(id " 291 + id 292 + "): you cannot specify both tag body ('" 293 + body 294 + "') and value attribute ('" 295 + value 296 + "'"); 297 } 298 thisValue = body; 299 actualType = "java.lang.String"; 300 } else { 301 thisValue = value; 302 } 303 304 if (thisValue == null) { 306 String beanName = name == null ? id : name; 308 String beanScope = scope; 309 310 if (scope == null) { 311 Class searchClass; 312 try { 313 searchClass = Class.forName(actualType); 314 } catch (ClassNotFoundException e) { 315 throw new JspException (e); 316 } 317 318 319 for (int i = 0; i < SCOPES.length; i++) { 322 thisValue = beanLookup(pageContext, beanName, 323 property, beanScope = SCOPES[i]); 324 if ((thisValue != null) 326 && searchClass.isAssignableFrom( 327 thisValue.getClass())) { 328 break; 329 } 330 } 331 } else { 332 thisValue = beanLookup(pageContext, beanName, 333 property, beanScope); 334 } 335 } 336 337 if ((thisValue == null) 341 && "java.lang.String".equals(actualType) 342 && !StringHandling.isNullOrEmpty(request.getParameter(id))) { 343 thisValue = request.getParameter(id); 344 } 345 346 if (thisValue == null) { 349 if ("java.lang.Boolean".equals(actualType)) { 351 thisValue = Boolean.FALSE; 352 } else if ("java.lang.Byte".equals(actualType)) { 353 thisValue = new Byte ((byte)0); 354 } else if ("java.lang.Character".equals(actualType)) { 355 thisValue = new Character ((char)0); 356 } else if ("java.lang.double".equals(actualType)) { 357 thisValue = new Double (0L); 358 } else if ("java.lang.Float".equals(actualType)) { 359 thisValue = new Float (0L); 360 } else if ("java.lang.Integer".equals(actualType)) { 361 thisValue = new Integer (0); 362 } else if ("java.lang.Long".equals(actualType)) { 363 thisValue = new Long (0L); 364 } else if ("java.lang.Short".equals(actualType)) { 365 thisValue = new Short ((short)0); 366 } else { 367 SecuritySession securitySession = (SecuritySession) 369 session.getAttribute("securitySession"); 370 PicoContainer container; 371 372 PicoContainerFactory factory; 376 try { 377 factory = PicoContainerFactory.getInstance(); 378 } catch (SystemException e) { 379 throw new JspException (e); 380 } 381 if (securitySession == null) { 382 container = factory.getGlobalContainer(); 383 } else { 384 container = securitySession.getContainer(); 385 } 386 try { 388 container = factory.override(container); 389 thisValue = 390 factory.instantiateOrOverride(container, 391 actualType); 392 } catch (SystemException e) { 393 throw new JspException (e); 394 } 395 } 396 } 397 398 if (thisValue == null) { 399 throw new JspException ("ERROR: could not instantiate id '" 400 + id 401 + "' for class '" 402 + actualType 403 + "'"); 404 } 405 406 int writeScope; 408 TagUtils tagUtils = TagUtils.getInstance(); 409 if (toScope != null) { 410 writeScope = tagUtils.getScope(toScope); 411 } else if (scope != null) { 412 writeScope = tagUtils.getScope(scope); 413 } else { 414 writeScope = PageContext.PAGE_SCOPE; 415 } 416 pageContext.setAttribute(id, thisValue, writeScope); 417 418 return EVAL_PAGE; 419 } 420 421 437 public int doStartTag() throws JspException { 438 return EVAL_BODY_BUFFERED; 439 } 440 441 442 449 public final String getId() { 450 return id; 451 } 452 453 461 public final String getName() { 462 return name; 463 } 464 472 public final String getProperty() { 473 return property; 474 } 475 476 483 public final String getScope() { 484 return scope; 485 } 486 494 public final String getToScope() { 495 return toScope; 496 } 497 498 504 public final String getType() { 505 return type; 506 } 507 515 public final Object getValue() { 516 return value; 517 } 518 519 526 public final void setId(final String string) { 527 id = string; 528 } 529 537 public final void setName(final String name) { 538 this.name = name; 539 } 540 548 public final void setProperty(final String property) { 549 this.property = property; 550 } 551 552 559 public final void setScope(final String string) { 560 scope = string; 561 } 562 570 public final void setToScope(final String toScope) { 571 this.toScope = toScope; 572 } 573 574 579 public final void setType(final String string) { 580 type = string; 581 } 582 590 public final void setValue(final Object value) { 591 this.value = value; 592 } 593 } 594 | Popular Tags |