Skip to content

Commit

Permalink
CHE-4910:Fix catching exception in case invalid POM to prevent fail s…
Browse files Browse the repository at this point in the history
…tart workspace (eclipse-che#5066)

* Fix catching exception in case invalid POM.
Fix dependency  on che-plugin-pullrequest-server in should be only in ws-agent.
Improve maven attribute resolving previos we always read them from pom.xml but MavenProjectManager already has all information about regitered projects

Signed-off-by: Vitalii Parfonov <vparfonov@codenvy.com>
  • Loading branch information
Vitalii Parfonov authored May 13, 2017
1 parent 9a17103 commit 79e7c44
Show file tree
Hide file tree
Showing 9 changed files with 613 additions and 114 deletions.
4 changes: 4 additions & 0 deletions assembly/assembly-wsagent-war/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-php-lang-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-pullrequest-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-python-lang-server</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions assembly/assembly-wsmaster-war/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-openshift-client</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-pullrequest-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-ssh-machine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
*/
public final class Constants {
// project categories
public static String JAVA_CATEGORY = "Java";
public static String JAVA_ID = "java";
public static final String JAVA_CATEGORY = "Java";
public static final String JAVA_ID = "java";
// project attribute names
public static String LANGUAGE = "language";
public static String LANGUAGE_VERSION = "languageVersion";
public static String FRAMEWORK = "framework";
public static String CONTAINS_JAVA_FILES = "containsJavaFiles";
public static String SOURCE_FOLDER = "java.source.folder";
public static String OUTPUT_FOLDER = "java.output.folder";
public static final String LANGUAGE = "language";
public static final String LANGUAGE_VERSION = "languageVersion";
public static final String FRAMEWORK = "framework";
public static final String CONTAINS_JAVA_FILES = "containsJavaFiles";
public static final String SOURCE_FOLDER = "java.source.folder";
public static final String OUTPUT_FOLDER = "java.output.folder";

public static String JAVAC = "javac";
public static final String JAVAC = "javac";

private Constants() {
throw new UnsupportedOperationException("Unused constructor.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,22 @@ public List<MavenProject> getAllProjects() {
}
}


public MavenProject getMavenProject(IProject iProject) {
readLock.lock();
try {
return projectToMavenProjectMap.get(iProject);
} finally {
readLock.unlock();
}
}


public MavenProject getMavenProject(String projectPath) {
final IProject project = workspaceProvider.get().getRoot().getProject(projectPath);
return getMavenProject(project);
}

private void fillMavenKeyMap(MavenProject mavenProject) {
MavenKey mavenKey = mavenProject.getMavenKey();
mavenWorkspaceCache.put(mavenKey, mavenProject.getPomFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.che.plugin.maven.server.core.project;

import org.eclipse.che.commons.lang.Pair;
import org.eclipse.che.commons.xml.XMLTreeException;
import org.eclipse.che.ide.maven.tools.Build;
import org.eclipse.che.ide.maven.tools.Model;
import org.eclipse.che.ide.maven.tools.Parent;
Expand Down Expand Up @@ -116,6 +117,8 @@ private ModelReadingResult doRead(File pom) {
model = Model.readFrom(pom);
} catch (IOException e) {
problems.add(MavenProjectProblem.newProblem(pom.getPath(), e.getMessage(), MavenProblemType.SYNTAX));
} catch (XMLTreeException xmlExc) {
problems.add(MavenProjectProblem.newProblem(pom.getPath(), xmlExc.getMessage(), MavenProblemType.STRUCTURE));
}

if (model == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*******************************************************************************
* Copyright (c) 2012-2017 Codenvy, S.A.
* 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:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.plugin.maven.server.projecttype;

import org.eclipse.che.api.core.ForbiddenException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.project.server.FileEntry;
import org.eclipse.che.api.project.server.FolderEntry;
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
import org.eclipse.che.api.project.server.type.ValueStorageException;
import org.eclipse.che.commons.xml.XMLTreeException;
import org.eclipse.che.ide.maven.tools.Model;
import org.eclipse.che.ide.maven.tools.Resource;
import org.eclipse.che.maven.data.MavenResource;
import org.eclipse.che.plugin.maven.server.core.MavenProjectManager;
import org.eclipse.che.plugin.maven.server.core.project.MavenProject;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.ext.java.shared.Constants.SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.ARTIFACT_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_PACKAGING;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_RESOURCES_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.GROUP_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PACKAGING;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_ARTIFACT_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_GROUP_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_VERSION;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.RESOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.TEST_SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.VERSION;

/**
* @author Vitalii Parfonov
*/
public class MavenValueProvider extends ReadonlyValueProvider {


private MavenProjectManager mavenProjectManager;
private FolderEntry projectFolder;

protected MavenValueProvider(MavenProjectManager mavenProjectManager, FolderEntry projectFolder) {
this.mavenProjectManager = mavenProjectManager;
this.projectFolder = projectFolder;
}

@Override
public List<String> getValues(String attributeName) throws ValueStorageException {
try {
if (mavenProjectManager == null) {
return readFromPom(attributeName);
}

final MavenProject mavenProject = mavenProjectManager.getMavenProject(projectFolder.getPath().toString());
if (mavenProject != null) {
return getFromMavenProject(mavenProject, attributeName);
} else {
return readFromPom(attributeName);
}
} catch (ServerException | ForbiddenException | IOException e) {
throwReadException(e);
} catch (XMLTreeException e) {
throw new ValueStorageException("Error parsing pom.xml : " + e.getMessage());
}
return null;
}

private List<String> getFromMavenProject(MavenProject mavenProject, String attributeName) throws ValueStorageException {
switch (attributeName) {
case ARTIFACT_ID:
return singletonList(mavenProject.getMavenKey().getArtifactId());
case GROUP_ID:
return singletonList(mavenProject.getMavenKey().getGroupId());
case PACKAGING:
String packaging = mavenProject.getPackaging();
return singletonList(packaging != null ? packaging : DEFAULT_PACKAGING);
case VERSION:
return singletonList(mavenProject.getMavenKey().getVersion());
case PARENT_ARTIFACT_ID:
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getArtifactId());
case PARENT_GROUP_ID:
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getGroupId());
case PARENT_VERSION:
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getVersion());
case SOURCE_FOLDER:
return (mavenProject.getSources() != null && !mavenProject.getSources().isEmpty()) ? mavenProject.getSources()
: singletonList(DEFAULT_SOURCE_FOLDER);
case TEST_SOURCE_FOLDER:
return (mavenProject.getTestSources() != null && !mavenProject.getTestSources().isEmpty()) ? mavenProject.getTestSources()
: singletonList(
DEFAULT_TEST_SOURCE_FOLDER);
case RESOURCE_FOLDER:
if (mavenProject.getResources() != null && !mavenProject.getResources().isEmpty()) {
return mavenProject.getResources().stream().map(MavenResource::getDirectory).collect(Collectors.toList());
} else {
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
}
default:
throw new ValueStorageException(String.format("Unknown attribute %s", attributeName));
}
}


private List<String> readFromPom(String attributeName)
throws ServerException, ForbiddenException, IOException, XMLTreeException, ValueStorageException {
final Model model = readModel(projectFolder);
switch (attributeName) {
case ARTIFACT_ID:
return singletonList(model.getArtifactId());
case GROUP_ID:
return singletonList(model.getGroupId());
case PACKAGING:
String packaging = model.getPackaging();
return singletonList(packaging != null ? packaging : DEFAULT_PACKAGING);
case VERSION:
return singletonList(model.getVersion());
case PARENT_ARTIFACT_ID:
return singletonList(model.getParent() == null ? "" : model.getParent().getArtifactId());
case PARENT_GROUP_ID:
return singletonList(model.getParent() == null ? "" : model.getParent().getGroupId());
case PARENT_VERSION:
return singletonList(model.getParent() == null ? "" : model.getParent().getVersion());
case SOURCE_FOLDER:
if (model.getBuild() != null && model.getBuild().getSourceDirectory() != null) {
return singletonList(model.getBuild().getSourceDirectory());
} else {
return singletonList(DEFAULT_SOURCE_FOLDER);
}
case TEST_SOURCE_FOLDER:
if (model.getBuild() != null && model.getBuild().getTestSourceDirectory() != null) {
return singletonList(model.getBuild().getTestSourceDirectory());
} else {
return singletonList(DEFAULT_TEST_SOURCE_FOLDER);
}
case RESOURCE_FOLDER:
if (model.getBuild() != null && model.getBuild().getResources() != null) {
return model.getBuild().getResources().stream().map(Resource::getDirectory).collect(Collectors.toList());
} else {
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
}
default:
throw new ValueStorageException(String.format("Unknown attribute %s", attributeName));
}
}

protected Model readModel(FolderEntry projectFolder) throws ValueStorageException, ServerException, ForbiddenException, IOException {
FileEntry pomFile = (FileEntry)projectFolder.getChild("pom.xml");
if (pomFile == null) {
throw new ValueStorageException("pom.xml does not exist.");
}
return Model.readFrom(pomFile.getInputStream());
}

protected void throwReadException(Exception e) throws ValueStorageException {
throw new ValueStorageException("Can't read pom.xml : " + e.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,120 +10,25 @@
*******************************************************************************/
package org.eclipse.che.plugin.maven.server.projecttype;

import org.eclipse.che.api.core.ForbiddenException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.project.server.FileEntry;
import org.eclipse.che.api.project.server.FolderEntry;
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
import org.eclipse.che.api.project.server.type.ValueProvider;
import org.eclipse.che.api.project.server.type.ValueProviderFactory;
import org.eclipse.che.api.project.server.type.ValueStorageException;
import org.eclipse.che.commons.xml.XMLTreeException;
import org.eclipse.che.ide.maven.tools.Build;
import org.eclipse.che.ide.maven.tools.Model;
import org.eclipse.che.ide.maven.tools.Resource;
import org.eclipse.che.plugin.maven.server.core.MavenProjectManager;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.eclipse.che.ide.ext.java.shared.Constants.SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.ARTIFACT_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_RESOURCES_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.GROUP_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PACKAGING;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_ARTIFACT_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_GROUP_ID;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_VERSION;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.RESOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.TEST_SOURCE_FOLDER;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.VERSION;
import javax.inject.Inject;

/**
* @author Evgen Vidolob
*/
public class MavenValueProviderFactory implements ValueProviderFactory {

protected Model readModel(FolderEntry projectFolder) throws ValueStorageException, ServerException, ForbiddenException, IOException {
FileEntry pomFile = (FileEntry)projectFolder.getChild("pom.xml");
if (pomFile == null) {
throw new ValueStorageException("pom.xml does not exist.");
}
return Model.readFrom(pomFile.getInputStream());
}
@Inject
MavenProjectManager mavenProjectManager;

protected void throwReadException(Exception e) throws ValueStorageException {
throw new ValueStorageException("Can't read pom.xml : " + e.getMessage());
}

@Override
public ValueProvider newInstance(FolderEntry projectFolder) {
return new MavenValueProvider(projectFolder);
return new MavenValueProvider(mavenProjectManager, projectFolder);
}

protected class MavenValueProvider extends ReadonlyValueProvider {

protected FolderEntry projectFolder;

protected MavenValueProvider(FolderEntry projectFolder) {
this.projectFolder = projectFolder;
}

@Override
public List<String> getValues(String attributeName) throws ValueStorageException {
try {
String value = "";
final Model model = readModel(projectFolder);
if (attributeName.equals(ARTIFACT_ID)) {
value = model.getArtifactId();
} else if (attributeName.equals(GROUP_ID)) {
value = model.getGroupId();
} else if (attributeName.equals(PACKAGING)) {
final String packaging = model.getPackaging();
value = packaging == null ? "" : packaging;
} else if (attributeName.equals(VERSION)) {
value = model.getVersion();
} else if (attributeName.equals(PARENT_ARTIFACT_ID) && model.getParent() != null) {
value = model.getParent().getArtifactId();
} else if (attributeName.equals(PARENT_GROUP_ID) && model.getParent() != null) {
value = model.getParent().getGroupId();
} else if (attributeName.equals(PARENT_VERSION) && model.getParent() != null) {
value = model.getParent().getVersion();
} else if (attributeName.equals(SOURCE_FOLDER)) {
Build build = model.getBuild();
if (build != null && build.getSourceDirectory() != null) {
value = build.getSourceDirectory();
} else {
value = DEFAULT_SOURCE_FOLDER;
}
} else if (attributeName.equals(TEST_SOURCE_FOLDER)) {
Build build = model.getBuild();
if (build != null && build.getTestSourceDirectory() != null) {
value = build.getTestSourceDirectory();
} else {
value = DEFAULT_TEST_SOURCE_FOLDER;
}
} else if (attributeName.equals(RESOURCE_FOLDER)) {
Build build = model.getBuild();
if (build != null && build.getResources() != null) {
return build.getResources().stream().map(Resource::getDirectory).collect(Collectors.toList());
} else {
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
}
}

return Collections.singletonList(value);
} catch (ServerException | ForbiddenException | IOException e) {
throwReadException(e);
} catch (XMLTreeException e) {
throw new ValueStorageException("Error parsing pom.xml : " + e.getMessage());
}
return null;
}
}
}
Loading

0 comments on commit 79e7c44

Please sign in to comment.