3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.scanner.deprecated.perspectives;
22 import com.google.common.collect.Maps;
24 import javax.annotation.CheckForNull;
26 import org.sonar.api.batch.fs.InputComponent;
27 import org.sonar.api.batch.fs.InputPath;
28 import org.sonar.api.batch.fs.internal.DefaultInputModule;
29 import org.sonar.api.component.Perspective;
30 import org.sonar.api.component.ResourcePerspectives;
31 import org.sonar.api.resources.Resource;
32 import org.sonar.api.resources.ResourceUtils;
33 import org.sonar.core.component.ComponentKeys;
34 import org.sonar.scanner.scan.filesystem.InputComponentStore;
36 public class ScannerPerspectives implements ResourcePerspectives {
38 private final Map<Class<?>, PerspectiveBuilder<?>> builders = Maps.newHashMap();
39 private final InputComponentStore componentStore;
40 private final DefaultInputModule module;
42 public ScannerPerspectives(PerspectiveBuilder[] builders, DefaultInputModule module, InputComponentStore componentStore) {
43 this.componentStore = componentStore;
46 for (PerspectiveBuilder builder : builders) {
47 this.builders.put(builder.getPerspectiveClass(), builder);
53 public <P extends Perspective> P as(Class<P> perspectiveClass, Resource resource) {
54 InputComponent component = componentStore.getByKey(getComponentKey(resource));
55 if (component != null) {
56 PerspectiveBuilder<P> builder = builderFor(perspectiveClass);
57 return builder.loadPerspective(perspectiveClass, component);
62 private String getComponentKey(Resource r) {
63 if (ResourceUtils.isProject(r) || /* For technical projects */ResourceUtils.isRootProject(r)) {
66 return ComponentKeys.createEffectiveKey(module.key(), r);
71 public <P extends Perspective> P as(Class<P> perspectiveClass, InputPath inputPath) {
72 PerspectiveBuilder<P> builder = builderFor(perspectiveClass);
73 return builder.loadPerspective(perspectiveClass, inputPath);
76 private <T extends Perspective> PerspectiveBuilder<T> builderFor(Class<T> clazz) {
77 PerspectiveBuilder<T> builder = (PerspectiveBuilder<T>) builders.get(clazz);
78 if (builder == null) {
79 throw new PerspectiveNotFoundException("Perspective class is not registered: " + clazz);