1 10 package org.mmbase.bridge.jsp.taglib; 11 import java.io.IOException ; 12 13 import javax.servlet.jsp.*; 14 15 import org.mmbase.bridge.*; 16 import org.mmbase.bridge.jsp.taglib.util.*; 17 import org.mmbase.util.logging.*; 18 19 20 21 22 50 51 public class ContextTag extends ContextReferrerTag implements ContextProvider { 52 private static final Logger log = Logging.getLoggerInstance(ContextTag.class); 53 54 public static final String CONTEXTTAG_KEY = "org.mmbase.taglib.context"; 55 public static final String CONTAINER_KEY_PREFIX = "org.mmbase.taglib.contextcontainer."; 56 public static final String DEFAULTENCODING_KEY = "org.mmbase.taglib.defaultencoding"; 57 public static final String ISELIGNORED_PARAM = "mmbase.taglib.isELIgnored"; 58 59 private ContextProvider parent = null; 60 private boolean searchedParent = false; 61 private static int latestNumber = 0; 62 63 private int number; 64 65 private CloudContext cloudContext; 66 67 private Attribute referid = Attribute.NULL; 68 private Attribute scope = Attribute.NULL; 69 70 public void setReferid(String r) throws JspTagException { 71 referid = getAttribute(r); 72 } 73 74 public void setScope(String s) throws JspTagException { 75 scope = getAttribute(s); 76 } 77 78 private int getScope() throws JspTagException { 79 String ss = scope.getString(this).toLowerCase(); 80 if ("".equals(ss)) { 81 return PageContext.PAGE_SCOPE; 82 } else if ("request".equals(ss)) { 83 return PageContext.REQUEST_SCOPE; 84 } else if ("session".equals(ss)) { 85 return PageContext.SESSION_SCOPE; 86 } else if ("page".equals(ss)) { 87 return PageContext.PAGE_SCOPE; 88 } else if ("application".equals(ss)) { 89 return PageContext.APPLICATION_SCOPE; 90 } else { 91 throw new JspTagException("Unknown scope '" + ss + "'"); 92 } 93 } 94 95 99 public void setCloudContext(CloudContext cc) { 100 cloudContext = cc; 101 String def = (String ) pageContext.getAttribute(DEFAULTENCODING_KEY); 102 if (def == null && cloudContext != null) { 103 pageContext.setAttribute(DEFAULTENCODING_KEY, cloudContext.getDefaultCharacterEncoding()); 104 } 105 } 106 107 108 public void setPageContext(PageContext pc) { 109 super.setPageContext(pc); log.debug("setting page context"); 111 } 112 113 116 ContextContainer createContainer(ContextContainer c) { number = latestNumber++; 118 ContextContainer container = null; 119 if (c == null && (!"true".equals(pageContext.getServletContext().getInitParameter(ISELIGNORED_PARAM)))) { 120 container = new PageContextContainer(pageContext); 121 } else { 122 container = new StandaloneContextContainer(pageContext, getId(), c); 123 } 124 pageContext.setAttribute(CONTAINER_KEY_PREFIX + number, container, PageContext.PAGE_SCOPE); 125 return container; 126 } 127 128 public ContextContainer getContextContainer() { 129 return (ContextContainer)pageContext.getAttribute(CONTAINER_KEY_PREFIX + number); 130 } 131 132 public int doStartTag() throws JspTagException { 133 log.debug("Start tag of ContextTag"); 134 parent = null; 135 searchedParent = false; 136 137 ContextContainer container; 138 int s = getScope(); 139 if (referid != Attribute.NULL || (s != PageContext.PAGE_SCOPE && getId() != null)) { 140 Object o; 141 if (s == PageContext.PAGE_SCOPE) { 142 o = getObject(referid.getString(this)); 143 } else { 144 String id = referid.getString(this); 145 if (id.equals("")) { 146 id = getId(); 147 if (id == null) throw new JspTagException("Must use id or referid attributes when using 'scope' attibute of context tag"); 148 } 149 o = pageContext.getAttribute(id, s); 150 } 151 if (o == null || "".equals(o)) { container = createContainer(getContextProvider().getContextContainer()); 153 } else { 154 if (! (o instanceof ContextContainer)) { 155 throw new JspTagException("Found context var '" + o + "' is not of type Context but of '" + o.getClass().getName()); 156 } 157 container = (ContextContainer) o; 158 container.setParent(pageContext, getContextProvider().getContextContainer()); 159 pageContext.setAttribute(CONTAINER_KEY_PREFIX + number, container, PageContext.PAGE_SCOPE); 160 } 161 } else { 162 container = createContainer(getContextProvider().getContextContainer()); 163 } 164 165 166 if (s != PageContext.PAGE_SCOPE) { 167 String id = getId(); 168 if (id == null) { 169 id = referid.getString(this); 170 } 171 pageContext.setAttribute(id, container, s); 172 } 173 setCloudContext(getContextTag().cloudContext); 174 if (getId() != null) { 175 if (log.isDebugEnabled()) { 176 log.debug("registering container " + container + " " + getId() + " with context " + getContextProvider().getContextContainer().getId()); 177 } 178 getContextProvider().getContextContainer().register(getId(), container, referid == Attribute.NULL); 179 } 180 log.debug("out"); 181 return EVAL_BODY; 183 } 184 185 192 public void registerNode(String key, Node node) throws JspTagException { 193 getContextContainer().registerNode(key, node); 194 } 195 196 197 204 public Object findAndRegister(int from, String referid, String newid) throws JspTagException { 205 return getContextContainer().findAndRegister(pageContext, from, referid, newid, true); 206 } 207 208 211 protected Object findAndRegister(int from, String referid, String newid, boolean check) throws JspTagException { 212 return getContextContainer().findAndRegister(pageContext, from, referid, newid, check); 213 } 214 215 218 public Object findAndRegister(String externid, String newid) throws JspTagException { 219 return getContextContainer().findAndRegister(pageContext, externid, newid); 220 } 221 222 223 226 public void register(String newid, Object n, boolean check) throws JspTagException { 227 getContextContainer().register(newid, n, check); 228 } 229 230 235 public void register(String newid, Object n) throws JspTagException { 236 getContextContainer().register(newid, n); 237 } 238 239 240 243 public void unRegister(String key) throws JspTagException { 244 getContextContainer().unRegister(key); 245 } 246 247 253 public void reregister(String id, Object n) throws JspTagException { 254 getContextContainer().reregister(id, n); 255 } 256 257 258 263 264 public boolean isPresent(String key) throws JspTagException { 265 return getContextContainer().isPresent(key); 266 } 267 268 271 public boolean isRegistered(String key) throws JspTagException { 272 return getContextContainer().isRegistered(key); 273 } 274 277 private boolean isRegisteredSomewhere(String key) throws JspTagException { 278 return getContextContainer().containsKey(key, true); } 280 281 284 public Object findAndRegister(String id) throws JspTagException { 285 return getContextContainer().findAndRegister(pageContext, id); 286 } 287 290 public String findAndRegisterString(String id) throws JspTagException { 291 return getContextContainer().findAndRegisterString(pageContext, id); 292 } 293 294 297 298 public Object getContainerObject(String key) throws JspTagException { 299 return getContextContainer().getObject(key); 300 301 } 302 303 306 307 public byte[] getBytes(String key) throws JspTagException { 308 return MultiPart.getMultipartRequest(pageContext).getBytes(key); 309 310 } 311 312 public org.apache.commons.fileupload.FileItem getFileItem(String key) throws JspTagException { 313 return MultiPart.getMultipartRequest(pageContext).getFileItem(key); 314 315 } 316 317 public int doAfterBody() throws JspTagException { 318 if (log.isDebugEnabled()) { 319 log.debug("after body of context " + getId()); 320 } 321 getContextContainer().release(pageContext, getContextProvider().getContextContainer()); if (EVAL_BODY == EVAL_BODY_BUFFERED) { 324 try { 325 if (bodyContent != null) { 326 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 327 } 328 } catch (IOException ioe){ 329 throw new TaglibException(ioe); 330 } 331 } 332 return SKIP_BODY; 333 } 334 335 public int doEndTag() throws JspTagException { 336 parent = null; 337 cloudContext = null; 338 return super.doEndTag(); 339 } 340 341 public void doFinally() { 342 parent = null; 343 cloudContext = null; 344 super.doFinally(); 345 } 346 347 public String toString() { 348 return getClass().getName() + " with id " + getId(); 349 } 350 } 351 352 353 | Popular Tags |