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: 045f514773c50a69036ceeea89587453f2dd5da1 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.validation.internal.operations;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jem.util.logger.LogEntry;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.wst.validation.internal.ConfigurationManager;
import org.eclipse.wst.validation.internal.InternalValidatorManager;
import org.eclipse.wst.validation.internal.ProjectConfiguration;
import org.eclipse.wst.validation.internal.ResourceConstants;
import org.eclipse.wst.validation.internal.ResourceHandler;
import org.eclipse.wst.validation.internal.TimeEntry;
import org.eclipse.wst.validation.internal.ValidatorMetaData;
import org.eclipse.wst.validation.internal.plugin.ValidationPlugin;

/**
 * Validation Framework Builder.
 * 
 * This builder is configured on J2EE IProjects automatically, can be added to other types of
 * projects through the Properties page, and launches validation on the project if the project has
 * build validation enabled.
 */
public class ValidationBuilder extends IncrementalProjectBuilder {
	public static final int NO_DELTA_CHANGE = -1;
	protected List referencedProjects;
	protected IWorkbenchContext workbenchContext = null;

	public ValidationBuilder() {
		super();
	}

	private IProject[] getAllReferencedProjects(IProject project, Set visitedProjects) {
		if (visitedProjects == null)
			visitedProjects = new HashSet();
		else if (visitedProjects.contains(project))
			return getReferencedProjects();
		else
			visitedProjects.add(project);
		if (referencedProjects == null)
			referencedProjects = new ArrayList();
		try {
			if (project.isAccessible()) {
				IProject[] refProjArray = project.getReferencedProjects();
				collectReferecedProject(refProjArray);
				for (int i = 0; i < refProjArray.length; i++) {
					IProject refProject = refProjArray[i];
					getAllReferencedProjects(refProject, visitedProjects);
				}
			}
			return getReferencedProjects();
		} catch (CoreException core) {
			return null;
		}
	}
	
	public IWorkbenchContext getWorkbenchContext() {
		if(workbenchContext == null) {
			workbenchContext = new WorkbenchContext();
			workbenchContext.setProject(getProject());
		}
		return workbenchContext;
	}


