1 16 17 18 package org.apache.velocity.tools.view.servlet; 19 20 21 import java.io.InputStream ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 25 import java.util.List ; 26 import java.util.Iterator ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.ArrayList ; 30 31 import javax.servlet.http.HttpSession ; 32 import javax.servlet.ServletContext ; 33 34 import org.apache.commons.digester.Digester; 35 import org.apache.commons.digester.RuleSet; 36 import org.apache.velocity.app.Velocity; 37 import org.apache.velocity.tools.view.DataInfo; 38 import org.apache.velocity.tools.view.ToolInfo; 39 import org.apache.velocity.tools.view.XMLToolboxManager; 40 import org.apache.velocity.tools.view.context.ToolboxContext; 41 import org.apache.velocity.tools.view.context.ViewContext; 42 import org.apache.velocity.tools.view.servlet.ServletToolboxRuleSet; 43 44 45 99 public class ServletToolboxManager extends XMLToolboxManager 100 { 101 102 104 public static final String SESSION_TOOLS_KEY = 105 ServletToolboxManager.class.getName() + ":session-tools"; 106 107 private ServletContext servletContext; 108 private Map appTools; 109 private ArrayList sessionToolInfo; 110 private ArrayList requestToolInfo; 111 private boolean createSession; 112 113 private static HashMap managersMap = new HashMap (); 114 private static RuleSet servletRuleSet = new ServletToolboxRuleSet(); 115 116 117 119 124 private ServletToolboxManager(ServletContext servletContext) 125 { 126 this.servletContext = servletContext; 127 appTools = new HashMap (); 128 sessionToolInfo = new ArrayList (); 129 requestToolInfo = new ArrayList (); 130 createSession = true; 131 } 132 133 134 136 141 public static synchronized ServletToolboxManager getInstance(ServletContext servletContext, 142 String toolboxFile) 143 { 144 if (!toolboxFile.startsWith("/")) 146 { 147 toolboxFile = "/" + toolboxFile; 148 } 149 150 String pathname = servletContext.getRealPath(toolboxFile); 152 153 ServletToolboxManager toolboxManager = 155 (ServletToolboxManager)managersMap.get(pathname); 156 157 if (toolboxManager == null) 158 { 159 InputStream is = null; 161 try 162 { 163 is = servletContext.getResourceAsStream(toolboxFile); 165 166 if (is != null) 167 { 168 Velocity.info("ServletToolboxManager: Using config file '" + 169 toolboxFile +"'"); 170 171 toolboxManager = new ServletToolboxManager(servletContext); 172 toolboxManager.load(is); 173 174 managersMap.put(pathname, toolboxManager); 176 177 Velocity.info("ServletToolboxManager: Toolbox setup complete."); 178 } 179 } 180 catch(Exception e) 181 { 182 Velocity.error("Problem loading toolbox '" + toolboxFile +"' : " + e); 183 184 StringWriter sw = new StringWriter (); 187 e.printStackTrace(new PrintWriter (sw)); 188 Velocity.error(sw.toString()); 189 } 190 finally 191 { 192 try 193 { 194 if (is != null) 195 { 196 is.close(); 197 } 198 } 199 catch(Exception ee) {} 200 } 201 } 202 return toolboxManager; 203 } 204 205 206 223 public void setCreateSession(boolean b) 224 { 225 createSession = b; 226 Velocity.debug("ServletToolboxManager: create-session is set to " + b); 227 } 228 229 230 237 public void setXhtml(Boolean value) 238 { 239 servletContext.setAttribute(ViewContext.XHTML, value); 240 Velocity.info("ServletToolboxManager: " + ViewContext.XHTML + 241 " is set to " + value); 242 } 243 244 245 247 251 protected RuleSet getRuleSet() 252 { 253 return servletRuleSet; 254 } 255 256 257 261 public void addTool(ToolInfo info) 262 { 263 if (info instanceof DataInfo) 264 { 265 appTools.put(info.getKey(), info.getInstance(null)); 267 } 268 else if (info instanceof ServletToolInfo) 269 { 270 ServletToolInfo sti = (ServletToolInfo)info; 271 272 if (ViewContext.REQUEST.equalsIgnoreCase(sti.getScope())) 273 { 274 requestToolInfo.add(sti); 275 } 276 else if (ViewContext.SESSION.equalsIgnoreCase(sti.getScope())) 277 { 278 sessionToolInfo.add(sti); 279 } 280 else if (ViewContext.APPLICATION.equalsIgnoreCase(sti.getScope())) 281 { 282 284 appTools.put(sti.getKey(), sti.getInstance(servletContext)); 285 } 286 else 287 { 288 Velocity.warn("ServletToolboxManager: Unknown scope '" + 289 sti.getScope() + "' - " + sti.getKey() + 290 " will be request scoped."); 291 requestToolInfo.add(sti); 292 } 293 } 294 else 295 { 296 requestToolInfo.add(info); 298 } 299 } 300 301 302 313 public ToolboxContext getToolboxContext(Object initData) 314 { 315 ViewContext ctx = (ViewContext)initData; 317 318 Map toolbox = new HashMap (appTools); 320 321 if (!sessionToolInfo.isEmpty()) 322 { 323 HttpSession session = ctx.getRequest().getSession(createSession); 324 if (session != null) 325 { 326 synchronized(getMutex(session)) 328 { 329 Map stmap = (Map )session.getAttribute(SESSION_TOOLS_KEY); 331 if (stmap == null) 332 { 333 stmap = new HashMap (sessionToolInfo.size()); 335 Iterator i = sessionToolInfo.iterator(); 336 while(i.hasNext()) 337 { 338 ToolInfo ti = (ToolInfo)i.next(); 339 stmap.put(ti.getKey(), ti.getInstance(ctx)); 340 } 341 session.setAttribute(SESSION_TOOLS_KEY, stmap); 342 } 343 toolbox.putAll(stmap); 345 } 346 } 347 } 348 349 Iterator i = requestToolInfo.iterator(); 351 while(i.hasNext()) 352 { 353 ToolInfo info = (ToolInfo)i.next(); 354 toolbox.put(info.getKey(), info.getInstance(ctx)); 355 } 356 357 return new ToolboxContext(toolbox); 358 } 359 360 361 365 protected Object getMutex(HttpSession session) 366 { 367 Object lock = session.getAttribute("session.mutex"); 370 if (lock == null) 371 { 372 synchronized(this) 374 { 375 lock = session.getAttribute("session.mutex"); 377 if (lock == null) 378 { 379 lock = new Object (); 380 session.setAttribute("session.mutex", lock); 381 } 382 } 383 } 384 return lock; 385 } 386 387 } 388 | Popular Tags |