1 13 package mondrian.web.taglib; 14 15 import mondrian.olap.Connection; 16 import mondrian.olap.DriverManager; 17 import mondrian.olap.Query; 18 import mondrian.olap.Result; 19 import mondrian.spi.impl.ServletContextCatalogLocator; 20 21 import org.apache.log4j.Logger; 22 import org.w3c.dom.Document ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.http.HttpSession ; 26 import javax.servlet.http.HttpSessionBindingEvent ; 27 import javax.servlet.http.HttpSessionBindingListener ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 30 33 34 public class ResultCache implements HttpSessionBindingListener { 35 private static final Logger LOGGER = Logger.getLogger(ResultCache.class); 36 private final static String ATTR_NAME = "mondrian.web.taglib.ResultCache."; 37 private Query query = null; 38 private Result result = null; 39 private Document document = null; 40 private ServletContext servletContext; 41 private Connection connection; 42 43 private ResultCache(ServletContext context) { 44 this.servletContext = context; 45 } 46 47 48 53 public static ResultCache getInstance( 54 HttpSession session, 55 ServletContext servletContext, 56 String name) { 57 String fqname = ATTR_NAME + name; 58 ResultCache resultCache = (ResultCache) session.getAttribute(fqname); 59 if (resultCache == null) { 60 resultCache = new ResultCache(servletContext); 61 session.setAttribute(fqname, resultCache); 62 } 63 return resultCache; 64 } 65 66 public void parse(String mdx) { 67 if (connection != null) { 68 query = connection.parseQuery(mdx); 69 setDirty(); 70 } else { 71 LOGGER.error("null connection"); 72 } 73 } 74 75 public Result getResult() { 76 if (result == null) { 77 long t1 = System.currentTimeMillis(); 78 result = connection.execute(query); 79 long t2 = System.currentTimeMillis(); 80 LOGGER.debug( 81 "Execute query took " + (t2 - t1) + " millisec"); 82 } 83 return result; 84 } 85 86 public Document getDOM() { 87 try { 88 if (document == null) { 89 document = DOMBuilder.build(getResult()); 90 } 91 return document; 92 } catch (ParserConfigurationException e) { 93 LOGGER.error(e); 94 throw new RuntimeException (e.toString()); 95 } 96 } 97 98 102 public Query getQuery() { 103 return query; 104 } 105 106 109 public void setQuery(Query query) { 110 this.query = query; 111 setDirty(); 112 } 113 116 public void setDirty() { 117 result = null; 118 document = null; 119 } 120 121 124 public void valueBound(HttpSessionBindingEvent ev) { 125 String connectString = 126 servletContext.getInitParameter("connectString"); 127 LOGGER.debug("connectString: " + connectString); 128 this.connection = 129 DriverManager.getConnection(connectString, new ServletContextCatalogLocator(servletContext), false); 130 if (this.connection == null) { 131 throw new RuntimeException ("No ROLAP connection from connectString: " + connectString); 132 } 133 } 134 135 138 public void valueUnbound(HttpSessionBindingEvent ev) { 139 if (connection != null) { 140 connection.close(); 141 } 142 } 143 144 145 } 146 147 | Popular Tags |