root / tags / v1_0_2_Build_911 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / AutomaticDataSource.java @ 11388
History | View | Annotate | Download (9.47 KB)
1 |
package com.hardcode.gdbms.engine.data; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
|
5 |
import org.apache.log4j.Logger; |
6 |
|
7 |
import com.hardcode.driverManager.Driver; |
8 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
9 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
10 |
import com.hardcode.gdbms.engine.data.persistence.Memento; |
11 |
import com.hardcode.gdbms.engine.data.persistence.MementoException; |
12 |
import com.hardcode.gdbms.engine.internalExceptions.InternalException; |
13 |
import com.hardcode.gdbms.engine.internalExceptions.InternalExceptionCatcher; |
14 |
import com.hardcode.gdbms.engine.internalExceptions.InternalExceptionEvent; |
15 |
import com.hardcode.gdbms.engine.internalExceptions.Task; |
16 |
import com.hardcode.gdbms.engine.internalExceptions.Timer; |
17 |
import com.hardcode.gdbms.engine.values.Value; |
18 |
import com.hardcode.gdbms.engine.values.ValueCollection; |
19 |
|
20 |
|
21 |
/**
|
22 |
* Decorator over data sources in order to apply a automatic opening mode
|
23 |
*
|
24 |
* @author Fernando Gonz?lez Cort?s
|
25 |
*/
|
26 |
public class AutomaticDataSource implements DataSource { |
27 |
private static Logger logger = Logger.getLogger(AutomaticDataSource.class.getName()); |
28 |
private DataSource ds; |
29 |
private boolean opened = false; |
30 |
private long timeout; |
31 |
private Timer timer = new Timer(); |
32 |
private long lastReset = 0; |
33 |
|
34 |
/**
|
35 |
* Creates a new AutomaticDataSource.
|
36 |
*
|
37 |
* @param ds DataSource to decorate
|
38 |
* @param timeout DataSource will close if there is no operation
|
39 |
* int timeout milliseconds
|
40 |
*/
|
41 |
public AutomaticDataSource(DataSource ds, long timeout) { |
42 |
this.ds = ds;
|
43 |
this.timeout = timeout;
|
44 |
} |
45 |
|
46 |
/**
|
47 |
* @see com.hardcode.gdbms.engine.data.DataSource#start()
|
48 |
*/
|
49 |
public void start() throws DriverException { |
50 |
//ignored
|
51 |
} |
52 |
|
53 |
/**
|
54 |
* @see com.hardcode.gdbms.engine.data.DataSource#stop()
|
55 |
*/
|
56 |
public void stop() throws DriverException { |
57 |
if (opened){
|
58 |
close(); |
59 |
} |
60 |
} |
61 |
|
62 |
/**
|
63 |
* Opens the datasource only if its closed
|
64 |
*
|
65 |
* @throws DriverException If the operation fails
|
66 |
*/
|
67 |
private synchronized void open() throws DriverException { |
68 |
if (opened) {
|
69 |
/*
|
70 |
* the resetTimer is a long time operation. Will
|
71 |
* only call it after a while
|
72 |
*/
|
73 |
if ((System.currentTimeMillis() - lastReset) > (DataSourceFactory.DEFAULT_DELAY / 2)) { |
74 |
//reset the timer
|
75 |
logger.info("timer reset");
|
76 |
timer.resetTimer(); |
77 |
lastReset = System.currentTimeMillis();
|
78 |
} |
79 |
} else {
|
80 |
opened = true;
|
81 |
|
82 |
// Se abre
|
83 |
ds.start(); |
84 |
logger.info("timer start");
|
85 |
|
86 |
// Se inicia el timer
|
87 |
timer.schedule(new Task() {
|
88 |
/**
|
89 |
* @see com.hardcode.gdbms.engine.internalExceptions.Task#execute()
|
90 |
*/
|
91 |
public void execute() { |
92 |
try {
|
93 |
synchronized (AutomaticDataSource.this) {
|
94 |
if (opened){
|
95 |
close(); |
96 |
} |
97 |
} |
98 |
} catch (DriverException e) {
|
99 |
InternalExceptionCatcher.callExceptionRaised(new InternalExceptionEvent(
|
100 |
AutomaticDataSource.this, |
101 |
new InternalException(
|
102 |
"Could not automatically close the data source",
|
103 |
e))); |
104 |
} |
105 |
} |
106 |
|
107 |
}, timeout); |
108 |
} |
109 |
} |
110 |
|
111 |
private void close() throws DriverException { |
112 |
synchronized (this) { |
113 |
//Cerramos el data source
|
114 |
opened = false;
|
115 |
ds.stop(); |
116 |
|
117 |
logger.info("datasource closed");
|
118 |
|
119 |
AutomaticDataSource.this.timer.cancelTimer(); |
120 |
AutomaticDataSource.this.timer = new Timer(); |
121 |
} |
122 |
} |
123 |
|
124 |
/**
|
125 |
* @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
126 |
*/
|
127 |
public String getName() { |
128 |
return ds.getName();
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
|
133 |
*/
|
134 |
public long[] getWhereFilter() throws IOException { |
135 |
return ds.getWhereFilter();
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
|
140 |
*/
|
141 |
public DataSourceFactory getDataSourceFactory() {
|
142 |
return ds.getDataSourceFactory();
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
|
147 |
*/
|
148 |
public Memento getMemento() throws MementoException { |
149 |
return ds.getMemento();
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* @see com.hardcode.gdbms.engine.data.DataSource#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
154 |
*/
|
155 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
156 |
ds.setDataSourceFactory(dsf); |
157 |
} |
158 |
|
159 |
/**
|
160 |
* @see com.hardcode.gdbms.engine.data.DataSource#setSourceInfo(com.hardcode.gdbms.engine.data.driver.DriverInfo)
|
161 |
*/
|
162 |
public void setSourceInfo(SourceInfo sourceInfo) { |
163 |
ds.setSourceInfo(sourceInfo); |
164 |
} |
165 |
|
166 |
/**
|
167 |
* @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
|
168 |
*/
|
169 |
public SourceInfo getSourceInfo() {
|
170 |
return ds.getSourceInfo();
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* @see com.hardcode.gdbms.engine.data.FieldNameAccess#getFieldIndexByName(java.lang.String)
|
175 |
*/
|
176 |
public int getFieldIndexByName(String fieldName) throws DriverException { |
177 |
open(); |
178 |
|
179 |
return ds.getFieldIndexByName(fieldName);
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
|
184 |
* int)
|
185 |
*/
|
186 |
public Value getFieldValue(long rowIndex, int fieldId) |
187 |
throws DriverException {
|
188 |
open(); |
189 |
|
190 |
return ds.getFieldValue(rowIndex, fieldId);
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
|
195 |
*/
|
196 |
public int getFieldCount() throws DriverException { |
197 |
open(); |
198 |
|
199 |
return ds.getFieldCount();
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
|
204 |
*/
|
205 |
public String getFieldName(int fieldId) throws DriverException { |
206 |
open(); |
207 |
|
208 |
return ds.getFieldName(fieldId);
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
213 |
*/
|
214 |
public long getRowCount() throws DriverException { |
215 |
open(); |
216 |
|
217 |
return ds.getRowCount();
|
218 |
} |
219 |
|
220 |
/**
|
221 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
|
222 |
*/
|
223 |
public int getFieldType(int i) throws DriverException { |
224 |
open(); |
225 |
|
226 |
return ds.getFieldType(i);
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
|
231 |
*/
|
232 |
public String getAsString() throws DriverException { |
233 |
return ds.getAsString();
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* @throws DriverException
|
238 |
* @see com.hardcode.gdbms.engine.data.DataSource#remove()
|
239 |
*/
|
240 |
public void remove() throws DriverException { |
241 |
ds.remove(); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
|
246 |
*/
|
247 |
public int[] getPrimaryKeys() throws DriverException { |
248 |
open(); |
249 |
return ds.getPrimaryKeys();
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
|
254 |
*/
|
255 |
public ValueCollection getPKValue(long rowIndex) throws DriverException { |
256 |
open(); |
257 |
return ds.getPKValue(rowIndex);
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
|
262 |
*/
|
263 |
public String getPKName(int fieldId) throws DriverException { |
264 |
open(); |
265 |
return ds.getPKName(fieldId);
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
|
270 |
*/
|
271 |
public int getPKType(int i) throws DriverException { |
272 |
open(); |
273 |
return ds.getPKType(i);
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* @throws DriverException
|
278 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
|
279 |
*/
|
280 |
public int getPKCardinality() throws DriverException { |
281 |
open(); |
282 |
return ds.getPKCardinality();
|
283 |
} |
284 |
|
285 |
/**
|
286 |
* @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
|
287 |
*/
|
288 |
public Value[] getRow(long rowIndex) throws DriverException { |
289 |
open(); |
290 |
return ds.getRow(rowIndex);
|
291 |
} |
292 |
|
293 |
/**
|
294 |
* @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
|
295 |
*/
|
296 |
public String[] getFieldNames() throws DriverException { |
297 |
open(); |
298 |
return ds.getFieldNames();
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
|
303 |
*/
|
304 |
public String[] getPKNames() throws DriverException { |
305 |
open(); |
306 |
return ds.getPKNames();
|
307 |
} |
308 |
|
309 |
/**
|
310 |
* @throws DriverException
|
311 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDataWare()
|
312 |
*/
|
313 |
public DataWare getDataWare(int mode) throws DriverException { |
314 |
return ds.getDataWare(mode);
|
315 |
} |
316 |
|
317 |
public int getFieldWidth(int i) throws DriverException { |
318 |
open(); |
319 |
return ds.getFieldWidth(i);
|
320 |
} |
321 |
|
322 |
public boolean isVirtualField(int fieldId) throws DriverException { |
323 |
// TODO Auto-generated method stub
|
324 |
return ds.isVirtualField(fieldId);
|
325 |
} |
326 |
|
327 |
public Driver getDriver() { |
328 |
return ds.getDriver();
|
329 |
} |
330 |
|
331 |
public void reload() throws DriverException, IOException { |
332 |
this.stop();
|
333 |
ds.reload(); |
334 |
} |
335 |
|
336 |
public void addDataSourceListener(IDataSourceListener listener) { |
337 |
ds.addDataSourceListener(listener); |
338 |
|
339 |
} |
340 |
|
341 |
public void removeDataSourceListener(IDataSourceListener listener) { |
342 |
ds.removeDataSourceListener(listener); |
343 |
|
344 |
} |
345 |
} |