Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4234010fce130490b8872d1b1e04391abce2d211 (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
/*******************************************************************************
 * Copyright (c) 2007 Oracle. 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.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.node;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.eclipse.jpt.utility.internal.SynchronizedBoolean;

/**
 * This implementation of Runnable will asynchronously validate
 * a branch of nodes. It will wait until a shared "validate" flag is
 * set to validate the branch. Once the the branch is validated,
 * the thread will quiesce until the flag is set again.
 * 
 * There are two ways to stop this thread:
 * 	- Thread.interrupt()
 * 	- set the "continue" flag to false
 * For now, these two are equivalent; but in the future we might
 * pass the "continue" flag to the Node.validateBranch()
 * method so we can short-circuit the validation instead of waiting
 * until the entire branch is validated.
 */
public class RunnableValidation
	implements Runnable
{
	/** The node whose branch this thread will validate. */
	private final Node node;

	/** When this flag is set to true, we kick off the validation routine. */
	private final SynchronizedBoolean validateFlag;

	/** When this flag is set to false, we allow this thread to die. */
	private final SynchronizedBoolean continueFlag;

	/** Log any exceptions encountered during validation with the following settings. */
	private final Logger exceptionLogger;
	private final Level exceptionLevel;
	private final String exceptionMessage;


	/**
	 * Construct a validation thread.
	 */
	public RunnableValidation(
			Node node,
			SynchronizedBoolean validateFlag,
			SynchronizedBoolean continueFlag,
			Logger exceptionLogger,
			Level exceptionLevel,
			String exceptionMessage
	) {
		super();
		this.node = node;
		this.validateFlag = validateFlag;
		this.continueFlag = continueFlag;
		this.exceptionLogger = exceptionLogger;
		this.exceptionLevel = exceptionLevel;
		this.exceptionMessage = exceptionMessage;
	}


	// ********** Runnable Implementation **********

	/**
	 * Loop while the "continue" flag is true and the thread
	 * has not been interrupted by another thread.
	 * In each loop: Wait until the "validate" flag is set to true,
	 * then set it back to false and validate the branch of nodes.
	 */
	public void run() {
		while (this.continueFlag.isTrue()) {
			try {
				this.validateFlag.waitToSetFalse();
			} catch (InterruptedException ex) {
				// we were interrupted while waiting, must be quittin' time
				return;
			}
			this.validateNode();
		}
	}

	/**
	 * Validate the node, logging any exceptions.
	 * If an exception occurs, we terminate the validation until the
	 * "validation" flag is set again. Some exceptions occur because
	 * of concurrent changes to the model that occur *after* validation
	 * starts but before it completes an entire pass over the model. If that
	 * is the case, things should be OK; because the exception will be
	 * caught and the "validation" flag will have been set again *during* the
	 * initial validation pass. So when we return from catching the exception
	 * we will simply re-start the validation, hopefully with the model in
	 * a consistent state that will prevent another exception from
	 * occurring. Of course, if we have any exceptions that are *not*
	 * the result of the model being in an inconsistent state, we will
	 * probably fill the log; and those exceptions are bugs that need
	 * to be fixed. (!) Hopefully the user will notice the enormous log and
	 * contact support....  ~bjv
	 */
	private void validateNode() {
		try {
			this.node.validateBranch();
		} catch (Throwable ex) {
			this.logException(ex);
		}
	}

	/**
	 * We need to do all this because Logger#log(LogRecord) does not pass through
	 * Logger#doLog(LogRecord) like all the other Logger#log(...) methods.
	 */
	private void logException(Throwable ex) {
		LogRecord logRecord = new LogRecord(this.exceptionLevel, this.exceptionMessage);
		logRecord.setParameters(new Object[] { this.node.displayString() });
		logRecord.setThrown(ex);
		logRecord.setLoggerName(this.exceptionLogger.getName());
		logRecord.setResourceBundle(this.exceptionLogger.getResourceBundle());
		this.exceptionLogger.log(logRecord);
	}

}

Back to the top