1 16 package org.apache.taglibs.utility.lang; 17 18 import java.lang.reflect.Method ; 19 import java.beans.*; 20 import java.io.IOException ; 21 22 import javax.servlet.*; 23 import javax.servlet.http.HttpSession ; 24 import javax.servlet.jsp.*; 25 import javax.servlet.jsp.tagext.*; 26 27 38 39 public class UseBeanTag extends BodyTagSupport { 40 41 String id; 42 String scope = "page"; 43 String classname; 44 String type; 45 String beanName; 46 boolean processRequest = false; 47 boolean init = false; 48 Object beanObject; 49 50 public void setId(String val) { 51 this.id = val; 52 } 53 54 public void setScope(String val) { 55 this.scope = val; 56 } 57 58 public void setClassname(String val) { 59 this.classname = val; 60 } 61 62 public void setType(String val) { 63 this.type = val; 64 } 65 66 public void setBeanName(String val) { 67 this.beanName = val; 68 } 69 70 public void setProcessRequest(boolean val) { 71 this.processRequest = val; 72 } 73 74 public void setInit(boolean val) { 75 this.init = val; 76 } 77 78 public void checkSyntax() throws JspException { 79 80 if ( id == null ) { 82 throw new JspException("useBean: id is missing"); 83 } 84 85 if (classname == null && type == null) { 86 throw new JspException ("useBean: both class and name attribute" + 87 " can't be unspecified"); 88 } 89 90 if (classname != null && beanName != null) { 91 throw new JspException ("useBean: can't specify both class and" + 92 " beanName"); 93 } 94 } 95 96 public int doStartTag() throws JspException { 97 98 checkSyntax(); 100 String clas = (this.classname == null)? type : this.classname; 101 102 try { 103 104 if (type == null) type = clas; 106 107 if (scope == null || scope.equals ("page")) { 108 109 beanObject = pageContext.getAttribute(id); 111 112 if (beanObject != null) { 113 if (!Class.forName(type).isInstance(beanObject)) 114 throw new JspException("ClassCastException: " + 115 id + " " + clas); 116 return SKIP_BODY; 117 } 118 119 synchronized(pageContext) { 120 beanObject = createBean(); 121 if (processRequest == true) 122 processReq(beanObject); 123 pageContext.setAttribute(id, beanObject); 124 setInit(true); 125 } 126 127 } else if (scope.equals ("request")) { 128 129 beanObject = 131 pageContext.getAttribute(id, pageContext.REQUEST_SCOPE); 132 133 if (beanObject != null) { 134 if (!Class.forName(type).isInstance(beanObject)) 135 throw new JspException("ClassCastException: " + 136 id + " " + clas); 137 return SKIP_BODY; 138 } 139 140 synchronized((ServletRequest) 141 pageContext.findAttribute(pageContext.REQUEST)) { 142 beanObject = createBean(); 143 if (processRequest == true) 144 processReq(beanObject); 145 pageContext.setAttribute(id, beanObject); 146 setInit(true); 147 } 148 149 } else if (scope.equals ("session")) { 150 151 beanObject = 153 pageContext.getAttribute(id, pageContext.SESSION_SCOPE); 154 155 if (beanObject != null) { 156 if (!Class.forName(type).isInstance(beanObject)) 157 throw new JspException("ClassCastException: " + 158 id + " " + clas); 159 return SKIP_BODY; 160 } 161 162 synchronized((HttpSession ) 163 pageContext.findAttribute(pageContext.SESSION)) { 164 beanObject = createBean(); 165 if (processRequest == true) 166 processReq(beanObject); 167 pageContext.setAttribute(id, beanObject); 168 setInit(true); 169 } 170 171 } else if (scope.equals ("application")) { 172 173 beanObject = 175 pageContext.getAttribute(id, pageContext.APPLICATION_SCOPE); 176 177 if (beanObject != null) { 178 if (!Class.forName(type).isInstance(beanObject)) 179 throw new JspException("ClassCastException: " + 180 id + " " + clas); 181 return SKIP_BODY; 182 } 183 184 synchronized((ServletContext) 185 pageContext.findAttribute(pageContext.APPLICATION)) { 186 beanObject = createBean(); 187 if (processRequest == true) 188 processReq(beanObject); 189 pageContext.setAttribute(id, beanObject); 190 setInit(true); 191 } 192 193 } 194 195 if (init == false) return SKIP_BODY; 197 else return EVAL_BODY_TAG; 198 } catch (Exception ex) { 199 throw new JspException(ex.getMessage()); 200 } 201 } 202 203 private Object createBean() throws JspException { 204 205 try { 208 if (classname != null || beanName != null) { 209 String className = (beanName != null) ? beanName : classname; 210 return (Beans.instantiate 211 (this.getClass().getClassLoader(),className)); 212 } 213 else { 214 throw new JspException ("useBean: bean must be located in" + 215 " the given scope."); 216 } 217 } catch (IOException iox) { 218 throw new JspException ("useBean: IO error while instantiating bean" + 219 iox.getMessage()); 220 } catch (ClassNotFoundException cnfex) { 221 throw new JspException ("useBean: class " + classname + " not found."); 222 } 223 } 224 225 public int doAfterBody() { 226 return SKIP_BODY; 227 } 228 229 public void processReq(Object bean) throws JspException { 230 try { 231 Class clazz = Class.forName("javax.servlet.ServletRequest"); 232 Method method = bean.getClass().getMethod("processRequest", 233 new Class [] { clazz }); 234 method.invoke(bean, new Object [] { 235 pageContext.findAttribute(pageContext.REQUEST) }); 236 237 } catch(Exception ex) { 238 throw new JspException(ex.getMessage()); 239 } 240 } 241 } 242 243 | Popular Tags |