1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.vfs.WriteStream; 33 import com.caucho.xml.QName; 34 35 import java.io.IOException ; 36 import java.lang.reflect.Constructor ; 37 import java.lang.reflect.Modifier ; 38 39 42 public class JspUseBean extends JspContainerNode { 43 private static final QName ID = new QName("id"); 44 private static final QName TYPE = new QName("type"); 45 private static final QName CLASS = new QName("class"); 46 private static final QName BEAN_NAME = new QName("beanName"); 47 private static final QName SCOPE = new QName("scope"); 48 49 private String _id; 50 private String _typeName; 51 private String _className; 52 private String _beanName; 53 private String _scope; 54 55 58 public void addAttribute(QName name, String value) 59 throws JspParseException 60 { 61 if (ID.equals(name)) 62 _id = value; 63 else if (TYPE.equals(name)) 64 _typeName = value; 65 else if (CLASS.equals(name)) 66 _className = value; 67 else if (BEAN_NAME.equals(name)) 68 _beanName = value; 69 else if (SCOPE.equals(name)) 70 _scope = value; 71 else 72 throw error(L.l("'{0}' is an invalid attribute in <jsp:useBean>", 73 name.getName())); 74 } 75 76 79 public JspNode addText(String text) 80 throws JspParseException 81 { 82 JspNode node = new StaticText(_gen, text, this); 83 84 addChild(node); 85 86 return node; 87 } 88 89 94 public void printXml(WriteStream os) 95 throws IOException 96 { 97 os.print("<jsp:userBean"); 98 99 if (_id != null) 100 os.print(" id=\"" + _id + "\""); 101 if (_typeName != null) 102 os.print(" type=\"" + _typeName + "\""); 103 if (_className != null) 104 os.print(" class=\"" + _className + "\""); 105 if (_beanName != null) 106 os.print(" beanName=\"" + _beanName + "\""); 107 if (_scope != null) 108 os.print(" scope=\"" + _scope + "\""); 109 os.print("/>"); 110 } 111 112 117 public void generate(JspJavaWriter out) 118 throws Exception 119 { 120 if (_id == null) 121 throw error(L.l("<jsp:useBean> expects an 'id' attribute. id specifies the variable name for the bean.")); 122 123 if (_typeName == null) 124 _typeName = _className; 125 126 if (_typeName == null) 127 throw error(L.l("<jsp:useBean> expects a 'type' or 'class' attribute. The 'type' specifies the Java type of the bean.")); 128 129 _gen.addBeanClass(_id, _typeName); 131 132 String context = null; 133 if (_scope == null || _scope.equals("page")) 134 context = "pageContext"; 135 else if (_scope.equals("request")) { 136 context = "pageContext.getRequest()"; 137 } 138 else if (_scope.equals("session")) { 139 context = "pageContext.getSession()"; 140 } 141 else if (_scope.equals("application")) { 142 context = "pageContext.getApplication()"; 143 } 144 else 145 throw error(L.l("Unknown scope '{0}' in <jsp:useBean>. Scope must be 'page', 'request', 'session', or 'application'.", _scope)); 146 147 out.println(_typeName + " " + _id + ";"); 149 150 if ("application".equals(_scope) || "session".equals(_scope)) { 152 out.println("synchronized (" + context + ") {"); 153 out.pushDepth(); 154 } 155 156 out.print(_id + " = (" + _typeName + ") " + context); 158 out.println(".getAttribute(\"" + _id + "\");"); 159 160 out.println("if (" + _id + " == null) {"); 162 out.pushDepth(); 163 164 boolean canInstantiate = false; 165 166 if (_className != null) { 168 String msg = canInstantiateBean(_className); 169 if (msg == null) { 170 out.println(_id + " = new " + _className + "();"); 171 canInstantiate = true; 172 } 173 else 174 out.println("throw new java.lang.InstantiationException(\"" + msg + "\");"); 175 } 176 else if (_beanName == null) 177 out.println("throw new java.lang.InstantiationException(\"jsp:useBean needs 'bean' or 'class'\");"); 178 else if (hasRuntimeAttribute(_beanName)) { 180 String beanName = getRuntimeAttribute(_beanName); 181 out.println(_id + " = (" + _typeName + 182 ") java.beans.Beans.instantiate(getClass().getClassLoader(), " + 183 beanName + ");"); 184 canInstantiate = true; 185 } 186 else { 188 out.println(_id + " = (" + _typeName + 189 ") java.beans.Beans.instantiate(getClass().getClassLoader(), \"" + 190 _beanName + "\");"); 191 canInstantiate = true; 192 } 193 194 if (! canInstantiate) { 196 } 197 else 198 out.println(context + ".setAttribute(\"" + _id + "\", " + _id + ");"); 199 200 if (canInstantiate) 202 generateChildren(out); 203 204 out.popDepth(); 205 out.println("}"); 206 207 if ("application".equals(_scope) || "session".equals(_scope)) { 209 out.popDepth(); 210 out.println("}"); 211 } 212 } 213 214 217 private String canInstantiateBean(String className) 218 throws Exception 219 { 220 try { 221 Class cl = _gen.getBeanClass(className); 222 int modifiers = cl.getModifiers(); 223 if (Modifier.isInterface(modifiers)) 224 return L.l("'{0}' is an interface", className); 225 if (Modifier.isAbstract(modifiers)) 226 return L.l("'{0}' is abstract", className); 227 if (! Modifier.isPublic(modifiers)) 228 return L.l("'{0}' must be public", className); 229 230 Constructor []constructors = cl.getConstructors(); 231 for (int i = 0; i < constructors.length; i++) { 232 233 Class []param = constructors[i].getParameterTypes(); 234 if (param.length == 0) 235 return null; 236 } 237 } catch (Exception e) { 238 throw error(e.getMessage()); 239 } 240 241 return L.l("'{0}' has no public zero-arg constructor", className); 242 } 243 } 244 | Popular Tags |