1 7 package com.inversoft.verge.util; 8 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.jsp.PageContext ; 12 13 import com.inversoft.beans.BeanException; 14 import com.inversoft.beans.JavaBean; 15 16 17 31 public class WebBean extends JavaBean { 32 33 private String id; 34 private int scope; 35 36 37 50 public WebBean(String id, int scope, Class klass) throws BeanException { 51 super(klass); 52 initialize(id, scope); 53 } 54 55 68 public WebBean(String id, int scope, String className) throws BeanException { 69 super(className); 70 initialize(id, scope); 71 } 72 73 85 protected WebBean() { 86 } 87 88 89 99 protected void initialize(String id, int scope) throws BeanException { 100 101 assert (id != null) : "id == null"; 102 if (scope != ScopeConstants.PAGE_INT && 103 scope != ScopeConstants.REQUEST_INT && 104 scope != ScopeConstants.SESSION_INT && 105 scope != ScopeConstants.APPLICATION_INT) { 106 throw new BeanException("Invalid scope value"); 107 } 108 109 this.id = id; 110 this.scope = scope; 111 } 112 113 118 public String getID() { 119 return id; 120 } 121 122 127 public int getScope() { 128 return scope; 129 } 130 131 136 public String getScopeStr() { 137 return ScopeTools.convertScope(scope); 138 } 139 140 152 public Object getInstance(PageContext pageContext, HttpServletRequest request) 153 throws BeanException { 154 155 Object bean = null; 156 switch (scope) { 157 case ScopeConstants.PAGE_INT: 158 if (pageContext == null) { 159 assert (false) : "Can't get page scope without PageContext"; 160 } 161 bean = pageContext.getAttribute(id); 162 break; 163 case ScopeConstants.REQUEST_INT: 164 bean = request.getAttribute(id); 165 break; 166 case ScopeConstants.SESSION_INT: 167 bean = request.getSession().getAttribute(id); 168 break; 169 case ScopeConstants.APPLICATION_INT: 170 bean = request.getSession().getServletContext().getAttribute(id); 171 break; 172 } 173 174 if (bean == null) { 175 bean = instantiate(); 176 switch (scope) { 177 case ScopeConstants.PAGE_INT: 178 if (pageContext == null) { 179 assert (false) : "Can't get page scope without PageContext"; 180 } 181 pageContext.setAttribute(id, bean); 182 break; 183 case ScopeConstants.REQUEST_INT: 184 request.setAttribute(id, bean); 185 break; 186 case ScopeConstants.SESSION_INT: 187 request.getSession().setAttribute(id, bean); 188 break; 189 case ScopeConstants.APPLICATION_INT: 190 request.getSession().getServletContext().setAttribute(id, bean); 191 break; 192 } 193 } 194 195 return bean; 196 } 197 198 208 public final Object getInstance(PageContext pageContext) throws BeanException { 209 return getInstance(pageContext, (HttpServletRequest ) pageContext.getRequest()); 210 } 211 212 222 public final Object getInstance(HttpServletRequest request) throws BeanException { 223 return getInstance(null, request); 224 } 225 226 231 public WebBeanProperty getWebBeanProperty(String propertyName) 232 throws BeanException { 233 return new WebBeanProperty(propertyName, this); 234 } 235 } 236 | Popular Tags |