Revision 25264

View differences:

branches/v2_0_0_prep/libraries/libTools/.classpath
5 5
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6 6
	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
7 7
	<classpathentry kind="lib" path="lib/easymock-1.2_Java1.3.jar"/>
8
	<classpathentry kind="lib" path="/libFMap_dal/lib/slf4j-api-1.5.0.jar"/>
8 9
	<classpathentry kind="output" path="bin"/>
9 10
</classpath>
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/task/TaskManager.java
1
package org.gvsig.tools.task;
2

  
3

  
4
public interface TaskManager {
5

  
6
	public Queue createQueue();
7
	
8
	/**
9
	 * Register the executor for the current thread.
10
	 * 
11
	 * @param executor
12
	 */
13
	public void registerExecutor(Executor executor) ;
14
	
15
	/**
16
	 * Get the associated executor to the current thread.
17
	 * 
18
	 * If not exist an executor for the current thread return a dumb
19
	 * executor. 
20
	 *  
21
	 * @return the executor for the current thread.
22
	 */
23
	public Executor getExecutor(); 	
24
}
0 25

  
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/task/impl/DefaultTaskManager.java
1
package org.gvsig.tools.task.impl;
2

  
3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6
import java.util.Timer;
7
import java.util.TimerTask;
8

  
9
import org.gvsig.tools.task.Executor;
10
import org.gvsig.tools.task.Queue;
11
import org.gvsig.tools.task.TaskManager;
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

  
15
public class DefaultTaskManager implements TaskManager {
16

  
17
    final static private Logger logger = LoggerFactory.getLogger(DefaultTaskManager.class);
18
    
19
	class ExecutorAndThread {
20
		Thread thread;
21
		Executor executor;
22
		
23
		ExecutorAndThread(Thread thread, Executor executor) {
24
			this.thread = thread;
25
			this.executor = executor;
26
		}
27
	}
28
	class DumbExecutor implements Executor {
29
		public void execute(Runnable command) {
30
			command.run();
31
		}
32
	}
33
	
34
	private Map executorsByThread;
35
	private Executor dumbExecutor;
36
	private Timer timer;
37
	
38
	public DefaultTaskManager() {
39
		this.executorsByThread = new HashMap();
40
		this.dumbExecutor = new DumbExecutor();
41
		startDeadsExecutorsCollector(60000); // 1 minuto.
42
	}
43
	
44
	/**
45
	 * Register the executor for the current thread.
46
	 * 
47
	 * @param executor
48
	 */
49
	public void registerExecutor(Executor executor) {
50
		synchronized (executorsByThread) {
51
			ExecutorAndThread x = new ExecutorAndThread(Thread.currentThread(), executor);
52
			Object key = new Long(x.thread.getId());
53
			this.executorsByThread.put(key, x);
54
		}
55
	}
56
	
57
	/**
58
	 * Get the associated executor to the current thread.
59
	 * 
60
	 * If not exist an executor for the current thread return a dumb
61
	 * executor. 
62
	 *  
63
	 * @return the executor for the current thread.
64
	 */
65
	public Executor getExecutor() {
66
		synchronized (executorsByThread) {
67
			Object key = new Long(Thread.currentThread().getId());
68
			ExecutorAndThread x = (ExecutorAndThread) this.executorsByThread.get(key); 
69
			if( x != null ) {
70
				if( x.thread.isAlive() ) {
71
					return x.executor;
72
				}
73
				this.executorsByThread.remove(x);
74
			}
75
			return this.dumbExecutor;
76
		}
77
	}
78

  
79
	private void startDeadsExecutorsCollector(long milis) {
80
		if (this.timer == null){
81
			this.timer = new Timer();
82
		}
83
		this.timer.scheduleAtFixedRate(new TimerTask() {
84
			public void run() {
85
				try {
86
					collectDeadExecutors();
87
				} catch (Exception e) {
88
					DefaultTaskManager.logger.warn("Exception collecting dead executors.", e);
89
				}
90
			}
91

  
92
		}, milis, milis);
93
	}
94

  
95
	/*
96
	private void stopDeadsExecutorsCollector() {
97
		if (this.timer != null) {
98
			this.timer.cancel();
99
		}
100

  
101
	}
102
	*/
103

  
104
	private void collectDeadExecutors() {
105
		synchronized (executorsByThread) {
106
			Iterator it = this.executorsByThread.values().iterator();
107
			while( it.hasNext() ) {
108
				ExecutorAndThread x = (ExecutorAndThread) it.next();
109
				if( ! x.thread.isAlive() ) {
110
					it.remove();
111
				}
112
			}
113
		}
114
	}
115

  
116
	public Queue createQueue() {
117
		return new DefaultQueue();
118
	}
119

  
120
	
121
}
0 122

  
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/task/impl/DefaultQueue.java
1
package org.gvsig.tools.task.impl;
2

  
3
import java.util.ArrayList;
4
import java.util.List;
5

  
6
import org.gvsig.tools.task.Queue;
7

  
8

  
9
public class DefaultQueue implements Queue {
10

  
11
	private List list = new ArrayList();
12

  
13
	public DefaultQueue() {
14
	}
15

  
16
	public void clear() {
17
		synchronized (list) {
18
			list.clear();
19
		}
20
	}
21

  
22
	public void put(Object data) {
23
		synchronized (list) {
24
			list.add(data);
25
			list.notify();
26
		}
27
	}
28

  
29
	public Object top() throws InterruptedException {
30
		Object data = null;
31
		synchronized (list) {
32
			if (list.isEmpty()) {
33
				list.wait();
34
			}
35
			data = list.get(0);
36
		}
37
		return data;
38
	}
39

  
40
	public Object top_nowait() {
41
		try {
42
			Object data;
43
			synchronized (list) {
44
				data = list.get(0);
45
			}
46
			return data;
47
		} catch (Exception except) {
48
			return null;
49
		}
50
	}
51

  
52
	public Object get() throws InterruptedException {
53
		Object data = null;
54
		synchronized (list) {
55
			if (list.isEmpty()) {
56
				list.wait();
57
			}
58
			data = list.get(0);
59
			list.remove(0);
60
		}
61
		return data;
62
	}
63

  
64
	public Object get_nowait() {
65
		try {
66
			Object data;
67
			synchronized (list) {
68
				data = list.get(0);
69
				list.remove(0);
70
			}
71
			return data;
72
		} catch (Exception except) {
73
			return null;
74
		}
75
	}
76

  
77
	public boolean isEmpty() {
78
		synchronized (list) {
79
			return list.isEmpty();
80
		}
81
	}
82

  
83
	public int getSize() {
84
		synchronized (list) {
85
			return list.size();
86
		}
87
	}
88
}
0 89

  
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/task/Queue.java
1
package org.gvsig.tools.task;
2

  
3

  
4
public interface Queue {
5

  
6
	public void put(Object data);
7

  
8
	public Object top() throws InterruptedException;
9

  
10
	public Object top_nowait();
11

  
12
	public Object get() throws InterruptedException;
13

  
14
	public Object get_nowait();
15

  
16
	public boolean isEmpty();
17

  
18
	public int getSize();
19

  
20
	public void clear();
21

  
22

  
23
}
0 24

  
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/ToolsLocator.java
8 8
import org.gvsig.tools.locator.LocatorException;
9 9
import org.gvsig.tools.operations.OperationManager;
10 10
import org.gvsig.tools.persistence.PersistenceManager;
11
import org.gvsig.tools.task.TaskManager;
11 12

  
12 13
public class ToolsLocator extends AbstractLocator {
13 14

  
......
25 26

  
26 27
    private static final String DYNOBJECT_MANAGER_DESCRIPTION = "DynObjectManager of gvSIG";
27 28

  
29
	public static final String TASK_MANAGER_NAME = "toolslocator.manager.task";
30

  
31
	private static final String TASK_MANAGER_DESCRIPTION = "TaskManager";
32

  
28 33
	/**
29 34
	 * Unique instance.
30 35
	 */
......
132 137
        getInstance().register(DYNOBJECT_MANAGER_NAME,
133 138
                DYNOBJECT_MANAGER_DESCRIPTION, clazz);
134 139
    }
