Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / AutomaticDataSource.java @ 37927

History | View | Annotate | Download (11.7 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.driver.exceptions.BadFieldDriverException;
9
import com.hardcode.gdbms.driver.exceptions.CloseDriverException;
10
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
11
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
12
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
13
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
14
import com.hardcode.gdbms.engine.data.edition.DataWare;
15
import com.hardcode.gdbms.engine.data.persistence.Memento;
16
import com.hardcode.gdbms.engine.data.persistence.MementoException;
17
import com.hardcode.gdbms.engine.internalExceptions.InternalException;
18
import com.hardcode.gdbms.engine.internalExceptions.InternalExceptionCatcher;
19
import com.hardcode.gdbms.engine.internalExceptions.InternalExceptionEvent;
20
import com.hardcode.gdbms.engine.internalExceptions.Task;
21
import com.hardcode.gdbms.engine.internalExceptions.Timer;
22
import com.hardcode.gdbms.engine.values.Value;
23
import com.hardcode.gdbms.engine.values.ValueCollection;
24

    
25

    
26
/**
27
 * Decorator over data sources in order to apply a automatic opening mode
28
 *
29
 * @author Fernando Gonz?lez Cort?s
30
 */
31
public class AutomaticDataSource implements DataSource {
32
    private static Logger logger = Logger.getLogger(AutomaticDataSource.class.getName());
33
    private DataSource ds;
34
    private boolean opened = false;
35
    private long timeout;
36
//    private Timer timer = new Timer();
37
    private long lastReset = 0;
38

    
39
    /**
40
     * Creates a new AutomaticDataSource.
41
     *
42
     * @param ds DataSource to decorate
43
     * @param timeout DataSource will close if there is no operation
44
     * int timeout milliseconds
45
     */
46
    public AutomaticDataSource(DataSource ds, long timeout) {
47
        this.ds = ds;
48
        this.timeout = timeout;
49
    }
50

    
51
    /**
52
     * @see com.hardcode.gdbms.engine.data.DataSource#start()
53
     */
54
    public void start() throws ReadDriverException {
55
        //ignored
56
    }
57

    
58
    /**
59
     * @see com.hardcode.gdbms.engine.data.DataSource#stop()
60
     */
61
    public void stop() throws ReadDriverException {
62
        if (opened){
63
            try {
64
                                close();
65
                        } catch (CloseDriverException e) {
66
                                throw new ReadDriverException(getName(),e);
67
                        }
68
        }
69
    }
70

    
71
    /**
72
     * Opens the datasource only if its closed
73
     * @throws OpenDriverException TODO
74
     */
75
    private synchronized void open() throws OpenDriverException {
76
        if (opened) {
77
            /*
78
             * the resetTimer is a long time operation. Will
79
             * only call it after a while
80
             */
81
//            if ((System.currentTimeMillis() - lastReset) > (DataSourceFactory.DEFAULT_DELAY / 2)) {
82
//                //reset the timer
83
////                logger.info("timer reset");
84
////                       timer.resetTimer();
85
//                lastReset = System.currentTimeMillis();
86
//            }
87
        } else {
88
            opened = true;
89

    
90
            // Se abre
91
            try {
92
                                ds.start();
93
                        } catch (ReadDriverException e1) {
94
                                throw new OpenDriverException(getName(),e1);
95
                        }
96
//            logger.info("timer start");
97

    
98
            // Se inicia el timer
99
//            timer.schedule(new Task() {
100
//                    /**
101
//                     * @see com.hardcode.gdbms.engine.internalExceptions.Task#execute()
102
//                     */
103
//                    public void execute() {
104
//                        try {
105
//                            synchronized (AutomaticDataSource.this) {
106
//                                if (opened){
107
//                                    close();
108
//                                }
109
//                            }
110
//                        } catch (CloseDriverException e) {
111
//                            InternalExceptionCatcher.callExceptionRaised(new InternalExceptionEvent(
112
//                                    AutomaticDataSource.this,
113
//                                    new InternalException(
114
//                                        "Could not automatically close the data source",
115
//                                        e)));
116
//                        }
117
//                    }
118
//
119
//                }, timeout);
120
        }
121
    }
122

    
123
    private void close() throws CloseDriverException {
124
        synchronized (this) {
125
            //Cerramos el data source
126
            opened = false;
127
            try {
128
                                ds.stop();
129
                        } catch (ReadDriverException e) {
130
                                throw new CloseDriverException(getName(),e);
131
                        }
132

    
133
//            logger.info("datasource closed");
134

    
135
//            AutomaticDataSource.this.timer.cancelTimer();
136
//            AutomaticDataSource.this.timer = new Timer();
137
        }
138
    }
139

    
140
    /**
141
     * @see com.hardcode.gdbms.engine.data.DataSource#getName()
142
     */
143
    public String getName() {
144
        return ds.getName();
145
    }
146

    
147
    /**
148
     * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
149
     */
150
    public long[] getWhereFilter() throws IOException {
151
        return ds.getWhereFilter();
152
    }
153

    
154
    /**
155
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
156
     */
157
    public DataSourceFactory getDataSourceFactory() {
158
        return ds.getDataSourceFactory();
159
    }
160

    
161
    /**
162
     * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
163
     */
164
    public Memento getMemento() throws MementoException {
165
        return ds.getMemento();
166
    }
167

    
168
    /**
169
     * @see com.hardcode.gdbms.engine.data.DataSource#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
170
     */
171
    public void setDataSourceFactory(DataSourceFactory dsf) {
172
        ds.setDataSourceFactory(dsf);
173
    }
174

    
175
    /**
176
     * @see com.hardcode.gdbms.engine.data.DataSource#setSourceInfo(com.hardcode.gdbms.engine.data.driver.DriverInfo)
177
     */
178
    public void setSourceInfo(SourceInfo sourceInfo) {
179
        ds.setSourceInfo(sourceInfo);
180
    }
181

    
182
    /**
183
     * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
184
     */
185
    public SourceInfo getSourceInfo() {
186
        return ds.getSourceInfo();
187
    }
188

    
189
    /**
190
     * @see com.hardcode.gdbms.engine.data.FieldNameAccess#getFieldIndexByName(java.lang.String)
191
     */
192
    public int getFieldIndexByName(String fieldName) throws ReadDriverException {
193
        try {
194
                        open();
195
                } catch (OpenDriverException e) {
196
                        throw new ReadDriverException(getName(),e);
197
                }
198

    
199
        return ds.getFieldIndexByName(fieldName);
200
    }
201

    
202
    /**
203
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
204
     *      int)
205
     */
206
    public Value getFieldValue(long rowIndex, int fieldId)
207
        throws ReadDriverException {
208
        try {
209
                        open();
210
                } catch (OpenDriverException e) {
211
                        throw new BadFieldDriverException(getName(),e,String.valueOf(fieldId));
212
                }
213

    
214
        return ds.getFieldValue(rowIndex, fieldId);
215
    }
216

    
217
    /**
218
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
219
     */
220
    public int getFieldCount() throws ReadDriverException {
221
        try {
222
                        open();
223
                } catch (OpenDriverException e) {
224
                        throw new ReadDriverException(getName(),e);
225
                }
226

    
227
        return ds.getFieldCount();
228
    }
229

    
230
    /**
231
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
232
     */
233
    public String getFieldName(int fieldId) throws ReadDriverException {
234
        try {
235
                        open();
236
                } catch (OpenDriverException e) {
237
                        throw new ReadDriverException(getName(),e);
238
                }
239

    
240
        return ds.getFieldName(fieldId);
241
    }
242

    
243
    /**
244
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
245
     */
246
    public long getRowCount() throws ReadDriverException {
247
        try {
248
                        open();
249
                } catch (OpenDriverException e) {
250
                        throw new ReadDriverException(getName(),e);
251
                }
252

    
253
        return ds.getRowCount();
254
    }
255

    
256
    /**
257
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
258
     */
259
    public int getFieldType(int i) throws ReadDriverException {
260
        try {
261
                        open();
262
                } catch (OpenDriverException e) {
263
                        throw new ReadDriverException(getName(),e);
264
                }
265

    
266
        return ds.getFieldType(i);
267
    }
268

    
269
    /**
270
     * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
271
     */
272
    public String getAsString() throws ReadDriverException {
273
        return ds.getAsString();
274
    }
275

    
276
        /**
277
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
278
         */
279
        public void remove() throws WriteDriverException {
280
                ds.remove();
281
        }
282

    
283
        /**
284
         * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
285
         */
286
        public int[] getPrimaryKeys() throws ReadDriverException {
287
        try {
288
                        open();
289
                } catch (OpenDriverException e) {
290
                        throw new ReadDriverException(getName(),e);
291
                }
292
                return ds.getPrimaryKeys();
293
        }
294

    
295
        /**
296
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
297
     */
298
    public ValueCollection getPKValue(long rowIndex) throws ReadDriverException {
299
        try {
300
                        open();
301
                } catch (OpenDriverException e) {
302
                        throw new ReadDriverException(getName(),e);
303
                }
304
        return ds.getPKValue(rowIndex);
305
    }
306

    
307
    /**
308
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
309
     */
310
    public String getPKName(int fieldId) throws ReadDriverException {
311
        try {
312
                        open();
313
                } catch (OpenDriverException e) {
314
                        throw new ReadDriverException(getName(),e);
315
                }
316
        return ds.getPKName(fieldId);
317
    }
318

    
319
    /**
320
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
321
     */
322
    public int getPKType(int i) throws ReadDriverException {
323
        try {
324
                        open();
325
                } catch (OpenDriverException e) {
326
                        throw new ReadDriverException(getName(),e);
327
                }
328
        return ds.getPKType(i);
329
    }
330

    
331
    /**
332
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
333
     */
334
    public int getPKCardinality() throws ReadDriverException {
335
        try {
336
                        open();
337
                } catch (OpenDriverException e) {
338
                        throw new ReadDriverException(getName(),e);
339
                }
340
        return ds.getPKCardinality();
341
    }
342

    
343
    /**
344
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
345
     */
346
    public Value[] getRow(long rowIndex) throws ReadDriverException {
347
        try {
348
                        open();
349
                } catch (OpenDriverException e) {
350
                        throw new ReadDriverException(getName(),e);
351
                }
352
        return ds.getRow(rowIndex);
353
    }
354

    
355
    /**
356
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
357
     */
358
    public String[] getFieldNames() throws ReadDriverException {
359
        try {
360
                        open();
361
                } catch (OpenDriverException e) {
362
                        throw new ReadDriverException(getName(),e);
363
                }
364
        return ds.getFieldNames();
365
    }
366

    
367
    /**
368
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
369
     */
370
    public String[] getPKNames() throws ReadDriverException {
371
        try {
372
                        open();
373
                } catch (OpenDriverException e) {
374
                        throw new ReadDriverException(getName(),e);
375
                }
376
        return ds.getPKNames();
377
    }
378

    
379
    /**
380
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare()
381
     */
382
    public DataWare getDataWare(int mode) throws ReadDriverException {
383
        return ds.getDataWare(mode);
384
    }
385

    
386
        public int getFieldWidth(int i) throws ReadDriverException {
387
        try {
388
                        open();
389
                } catch (OpenDriverException e) {
390
                        throw new ReadDriverException(getName(),e);
391
                }
392
        return ds.getFieldWidth(i);
393
        }
394

    
395
        public boolean isVirtualField(int fieldId) throws ReadDriverException {
396
                // TODO Auto-generated method stub
397
                return ds.isVirtualField(fieldId);
398
        }
399

    
400
        public Driver getDriver() {
401
                return ds.getDriver();
402
        }
403

    
404
        public void reload() throws ReloadDriverException {
405
                try {
406
                        this.stop();
407
                } catch (ReadDriverException e) {
408
                        throw new ReloadDriverException(getName(),e);
409
                }
410
                ds.reload();
411
        }
412

    
413
        public void addDataSourceListener(IDataSourceListener listener) {
414
                ds.addDataSourceListener(listener);
415

    
416
        }
417

    
418
        public void removeDataSourceListener(IDataSourceListener listener) {
419
                ds.removeDataSourceListener(listener);
420

    
421
        }
422
}