1 16 17 18 package org.apache.taglibs.jndi; 19 20 import javax.servlet.jsp.*; 21 import javax.servlet.jsp.tagext.*; 22 import javax.naming.*; 23 import java.io.*; 24 25 30 public class ListTag extends BodyTagSupport { 31 32 private Context context; 33 private String contextRef; 34 private String nameId; 35 private String classId; 36 private String objId; 37 private int nameScope = PageContext.PAGE_SCOPE; 38 private int classScope = PageContext.PAGE_SCOPE; 39 private int objScope = PageContext.PAGE_SCOPE; 40 41 private String name; 42 private Name nameObject; 43 private boolean bindings; 44 45 private NamingEnumeration nameEnum; 46 private NameClassPair currentListing; 47 48 49 public ListTag() { 50 } 51 52 55 public Context getContext() { 56 return context; 57 } 58 59 63 public void setContext(Context context) { 64 this.context = context; 65 } 66 67 71 public void setContextRef(String contextRef) { 72 this.contextRef = contextRef; 73 } 74 75 79 public String getNameId() { 80 return nameId; 81 } 82 83 87 public void setNameId(String nameId) { 88 this.nameId = nameId; 89 } 90 91 95 public void setNameScope(String nameScope) { 96 this.nameScope = decodeScope(nameScope); 97 } 98 99 103 public String getClassId() { 104 return classId; 105 } 106 107 111 public void setClassId(String classId) { 112 this.classId = classId; 113 } 114 115 119 public void setClassScope(String classScope) { 120 this.classScope = decodeScope(classScope); 121 } 122 123 127 public String getObjId() { 128 return objId; 129 } 130 131 135 public void setObjId(String objId) { 136 this.objId = objId; 137 } 138 139 142 public void setObjScope(String objScope) { 143 this.objScope = decodeScope(objScope); 144 } 145 146 150 public String getName() { 151 return name; 152 } 153 154 158 public void setName(String name) { 159 this.name = name; 160 } 161 162 166 public Name getNameObject() { 167 return nameObject; 168 } 169 170 174 public void setNameObject(Name nameObject) { 175 this.nameObject = nameObject; 176 } 177 178 182 public boolean getBindings() { 183 return bindings; 184 } 185 186 190 public void setBindings(boolean bindings) { 191 this.bindings = bindings; 192 } 193 194 public int doStartTag() throws JspException { 195 if( contextRef != null ) { 196 context = null; 197 Object o = pageContext.findAttribute(contextRef); 198 if (o instanceof Context) { 199 context = (Context)o; 200 } 201 } 202 if (context == null) { 203 throw new JspException("Context is not set for list itteration"); 204 } 205 try { 206 if (nameObject != null) { 207 if (bindings) { 208 nameEnum = context.listBindings(nameObject); 209 } else { 210 nameEnum = context.list(nameObject); 211 } 212 } else { 213 if (bindings) { 214 nameEnum = context.listBindings(name); 215 } else { 216 nameEnum = context.list(name); 217 } 218 } 219 220 if (nameEnum.hasMoreElements()) { 221 return EVAL_BODY_TAG; 222 } else { 223 return SKIP_BODY; 224 } 225 } catch (NamingException ne) { 226 throw new JspTagException("JNDI list tag could not list bindings: "+ne.getMessage()); 227 } 228 } 229 230 public void doInitBody() { 231 currentListing = (NameClassPair) nameEnum.nextElement(); 232 if (nameId != null) { 233 pageContext.setAttribute(nameId, 234 currentListing.getName(), nameScope); 235 } 236 if (classId != null) { 237 pageContext.setAttribute(classId, 238 currentListing.getClassName(), classScope); 239 } 240 if (bindings && (objId != null)) { 241 if (currentListing instanceof Binding) { 242 pageContext.setAttribute(objId, 243 ((Binding)currentListing).getObject(), objScope); 244 } else { 245 pageContext.setAttribute(objId, "Uh Oh, no Binding!", objScope); 246 } 247 } 248 } 249 250 public int doAfterBody() { 251 if (nameEnum.hasMoreElements()) { 252 currentListing = (NameClassPair) nameEnum.nextElement(); 253 if (nameId != null) { 254 pageContext.setAttribute(nameId, 255 currentListing.getName(), nameScope); 256 } 257 if (classId != null) { 258 pageContext.setAttribute(classId, 259 currentListing.getClassName(), classScope); 260 } 261 if (bindings && (objId != null)) { 262 if (currentListing instanceof Binding) { 263 pageContext.setAttribute(objId, 264 ((Binding)currentListing).getObject(), objScope); 265 } else { 266 pageContext.setAttribute(objId, "Uh Oh, no Binding!", objScope); 267 } 268 } 269 return EVAL_BODY_TAG; 270 } else { 271 return SKIP_BODY; 272 } 273 } 274 275 public int doEndTag() throws JspException { 276 try { 277 if (bodyContent != null) { 278 bodyContent.writeOut(pageContext.getOut()); 279 } 280 } catch (IOException ioe) { 281 throw new JspException(ioe.toString()); 282 } 283 if (nameId != null && nameScope != PageContext.REQUEST_SCOPE) { 284 pageContext.removeAttribute(nameId, nameScope); 285 } 286 if (classId != null && classScope != PageContext.REQUEST_SCOPE) { 287 pageContext.removeAttribute(classId, classScope); 288 } 289 if (bindings && (objId != null) && objScope != PageContext.REQUEST_SCOPE) { 290 pageContext.removeAttribute(objId, objScope); 291 } 292 try { 293 nameEnum.close(); 294 } catch (NamingException ne) { 295 } 297 return EVAL_PAGE; 298 } 299 300 static int decodeScope(String scope) { 301 if (scope.equalsIgnoreCase("page")) { 302 return PageContext.PAGE_SCOPE; 303 } else if (scope.equalsIgnoreCase("request")) { 304 return PageContext.REQUEST_SCOPE; 305 } else if (scope.equalsIgnoreCase("session")) { 306 return PageContext.SESSION_SCOPE; 307 } else if (scope.equalsIgnoreCase("application")) { 308 return PageContext.APPLICATION_SCOPE; 309 } else { 310 return -1; 311 } 312 } 313 } 314 | Popular Tags |