Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8b5bc230dc31dbbf37ef38529887f3230f5af942 (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
/***********************************************************************
 * 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.ui.internal.navigator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.j2ee.componentcore.util.EARVirtualComponent;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
import org.eclipse.jst.j2ee.model.ModelProviderManager;
import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities;
import org.eclipse.jst.javaee.application.Application;
import org.eclipse.jst.jee.ui.internal.navigator.ear.AbstractEarNode;
import org.eclipse.jst.jee.ui.internal.navigator.ear.GroupEARProvider;
import org.eclipse.jst.jee.ui.plugin.JEEUIPlugin;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;

/**
 * Application Client 5.0 Content provider is Deployment Descriptor content provider, 
 * used for constructing of the descriptor tree in project explorer. 
 * 
 * @author Dimitar Giormov
 */
public class Ear5ContentProvider extends JEE5ContentProvider {

	public final static String EAR_DEFAULT_LIB = "/lib"; //$NON-NLS-1$

	private List getComponentReferencesAsList(List componentTypes, IVirtualComponent virtualComponent, IPath runtimePath) {
		List components = new ArrayList();
		IVirtualComponent earComponent = virtualComponent;
		if (earComponent != null ) {
			IVirtualReference[] refComponents = earComponent.getReferences();
			for (int i = 0; i < refComponents.length; i++) {
				IVirtualComponent module = refComponents[i].getReferencedComponent();
				if (module == null) continue;
				// if component types passed in is null then return all components
				if (componentTypes == null || componentTypes.size() == 0) {
					components.add(refComponents[i]);
				} else {
					if (componentTypes.contains(JavaEEProjectUtilities.getJ2EEComponentType(module))) {
						components.add(refComponents[i]);
					}
				}
			}
		}
		return components;
	}

	public IVirtualReference[] getUtilityModuleReferences(IVirtualComponent component) {  
		List explicitUtilityReferences = 
			getComponentReferencesAsList(Collections.singletonList(J2EEProjectUtilities.UTILITY), component, null);

		// fetch other Utility Jars attached to the EAR project 
		List implicitUtilityReferenceTypes =
			Arrays.asList(new String[] {  
					IModuleConstants.JST_APPCLIENT_MODULE,
					IModuleConstants.JST_WEB_MODULE,	 
					IModuleConstants.JST_EJB_MODULE 
			});

		List implicitUtilityReferences = 
			getComponentReferencesAsList(implicitUtilityReferenceTypes, component, null);

		List allUtilityModuleReferences = new ArrayList();
		allUtilityModuleReferences.addAll(explicitUtilityReferences);
		allUtilityModuleReferences.addAll(implicitUtilityReferences);

		if(allUtilityModuleReferences.size() > 0)
			return (IVirtualReference[]) allUtilityModuleReferences.toArray(new IVirtualReference[allUtilityModuleReferences.size()]);
		return new IVirtualReference[0];

	}

	public Object[] getChildren(Object aParentElement) {
		IProject project = null;
		List children = new ArrayList();
		if (aParentElement instanceof GroupEARProvider) {
			project = (IProject) ((GroupEARProvider)aParentElement).getProject();

			IVirtualComponent projectComponent = ComponentCore.createComponent(project);
			try {
				IFacetedProject facetedProject = ProjectFacetsManager.create(project);
				if (facetedProject != null && 
						facetedProject.hasProjectFacet(
								ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_EAR_MODULE).getVersion(
										J2EEVersionConstants.VERSION_5_0_TEXT))) {

					List libs = getComponentReferencesAsList(Collections.singletonList(J2EEProjectUtilities.UTILITY), projectComponent,
							new Path("/" + EAR_DEFAULT_LIB)); //$NON-NLS-1$

							ArrayList bundledLibs = new ArrayList();
							ArrayList appLibsInTheRoot = new ArrayList();

							for (int i = 0; i < libs.size(); i++) {
								IVirtualReference reference = (IVirtualReference) libs.get(i);
								IPath runtimePath = reference.getRuntimePath();

								if (runtimePath != null && runtimePath.segment(0) != null && 
//										runtimePath.equals(new Path(EAR_DEFAULT_LIB))) {
									!runtimePath.isRoot()) {
									bundledLibs.add(libs.get(i));
								} else {
									appLibsInTheRoot.add(libs.get(i));
								}
							}
							
							String libDir = EAR_DEFAULT_LIB;
							if (J2EEProjectUtilities.isJEEProject(project)) {
								String oldLibDir = ((Application)ModelProviderManager.getModelProvider(project).getModelObject()).getLibraryDirectory();
								if (oldLibDir == null) oldLibDir = EAR_DEFAULT_LIB;
								libDir = oldLibDir;
							}
//
//							List implicitUtilityReferenceTypes =
//								Arrays.asList(new String[] {  
//										IModuleConstants.JST_APPCLIENT_MODULE,
//										IModuleConstants.JST_WEB_MODULE,
//										IModuleConstants.JST_EJB_MODULE,
//										IModuleConstants.JST_CONNECTOR_MODULE});
//

//							List modules = getComponentReferencesAsList(implicitUtilityReferenceTypes, projectComponent, new Path("/")); //$NON-NLS-1$
//							ModulesNode modulesNode = new ModulesNode(project);

//							children.add(modulesNode);
//							children.add(bundledLibsNode);
				}
			} catch (CoreException e) {
				String msg = "Error in the JEEContentProvider.getChildren() for parent:" +  aParentElement; //$NON-NLS-1$
				JEEUIPlugin.getDefault().logError(msg, e);
			}
		} else if (aParentElement instanceof AbstractEarNode) {
			return ((AbstractEarNode) aParentElement).getModules().toArray();
		} else if (aParentElement instanceof IAdaptable) {
			project = (IProject) ((IAdaptable) aParentElement).getAdapter(IPROJECT_CLASS);
			if (project != null && J2EEProjectUtilities.isEARProject(project)) {
				IFacetedProject facetedProject;
				try {
					facetedProject = ProjectFacetsManager.create(project);
					if (facetedProject != null && 
							facetedProject.hasProjectFacet(
									ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_EAR_MODULE).getVersion(
											J2EEVersionConstants.VERSION_5_0_TEXT))) {
						GroupEARProvider element = (GroupEARProvider) getCachedContentProvider(project);
						children.add(element);
					}
				} catch (CoreException e) {
					String msg = "Error in the JEEContentProvider.getChildren() for parent:" +  aParentElement; //$NON-NLS-1$
					JEEUIPlugin.getDefault().logError(msg, e);
				}
			}
		}
		return children.toArray();
	}

	public boolean hasChildren(Object element) {
		if (element instanceof AbstractEarNode) {
			return ((AbstractEarNode) element).getModules().size() > 0;
		} else if (element instanceof GroupEARProvider){
			return true;
		} else return false;
	}

	public Object getParent(Object object) {
		if (object instanceof AbstractEarNode){
			return ((AbstractEarNode) object).getEarProject(); 
		}
		return null;
	}

	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}

	@Override
	protected AbstractGroupProvider getNewContentProviderInstance(IProject project) {
		return new GroupEARProvider((Application) getCachedModelProvider(project).getModelObject(), (EARVirtualComponent)ComponentCore.createComponent(project));
	}

	public EarLibVisitor getVisitor() {
		return new EarLibVisitor();
	}

	protected class EarLibVisitor implements IResourceDeltaVisitor {

		private Set<IProject> projects = new HashSet<IProject>();

		public boolean visit(IResourceDelta delta) throws CoreException {
			IResourceDelta[] affectedChildren = delta.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.REMOVED | IResourceDelta.REPLACED);
			if (affectedChildren != null){
				for (int i = 0; i < affectedChildren.length; i++) {
					if (affectedChildren[i].getResource() != null && affectedChildren[i].getResource().getName() != null
							&& affectedChildren[i].getResource().getName().toLowerCase().endsWith(".jar")){ //$NON-NLS-1$
						projects.add(affectedChildren[i].getResource().getProject());
					}
				}
			}
			return true;
		}

		protected Set<IProject> getChangedProject() {
			return projects;
		}

	}

	public void resourceChanged(IResourceChangeEvent event) {
		super.resourceChanged(event);
		if (event.getType() == IResourceChangeEvent.POST_CHANGE){
			EarLibVisitor visitor = getVisitor();
			if (visitor == null){
				return;
			}
			try {
				event.getDelta().accept(visitor);
				Set<IProject> changedProject = visitor.getChangedProject();
				for (IProject project : changedProject) {
					projectChanged(project);
				}
				
			} catch (CoreException e) {
				JEEUIPlugin.logError("Could not refresh changed project.", e); //$NON-NLS-1$
			}
		}
	} 

}

Back to the top