Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 4fa6beb0e356fa51490518d7ce98b976bdc218cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/********************************************************************************
 * Copyright (c) 2002, 2006 IBM Corporation. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Initial Contributors:
 * The following IBM employees contributed to the Remote System Explorer
 * component that contains this file: David McKnight, Kushal Munir, 
 * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, 
 * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
 * 
 * Contributors:
 * {Name} (company) - description of contribution.
 ********************************************************************************/

package org.eclipse.dstore.core.model;

/**
 * The Handler class is the base class for the threaded mechanisms in
 * the DataStore.  This is a thread that periodically does some activity.
 * The frequency of handling can be configured.
 */
public abstract class Handler extends Thread
{


	protected int _waitIncrement;
	protected DataStore _dataStore;
	protected boolean _keepRunning;

	/**
	 * Constructor
	 */
	public Handler()
	{
		_keepRunning = true;
		_waitIncrement = 100;
	}

	/**
	 * Sets the time interval to wait between handling
	 * @param value the wait interval
	 */
	public void setWaitTime(int value)
	{
		_waitIncrement = value;
	}

	/**
	 * Returns the time interval to wait between handling
	 * @return the wait interval
	 */
	public int getWaitTime()
	{
		return _waitIncrement;
	}

	/**
	 * Sets the associated DataStore
	 * @param dataStore
	 */
	public void setDataStore(DataStore dataStore)
	{
		_dataStore = dataStore;
	}

	/**
	 * Indicates whether the handler is finished or not
	 * @return whether the handler is finished or not
	 */
	public boolean isFinished()
	{
		return !_keepRunning;
	}

	/**
	 * Finish handling
	 */
	public void finish()
	{
		if (_keepRunning)
		{

			_waitIncrement = 0;
			_keepRunning = false;

			/* causes hang
			try
			{
			    interrupt();
			    join();
			}
			catch (InterruptedException e)
			{
			    System.out.println(e);
			}
			*/
			handle();
		}
	}

	/**
	 * Implemented to provide the periodic activity to be done in a handler.
	 * This method is called between wait intervals by the handler thread.
	 */
	public abstract void handle();

	/**
	 * Runs the handler loop in a thread.
	 */
	public void run()
	{
		while (_keepRunning)
		{
			/*
			try
			{
				Thread.sleep(_waitIncrement);
				Thread.yield();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
				finish();
				return;
			}
			*/
			waitForInput();

			handle();
		}
	}
	
	/**
	 * Causes the current thread to wait until this class request has been
	 * fulfilled.
	 */
	public synchronized void waitForInput()
	{
		try
		{
			wait();		
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
			finish();
			return;
		}
	}
	
	/**
	 * Causes all threads waiting for this class request to be filled
	 * to wake up.
	 */
	public synchronized void notifyInput()
	{
		notifyAll();
	}
}

Back to the top