File: ValidationPlugin.java

package info (click to toggle)
eclipse-wtp 3.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,860 kB
  • sloc: java: 215,049; xml: 3,946; sh: 70; makefile: 9
file content (199 lines) | stat: -rw-r--r-- 7,265 bytes parent folder | download
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
/*******************************************************************************
 * Copyright (c) 2001, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.validation.internal.plugin;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.wst.validation.ValidationFramework;
import org.eclipse.wst.validation.internal.DependencyIndex;
import org.eclipse.wst.validation.internal.EventManager;
import org.eclipse.wst.validation.internal.ProjectUnavailableError;
import org.eclipse.wst.validation.internal.ResourceUnavailableError;
import org.eclipse.wst.validation.internal.Tracing;
import org.eclipse.wst.validation.internal.core.Message;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

/**
 * How does this plug-in get activated? There are many other plug-ins that depend on this plug-in, so there are
 * many paths that will activate it. One common path is the J2EE perspective. If the J2EE plug-in start method, they
 * reference a class in this plug-in, which activates the plug-in. For that case this plug-in will be active even before
 * the workbench is up.
 */
public class ValidationPlugin extends Plugin {
	public static final String 	VALIDATION_PROP_FILE_NAME = "validate_base"; //$NON-NLS-1$
	
	/** org.eclipse.wst.validation - the plug-in id */
	public static final String 	PLUGIN_ID = "org.eclipse.wst.validation"; //$NON-NLS-1$
	private static ValidationPlugin _plugin;
	private static Message 		_message;
	public static final String VALIDATION_BUILDER_ID = PLUGIN_ID + ".validationbuilder"; //$NON-NLS-1$// plugin id of the validation builder
	public static final String VALIDATOR_EXT_PT_ID = "validator"; //$NON-NLS-1$// extension point declaration of the validator 
	private ServiceTracker<IWorkspace, IWorkspace> workspaceServiceTracker;
	 
	public ValidationPlugin() {
		super();
		if (_plugin == null)_plugin = this;
	}
	
	/**
	 * Answer the name of the bundle's property file.
	 * 
	 * @deprecated Use getBundlePropertyFileName() instead.
	 */
	public static String getBundleName() {
		return getBundlePropertyFileName();
	}

	/**
	 * Answer the name of the bundle's property file.
	 */
	public static String getBundlePropertyFileName() {
		return VALIDATION_PROP_FILE_NAME;
	}

	public static Message getMessage() {
		if (_message == null) {
			_message = new Message();
			_message.setBundleName(getBundlePropertyFileName());
		}
		// clear the message for reuse
		_message.setId(null);
		_message.setParams(null);
		_message.setTargetObject(null);
		_message.setGroupName(null);
		_message.setSeverity(IMessage.LOW_SEVERITY);
		return _message;
	}

	public static ValidationPlugin getPlugin() {
		return _plugin;
	}

	public static boolean isActivated() {
		Bundle bundle = Platform.getBundle(PLUGIN_ID);
		if (bundle != null)
			return bundle.getState() == Bundle.ACTIVE;
		return false;
	}

	public void start(BundleContext context) throws Exception {
		super.start(context);
		workspaceServiceTracker = new ServiceTracker<>(context, IWorkspace.class, null) {
			@Override
			public IWorkspace addingService(ServiceReference<IWorkspace> reference) {
				IWorkspace workspace = super.addingService(reference);
				workspace.addResourceChangeListener(EventManager.getManager(),
				IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.PRE_DELETE |
				IResourceChangeEvent.POST_BUILD | IResourceChangeEvent.PRE_BUILD | IResourceChangeEvent.POST_CHANGE);
				DependencyIndex di = (DependencyIndex)ValidationFramework.getDefault().getDependencyIndex();
				try {
					workspace.addSaveParticipant(ValidationPlugin.this, di);
				} catch (CoreException e) {
					throw new RuntimeException(e);
				}
				return workspace;
			}
		};
		workspaceServiceTracker.open();
//		ws.addResourceChangeListener(ValOperationManager.getDefault(), 
//			IResourceChangeEvent.POST_BUILD | IResourceChangeEvent.PRE_BUILD);

	}

	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		var ws = workspaceServiceTracker.getService();
		if (ws != null) {
		      ws.removeResourceChangeListener(EventManager.getManager());
		}
		workspaceServiceTracker.close();		
		ValidationFramework.getDefault().cancel();
//		ResourcesPlugin.getWorkspace().removeResourceChangeListener( ValOperationManager.getDefault() );		
		EventManager.getManager().shutdown();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.frameworks.internal.WTPPlugin#getPluginID()
	 */
	public String getPluginID() {
		return PLUGIN_ID;
	}
	
	public static IEclipsePreferences getPreferences(IProject project){
		IScopeContext projectContext = new ProjectScope(project);
		return projectContext.getNode(PLUGIN_ID);
	}
	
	/**
	 * Write this exception to the log.
	 * <p>
	 * We are in the transition of moving to a new approach for localized messages. This is the new 
	 * approach for exceptions.
	 * 
	 * @param e the throwable, this can be null in which case it is a nop.
	 */
	public void handleException(Throwable e){
		handleException(e, IStatus.ERROR);
	}
	
	/**
	 * Write this exception to the log.
	 * <p>
	 * We are in the transition of moving to a new approach for localized messages. This is the new 
	 * approach for exceptions.
	 * 
	 * @param e the throwable, this can be null in which case it is a nop.
	 * @param severity the severity of the message. This must be one of the IStatus severities. 
	 */
	public void handleException(Throwable e, int severity){
		if (e == null)return;
		Status status = new Status(severity, PLUGIN_ID, e.getLocalizedMessage(), e);
		getLog().log(status);
	}
	
	public void handleProjectUnavailableError(ProjectUnavailableError e){
		if (Tracing.isLogging())handleException(e);
	}
	
	public void handleResourceUnavailableError(ResourceUnavailableError e){
		if (Tracing.isLogging())handleException(e);
	}
	
	/** 
	 * Write a message into the log. 
	 * 
	 * We are in the transition of moving to a new approach for localized messages. This is the new 
	 * approach for exceptions.
	 * 
	 * @param severity message severity, see IStaus
	 * @param message a localized message
	 */
	public void logMessage(int severity, String message){
		Status status = new Status(severity, PLUGIN_ID, message);
		getLog().log(status);
		
	}	
}