Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / taskplanning / retrieving / RetrieveQueue.java @ 40769

History | View | Annotate | Download (3.28 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.taskplanning.retrieving;
25

    
26
import java.util.Date;
27
import java.util.Vector;
28

    
29
import org.gvsig.remoteclient.taskplanning.FIFOTaskPlanner;
30
import org.gvsig.remoteclient.taskplanning.IQueue;
31
import org.gvsig.remoteclient.taskplanning.IRunnableTask;
32
import org.gvsig.remoteclient.taskplanning.ITaskPlanner;
33

    
34
/**
35
 * @author jaume dominguez faus - jaume.dominguez@iver.es
36
 *                    Luis W. Sevilla (sevilla_lui@gva.es)
37
 */
38
public class RetrieveQueue implements IQueue {
39
        private String hostName;
40
        private Date startTime;
41
        private Vector tasks = new Vector();
42
        private boolean waiting;
43
        private ITaskPlanner taskPlanner;
44
        private Worker worker;
45
        
46
        /**
47
         * 
48
         */
49
        public RetrieveQueue(String hName) {
50
                hostName = hName;
51
                startTime = new Date();
52
                worker = new Worker();
53
                new Thread(worker).start();
54
        }
55
        
56
        
57
        public IRunnableTask put(IRunnableTask task) {
58
                tasks.add(task);
59
                if (waiting) {
60
                        synchronized (this) {
61
                                notifyAll();
62
                        }
63
                }
64
                return task;
65
        }
66
        
67
        public IRunnableTask take() {
68
                if (tasks.isEmpty()) {
69
                        synchronized (this) {
70
                                waiting = true;
71
                                try {
72
                                        wait();
73
                                } catch (InterruptedException ie) {
74
                                        waiting = false;
75
                                }
76
                        }
77
                }
78
                return getTaskPlanner().nextTask() ;
79
        }
80
        
81
        
82
        
83
        public boolean isEmpty() {
84
                synchronized (this) {
85
                        return tasks.isEmpty() && !worker.r.isRunning();
86
                }
87
        }
88
        
89
        
90

    
91
        public ITaskPlanner getTaskPlanner() {
92
                if (taskPlanner == null) {
93
                        taskPlanner = new FIFOTaskPlanner(this);
94
                }
95
                return taskPlanner;
96
        }
97

    
98

    
99
        public void setTaskPlanner(ITaskPlanner planner) {
100
                taskPlanner = planner;
101
        }
102

    
103

    
104
        public void pause() {
105
                waiting = true;
106
        }
107

    
108

    
109
        public void resume() {
110
                waiting = false;
111
        }
112

    
113

    
114
        public Vector getTasks() {
115
                return tasks;
116
        }
117

    
118
        private class Worker implements Runnable {
119
                URLRetrieveTask r;
120
                int i = 0; 
121
                public void run() {
122
                        while (true) {
123
                                r = (URLRetrieveTask) take();
124
                                r.execute();
125
                        }
126
                }
127
        }
128

    
129
        protected URLRetrieveTask getURLPreviousRequest(URLRequest request) {
130
                // Is the one currently running?
131
                /*URLRetrieveTask aux = (URLRetrieveTask) worker.r;
132
                if (request.equals(aux.getRequest())) {
133
                                return aux;
134
                }*/        
135
                // Is one of those in the queue?
136
                for (int i = 0; i < tasks.size(); i++) {
137
                        URLRetrieveTask task = (URLRetrieveTask) tasks.get(i);
138
                        URLRequest aWorkingRequest = task.getRequest();
139
                        if (aWorkingRequest.equals(request)) {
140
                                return task;
141
                        }
142
                }
143
                return null;
144
        }
145
}