|
| 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 | +} |
0 commit comments