140

  
141
	/**
142
	 * Return a reference to TaskManager.
143
	 *
144
	 * @return a reference to PersistenceManager
145
	 * @throws LocatorException
146
	 *             if there is no access to the class or the class cannot be
147
	 *             instantiated
148
	 * @see Locator#get(String)
149
	 */
150
	public static TaskManager getTaskManager()
151
			throws LocatorException {
152
		return (TaskManager) getInstance().get(TASK_MANAGER_NAME);
153
	}
154

  
155
	/**
156
	 * Registers the Class implementing the PersistenceManager interface.
157
	 *
158
	 * @param clazz
159
	 *            implementing the PersistenceManager interface
160
	 */
161
	public static void registerTaskManager(Class clazz) {
162
		getInstance().register(TASK_MANAGER_NAME,
163
				TASK_MANAGER_DESCRIPTION,
164
				clazz);
165
	}
166

  
167
	public static void registerDefaultTaskManager(Class clazz) {
168
		getInstance().registerDefault(TASK_MANAGER_NAME,
169
				TASK_MANAGER_DESCRIPTION, clazz);
170
	}
171

  
172

  
135 173
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/ToolsLibrary.java
30 30
import org.gvsig.tools.locator.Library;
31 31
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
32 32
import org.gvsig.tools.operations.impl.DefaultOperationManager;
33
import org.gvsig.tools.task.impl.DefaultTaskManager;
33 34

  
34 35

  
35 36
public class ToolsLibrary implements Library {
36 37

  
37 38
    public void initialize() throws ReferenceNotRegisteredException {
38
    	ToolsLocator
39
				.registerDefaultOperationManager(DefaultOperationManager.class);
39
    	ToolsLocator.registerDefaultOperationManager(DefaultOperationManager.class);
40
    	ToolsLocator.registerDefaultTaskManager(DefaultTaskManager.class);
40 41
        ToolsLocator.registerDynObjectManager(DefaultDynObjectManager.class);
41 42
	}
42 43

  

Also available in: Unified diff