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: 75908adade767faf37cd5e8b010299b599435eac (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/***********************************************************************
 * Copyright (c) 2008 by SAP AG, Walldorf. 
 * 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:
 *     SAP AG - initial API and implementation
 ***********************************************************************/
package org.eclipse.jst.jee.model.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElementDelta;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jst.j2ee.model.IModelProviderEvent;
import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils;
import org.eclipse.jst.javaee.core.EjbLocalRef;
import org.eclipse.jst.javaee.core.JavaEEObject;
import org.eclipse.jst.javaee.core.Listener;
import org.eclipse.jst.javaee.core.ResourceRef;
import org.eclipse.jst.javaee.core.SecurityRole;
import org.eclipse.jst.javaee.core.SecurityRoleRef;
import org.eclipse.jst.javaee.ejb.SecurityIdentityType;
import org.eclipse.jst.javaee.web.Filter;
import org.eclipse.jst.javaee.web.FilterMapping;
import org.eclipse.jst.javaee.web.Servlet;
import org.eclipse.jst.javaee.web.ServletMapping;
import org.eclipse.jst.javaee.web.WebApp;
import org.eclipse.jst.javaee.web.WebFactory;
import org.eclipse.jst.jee.model.internal.common.AbstractAnnotationModelProvider;
import org.eclipse.jst.jee.model.internal.common.ManyToOneRelation;
import org.eclipse.jst.jee.model.internal.common.Result;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;

/**
 * @author Kiril Mitov k.mitov@sap.com
 * 
 */
public class WebAnnotationReader extends AbstractAnnotationModelProvider<WebApp> {
	
	private static final String WEB_SERVLET = "WebServlet"; //$NON-NLS-1$
	private static final String WEB_SERVLET_FQ = "javax.servlet.annotation.WebServlet"; //$NON-NLS-1$
	
	private static final String WEB_LISTENER = "WebListener"; //$NON-NLS-1$
	private static final String WEB_LISTENER_FQ = "javax.servlet.annotation.WebListener"; //$NON-NLS-1$

	private static final String WEB_FILTER = "WebFilter"; //$NON-NLS-1$
	private static final String WEB_FILTER_FQ = "javax.servlet.annotation.WebFilter"; //$NON-NLS-1$

	private ManyToOneRelation<JavaEEObject, ICompilationUnit> modelToUnit;

	private ManyToOneRelation<JavaEEObject, ICompilationUnit> modelToInterfaceUnit;

	private WebAnnotationFactory annotationFactory;

	private WebApp ddApp;

	public WebAnnotationReader(IFacetedProject facetedProject, WebApp ddApp) {
		super(facetedProject);
		if (ddApp == null)
			throw new IllegalArgumentException("The deployment descriptor model can not be null!"); //$NON-NLS-1$
		this.ddApp = ddApp;
	}

	@Override
	protected void preLoad() {
		modelObject = WebFactory.eINSTANCE.createWebApp();
		annotationFactory = WebAnnotationFactory.createFactory();
	}

	@Override
	protected void loadModel() throws CoreException {
		IJavaProject javaProject = JavaCore.create(facetedProject.getProject());
		final Collection<ICompilationUnit> javaUnits = new HashSet<ICompilationUnit>();
		for (final IPackageFragmentRoot root : javaProject.getAllPackageFragmentRoots()) {
			visitJavaFiles(javaUnits, root);
		}
		annotationFactory = WebAnnotationFactory.createFactory();

		modelToInterfaceUnit = new ManyToOneRelation<JavaEEObject, ICompilationUnit>();
		modelToUnit = new ManyToOneRelation<JavaEEObject, ICompilationUnit>();
		for (ICompilationUnit unit : javaUnits) {
			Result result = analyzeCompilationUnit(unit);
			if (result == null)
				continue;
			processResult(unit, result);
		}
	}

	/**
	 * Analyze this unit for a web artifact. If the file is not a valid java
	 * compilation unit or it does not contain a class the method returns
	 * <code>null</code>
	 * 
	 * Only the primary type of the compilation unit is processed.
	 * 
	 * @param unit
	 *            the unit to be processed.
	 * @return result from processing the file
	 * @throws JavaModelException
	 */
	private Result analyzeCompilationUnit(ICompilationUnit unit) throws JavaModelException {
		IType rootType = unit.findPrimaryType();
		/*
		 * If the compilation unit is not valid and can not be compiled the
		 * rootType may be null. This can happen if a class is define as follows
		 * <code> @Stateless public SomeClass implements SomeInterface{}</code>.
		 * Here the "class" word is missed and the type is not valid.
		 */
		if (rootType == null || !rootType.isClass())
			return null;
		for (Iterator iter = ddApp.getServlets().iterator(); iter.hasNext();) {
			Servlet servlet = (Servlet) iter.next();
			if (rootType.getFullyQualifiedName().equals(servlet.getServletClass()))
				return annotationFactory.createServlet(rootType, servlet.getServletName());
		}
		for (Iterator iter = ddApp.getListeners().iterator(); iter.hasNext();) {
			Listener listener = (Listener) iter.next();
			if (rootType.getFullyQualifiedName().equals(listener.getListenerClass()))
				return annotationFactory.createListener(rootType);
		}
		for (Iterator iter = ddApp.getFilters().iterator(); iter.hasNext();) {
			Filter filter = (Filter) iter.next();
			if (rootType.getFullyQualifiedName().equals(filter.getFilterClass())) {
				return annotationFactory.createFilter(rootType, filter.getFilterName());
			}
		}
		if(Float.parseFloat(facetedProject.getProjectFacetVersion(WebFacetUtils.WEB_FACET).getVersionString()) > 2.5){
			return createJavaeeObject(rootType);	
		}
		return null;
	}
	
	public Result createJavaeeObject(IType type) throws JavaModelException {
		Result result = null;
		for (IAnnotation annotation : type.getAnnotations()) {
			String annotationName = annotation.getElementName();
			if (WEB_SERVLET.equals(annotationName) || WEB_SERVLET_FQ.equals(annotationName)) {
				result = annotationFactory.createServlet(type, null);
				break;
			} else if (WEB_FILTER.equals(annotationName) || WEB_FILTER_FQ.equals(annotationName)) {
				result = annotationFactory.createFilter(type, null);
				break;
			} else if (WEB_LISTENER.equals(annotationName) || WEB_LISTENER_FQ.equals(annotationName)) {
				result = annotationFactory.createListener(type);
				break;
			} 
		}
		return result;
	}

	/**
	 * Process the result from parsing the unit. Depending on the result this
	 * might include adding a session bean, message driven bean, securityRole
	 * etc.
	 * 
	 * @param unit
	 * @param result
	 * @throws JavaModelException
	 */
	private void processResult(ICompilationUnit unit, Result result) throws JavaModelException {
		JavaEEObject mainObject = result.getMainObject();
		if (Servlet.class.isInstance(mainObject))
			servletFound(unit, (Servlet) result.getMainObject(), result.getDependedTypes());
		if (Listener.class.isInstance(mainObject))
			listenerFound(unit, (Listener) result.getMainObject(), result.getDependedTypes());
		if (Filter.class.isInstance(mainObject))
			filterFound(unit, (Filter) result.getMainObject(), result.getDependedTypes());
		for (JavaEEObject additional : result.getAdditional()) {
			if (EjbLocalRef.class.isInstance(additional)) {
				ejbLocalRefFound(unit, (EjbLocalRef) additional, result.getDependedTypes());
			} else if (ResourceRef.class.isInstance(additional)) {
				resourceRefFound(unit, (ResourceRef) additional, result.getDependedTypes());
			} else if (SecurityRole.class.isInstance(additional)) {
				securityRoleFound(result.getMainObject(), (SecurityRole) additional);
			} else if (SecurityIdentityType.class.isInstance(additional)) {
				securityIdentityTypeFound(unit, (SecurityIdentityType) additional);
			} else if (ServletMapping.class.isInstance(additional)) {
				servletMappingFound(unit, (ServletMapping)additional, result.getDependedTypes());
			} else if (FilterMapping.class.isInstance(additional)) {
				filterMappingFound(unit, (FilterMapping)additional, result.getDependedTypes());
			}
		}
	}

	private void filterMappingFound(ICompilationUnit unit,
			FilterMapping additional, Collection<IType> dependedTypes) throws JavaModelException {
		modelObject.getFilterMappings().add(additional);
		connectObjectWithFile(unit, additional, dependedTypes);
	}

	private void servletMappingFound(ICompilationUnit unit,
			ServletMapping additional, Collection<IType> dependedTypes) throws JavaModelException {
		modelObject.getServletMappings().add(additional);
		connectObjectWithFile(unit, additional, dependedTypes);
	}

	private void servletFound(ICompilationUnit unit, Servlet servlet, Collection<IType> dependedTypes) throws JavaModelException {
		modelObject.getServlets().add(servlet);
		connectObjectWithFile(unit, servlet, dependedTypes);
	}
	
	private void listenerFound(ICompilationUnit unit, Listener listener, Collection<IType> dependedTypes) throws JavaModelException {
		modelObject.getListeners().add(listener);
		connectObjectWithFile(unit, listener, dependedTypes);
	}
	
	private void filterFound(ICompilationUnit unit, Filter filter, Collection<IType> dependedTypes) throws JavaModelException {
		modelObject.getFilters().add(filter);
		connectObjectWithFile(unit, filter, dependedTypes);
	}

	private void securityIdentityTypeFound(ICompilationUnit file, SecurityIdentityType additional) {
	}

	private void resourceRefFound(ICompilationUnit unit, ResourceRef resourceRef, Collection<IType> dependedTypes)
			throws JavaModelException {
		modelObject.getResourceRefs().add(resourceRef);
		connectObjectWithFile(unit, resourceRef, dependedTypes);
	}

	private void ejbLocalRefFound(ICompilationUnit unit, EjbLocalRef localRef, Collection<IType> dependedTypes)
			throws JavaModelException {
		modelObject.getEjbLocalRefs().add(localRef);
		connectObjectWithFile(unit, localRef, dependedTypes);
	}

	private void connectObjectWithFile(ICompilationUnit unit, JavaEEObject localRef, Collection<IType> dependedTypes)
			throws JavaModelException {
		modelToUnit.connect(localRef, unit);
		for (IType type : dependedTypes) {
			if (type.isBinary() || type.isInterface() == false)
				continue;
			modelToInterfaceUnit.connect(localRef, type.getCompilationUnit());
		}
	}

	@Override
	protected void processAddedCompilationUnit(IModelProviderEvent modelEvent, ICompilationUnit unit)
			throws CoreException {
		Result result = analyzeCompilationUnit(unit);
		if (result == null || result.isEmpty())
			return;
		processResult(unit, result);
		modelEvent.addResource(unit);
	}

	@Override
	protected void processChangedCompilationUnit(IModelProviderEvent modelEvent, ICompilationUnit unit)
			throws CoreException {
		if (modelToUnit.containsTarget(unit))
			processChangedModelUnit(modelEvent, unit);
		else
			processAddedCompilationUnit(modelEvent, unit);
	}

	private void processChangedModelUnit(IModelProviderEvent modelEvent, ICompilationUnit unit) throws CoreException {
		processRemovedCompilationUnit(modelEvent, unit);
		processAddedCompilationUnit(modelEvent, unit);
	}

	@Override
	protected void processRemovedCompilationUnit(IModelProviderEvent modelEvent, ICompilationUnit unit)
			throws CoreException {
		if (modelToUnit.containsTarget(unit))
			processRemovedModelResource(modelEvent, unit);
		else if (modelToInterfaceUnit.containsTarget(unit))
			processRemoveInterface(modelEvent, unit);
	}

	private void processRemoveInterface(IModelProviderEvent event, ICompilationUnit unit) {
	}

	@Override
	protected void processRemovedPackage(IModelProviderEvent modelEvent, IJavaElementDelta delta) throws CoreException {
		Collection<ICompilationUnit> clonedCollection = cloneCollection(modelToUnit.getTargets());
		for (ICompilationUnit unit : clonedCollection) {
			if (unit.getParent().getElementName().equals(delta.getElement().getElementName())) {
				processRemovedCompilationUnit(modelEvent, unit);
			}
		}
	}

	private Collection<ICompilationUnit> cloneCollection(Collection<ICompilationUnit> targets) {
		Collection<ICompilationUnit> result = new ArrayList<ICompilationUnit>();
		for (ICompilationUnit iCompilationUnit : targets) {
			result.add(iCompilationUnit);
		}
		return result;
	}

	private void processRemovedModelResource(IModelProviderEvent event, ICompilationUnit file) {
		Collection<JavaEEObject> modelObjects = modelToUnit.getSources(file);
		for (JavaEEObject o : modelObjects) {
			if (Servlet.class.isInstance(o))
				disconnectFromRoles(o);
			EcoreUtil.remove((EObject) o);
		}
		modelToUnit.disconnect(file);
		event.setEventCode(event.getEventCode() | IModelProviderEvent.REMOVED_RESOURCE);
		event.addResource(file);
	}

	public void modify(Runnable runnable, IPath modelPath) {
	}

	public IStatus validateEdit(IPath modelPath, Object context) {
		return null;
	}

	@Override
	protected Collection<SecurityRoleRef> getSecurityRoleRefs(JavaEEObject target) {
		if (Servlet.class.isInstance(target))
			return ((Servlet) target).getSecurityRoleRefs();
		return null;
	}

	@Override
	protected Collection<SecurityRole> getSecurityRoles() {
		return modelObject.getSecurityRoles();
	}

}

Back to the top