Skip to content

Commit e352da4

Browse files
author
Vitalii Parfonov
authored
Fix catching exception in case invalid POM to prevent fail start workspace (eclipse-che#5015)
* 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>
1 parent 52e418b commit e352da4

8 files changed

Lines changed: 605 additions & 113 deletions

File tree

assembly/assembly-wsagent-war/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
<groupId>org.eclipse.che.plugin</groupId>
9292
<artifactId>che-plugin-php-lang-server</artifactId>
9393
</dependency>
94+
<dependency>
95+
<groupId>org.eclipse.che.plugin</groupId>
96+
<artifactId>che-plugin-pullrequest-server</artifactId>
97+
</dependency>
9498
<dependency>
9599
<groupId>org.eclipse.che.plugin</groupId>
96100
<artifactId>che-plugin-python-lang-server</artifactId>

assembly/assembly-wsmaster-war/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@
210210
<groupId>org.eclipse.che.plugin</groupId>
211211
<artifactId>che-plugin-openshift-client</artifactId>
212212
</dependency>
213-
<dependency>
214-
<groupId>org.eclipse.che.plugin</groupId>
215-
<artifactId>che-plugin-pullrequest-server</artifactId>
216-
</dependency>
217213
<dependency>
218214
<groupId>org.eclipse.che.plugin</groupId>
219215
<artifactId>che-plugin-ssh-machine</artifactId>

plugins/plugin-java/che-plugin-java-ext-lang-shared/src/main/java/org/eclipse/che/ide/ext/java/shared/Constants.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
*/
1717
public final class Constants {
1818
// project categories
19-
public static String JAVA_CATEGORY = "Java";
20-
public static String JAVA_ID = "java";
19+
public static final String JAVA_CATEGORY = "Java";
20+
public static final String JAVA_ID = "java";
2121
// project attribute names
22-
public static String LANGUAGE = "language";
23-
public static String LANGUAGE_VERSION = "languageVersion";
24-
public static String FRAMEWORK = "framework";
25-
public static String CONTAINS_JAVA_FILES = "containsJavaFiles";
26-
public static String SOURCE_FOLDER = "java.source.folder";
27-
public static String OUTPUT_FOLDER = "java.output.folder";
22+
public static final String LANGUAGE = "language";
23+
public static final String LANGUAGE_VERSION = "languageVersion";
24+
public static final String FRAMEWORK = "framework";
25+
public static final String CONTAINS_JAVA_FILES = "containsJavaFiles";
26+
public static final String SOURCE_FOLDER = "java.source.folder";
27+
public static final String OUTPUT_FOLDER = "java.output.folder";
2828

29-
public static String JAVAC = "javac";
29+
public static final String JAVAC = "javac";
3030

3131
private Constants() {
3232
throw new UnsupportedOperationException("Unused constructor.");

plugins/plugin-maven/che-plugin-maven-server/src/main/java/org/eclipse/che/plugin/maven/server/core/MavenProjectManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,22 @@ public List<MavenProject> getAllProjects() {
360360
}
361361
}
362362

363+
364+
public MavenProject getMavenProject(IProject iProject) {
365+
readLock.lock();
366+
try {
367+
return projectToMavenProjectMap.get(iProject);
368+
} finally {
369+
readLock.unlock();
370+
}
371+
}
372+
373+
374+
public MavenProject getMavenProject(String projectPath) {
375+
final IProject project = workspaceProvider.get().getRoot().getProject(projectPath);
376+
return getMavenProject(project);
377+
}
378+
363379
private void fillMavenKeyMap(MavenProject mavenProject) {
364380
MavenKey mavenKey = mavenProject.getMavenKey();
365381
mavenWorkspaceCache.put(mavenKey, mavenProject.getPomFile());

plugins/plugin-maven/che-plugin-maven-server/src/main/java/org/eclipse/che/plugin/maven/server/core/project/MavenModelReader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.eclipse.che.plugin.maven.server.core.project;
1212

1313
import org.eclipse.che.commons.lang.Pair;
14+
import org.eclipse.che.commons.xml.XMLTreeException;
1415
import org.eclipse.che.ide.maven.tools.Build;
1516
import org.eclipse.che.ide.maven.tools.Model;
1617
import org.eclipse.che.ide.maven.tools.Parent;
@@ -116,6 +117,8 @@ private ModelReadingResult doRead(File pom) {
116117
model = Model.readFrom(pom);
117118
} catch (IOException e) {
118119
problems.add(MavenProjectProblem.newProblem(pom.getPath(), e.getMessage(), MavenProblemType.SYNTAX));
120+
} catch (XMLTreeException xmlExc) {
121+
problems.add(MavenProjectProblem.newProblem(pom.getPath(), xmlExc.getMessage(), MavenProblemType.STRUCTURE));
119122
}
120123

121124
if (model == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2012-2017 Codenvy, S.A.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Codenvy, S.A. - initial API and implementation
10+
*******************************************************************************/
11+
package org.eclipse.che.plugin.maven.server.projecttype;
12+
13+
import org.eclipse.che.api.core.ForbiddenException;
14+
import org.eclipse.che.api.core.ServerException;
15+
import org.eclipse.che.api.project.server.FileEntry;
16+
import org.eclipse.che.api.project.server.FolderEntry;
17+
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
18+
import org.eclipse.che.api.project.server.type.ValueStorageException;
19+
import org.eclipse.che.commons.xml.XMLTreeException;
20+
import org.eclipse.che.ide.maven.tools.Model;
21+
import org.eclipse.che.ide.maven.tools.Resource;
22+
import org.eclipse.che.maven.data.MavenResource;
23+
import org.eclipse.che.plugin.maven.server.core.MavenProjectManager;
24+
import org.eclipse.che.plugin.maven.server.core.project.MavenProject;
25+
26+
import java.io.IOException;
27+
import java.util.Arrays;
28+
import java.util.List;
29+
import java.util.stream.Collectors;
30+
31+
import static java.util.Collections.singletonList;
32+
import static org.eclipse.che.ide.ext.java.shared.Constants.SOURCE_FOLDER;
33+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.ARTIFACT_ID;
34+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_PACKAGING;
35+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_RESOURCES_FOLDER;
36+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_SOURCE_FOLDER;
37+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER;
38+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_SOURCE_FOLDER;
39+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.GROUP_ID;
40+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PACKAGING;
41+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_ARTIFACT_ID;
42+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_GROUP_ID;
43+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_VERSION;
44+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.RESOURCE_FOLDER;
45+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.TEST_SOURCE_FOLDER;
46+
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.VERSION;
47+
48+
/**
49+
* @author Vitalii Parfonov
50+
*/
51+
public class MavenValueProvider extends ReadonlyValueProvider {
52+
53+
54+
private MavenProjectManager mavenProjectManager;
55+
private FolderEntry projectFolder;
56+
57+
protected MavenValueProvider(MavenProjectManager mavenProjectManager, FolderEntry projectFolder) {
58+
this.mavenProjectManager = mavenProjectManager;
59+
this.projectFolder = projectFolder;
60+
}
61+
62+
@Override
63+
public List<String> getValues(String attributeName) throws ValueStorageException {
64+
try {
65+
final MavenProject mavenProject = mavenProjectManager.getMavenProject(projectFolder.getPath().toString());
66+
if (mavenProject != null) {
67+
return getFromMavenProject(mavenProject, attributeName);
68+
} else {
69+
return readFromPom(attributeName);
70+
}
71+
} catch (ServerException | ForbiddenException | IOException e) {
72+
throwReadException(e);
73+
} catch (XMLTreeException e) {
74+
throw new ValueStorageException("Error parsing pom.xml : " + e.getMessage());
75+
}
76+
return null;
77+
}
78+
79+
private List<String> getFromMavenProject(MavenProject mavenProject, String attributeName) throws ValueStorageException {
80+
switch (attributeName) {
81+
case ARTIFACT_ID:
82+
return singletonList(mavenProject.getMavenKey().getArtifactId());
83+
case GROUP_ID:
84+
return singletonList(mavenProject.getMavenKey().getGroupId());
85+
case PACKAGING:
86+
String packaging = mavenProject.getPackaging();
87+
return singletonList(packaging != null ? packaging : DEFAULT_PACKAGING);
88+
case VERSION:
89+
return singletonList(mavenProject.getMavenKey().getVersion());
90+
case PARENT_ARTIFACT_ID:
91+
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getArtifactId());
92+
case PARENT_GROUP_ID:
93+
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getGroupId());
94+
case PARENT_VERSION:
95+
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getVersion());
96+
case SOURCE_FOLDER:
97+
return (mavenProject.getSources() != null && !mavenProject.getSources().isEmpty()) ? mavenProject.getSources()
98+
: singletonList(DEFAULT_SOURCE_FOLDER);
99+
case TEST_SOURCE_FOLDER:
100+
return (mavenProject.getTestSources() != null && !mavenProject.getTestSources().isEmpty()) ? mavenProject.getTestSources()
101+
: singletonList(
102+
DEFAULT_TEST_SOURCE_FOLDER);
103+
case RESOURCE_FOLDER:
104+
if (mavenProject.getResources() != null && !mavenProject.getResources().isEmpty()) {
105+
return mavenProject.getResources().stream().map(MavenResource::getDirectory).collect(Collectors.toList());
106+
} else {
107+
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
108+
}
109+
default:
110+
throw new ValueStorageException(String.format("Unknown attribute %s", attributeName));
111+
}
112+
}
113+
114+
115+
private List<String> readFromPom(String attributeName)
116+
throws ServerException, ForbiddenException, IOException, XMLTreeException, ValueStorageException {
117+
final Model model = readModel(projectFolder);
118+
switch (attributeName) {
119+
case ARTIFACT_ID:
120+
return singletonList(model.getArtifactId());
121+
case GROUP_ID:
122+
return singletonList(model.getGroupId());
123+
case PACKAGING:
124+
String packaging = model.getPackaging();
125+
return singletonList(packaging != null ? packaging : DEFAULT_PACKAGING);
126+
case VERSION:
127+
return singletonList(model.getVersion());
128+
case PARENT_ARTIFACT_ID:
129+
return singletonList(model.getParent() == null ? "" : model.getParent().getArtifactId());
130+
case PARENT_GROUP_ID:
131+
return singletonList(model.getParent() == null ? "" : model.getParent().getGroupId());
132+
case PARENT_VERSION:
133+
return singletonList(model.getParent() == null ? "" : model.getParent().getVersion());
134+
case SOURCE_FOLDER:
135+
if (model.getBuild() != null && model.getBuild().getSourceDirectory() != null) {
136+
return singletonList(model.getBuild().getSourceDirectory());
137+
} else {
138+
return singletonList(DEFAULT_SOURCE_FOLDER);
139+
}
140+
case TEST_SOURCE_FOLDER:
141+
if (model.getBuild() != null && model.getBuild().getTestSourceDirectory() != null) {
142+
return singletonList(model.getBuild().getTestSourceDirectory());
143+
} else {
144+
return singletonList(DEFAULT_TEST_SOURCE_FOLDER);
145+
}
146+
case RESOURCE_FOLDER:
147+
if (model.getBuild() != null && model.getBuild().getResources() != null) {
148+
return model.getBuild().getResources().stream().map(Resource::getDirectory).collect(Collectors.toList());
149+
} else {
150+
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
151+
}
152+
default:
153+
throw new ValueStorageException(String.format("Unknown attribute %s", attributeName));
154+
}
155+
}
156+
157+
protected Model readModel(FolderEntry projectFolder) throws ValueStorageException, ServerException, ForbiddenException, IOException {
158+
FileEntry pomFile = (FileEntry)projectFolder.getChild("pom.xml");
159+
if (pomFile == null) {
160+
throw new ValueStorageException("pom.xml does not exist.");
161+
}
162+
return Model.readFrom(pomFile.getInputStream());
163+
}
164+
165+
protected void throwReadException(Exception e) throws ValueStorageException {
166+
throw new ValueStorageException("Can't read pom.xml : " + e.getMessage());
167+
}
168+
169+
}

plugins/plugin-maven/che-plugin-maven-server/src/main/java/org/eclipse/che/plugin/maven/server/projecttype/MavenValueProviderFactory.java

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -10,120 +10,25 @@
1010
*******************************************************************************/
1111
package org.eclipse.che.plugin.maven.server.projecttype;
1212

13-
import org.eclipse.che.api.core.ForbiddenException;
14-
import org.eclipse.che.api.core.ServerException;
15-
import org.eclipse.che.api.project.server.FileEntry;
1613
import org.eclipse.che.api.project.server.FolderEntry;
17-
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
1814
import org.eclipse.che.api.project.server.type.ValueProvider;
1915
import org.eclipse.che.api.project.server.type.ValueProviderFactory;
20-
import org.eclipse.che.api.project.server.type.ValueStorageException;
21-
import org.eclipse.che.commons.xml.XMLTreeException;
22-
import org.eclipse.che.ide.maven.tools.Build;
23-
import org.eclipse.che.ide.maven.tools.Model;
24-
import org.eclipse.che.ide.maven.tools.Resource;
16+
import org.eclipse.che.plugin.maven.server.core.MavenProjectManager;
2517

26-
import java.io.IOException;
27-
import java.util.Arrays;
28-
import java.util.Collections;
29-
import java.util.List;
30-
import java.util.stream.Collectors;
31-
32-
import static org.eclipse.che.ide.ext.java.shared.Constants.SOURCE_FOLDER;
33-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.ARTIFACT_ID;
34-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_RESOURCES_FOLDER;
35-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_SOURCE_FOLDER;
36-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER;
37-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.DEFAULT_TEST_SOURCE_FOLDER;
38-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.GROUP_ID;
39-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PACKAGING;
40-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_ARTIFACT_ID;
41-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_GROUP_ID;
42-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.PARENT_VERSION;
43-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.RESOURCE_FOLDER;
44-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.TEST_SOURCE_FOLDER;
45-
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.VERSION;
18+
import javax.inject.Inject;
4619

4720
/**
4821
* @author Evgen Vidolob
4922
*/
5023
public class MavenValueProviderFactory implements ValueProviderFactory {
5124

52-
protected Model readModel(FolderEntry projectFolder) throws ValueStorageException, ServerException, ForbiddenException, IOException {
53-
FileEntry pomFile = (FileEntry)projectFolder.getChild("pom.xml");
54-
if (pomFile == null) {
55-
throw new ValueStorageException("pom.xml does not exist.");
56-
}
57-
return Model.readFrom(pomFile.getInputStream());
58-
}
25+
@Inject
26+
MavenProjectManager mavenProjectManager;
5927

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

6429
@Override
6530
public ValueProvider newInstance(FolderEntry projectFolder) {
66-
return new MavenValueProvider(projectFolder);
31+
return new MavenValueProvider(mavenProjectManager, projectFolder);
6732
}
6833

69-
protected class MavenValueProvider extends ReadonlyValueProvider {
70-
71-
protected FolderEntry projectFolder;
72-
73-
protected MavenValueProvider(FolderEntry projectFolder) {
74-
this.projectFolder = projectFolder;
75-
}
76-
77-
@Override
78-
public List<String> getValues(String attributeName) throws ValueStorageException {
79-
try {
80-
String value = "";
81-
final Model model = readModel(projectFolder);
82-
if (attributeName.equals(ARTIFACT_ID)) {
83-
value = model.getArtifactId();
84-
} else if (attributeName.equals(GROUP_ID)) {
85-
value = model.getGroupId();
86-
} else if (attributeName.equals(PACKAGING)) {
87-
final String packaging = model.getPackaging();
88-
value = packaging == null ? "" : packaging;
89-
} else if (attributeName.equals(VERSION)) {
90-
value = model.getVersion();
91-
} else if (attributeName.equals(PARENT_ARTIFACT_ID) && model.getParent() != null) {
92-
value = model.getParent().getArtifactId();
93-
} else if (attributeName.equals(PARENT_GROUP_ID) && model.getParent() != null) {
94-
value = model.getParent().getGroupId();
95-
} else if (attributeName.equals(PARENT_VERSION) && model.getParent() != null) {
96-
value = model.getParent().getVersion();
97-
} else if (attributeName.equals(SOURCE_FOLDER)) {
98-
Build build = model.getBuild();
99-
if (build != null && build.getSourceDirectory() != null) {
100-
value = build.getSourceDirectory();
101-
} else {
102-
value = DEFAULT_SOURCE_FOLDER;
103-
}
104-
} else if (attributeName.equals(TEST_SOURCE_FOLDER)) {
105-
Build build = model.getBuild();
106-
if (build != null && build.getTestSourceDirectory() != null) {
107-
value = build.getTestSourceDirectory();
108-
} else {
109-
value = DEFAULT_TEST_SOURCE_FOLDER;
110-
}
111-
} else if (attributeName.equals(RESOURCE_FOLDER)) {
112-
Build build = model.getBuild();
113-
if (build != null && build.getResources() != null) {
114-
return build.getResources().stream().map(Resource::getDirectory).collect(Collectors.toList());
115-
} else {
116-
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
117-
}
118-
}
119-
120-
return Collections.singletonList(value);
121-
} catch (ServerException | ForbiddenException | IOException e) {
122-
throwReadException(e);
123-
} catch (XMLTreeException e) {
124-
throw new ValueStorageException("Error parsing pom.xml : " + e.getMessage());
125-
}
126-
return null;
127-
}
128-
}
12934
}

0 commit comments

Comments
 (0)