Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMickael Istria2016-04-20 13:14:10 +0000
committerMickael Istria2016-04-20 14:43:52 +0000
commit6921b4538f904c74670060b47165e2b736a3647e (patch)
treefd15e3d9bf2534dd002cf0ef29fff7cb55721f86 /plugins/org.eclipse.jst.servlet.ui/servlet_ui/org/eclipse
parent2727b640467acb92b56d099fa38b9eee0a3490e3 (diff)
downloadwebtools.javaee-6921b4538f904c74670060b47165e2b736a3647e.tar.gz
webtools.javaee-6921b4538f904c74670060b47165e2b736a3647e.tar.xz
webtools.javaee-6921b4538f904c74670060b47165e2b736a3647e.zip
Bug 492096 - Contribute Servlet project detection to Smart Importer
Change-Id: Ia4fd3ceaad07d918675adb2d34a470a862c9bf98 Signed-off-by: Mickael Istria <mistria@redhat.com>
Diffstat (limited to 'plugins/org.eclipse.jst.servlet.ui/servlet_ui/org/eclipse')
-rw-r--r--plugins/org.eclipse.jst.servlet.ui/servlet_ui/org/eclipse/jst/servlet/ui/internal/wizard/ServletProjectConfigurator.java113
1 files changed, 113 insertions, 0 deletions
diff --git a/plugins/org.eclipse.jst.servlet.ui/servlet_ui/org/eclipse/jst/servlet/ui/internal/wizard/ServletProjectConfigurator.java b/plugins/org.eclipse.jst.servlet.ui/servlet_ui/org/eclipse/jst/servlet/ui/internal/wizard/ServletProjectConfigurator.java
new file mode 100644
index 000000000..1a8ba7b9b
--- /dev/null
+++ b/plugins/org.eclipse.jst.servlet.ui/servlet_ui/org/eclipse/jst/servlet/ui/internal/wizard/ServletProjectConfigurator.java
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Red Hat Inc.
+ * 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:
+ * - Mickael Istria (Red Hat Inc.) - initial implementation
+ *******************************************************************************/
+package org.eclipse.jst.servlet.ui.internal.wizard;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Set;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ProjectScope;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jface.wizard.IWizard;
+import org.eclipse.jst.j2ee.project.facet.IJ2EEModuleFacetInstallDataModelProperties;
+import org.eclipse.jst.j2ee.web.project.facet.WebFacetInstallDataModelProvider;
+import org.eclipse.jst.servlet.ui.internal.plugin.ServletUIPlugin;
+import org.eclipse.ui.wizards.datatransfer.ProjectConfigurator;
+import org.eclipse.ui.wizards.datatransfer.RecursiveFileFinder;
+import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory;
+import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
+import org.eclipse.wst.common.project.facet.core.IFacetedProject;
+import org.eclipse.wst.common.project.facet.core.IProjectFacet;
+import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class ServletProjectConfigurator implements ProjectConfigurator {
+
+ public boolean canConfigure(IProject project, Set<IPath> ignoredDirectories, IProgressMonitor monitor) {
+ try {
+ RecursiveFileFinder finder = new RecursiveFileFinder("web.xml", ignoredDirectories); //$NON-NLS-1$
+ project.accept(finder);
+ return finder.getFile() != null;
+ } catch (CoreException ex) {
+ ServletUIPlugin.log(ex);
+ return false;
+ }
+ }
+
+ @Deprecated
+ public IWizard getConfigurationWizard() {
+ return null;
+ }
+
+ public void configure(IProject project, Set<IPath> ignoredDirectories, IProgressMonitor monitor) {
+ try {
+ if (!ProjectFacetsManager.isProjectFacetDefined(project.getName())) {
+ IFacetedProject facetedProject = ProjectFacetsManager.create(project, true, monitor);
+ IProjectFacet JAVA_FACET = ProjectFacetsManager.getProjectFacet("jst.java"); //$NON-NLS-1$
+
+ ProjectScope ps = new ProjectScope(project);
+ IEclipsePreferences JDPprojectNode = ps.getNode(JavaCore.PLUGIN_ID);
+ String compilerCompliance = JDPprojectNode.get(JavaCore.COMPILER_COMPLIANCE, "1.7"); //$NON-NLS-1$
+ if (!facetedProject.hasProjectFacet(JAVA_FACET)) {
+ facetedProject.installProjectFacet(JAVA_FACET.getVersion(compilerCompliance), null, monitor);
+ }
+ }
+
+ IFacetedProject facetedProject = ProjectFacetsManager.create(project, true, monitor);
+
+ IProjectFacet WEB_FACET = ProjectFacetsManager.getProjectFacet("jst.web"); //$NON-NLS-1$
+ if (!facetedProject.hasProjectFacet(WEB_FACET)) {
+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+ RecursiveFileFinder finder = new RecursiveFileFinder("web.xml", ignoredDirectories); //$NON-NLS-1$
+ project.accept(finder);
+ InputStream webXmlStream = finder.getFile().getContents();
+ Document doc = dBuilder.parse(webXmlStream);
+ webXmlStream.close();
+
+ IDataModel aFacetInstallDataModel = DataModelFactory.createDataModel(new WebFacetInstallDataModelProvider());
+ aFacetInstallDataModel.setBooleanProperty(IJ2EEModuleFacetInstallDataModelProperties.ADD_TO_EAR, false);
+ String version = ((Element)doc.getElementsByTagName("web-app").item(0)).getAttribute("version"); //$NON-NLS-1$ //$NON-NLS-2$
+ if (version.isEmpty()) {
+ // TODO decide this according to JRE version : Java6 => servlet 2.5; Java 7 => servlet 3.1
+ version = "3.1"; //$NON-NLS-1$
+ }
+ facetedProject.installProjectFacet(WEB_FACET.getVersion(version), aFacetInstallDataModel, monitor);
+ }
+ } catch (Exception ex) {
+ ServletUIPlugin.log(ex);
+ }
+ }
+
+ public boolean shouldBeAnEclipseProject(IContainer container, IProgressMonitor monitor) {
+ return false; // TODO: can we make sure a dir is a JEE project?
+ }
+
+ public Set<IFolder> getDirectoriesToIgnore(IProject project, IProgressMonitor monitor) {
+ return null;
+ }
+
+ public Set<File> findConfigurableLocations(File root, IProgressMonitor monitor) {
+ return Collections.emptySet();
+ }
+}

Back to the top