1 16 17 package org.apache.taglibs.jndi; 18 19 import javax.servlet.jsp.*; 20 import javax.servlet.jsp.tagext.*; 21 22 import java.io.*; 23 import java.util.Hashtable ; 24 import java.util.Enumeration ; 25 26 import javax.naming.*; 27 32 public class UseContextTag extends BodyTagSupport { 33 34 Hashtable env = new Hashtable (); 35 private int scope = PageContext.PAGE_SCOPE; 37 String url; 38 39 42 public UseContextTag() { 43 44 } 45 46 51 public void setEnvRef(String envRef) { 52 Object o = pageContext.findAttribute(envRef); 53 if ((o != null) && (o instanceof Hashtable )) { 54 setEnv((Hashtable )o); 55 } 56 } 57 58 62 public Hashtable getEnv() { 63 return env; 64 } 65 66 71 public void setEnv(Hashtable env) { 72 Enumeration e = env.keys(); 73 while (e.hasMoreElements()) { 74 String s = (String )e.nextElement(); 75 this.env.put(s, env.get(s)); 78 } 79 } 80 81 85 public String getProviderUrl() { 86 try { 87 return (String ) env.get(Context.PROVIDER_URL); 88 } catch (ClassCastException cce) { 89 return null; 90 } 91 } 92 93 97 public void setProviderUrl(String providerUrl) { 98 env.put(Context.PROVIDER_URL, providerUrl); 99 } 100 101 105 public String getUrl() { 106 return url; 107 } 108 109 113 public void setUrl(String url) { 114 this.url = url; 115 } 116 117 121 public String getInitialFactory() { 122 try { 123 return (String ) env.get(Context.INITIAL_CONTEXT_FACTORY); 124 } catch (ClassCastException cce) { 125 return null; 126 } 127 } 128 129 133 public void setInitialFactory(String initialFactory) { 134 env.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory); 135 } 136 137 141 public String getAuthoritative() { 142 try { 143 return (String ) env.get(Context.AUTHORITATIVE); 144 } catch (ClassCastException cce) { 145 return null; 146 } 147 } 148 149 153 public void setAuthoritative(String authoritative) { 154 env.put(Context.AUTHORITATIVE, authoritative); 155 } 156 157 161 public int getBatchsize() { 162 Object o = env.get(Context.BATCHSIZE); 163 if (o instanceof Integer ) { 164 return ((Integer )o).intValue(); 165 } else if (o != null) { 166 return Integer.parseInt(o.toString()); 167 } else { 168 return -1; 169 } 170 } 171 172 176 public void setBatchsize(int batchsize) { 177 env.put(Context.BATCHSIZE, new Integer (batchsize)); 178 } 179 180 184 public String getObjectFactories() { 185 try { 186 return (String ) env.get(Context.OBJECT_FACTORIES); 187 } catch (ClassCastException cce) { 188 return null; 189 } 190 } 191 192 196 public void setObjectFactories(String objectFactories) { 197 env.put(Context.OBJECT_FACTORIES, objectFactories); 198 } 199 200 204 public String getStateFactories() { 205 try { 206 return (String ) env.get(Context.STATE_FACTORIES); 207 } catch (ClassCastException cce) { 208 return null; 209 } 210 } 211 212 216 public void setStateFactories(String stateFactories) { 217 env.put(Context.STATE_FACTORIES, stateFactories); 218 } 219 220 224 public String getUrlPkgPrefixes() { 225 try { 226 return (String ) env.get(Context.URL_PKG_PREFIXES); 227 } catch (ClassCastException cce) { 228 return null; 229 } 230 } 231 232 236 public void setUrlPkgPrefixes(String urlPkgPrefixes) { 237 env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes); 238 } 239 240 244 public String getDnsUrl() { 245 try { 246 return (String ) env.get(Context.DNS_URL); 247 } catch (ClassCastException cce) { 248 return null; 249 } 250 } 251 252 256 public void setDnsUrl(String dnsUrl) { 257 env.put(Context.DNS_URL, dnsUrl); 258 } 259 260 public void supplementFromAttributes(String entry) { 261 if (!env.containsKey(entry)) { 262 Object o = pageContext.findAttribute(entry); 263 if (o != null) { 264 env.put(entry, o); 265 } 266 } 267 } 268 269 273 public void setScope(String scope) { 274 if (scope.equalsIgnoreCase("request")) { 275 this.scope = PageContext.REQUEST_SCOPE; 276 } else if (scope.equalsIgnoreCase("session")) { 277 this.scope = PageContext.SESSION_SCOPE; 278 } else if (scope.equalsIgnoreCase("application")) { 279 this.scope = PageContext.APPLICATION_SCOPE; 280 } 281 } 282 283 protected void pullInSupplimentalAttributes() { 284 supplementFromAttributes(Context.INITIAL_CONTEXT_FACTORY); 285 supplementFromAttributes(Context.OBJECT_FACTORIES); 286 supplementFromAttributes(Context.STATE_FACTORIES); 287 supplementFromAttributes(Context.URL_PKG_PREFIXES); 288 supplementFromAttributes(Context.PROVIDER_URL); 289 supplementFromAttributes(Context.DNS_URL); 290 } 291 292 protected Object getObjectToExport() throws JspException 293 { 294 try { 295 Context ctx = new InitialContext(env); 296 if (url != null) { 297 Object o = ctx.lookup(url); 298 if (o instanceof Context) { 299 ctx = (Context) o; 300 } 301 } 302 return ctx; 303 } catch (NamingException ne) { 304 throw new JspException("JNDI useContext tag could not find url: "+ne.getMessage()); 305 } 306 } 307 308 public Class getClassOfExportedObject() { 309 return Context.class; 310 } 311 312 public int doEndTag() throws JspException { 313 Object o = pageContext.getAttribute(getId(), scope); 314 if (o != null) { 315 if (!getClassOfExportedObject().isInstance(o)) { 316 o = null; 317 } 318 } 319 if (o == null) { 320 pullInSupplimentalAttributes(); 321 o = getObjectToExport(); 322 pageContext.setAttribute(getId(), o, scope); 323 } 324 return EVAL_PAGE; 325 } 326 } 327 | Popular Tags |