Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / data / file / FileDataSourceAdapter.java @ 1956

History | View | Annotate | Download (2.35 KB)

1
package com.hardcode.gdbms.engine.data.file;
2

    
3
import com.hardcode.gdbms.engine.data.driver.DriverCommons;
4
import com.hardcode.gdbms.engine.data.driver.DriverException;
5
import com.hardcode.gdbms.engine.data.driver.DriverInfo;
6
import com.hardcode.gdbms.engine.data.driver.FileDriver;
7
import com.hardcode.gdbms.engine.data.driver.ReadAccess;
8

    
9
import java.io.File;
10
import java.io.IOException;
11

    
12

    
13
/**
14
 * Adapta la interfaz FileDriver a la interfaz DataSource
15
 *
16
 * @author Fernando Gonz?lez Cort?s
17
 */
18
class FileDataSourceAdapter extends AbstractFileDataSource implements FileDataSource {
19
        private File file;
20
        private FileDriver driver;
21
        private int sem = 0;
22

    
23
        /**
24
         * @see com.hardcode.gdbms.engine.data.DataSource#start()
25
         */
26
        public void start() throws DriverException {
27
                try {
28
                        if (sem == 0) {
29
                                driver.open(file);
30

    
31
                                if (driver instanceof DriverCommons) {
32
                                        ((DriverCommons) driver).setDataSourceFactory(getDataSourceFactory());
33
                                }
34
                        }
35

    
36
                        sem++;
37
                } catch (IOException e) {
38
                        throw new DriverException(e);
39
                }
40
        }
41

    
42
        /**
43
         * @see com.hardcode.gdbms.engine.data.DataSource#stop()
44
         */
45
        public void stop() throws DriverException {
46
                try {
47
                        sem--;
48

    
49
                        if (sem == 0) {
50
                                driver.close();
51
                        } else if (sem < 0) {
52
                                throw new RuntimeException("DataSource closed too many times");
53
                        }
54
                } catch (IOException e) {
55
                        throw new DriverException(e);
56
                }
57
        }
58

    
59
        /**
60
         * Asigna el driver al adaptador
61
         *
62
         * @param driver The driver to set.
63
         */
64
        public void setDriver(FileDriver driver) {
65
                this.driver = driver;
66
        }
67

    
68
        /**
69
         * Sets the source information of the DataSource
70
         *
71
         * @param sourceInfo The file to set.
72
         */
73
        public void setSourceInfo(DriverInfo sourceInfo) {
74
                super.setSourceInfo(sourceInfo);
75
                file = new File(((FileDriverInfo) sourceInfo).file);
76
        }
77

    
78
        /**
79
         * @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
80
         */
81
        public ReadAccess getReadDriver() {
82
                return driver;
83
        }
84

    
85
        /**
86
         * @see com.hardcode.gdbms.engine.data.file.AbstractFileDataSource#getDriverInfo()
87
         */
88
        public DriverCommons getDriverInfo() {
89
                if (driver instanceof DriverCommons) {
90
                        return (DriverCommons) driver;
91
                } else {
92
                        return null;
93
                }
94
        }
95

    
96
        /**
97
         * @see com.hardcode.gdbms.engine.data.DataSource#getDBMS()
98
         */
99
        public String getDBMS() {
100
                return sourceInfo.dbms;
101
        }
102

    
103
        /**
104
         * @see com.hardcode.gdbms.engine.data.DataSource#getName()
105
         */
106
        public String getName() {
107
                return sourceInfo.name;
108
        }
109
}