	/**
	 * @param referencedProjects2
	 * @param refProjArray
	 */
	private void collectReferecedProject(IProject[] refProjArray) {
		for (int i = 0; i < refProjArray.length; i++) {
			IProject project = refProjArray[i];
			if (!referencedProjects.contains(project))
				referencedProjects.add(project);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.resources.IncrementalProjectBuilder#clean(org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected void clean(IProgressMonitor monitor) throws CoreException {
		IProject currentProject = getProject();
		if (currentProject == null || !currentProject.isAccessible())
			return;
		try {
			ProjectConfiguration prjp = ConfigurationManager.getManager().getProjectConfiguration(currentProject);
			ValidatorMetaData[] vmds = prjp.getValidators();
			for (int i = 0; i < vmds.length; i++) {
				ValidatorMetaData vmd = vmds[i];
				// For validators who aren't going to run, clear their messages from the task list.
				// Don't need to check for duplicate entries because each Validator must be unique.
				// The uniqueness of each Validator is checked by the plugin registry.
				WorkbenchReporter.removeAllMessages(currentProject, vmd.getValidatorNames(), null);
			}
		} catch (InvocationTargetException exc) {
			Logger logger = ValidationPlugin.getPlugin().getMsgLogger();
			if (logger.isLoggingLevel(Level.SEVERE)) {
				LogEntry entry = ValidationPlugin.getLogEntry();
				entry.setSourceIdentifier("ValidatorManager.updateTaskList(" + currentProject.getName() + ")"); //$NON-NLS-1$  //$NON-NLS-2$
				entry.setTargetException(exc);
				logger.write(Level.SEVERE, entry);
				if (exc.getTargetException() != null) {
					entry.setTargetException(exc);
					logger.write(Level.SEVERE, entry);
				}
			}
		}

	}

	private IProject[] getReferencedProjects() {
		IProject[] refProjArray = new IProject[referencedProjects.size()];
		for (int i = 0; i < referencedProjects.size(); i++) {
			refProjArray[i] = (IProject) referencedProjects.get(i);
		}
		return refProjArray;
	}

	public IProject[] build(int kind, Map parameters, IProgressMonitor monitor) {
		long start = System.currentTimeMillis();
		int executionMap = 0x0;
		Logger logger = ValidationPlugin.getPlugin().getMsgLogger();
		IResourceDelta delta = null;
		IProject project = getProject();
		IProject[] referenced = getAllReferencedProjects(project, null);
		try {
			if (ValidatorManager.getManager().isSuspended(project)) {
				// Do not perform validation on this project
				executionMap |= 0x1;
				return referenced;
			}
			ProjectConfiguration prjp = ConfigurationManager.getManager().getProjectConfiguration(project);
			delta = getDelta(project);
			boolean doFullBuild = (kind == FULL_BUILD);
//			boolean doAutoBuild = ((delta != null) && (kind == AUTO_BUILD));
//			boolean doIncrementalBuild = ((delta != null) && (kind == INCREMENTAL_BUILD));
//			if ((doFullBuild || doIncrementalBuild) && !prjp.isBuildValidate()) {
//				// Is a build validation about to be invoked? If so, does the
//				// user want build validation to run?
//				executionMap |= 0x2;
//				return referenced;
//			}
			// It is possible for kind to == AUTO_BUILD while delta is null
			// (saw this
			// when creating a project by copying another project.)
			// However, a "Rebuild Project" will invoke this builder with
			// kind==FULL_BUILD
			// and a null delta, and validation should run in that case.
			if (!doFullBuild && delta == null) {
				if (isReferencedProjectInDelta(referenced)) {
					performFullBuildForReferencedProjectChanged(monitor, prjp);
				} else {
					String[] msgParms = new String[]{project.getName()};
					monitor.subTask(ResourceHandler.getExternalizedMessage(ResourceConstants.VBF_STATUS_NULL_DELTA, msgParms));
					// A null delta means that a full build must be performed,
					// but this builder was invoked with an incremental or
					// automatic
					// build kind. Return without doing anything so that the
					// user
					// doesn't have to wait forever.
					executionMap |= 0x4;
				}
				return referenced;
			}
			if (doFullBuild) {
				performFullBuild(monitor, prjp);
			} else {
//				if (doAutoBuild && !prjp.isAutoValidate()) {
//					executionMap |= 0x8;
//					return referenced;
//				}
				if (delta.getAffectedChildren().length == 0) {
					if (isReferencedProjectInDelta(referenced))
						performFullBuildForReferencedProjectChanged(monitor, prjp);
					else
						executionMap |= 0x10;
					return referenced;
				}
				EnabledIncrementalValidatorsOperation operation = new EnabledIncrementalValidatorsOperation(project, delta, true);
				operation.run(monitor);
			}
			return referenced;
		} catch (InvocationTargetException exc) {
			logInvocationTargetException(logger, exc);
			executionMap |= 0x20;
			return referenced;
		} catch (Exception exc) {
			logBuildError(logger, exc);
			executionMap |= 0x40;
			return referenced;
		} finally {
			referencedProjects = null;
			// The builder's time needs to be FINE because the builder is
			// called often.
			if (logger.isLoggingLevel(Level.FINE)) {
				logBuilderTimeEntry(start, executionMap, logger, delta);
			}
		}
	}

	private boolean isReferencedProjectInDelta(IProject[] referenced) {
		IProject p = null;
		for (int i = 0; i < referenced.length; i++) {
			p = referenced[i];
			IResourceDelta delta = getDelta(p);
			if (delta != null && delta.getAffectedChildren().length > 0)
				return true;
		}
		return false;
	}

	private void performFullBuildForReferencedProjectChanged(IProgressMonitor monitor, ProjectConfiguration prjp) throws InvocationTargetException {
		performFullBuild(monitor, prjp, true);
	}

	private void performFullBuild(IProgressMonitor monitor, ProjectConfiguration prjp) throws InvocationTargetException {
		performFullBuild(monitor, prjp, false);
	}

	private void performFullBuild(IProgressMonitor monitor, ProjectConfiguration prjp, boolean onlyDependentValidators) throws InvocationTargetException {
		ValidatorMetaData[] enabledValidators = prjp.getEnabledFullBuildValidators(true, onlyDependentValidators);
		if ((enabledValidators != null) && (enabledValidators.length > 0)) {
			Set enabledValidatorsSet = InternalValidatorManager.wrapInSet(enabledValidators);
			EnabledValidatorsOperation op = new EnabledValidatorsOperation(getProject(), enabledValidatorsSet, true);
			op.run(monitor);
		}
	}

	private void logInvocationTargetException(Logger logger, InvocationTargetException exc) {
		if (logger.isLoggingLevel(Level.SEVERE)) {
			LogEntry entry = ValidationPlugin.getLogEntry();
			entry.setSourceID("ValidationBuilder::build"); //$NON-NLS-1$
			entry.setTargetException(exc);
			logger.write(Level.SEVERE, entry);
			if (exc.getTargetException() != null) {
				entry.setTargetException(exc);
				logger.write(Level.SEVERE, entry);
			}
		}
	}

	private void logBuildError(Logger logger, Throwable exc) {
		if (logger.isLoggingLevel(Level.SEVERE)) {
			if( ! (exc instanceof OperationCanceledException) ){
				LogEntry entry = ValidationPlugin.getLogEntry();
				entry.setSourceID("ValidationBuilder.build(int, Map, IProgressMonitor)"); //$NON-NLS-1$
				entry.setTargetException(exc);
				logger.write(Level.SEVERE, entry);
			}
		}
	}

	private void logBuilderTimeEntry(long start, int executionMap, Logger logger, IResourceDelta delta) {
		TimeEntry entry = ValidationPlugin.getTimeEntry();
		entry.setSourceID("ValidationBuilder.build(int, Map, IProgressMonitor)"); //$NON-NLS-1$
		entry.setProjectName(getProject().getName()); //$NON-NLS-1$  //$NON-NLS-2$
		entry.setExecutionMap(executionMap);
		entry.setElapsedTime(System.currentTimeMillis() - start);
		if (delta == null) {
			entry.setDetails("delta == null"); //$NON-NLS-1$
		}
		entry.setToolName("ValidationBuilder"); //$NON-NLS-1$
		logger.write(Level.FINE, entry);
	}
}

Back to the top