View Javadoc

1   /*
2    * $Id: DefaultHSQLInMemoryStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
3    *
4    * Copyright 2006 Commsen International
5    * 
6    * Licensed under the Common Public License, Version 1.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.opensource.org/licenses/cpl1.0.txt
11   * 
12   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
14   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   */
17  package com.commsen.stopwatch.storages;
18  
19  import java.sql.SQLException;
20  import java.sql.Statement;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.commsen.stopwatch.StopwatchStorageException;
25  
26  /***
27   * Default stopwatch storage. It uses in-memory HSQL database.
28   * 
29   * 
30   * @author Milen Dyankov
31   * 
32   */
33  public class DefaultHSQLInMemoryStorage extends AbstractDatabaseStorage {
34  
35  	/***
36  	 * Logger for this class
37  	 */
38  	private static final Logger log = Logger.getLogger(DefaultHSQLInMemoryStorage.class);
39  
40  
41  	protected String getDriver() {
42  		return "org.hsqldb.jdbcDriver";
43  	}
44  
45  
46  	protected String getConnectionString() {
47  		return "jdbc:hsqldb:mem:stopwatch";
48  	}
49  
50  
51  	protected String getUser() {
52  		return "sa";
53  	}
54  
55  
56  	protected String getPassword() {
57  		return "";
58  	}
59  
60  
61  	protected String getLastIdentityQuery() {
62  		return "CALL IDENTITY()";
63  	}
64  
65  
66  	protected String getTableName() {
67  		return "stopwatch";
68  	}
69  
70  
71  	protected String getReturnColumns() {
72  		return "  count(1), " + "  min (DATEDIFF('ms', _start, _end)) as minTime," + "  max (DATEDIFF('ms', _start, _end)) as maxTime," + "  avg (DATEDIFF('ms', _start, _end)) as avgTime,"
73  		        + "  sum (DATEDIFF('ms', _start, _end)) as totalTime ";
74  	}
75  
76  
77  	protected String getTruncTableQuery() {
78  		return null;
79  	}
80  
81  	private boolean debugEnabled;
82  
83  
84  	/***
85  	 * @see com.commsen.stopwatch.StopwatchStorage#freeze()
86  	 */
87  	public void freeze() throws StopwatchStorageException {
88  		Statement statement = null;
89  		try {
90  			statement = updateConnection.createStatement();
91  			statement.execute("delete from stopwatch where _end is NULL");
92  		} catch (SQLException e) {
93  			getLogger().error(e, e);
94  		} finally {
95  			if (statement != null) {
96  				try {
97  					statement.close();
98  				} catch (SQLException e) {
99  					getLogger().error(e, e);
100 				}
101 			}
102 		}
103 	}
104 
105 
106 	/***
107 	 * 
108 	 * @throws StopwatchStorageException
109 	 * @see com.commsen.stopwatch.StopwatchStorage#close()
110 	 */
111 	public void close() throws StopwatchStorageException {
112 		Statement statement = null;
113 		try {
114 			statement = updateConnection.createStatement();
115 			statement.execute("SHUTDOWN");
116 		} catch (SQLException e) {
117 			throw new StopwatchStorageException("database error", e);
118 		} finally {
119 			if (statement != null) {
120 				try {
121 					statement.close();
122 				} catch (SQLException e) {
123 					throw new StopwatchStorageException("database error", e);
124 				}
125 			}
126 		}
127 		super.close();
128 	}
129 
130 
131 	protected Logger getLogger() {
132 		return log;
133 	}
134 
135 
136 	/***
137 	 * @return Returns the debugEnabled.
138 	 */
139 	public boolean isDebugEnabled() {
140 		return debugEnabled;
141 	}
142 
143 
144 	/***
145 	 * @param debugEnabled The debugEnabled to set.
146 	 */
147 	public void setDebugEnabled(boolean debugEnabled) {
148 		this.debugEnabled = debugEnabled;
149 	}
150 
151 }