KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > web > taglib > ResultCache


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/web/taglib/ResultCache.java#13 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2002-2002 Kana Software, Inc.
7 // Copyright (C) 2002-2006 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // Andreas Voss, 22 March, 2002
12 */

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 JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
27 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29
30 /**
31  * holds a query/result pair in the users session
32  */

33
34 public class ResultCache implements HttpSessionBindingListener JavaDoc {
35     private static final Logger LOGGER = Logger.getLogger(ResultCache.class);
36     private final static String JavaDoc ATTR_NAME = "mondrian.web.taglib.ResultCache.";
37     private Query query = null;
38     private Result result = null;
39     private Document JavaDoc document = null;
40     private ServletContext JavaDoc servletContext;
41     private Connection connection;
42
43     private ResultCache(ServletContext JavaDoc context) {
44         this.servletContext = context;
45     }
46
47
48     /**
49      * Retrieves a cached query. It is identified by its name and the
50      * current session. The servletContext parameter is necessary because
51      * HttpSession.getServletContext was not added until J2EE 1.3.
52      */

53     public static ResultCache getInstance(
54         HttpSession JavaDoc session,
55         ServletContext JavaDoc servletContext,
56         String JavaDoc name) {
57         String JavaDoc 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 JavaDoc 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 JavaDoc getDOM() {
87         try {
88             if (document == null) {
89                 document = DOMBuilder.build(getResult());
90             }
91             return document;
92         } catch (ParserConfigurationException JavaDoc e) {
93             LOGGER.error(e);
94             throw new RuntimeException JavaDoc(e.toString());
95         }
96     }
97
98     /**
99      * Returns the {@link Query}. If you modify the query, call
100      * <code>{@link #setDirty}(true)</code>.
101      */

102     public Query getQuery() {
103         return query;
104     }
105
106     /**
107      * Sets the query. Automatically calls <code>{@link #setDirty}(true)</code>.
108      */

109     public void setQuery(Query query) {
110         this.query = query;
111         setDirty();
112     }
113     /**
114      * set to dirty after you have modified the query to force a recalcuation
115      */

116     public void setDirty() {
117         result = null;
118         document = null;
119     }
120
121     /**
122      * create a new connection to Mondrian
123      */

124     public void valueBound(HttpSessionBindingEvent JavaDoc ev) {
125         String JavaDoc 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 JavaDoc("No ROLAP connection from connectString: " + connectString);
132         }
133     }
134
135     /**
136      * close connection
137      */

138     public void valueUnbound(HttpSessionBindingEvent JavaDoc ev) {
139         if (connection != null) {
140             connection.close();
141         }
142     }
143
144
145 }
146
147 // End ResultCache.java
148
Popular Tags