svn-gvsig-desktop / tags / v1_1_Build_1007 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDisconnectedDBAdapter.java @ 12478
History | View | Annotate | Download (8.41 KB)
1 | 2183 | fernando | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package com.iver.cit.gvsig.fmap.layers; |
||
42 | |||
43 | import java.awt.geom.Rectangle2D; |
||
44 | import java.io.ByteArrayOutputStream; |
||
45 | import java.io.File; |
||
46 | import java.io.FileOutputStream; |
||
47 | import java.io.IOException; |
||
48 | import java.io.ObjectOutputStream; |
||
49 | import java.nio.ByteBuffer; |
||
50 | import java.nio.channels.FileChannel; |
||
51 | import java.sql.ResultSet; |
||
52 | import java.sql.SQLException; |
||
53 | import java.sql.Statement; |
||
54 | |||
55 | import com.hardcode.driverManager.DriverLoadException; |
||
56 | import com.hardcode.gdbms.engine.data.DataSource; |
||
57 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
58 | import com.hardcode.gdbms.engine.data.NoSuchTableException; |
||
59 | import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
||
60 | import com.hardcode.gdbms.engine.values.Value; |
||
61 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
62 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
63 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
64 | 11928 | caballero | import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver; |
65 | 2183 | fernando | |
66 | |||
67 | |||
68 | /**
|
||
69 | * Adapta un driver de base de datos vectorial a la interfaz vectorial,
|
||
70 | * manteniendo adem?s el estado necesario por una capa vectorial de base de
|
||
71 | * datos (par?metros de la conexi?n)
|
||
72 | */
|
||
73 | public class VectorialDisconnectedDBAdapter extends VectorialAdapter { |
||
74 | private static final int REFRESH = 0; |
||
75 | private static final int RECEIVING = 1; |
||
76 | private static final int LOCAL = 2; |
||
77 | 3959 | caballero | |
78 | 2183 | fernando | private int numReg=-1; |
79 | 3959 | caballero | private SelectableDataSource ds = null; |
80 | 2183 | fernando | private int status = REFRESH; |
81 | private VectorialDBAdapter connectedAdapter;
|
||
82 | 3959 | caballero | |
83 | 2183 | fernando | private File dataFile; |
84 | private File indexFile; |
||
85 | 3959 | caballero | |
86 | 2183 | fernando | /**
|
87 | * incrementa el contador de las veces que se ha abierto el fichero.
|
||
88 | * Solamente cuando el contador est? a cero pide al driver que conecte con
|
||
89 | * la base de datos
|
||
90 | */
|
||
91 | public void start() { |
||
92 | switch (status){
|
||
93 | case REFRESH:
|
||
94 | Thread t = new Thread(new Receiver()); |
||
95 | t.run(); |
||
96 | break;
|
||
97 | case RECEIVING:
|
||
98 | case LOCAL:
|
||
99 | 3959 | caballero | |
100 | 2183 | fernando | break;
|
101 | } |
||
102 | 3959 | caballero | |
103 | 2183 | fernando | } |
104 | |||
105 | /**
|
||
106 | * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
||
107 | * al driver que cierre la conexion con el servidor de base de datos
|
||
108 | */
|
||
109 | public void stop() { |
||
110 | 11928 | caballero | ((IVectorialDatabaseDriver)driver).close(); |
111 | 2183 | fernando | } |
112 | |||
113 | /**
|
||
114 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
||
115 | */
|
||
116 | public IGeometry getShape(int index) throws DriverIOException { |
||
117 | 3271 | fjp | try {
|
118 | 11928 | caballero | return ((IVectorialDatabaseDriver)driver).getShape(index);
|
119 | 3271 | fjp | } catch (IOException e) { |
120 | throw new DriverIOException(e); |
||
121 | } |
||
122 | 2183 | fernando | } |
123 | 3959 | caballero | |
124 | 2183 | fernando | private String getTableName(){ |
125 | 11928 | caballero | return ((IVectorialDatabaseDriver)driver).getTableName();
|
126 | 2183 | fernando | } |
127 | |||
128 | /**
|
||
129 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
||
130 | */
|
||
131 | public int getShapeType() throws DriverIOException { |
||
132 | return FShape.MULTI;
|
||
133 | } |
||
134 | |||
135 | /**
|
||
136 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
||
137 | */
|
||
138 | 3959 | caballero | public SelectableDataSource getRecordset() throws DriverLoadException { |
139 | 2183 | fernando | if (driver instanceof ObjectDriver) |
140 | { |
||
141 | 2217 | fernando | String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
142 | 2183 | fernando | try {
|
143 | 3959 | caballero | ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
|
144 | 2183 | fernando | } catch (NoSuchTableException e) {
|
145 | throw new RuntimeException(e); |
||
146 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
147 | throw new RuntimeException(e); |
||
148 | } |
||
149 | } |
||
150 | return ds;
|
||
151 | } |
||
152 | 3959 | caballero | |
153 | 2183 | fernando | public class Receiver implements Runnable{ |
154 | private ByteBuffer bb; |
||
155 | private int currentCapacity = 0; |
||
156 | private FileChannel channel; |
||
157 | 3959 | caballero | |
158 | 2183 | fernando | private ByteBuffer indexBuffer = ByteBuffer.allocate(4); |
159 | private FileChannel indexChannel; |
||
160 | |||
161 | private Rectangle2D extent = new Rectangle2D.Double(); |
||
162 | |||
163 | public ByteBuffer getBuffer(int capacity){ |
||
164 | if (capacity > currentCapacity){
|
||
165 | bb = ByteBuffer.allocate(capacity);
|
||
166 | currentCapacity = capacity; |
||
167 | } |
||
168 | 3959 | caballero | |
169 | 2183 | fernando | return bb;
|
170 | } |
||
171 | 3959 | caballero | |
172 | 2183 | fernando | private void writeObject(byte[] bytes) throws IOException{ |
173 | //Se escribe al fichero de datos
|
||
174 | bb = getBuffer(bytes.length + 4);
|
||
175 | bb.clear(); |
||
176 | bb.putInt(bytes.length); |
||
177 | bb.put(bytes); |
||
178 | bb.flip(); |
||
179 | channel.write(bb); |
||
180 | 3959 | caballero | |
181 | 2183 | fernando | //Se actualiza el fichero de ?ndice
|
182 | indexBuffer.clear(); |
||
183 | indexBuffer.putInt((int) channel.position());
|
||
184 | indexBuffer.flip(); |
||
185 | indexChannel.write(indexBuffer); |
||
186 | } |
||
187 | 3959 | caballero | |
188 | 2183 | fernando | /**
|
189 | * @see java.lang.Runnable#run()
|
||
190 | */
|
||
191 | public void run() { |
||
192 | try {
|
||
193 | //Abrimos el fichero de datos
|
||
194 | dataFile = new File("/root/tirar/gvSIGtmp.gvs"); |
||
195 | FileOutputStream fs = new FileOutputStream(dataFile); |
||
196 | channel = fs.getChannel(); |
||
197 | 3959 | caballero | |
198 | 2183 | fernando | //Abrimos el fichero de indice
|
199 | indexFile = new File("/root/tirar/gvSIGindex.gvi"); |
||
200 | FileOutputStream indexfs = new FileOutputStream(indexFile); |
||
201 | indexChannel = indexfs.getChannel(); |
||
202 | 3959 | caballero | |
203 | 2183 | fernando | //Creamos un adaptador conectado para bajarnos los datos
|
204 | 11928 | caballero | IVectorialDatabaseDriver d = (IVectorialDatabaseDriver) VectorialDisconnectedDBAdapter.this.driver; |
205 | 2183 | fernando | connectedAdapter = new VectorialDBAdapter();
|
206 | connectedAdapter.setDriver(d); |
||
207 | 3959 | caballero | |
208 | 2183 | fernando | //Escribimos el n?mero de campos
|
209 | indexBuffer.clear(); |
||
210 | indexBuffer.putInt(connectedAdapter.getRecordset().getFieldCount()); |
||
211 | indexBuffer.flip(); |
||
212 | indexChannel.write(indexBuffer); |
||
213 | indexChannel.position(indexChannel.position() + 4);
|
||
214 | |||
215 | //Reservamos espacio para el n?mero de campos
|
||
216 | indexChannel.position(indexChannel.position() + 4 * 8); |
||
217 | 3959 | caballero | |
218 | 2183 | fernando | connectedAdapter.start(); |
219 | 3959 | caballero | |
220 | 2183 | fernando | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
221 | ObjectOutputStream oos = new ObjectOutputStream(bytes); |
||
222 | int geom = 0; |
||
223 | for (int i = 0; i < connectedAdapter.getShapeCount(); i++) { |
||
224 | geom++; |
||
225 | IGeometry g = connectedAdapter.getShape(i); |
||
226 | extent.add(g.getBounds2D()); |
||
227 | 3959 | caballero | |
228 | 2183 | fernando | //Se obtienen los bytes del objeto a serializar
|
229 | bytes = new ByteArrayOutputStream(); |
||
230 | oos = new ObjectOutputStream(bytes); |
||
231 | oos.writeObject(g); |
||
232 | oos.close(); |
||
233 | 3959 | caballero | |
234 | 2183 | fernando | //Se escribe al fichero
|
235 | writeObject(bytes.toByteArray()); |
||
236 | 3959 | caballero | |
237 | 2183 | fernando | for (int j = 0; j < connectedAdapter.getRecordset().getFieldCount(); j++) { |
238 | Value v = connectedAdapter.getRecordset().getFieldValue(i, j); |
||
239 | 3959 | caballero | |
240 | 2183 | fernando | //Se obtienen los bytes del objeto a serializar
|
241 | bytes = new ByteArrayOutputStream(); |
||
242 | oos = new ObjectOutputStream(bytes); |
||
243 | oos.writeObject(v); |
||
244 | oos.close(); |
||
245 | 3959 | caballero | |
246 | 2183 | fernando | //Se escribe al fichero
|
247 | writeObject(bytes.toByteArray()); |
||
248 | } |
||
249 | } |
||
250 | 3959 | caballero | |
251 | 2183 | fernando | //Escribimos el n?mero de geometr?as en el fichero de ?ndice
|
252 | indexBuffer.clear(); |
||
253 | indexBuffer.putInt(geom); |
||
254 | indexBuffer.flip(); |
||
255 | indexChannel.position(4);
|
||
256 | indexChannel.write(indexBuffer); |
||
257 | indexChannel.position(indexChannel.position() + 4);
|
||
258 | 3959 | caballero | |
259 | 2183 | fernando | //Escribimos el extent
|
260 | ByteBuffer extentBuffer = ByteBuffer.allocate(4*8); |
||
261 | extentBuffer.putDouble(extent.getMinX()); |
||
262 | extentBuffer.putDouble(extent.getMinY()); |
||
263 | extentBuffer.putDouble(extent.getMaxX()); |
||
264 | extentBuffer.putDouble(extent.getMaxY()); |
||
265 | extentBuffer.flip(); |
||
266 | indexChannel.position(8);
|
||
267 | indexChannel.write(extentBuffer); |
||
268 | 3959 | caballero | |
269 | 2183 | fernando | //Cerramos
|
270 | channel.close(); |
||
271 | indexChannel.close(); |
||
272 | 3959 | caballero | |
273 | 2183 | fernando | connectedAdapter.stop(); |
274 | 3959 | caballero | |
275 | 2183 | fernando | status = LOCAL; |
276 | } catch (DriverIOException e) {
|
||
277 | throw new RuntimeException(e); |
||
278 | } catch (IOException e) { |
||
279 | throw new RuntimeException(e); |
||
280 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
281 | throw new RuntimeException(e); |
||
282 | } catch (DriverLoadException e) {
|
||
283 | throw new RuntimeException(e); |
||
284 | } |
||
285 | } |
||
286 | 3959 | caballero | |
287 | 2183 | fernando | } |
288 | } |