Statistics
| Revision:

gvsig-raster / org.gvsig.raster.reproject / trunk / org.gvsig.raster.reproject / org.gvsig.raster.reproject.app.reprojectclient / src / main / java / org / gvsig / raster / reproject / app / preparelayer / ReprojectionQueue.java @ 2475

History | View | Annotate | Download (3.55 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.reproject.app.preparelayer;
23

    
24
import org.gvsig.fmap.dal.coverage.RasterLocator;
25
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
26
import org.gvsig.raster.algorithm.process.IProcessActions;
27
import org.gvsig.raster.algorithm.process.ProcessException;
28
import org.gvsig.raster.algorithm.process.DataProcess;
29
import org.gvsig.raster.util.Queue;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
/**
34
 * Cola de procesos de ejecuci?n exclusiva. Los procesos de esta lista se ir?n ejecutando
35
 * por orden de llegada impidiendo que se ejecuten dos al mismo tiempo.
36
 * 
37
 * 16/05/2008
38
 * @author Nacho Brodin nachobrodin@gmail.com
39
 */
40
public class ReprojectionQueue extends Thread {
41
        private Queue                     queue                       = new Queue();
42
        private DataProcess             executionProcess            = null;
43
        static private ReprojectionQueue  singleton                   = null;
44
        private Logger                    logger                      = LoggerFactory.getLogger(ReprojectionQueue.class.toString());
45
        
46
        /**
47
         * Devuelve una instancia al unico objeto de UniqueProcessQueue que puede existir.
48
         * @return
49
         */
50
        static public ReprojectionQueue getSingleton() {
51
                if(singleton == null) {
52
                        singleton = new ReprojectionQueue();
53
                        synchronized (singleton) {
54
                                singleton.start();
55
                        }
56
                }
57
                return singleton;
58
        }
59
        
60
        public void run() {
61
                //Cuando arranca el thread se duerme porque la cola est? vacia
62
                synchronized (this) {
63
                        try {
64
                                wait();
65
                        } catch (InterruptedException e) {
66
                        }
67
                }
68
                
69
                while(true) {
70
                        if(queue.size() >= 1) {
71
                                Object[] obj = (Object[])queue.get();
72
                                executionProcess = ((DataProcess) obj[0]);
73
                                if (executionProcess != null) {
74
                                        executionProcess.showIncrementableWindow();
75
                                        try {
76
                                                executionProcess.execute();
77
                                        } catch (ProcessInterruptedException e) {
78
                                        } catch (ProcessException e) {
79
                                                logger.debug(RasterLocator.getManager().getRasterUtils().getTrace(e));
80
                                        }
81
                                        executionProcess.getIncrementableTask().processFinalize();
82
                                        
83
                                        //Despierta el objeto que llam? cuando termina el calculo
84
                                        //para que pueda recoger los resultados
85
                                        synchronized (obj[1]) {
86
                                                obj[1].notify();        
87
                                        }
88
                                        
89
                                        //Cuando la cola est? vacia se suspende el hilo.
90
                                        if(queue.size() <= 0) {
91
                                                synchronized (this) {
92
                                                        try {
93
                                                                this.wait();
94
                                                        } catch (InterruptedException e) {
95
                                                        }
96
                                                }
97
                                        }
98
                                                
99
                                }
100
                        }
101
                }
102
        }        
103
        
104
        /**
105
         * A?ade un proceso a la cola.
106
         * @param id Identificador del proceso
107
         * @param process Proceso
108
         * @throws InterruptedException 
109
         */
110
        public synchronized void add(DataProcess process, IProcessActions obj) {
111
                queue.put(new Object[]{process, obj});
112
        }
113

    
114
}