Skip to content

Commit 79e7c44

Browse files
author
Vitalii Parfonov
authored
CHE-4910:Fix catching exception in case invalid POM to prevent fail start 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>
1 parent 9a17103 commit 79e7c44

9 files changed

Lines changed: 613 additions & 114 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,173 @@
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+
if (mavenProjectManager == null) {
66+
return readFromPom(attributeName);
67+
}
68+
69+
final MavenProject mavenProject = mavenProjectManager.getMavenProject(projectFolder.getPath().toString());
70+
if (mavenProject != null) {
71+
return getFromMavenProject(mavenProject, attributeName);
72+
} else {
73+
return readFromPom(attributeName);
74+
}
75+
} catch (ServerException | ForbiddenException | IOException e) {
76+
throwReadException(e);
77+
} catch (XMLTreeException e) {
78+
throw new ValueStorageException("Error parsing pom.xml : " + e.getMessage());
79+
}
80+
return null;
81+
}
82+
83+
private List<String> getFromMavenProject(MavenProject mavenProject, String attributeName) throws ValueStorageException {
84+
switch (attributeName) {
85+
case ARTIFACT_ID:
86+
return singletonList(mavenProject.getMavenKey().getArtifactId());
87+
case GROUP_ID:
88+
return singletonList(mavenProject.getMavenKey().getGroupId());
89+
case PACKAGING:
90+
String packaging = mavenProject.getPackaging();
91+
return singletonList(packaging != null ? packaging : DEFAULT_PACKAGING);
92+
case VERSION:
93+
return singletonList(mavenProject.getMavenKey().getVersion());
94+
case PARENT_ARTIFACT_ID:
95+
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getArtifactId());
96+
case PARENT_GROUP_ID:
97+
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getGroupId());
98+
case PARENT_VERSION:
99+
return singletonList(mavenProject.getParentKey() == null ? "" : mavenProject.getParentKey().getVersion());
100+
case SOURCE_FOLDER:
101+
return (mavenProject.getSources() != null && !mavenProject.getSources().isEmpty()) ? mavenProject.getSources()
102+
: singletonList(DEFAULT_SOURCE_FOLDER);
103+
case TEST_SOURCE_FOLDER:
104+
return (mavenProject.getTestSources() != null && !mavenProject.getTestSources().isEmpty()) ? mavenProject.getTestSources()
105+
: singletonList(
106+
DEFAULT_TEST_SOURCE_FOLDER);
107+
case RESOURCE_FOLDER:
108+
if (mavenProject.getResources() != null && !mavenProject.getResources().isEmpty()) {
109+
return mavenProject.getResources().stream().map(MavenResource::getDirectory).collect(Collectors.toList());
110+
} else {
111+
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
112+
}
113+
default:
114+
throw new ValueStorageException(String.format("Unknown attribute %s", attributeName));
115+
}
116+
}
117+
118+
119+
private List<String> readFromPom(String attributeName)
120+
throws ServerException, ForbiddenException, IOException, XMLTreeException, ValueStorageException {
121+
final Model model = readModel(projectFolder);
122+
switch (attributeName) {
123+
case ARTIFACT_ID:
124+
return singletonList(model.getArtifactId());
125+
case GROUP_ID:
126+
return singletonList(model.getGroupId());
127+
case PACKAGING:
128+
String packaging = model.getPackaging();
129+
return singletonList(packaging != null ? packaging : DEFAULT_PACKAGING);
130+
case VERSION:
131+
return singletonList(model.getVersion());
132+
case PARENT_ARTIFACT_ID:
133+
return singletonList(model.getParent() == null ? "" : model.getParent().getArtifactId());
134+
case PARENT_GROUP_ID:
135+
return singletonList(model.getParent() == null ? "" : model.getParent().getGroupId());
136+
case PARENT_VERSION:
137+
return singletonList(model.getParent() == null ? "" : model.getParent().getVersion());
138+
case SOURCE_FOLDER:
139+
if (model.getBuild() != null && model.getBuild().getSourceDirectory() != null) {
140+
return singletonList(model.getBuild().getSourceDirectory());
141+
} else {
142+
return singletonList(DEFAULT_SOURCE_FOLDER);
143+
}
144+
case TEST_SOURCE_FOLDER:
145+
if (model.getBuild() != null && model.getBuild().getTestSourceDirectory() != null) {
146+
return singletonList(model.getBuild().getTestSourceDirectory());
147+
} else {
148+
return singletonList(DEFAULT_TEST_SOURCE_FOLDER);
149+
}
150+
case RESOURCE_FOLDER:
151+
if (model.getBuild() != null && model.getBuild().getResources() != null) {
152+
return model.getBuild().getResources().stream().map(Resource::getDirectory).collect(Collectors.toList());
153+
} else {
154+
return Arrays.asList(DEFAULT_RESOURCES_FOLDER, DEFAULT_TEST_RESOURCES_FOLDER);
155+
}
156+
default:
157+
throw new ValueStorageException(String.format("Unknown attribute %s", attributeName));
158+
}
159+
}
160+
161+
protected Model readModel(FolderEntry projectFolder) throws ValueStorageException, ServerException, ForbiddenException, IOException {
162+
FileEntry pomFile = (FileEntry)projectFolder.getChild("pom.xml");
163+
if (pomFile == null) {
164+
throw new ValueStorageException("pom.xml does not exist.");
165+
}
166+
return Model.readFrom(pomFile.getInputStream());
167+
}
168+
169+
protected void throwReadException(Exception e) throws ValueStorageException {
170+
throw new ValueStorageException("Can't read pom.xml : " + e.getMessage());
171+
}
172+
173+
}

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)