Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9081ac47bc8ad1158cddabc77b8b1bbd7ab51a11 (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
/*******************************************************************************
 * Copyright (c) 2005 Oracle Corporation.
 * 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:
 *    Ian Trimble - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.jst.jsf.core.internal.provisional.jsfappconfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jst.j2ee.common.ParamValue;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.web.componentcore.util.WebArtifactEdit;
import org.eclipse.jst.j2ee.webapplication.ContextParam;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.internal.Messages;
import org.eclipse.jst.jsf.core.internal.provisional.IJSFCoreConstants;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;

/**
 * JSFAppConfigUtils provides utility methods useful in processing of a JSF
 * application configuration.
 * 
 * @author Ian Trimble - Oracle
 */
public class JSFAppConfigUtils {

	/**
	 * Name of JSF CONFIG_FILES context parameter ("javax.faces.CONFIG_FILES").
	 */
	public static final String CONFIG_FILES_CONTEXT_PARAM_NAME = "javax.faces.CONFIG_FILES";

	/**
	 * Location in JAR file of application configuration resource file
	 * ("META-INF/faces-config.xml"). 
	 */
	public static final String FACES_CONFIG_IN_JAR_PATH = "META-INF/faces-config.xml";

    
    /**
     * @param project
     * @param minVersion
     * @return true if project is a JSF facet project and the version of the project
     * is at least minVersion.
     */
    public static boolean isValidJSFProject(IProject project, String minVersion)
    {
        boolean isValid = false;
        
        final IProjectFacetVersion projectFacetVersion = getProjectFacet(project);
        
        if (projectFacetVersion != null)
        {
            try
            {
                final String versionString = 
                    projectFacetVersion.getVersionString();
                final Comparator comparator = 
                    projectFacetVersion.getProjectFacet().getVersionComparator();
                final int compareToMin = 
                    comparator.compare(versionString, minVersion);
                
                if (compareToMin >=0)
                {
                    return true;
                }
            }
            catch (CoreException ce)
            {
                JSFCorePlugin.log(ce, "Error checking facet version");
            }
        }
        return isValid;
    }
    
	/**
	 * Tests if the passed IProject instance is a valid JSF project in the
	 * following ways:
	 * <ul>
	 * <li>project is not null and is accessible, </li>
	 * <li>project has the JSF facet set on it.</li>
	 * </ul>
	 * 
	 * @param project IProject instance to be tested.
	 * @return true if the IProject instance is a valid JSF project, else
	 * false.
	 */
	public static boolean isValidJSFProject(IProject project) {
		boolean isValid = false;
        IProjectFacetVersion projectFacet = getProjectFacet(project);
        if (projectFacet != null)
        {
            isValid = true;
        }
		return isValid;
	}

    private static IProjectFacetVersion getProjectFacet(IProject project)
    {
        //check for null or inaccessible project
        if (project != null && project.isAccessible()) {
            //check for JSF facet on project
            try {
                IFacetedProject facetedProject = ProjectFacetsManager.create(project);
                if (facetedProject != null) {
                    Set projectFacets = facetedProject.getProjectFacets();
                    Iterator itProjectFacets = projectFacets.iterator();
                    while (itProjectFacets.hasNext()) {
                        IProjectFacetVersion projectFacetVersion = (IProjectFacetVersion)itProjectFacets.next();
                        if (IJSFCoreConstants.JSF_CORE_FACET_ID.equals(projectFacetVersion.getProjectFacet().getId()))
                        {
                            return projectFacetVersion;
                        }
                    }
                }
            } catch(CoreException ce) {
                //log error
                JSFCorePlugin.log(IStatus.ERROR, ce.getLocalizedMessage(), ce);
            }
        }
        return null;
    }
    
	/**
	 * Gets an IVirtualFolder instance which represents the root context's
	 * web content folder.
	 * 
	 * @param project IProject instance for which to get the folder.
	 * @return IVirtualFolder instance which represents the root context's
	 * web content folder.
	 */
	public static IVirtualFolder getWebContentFolder(IProject project) {
		IVirtualFolder folder = null;
		IVirtualComponent component = ComponentCore.createComponent(project);
		if (component != null) {
			folder = component.getRootFolder();
		}
		return folder;
	}

	/**
	 * Gets an IPath instance representing the path of the passed IFile
	 * instance relative to the web content folder.
	 * 
	 * @param file IFile instance for which a path is required.
	 * @return IPath instance representing the path relative to the web content
	 * folder.
	 */
	public static IPath getWebContentFolderRelativePath(IFile file) {
		IPath path = null;
		if (file != null) {
			IVirtualFolder webContentFolder = getWebContentFolder(file.getProject());
			if (webContentFolder != null) {
				IPath webContentPath = webContentFolder.getProjectRelativePath();
				IPath filePath = file.getProjectRelativePath();
				int matchingFirstSegments = webContentPath.matchingFirstSegments(filePath);
				path = filePath.removeFirstSegments(matchingFirstSegments);
			}
		}
		return path;
	}

	/**
	 * Gets list of application configuration file names as listed in the JSF
	 * CONFIG_FILES context parameter ("javax.faces.CONFIG_FILES"). Will return
	 * an empty list if WebArtifactEdit is null, if WebApp is null, if context
	 * parameter does not exist, or if trimmed context parameter's value is
	 * an empty String.
	 * 
	 * @param project IProject instance for which to get the context
	 * parameter's value.
	 * @return List of application configuration file names as listed in the
	 * JSF CONFIG_FILES context parameter ("javax.faces.CONFIG_FILES"); list
	 * may be empty.
	 */
	public static List getConfigFilesFromContextParam(IProject project) {
		ArrayList filesList = new ArrayList();
		if (isValidJSFProject(project)) {
			WebArtifactEdit webArtifactEdit = WebArtifactEdit.getWebArtifactEditForRead(project);
			if (webArtifactEdit != null) {
				WebApp webApp = null;
				try {
					webApp = webArtifactEdit.getWebApp();
				} catch(ClassCastException cce) {
					//occasionally thrown from WTP code in RC3 and possibly later
					JSFCorePlugin.log(IStatus.ERROR, cce.getLocalizedMessage(), cce);
					return filesList;
				}
				if (webApp != null) {
					String filesString = null;
					//need to branch here due to model version differences (BugZilla #119442)
					if (webApp.getVersionID() == J2EEVersionConstants.WEB_2_3_ID) {
						EList contexts = webApp.getContexts();
						Iterator itContexts = contexts.iterator();
						while (itContexts.hasNext()) {
							ContextParam contextParam = (ContextParam)itContexts.next();
							if (contextParam.getParamName().equals(CONFIG_FILES_CONTEXT_PARAM_NAME)) {
								filesString = contextParam.getParamValue();
								break;
							}
						}
					} else {
						EList contextParams = webApp.getContextParams();
						Iterator itContextParams = contextParams.iterator();
						while (itContextParams.hasNext()) {
							ParamValue paramValue = (ParamValue)itContextParams.next();
							if (paramValue.getName().equals(CONFIG_FILES_CONTEXT_PARAM_NAME)) {
								filesString = paramValue.getValue();
								break;
							}
						}
					}
					if (filesString != null && filesString.trim().length() > 0) {
						StringTokenizer stFilesString = new StringTokenizer(filesString, ",");
						while (stFilesString.hasMoreTokens()) {
							String configFile = stFilesString.nextToken().trim();
							filesList.add(configFile);
						}
					}
				}
				webArtifactEdit.dispose();
			}
		}
		return filesList;
	}

	/**
	 * Gets list of JAR file names, where each file name represents a JAR on
	 * the classpath that contains a /META-INF/faces-config.xml entry. Will
	 * return an empty list if no such JAR files are located.
	 * 
	 * @param project IProject instance for which to scan the classpath.
	 * @return List of JAR file names, where each file name represents a JAR
	 * on the classpath that contains a ...META-INF/faces-config.xml entry;
	 * list may be empty.
	 * @throws CoreException Thrown when underlying calls into JavaCore fail.
	 * @throws IOException Thrown when attempt to open JAR to determine if it
	 * contains a /META-INF/faces-config.xml entry fails.
	 */
	public static List getConfigFileJARsFromClasspath(IProject project) throws CoreException, IOException {
		ArrayList JARsList = new ArrayList();
		if (project.hasNature(JavaCore.NATURE_ID)) {
			IJavaProject javaProject = JavaCore.create(project);
			if (javaProject != null) {
				IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
				if (classpathEntries != null && classpathEntries.length > 0) {
					for (int i = 0; i < classpathEntries.length; i++) {
						IClasspathEntry classpathEntry = classpathEntries[i];
						if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
							IPath libraryPath = classpathEntry.getPath();
							if (libraryPath.getFileExtension() != null && libraryPath.getFileExtension().length() > 0) {
								//TODO: find better way to determine if workspace must be prepended to path
								if (libraryPath.getDevice() == null) {
									IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
									IPath workspaceRootPath = workspaceRoot.getLocation();
									libraryPath = workspaceRootPath.append(libraryPath);
								}
								String libraryPathString = libraryPath.toString();
								JarFile jarFile = null;
								try {
									jarFile = new JarFile(libraryPathString);
									if (jarFile != null) {
										JarEntry jarEntry = jarFile.getJarEntry(FACES_CONFIG_IN_JAR_PATH);
										if (jarEntry != null) {
											JARsList.add(libraryPathString);
										}
									}
								} catch(IOException ioe) {
									JSFCorePlugin.log(
											IStatus.ERROR,
											NLS.bind(Messages.JSFAppConfigUtils_ErrorOpeningJarFile, libraryPathString),
											ioe);
								} finally {
									if (jarFile != null) {
										jarFile.close();
									}
								}
							}
						}
					}
				}
			}
		}
		return JARsList;
	}

}

Back to the top