1 13 package com.tonbeller.wcf.component; 14 15 import javax.servlet.http.HttpSession ; 16 import javax.servlet.jsp.JspException ; 17 import javax.servlet.jsp.tagext.TagSupport ; 18 19 import org.apache.log4j.Logger; 20 21 import com.tonbeller.wcf.bookmarks.BookmarkManager; 22 import com.tonbeller.wcf.bookmarks.Bookmarkable; 23 import com.tonbeller.wcf.controller.RequestContext; 24 25 30 public abstract class ComponentTag extends TagSupport { 31 boolean objectCreated; 32 boolean visible = true; 33 boolean validate = false; 34 String role; 35 36 private static Logger logger = Logger.getLogger(ComponentTag.class); 37 38 43 protected abstract Component createComponent(RequestContext context) throws Exception ; 44 45 49 public int doStartTag() throws JspException { 50 logger.info(id); 51 try { 52 objectCreated = false; 53 HttpSession session = pageContext.getSession(); 54 if (session.getAttribute(getId()) == null) { 55 if (logger.isInfoEnabled()) 56 logger.info("creating " + id); 57 Component comp = createComponent(RequestContext.instance()); 58 session.setAttribute(getId(), comp); 59 objectCreated = true; 60 return EVAL_BODY_INCLUDE; 61 } 62 return SKIP_BODY; 63 } catch (Exception e) { 64 logger.error("trouble creating " + getId(), e); 65 throw new JspException (e); 66 } 67 } 68 69 72 public Component getComponent() { 73 return (Component) pageContext.getSession().getAttribute(getId()); 74 } 75 76 80 public boolean isObjectCreated() { 81 return objectCreated; 82 } 83 84 85 public int doEndTag() throws JspException { 86 logger.info(id); 87 if (objectCreated) { 88 try { 89 Component comp = getComponent(); 90 comp.initialize(RequestContext.instance()); 91 comp.setVisible(visible); 92 if (comp instanceof ComponentSupport) 93 ((ComponentSupport)comp).setAutoValidate(validate); 94 if (comp instanceof RoleExprHolder) 95 ((RoleExprHolder) comp).setRoleExpr(role); 96 if (comp instanceof Bookmarkable) 97 BookmarkManager.instance(pageContext.getSession()).restoreAttributeState(id); 98 } catch (Exception e) { 99 logger.error("trouble initializing " + getId(), e); 100 throw new JspException (e.toString(), e); 101 } 102 } 103 return super.doEndTag(); 104 } 105 106 109 public void setVisible(boolean b) { 110 visible = b; 111 } 112 113 116 public void setRole(String role) { 117 this.role = role; 118 } 119 120 123 public void setValidate(boolean validate) { 124 this.validate = validate; 125 } 126 } 127 | Popular Tags |