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 @ 40559

History | View | Annotate | Download (3.31 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
/*
25
 * Created on 01-oct-2005
26
 */
27
package org.gvsig.remoteclient.taskplanning.retrieving;
28

    
29
import java.util.Date;
30
import java.util.Vector;
31

    
32
import org.gvsig.remoteclient.taskplanning.FIFOTaskPlanner;
33
import org.gvsig.remoteclient.taskplanning.IQueue;
34
import org.gvsig.remoteclient.taskplanning.IRunnableTask;
35
import org.gvsig.remoteclient.taskplanning.ITaskPlanner;
36

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

    
94
        public ITaskPlanner getTaskPlanner() {
95
                if (taskPlanner == null) {
96
                        taskPlanner = new FIFOTaskPlanner(this);
97
                }
98
                return taskPlanner;
99
        }
100

    
101

    
102
        public void setTaskPlanner(ITaskPlanner planner) {
103
                taskPlanner = planner;
104
        }
105

    
106

    
107
        public void pause() {
108
                waiting = true;
109
        }
110

    
111

    
112
        public void resume() {
113
                waiting = false;
114
        }
115

    
116

    
117
        public Vector getTasks() {
118
                return tasks;
119
        }
120

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

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