import org.sonar.ce.es.EsIndexerEnabler;
import org.sonar.ce.property.CePropertyDefinitions;
import org.sonar.ce.settings.ComputeEngineSettings;
+import org.sonar.ce.user.CeUserSession;
import org.sonar.core.component.DefaultResourceTypes;
import org.sonar.core.config.CorePropertyDefinitions;
import org.sonar.core.i18n.DefaultI18n;
new TempFolderProvider(),
System2.INSTANCE,
+ // user session
+ CeUserSession.class,
+
// DB
DbClient.class,
DaoModule.class,
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.ce.user;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.sonar.server.user.UserSession;
+
+/**
+ * Implementation of {@link UserSession} which provide not implementation of any method.
+ * <p>
+ * Any use of {@link UserSession} in the Compute Engine will raise an error.
+ * </p>
+ */
+public class CeUserSession implements UserSession {
+
+ public static final String UOE_MESSAGE = "UserSession must not be used from within the Compute Engine";
+
+ @Override
+ public String getLogin() {
+ return notImplemented();
+ }
+
+ @Override
+ public String getName() {
+ return notImplemented();
+ }
+
+ @Override
+ public Integer getUserId() {
+ return notImplemented();
+ }
+
+ @Override
+ public Set<String> getUserGroups() {
+ return notImplemented();
+ }
+
+ @Override
+ public boolean isLoggedIn() {
+ return notImplementedBooleanMethod();
+ }
+
+ @Override
+ public Locale locale() {
+ return notImplemented();
+ }
+
+ @Override
+ public UserSession checkLoggedIn() {
+ return notImplemented();
+ }
+
+ @Override
+ public UserSession checkPermission(String globalPermission) {
+ return notImplemented();
+ }
+
+ @Override
+ public UserSession checkGlobalPermission(String globalPermission) {
+ return notImplemented();
+ }
+
+ @Override
+ public UserSession checkAnyPermissions(Collection<String> globalPermissions) {
+ return notImplemented();
+ }
+
+ @Override
+ public boolean hasPermission(String globalPermission) {
+ return notImplementedBooleanMethod();
+ }
+
+ @Override
+ public boolean hasGlobalPermission(String globalPermission) {
+ return notImplementedBooleanMethod();
+ }
+
+ @Override
+ public List<String> globalPermissions() {
+ return notImplemented();
+ }
+
+ @Override
+ public UserSession checkComponentPermission(String projectPermission, String componentKey) {
+ return notImplemented();
+ }
+
+ @Override
+ public UserSession checkComponentUuidPermission(String permission, String componentUuid) {
+ return notImplemented();
+ }
+
+ @Override
+ public boolean hasComponentPermission(String permission, String componentKey) {
+ return notImplementedBooleanMethod();
+ }
+
+ @Override
+ public boolean hasComponentUuidPermission(String permission, String componentUuid) {
+ return notImplementedBooleanMethod();
+ }
+
+ private static <T> T notImplemented() {
+ throw new UnsupportedOperationException(UOE_MESSAGE);
+ }
+
+ private static boolean notImplementedBooleanMethod() {
+ throw new UnsupportedOperationException(UOE_MESSAGE);
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.ce.user;
+
+import javax.annotation.ParametersAreNonnullByDefault;
);
assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
- + 21 // level 1
+ + 22 // level 1
+ 47 // content of DaoModule
+ 1 // content of EsSearchModule
+ 58 // content of CorePropertyDefinitions
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.ce.user;
+
+import com.google.common.base.Predicate;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import static com.google.common.collect.FluentIterable.from;
+import static java.util.Arrays.asList;
+import static org.sonar.test.ExceptionCauseMatcher.hasType;
+
+@RunWith(DataProviderRunner.class)
+public class CeUserSessionTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private CeUserSession underTest = new CeUserSession();
+
+ @DataProvider
+ public static Object[][] ceUserSessionPublicMethods() {
+ List<Method> declaredMethods = from(asList(CeUserSession.class.getDeclaredMethods()))
+ .filter(PublicMethodPredicate.INSTANCE).toList();
+ Object[][] res = new Object[declaredMethods.size()][1];
+ int i = 0;
+ for (Method declaredMethod : declaredMethods) {
+ res[i][0] = declaredMethod;
+ i++;
+ }
+ return res;
+ }
+
+ @Test
+ @UseDataProvider("ceUserSessionPublicMethods")
+ public void all_methods_of_CeUserSession_throw_UOE(Method method) throws InvocationTargetException, IllegalAccessException {
+ int parametersCount = method.getParameterTypes().length;
+ switch (parametersCount) {
+ case 2:
+ expectUOE();
+ method.invoke(underTest, null, null);
+ break;
+ case 1:
+ expectUOE();
+ method.invoke(underTest, (Object) null);
+ break;
+ case 0:
+ expectUOE();
+ method.invoke(underTest);
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported number of parameters " + parametersCount);
+ }
+ }
+
+ private void expectUOE() {
+ expectedException.expect(InvocationTargetException.class);
+ expectedException.expectCause(
+ hasType(UnsupportedOperationException.class)
+ .andMessage("UserSession must not be used from within the Compute Engine")
+ );
+ }
+
+ private enum PublicMethodPredicate implements Predicate<Method> {
+ INSTANCE;
+
+ @Override
+ public boolean apply(Method input) {
+ return Modifier.isPublic(input.getModifiers());
+ }
+ }
+}
import org.sonar.server.view.index.ViewIndexDefinition;
import static com.google.common.collect.Lists.newArrayList;
-import static java.util.Objects.requireNonNull;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.ACTION_PLANS;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.ASSIGNEES;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.AUTHORS;
private final Sorting sorting;
private final System2 system;
- @CheckForNull
private final UserSession userSession;
- /**
- * Constructor for Pico when no UserSession is available such as in the Compute Engine.
- */
- public IssueIndex(EsClient client, System2 system) {
- this(client, system, null);
- }
-
- public IssueIndex(EsClient client, System2 system, @Nullable UserSession userSession) {
+ public IssueIndex(EsClient client, System2 system, UserSession userSession) {
super(client);
this.system = system;
*/
@CheckForNull
public IssueDoc getNullableByKey(String key) {
- checkNotCalledFromCE();
-
SearchResult<IssueDoc> result = search(IssueQuery.builder(userSession).issueKeys(newArrayList(key)).build(), new SearchOptions());
if (result.getTotal() == 1) {
return result.getDocs().get(0);
}
public SearchResult<IssueDoc> search(IssueQuery query, SearchOptions options) {
- checkNotCalledFromCE();
-
SearchRequestBuilder requestBuilder = getClient()
.prepareSearch(IssueIndexDefinition.INDEX)
.setTypes(IssueIndexDefinition.TYPE_ISSUE);
* Only fields needed for the batch are returned.
*/
public Iterator<IssueDoc> selectIssuesForBatch(ComponentDto component) {
- checkNotCalledFromCE();
-
BoolFilterBuilder filter = FilterBuilders.boolFilter()
.must(createAuthorizationFilter(true, userSession.getLogin(), userSession.getUserGroups()))
.mustNot(FilterBuilders.termsFilter(IssueIndexDefinition.FIELD_ISSUE_STATUS, Issue.STATUS_CLOSED));
return EsUtils.scroll(getClient(), response.getScrollId(), DOC_CONVERTER);
}
-
- private void checkNotCalledFromCE() {
- requireNonNull(userSession, "getNullableByKey can not be used from the Compute Engine");
- }
}