Extract global commentlink config into a Provider
Change-Id: I4f28bff33a0955202767e7a9ea90e256f9a66da7
This commit is contained in:
		@@ -94,6 +94,8 @@ import com.google.gerrit.server.patch.PatchListCacheImpl;
 | 
			
		||||
import com.google.gerrit.server.patch.PatchSetInfoFactory;
 | 
			
		||||
import com.google.gerrit.server.project.AccessControlModule;
 | 
			
		||||
import com.google.gerrit.server.project.ChangeControl;
 | 
			
		||||
import com.google.gerrit.server.project.CommentLinkInfo;
 | 
			
		||||
import com.google.gerrit.server.project.CommentLinkProvider;
 | 
			
		||||
import com.google.gerrit.server.project.PerformCreateProject;
 | 
			
		||||
import com.google.gerrit.server.project.PermissionCollection;
 | 
			
		||||
import com.google.gerrit.server.project.ProjectCacheImpl;
 | 
			
		||||
@@ -113,6 +115,8 @@ import com.google.inject.TypeLiteral;
 | 
			
		||||
import org.apache.velocity.runtime.RuntimeInstance;
 | 
			
		||||
import org.eclipse.jgit.lib.Config;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/** Starts global state with standard dependencies. */
 | 
			
		||||
public class GerritGlobalModule extends FactoryModule {
 | 
			
		||||
@@ -253,5 +257,8 @@ public class GerritGlobalModule extends FactoryModule {
 | 
			
		||||
    bind(AccountManager.class);
 | 
			
		||||
    bind(ChangeUserName.CurrentUser.class);
 | 
			
		||||
    factory(ChangeUserName.Factory.class);
 | 
			
		||||
 | 
			
		||||
    bind(new TypeLiteral<List<CommentLinkInfo>>() {})
 | 
			
		||||
        .toProvider(CommentLinkProvider.class).in(SINGLETON);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -45,6 +45,7 @@ import com.google.gerrit.reviewdb.client.Project.SubmitType;
 | 
			
		||||
import com.google.gerrit.server.account.GroupBackend;
 | 
			
		||||
import com.google.gerrit.server.config.ConfigUtil;
 | 
			
		||||
import com.google.gerrit.server.mail.Address;
 | 
			
		||||
import com.google.gerrit.server.project.CommentLinkInfo;
 | 
			
		||||
 | 
			
		||||
import org.eclipse.jgit.errors.ConfigInvalidException;
 | 
			
		||||
import org.eclipse.jgit.lib.CommitBuilder;
 | 
			
		||||
@@ -64,8 +65,14 @@ import java.util.HashSet;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.regex.Pattern;
 | 
			
		||||
 | 
			
		||||
public class ProjectConfig extends VersionedMetaData {
 | 
			
		||||
  public static final String COMMENTLINK = "commentlink";
 | 
			
		||||
  private static final String KEY_MATCH = "match";
 | 
			
		||||
  private static final String KEY_HTML = "html";
 | 
			
		||||
  private static final String KEY_LINK = "link";
 | 
			
		||||
 | 
			
		||||
  private static final String PROJECT_CONFIG = "project.config";
 | 
			
		||||
  private static final String GROUP_LIST = "groups";
 | 
			
		||||
 | 
			
		||||
@@ -148,6 +155,22 @@ public class ProjectConfig extends VersionedMetaData {
 | 
			
		||||
    return r;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static CommentLinkInfo buildCommentLink(Config cfg, String name)
 | 
			
		||||
      throws IllegalArgumentException {
 | 
			
		||||
    String match = cfg.getString(COMMENTLINK, name, KEY_MATCH);
 | 
			
		||||
 | 
			
		||||
    // Unfortunately this validation isn't entirely complete. Clients
 | 
			
		||||
    // can have exceptions trying to evaluate the pattern if they don't
 | 
			
		||||
    // support a token used, even if the server does support the token.
 | 
			
		||||
    //
 | 
			
		||||
    // At the minimum, we can trap problems related to unmatched groups.
 | 
			
		||||
    Pattern.compile(match);
 | 
			
		||||
 | 
			
		||||
    String link = cfg.getString(COMMENTLINK, name, KEY_LINK);
 | 
			
		||||
    String html = cfg.getString(COMMENTLINK, name, KEY_HTML);
 | 
			
		||||
    return new CommentLinkInfo(name, match, link, html);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public ProjectConfig(Project.NameKey projectName) {
 | 
			
		||||
    this.projectName = projectName;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,43 @@
 | 
			
		||||
// Copyright (C) 2012 The Android Open Source Project
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
// http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
package com.google.gerrit.server.project;
 | 
			
		||||
 | 
			
		||||
import static com.google.common.base.Preconditions.checkArgument;
 | 
			
		||||
 | 
			
		||||
import com.google.common.base.Strings;
 | 
			
		||||
 | 
			
		||||
/** Info about a single commentlink section in a config. */
 | 
			
		||||
public class CommentLinkInfo {
 | 
			
		||||
  public final String match;
 | 
			
		||||
  public final String link;
 | 
			
		||||
  public final String html;
 | 
			
		||||
 | 
			
		||||
  public transient final String name;
 | 
			
		||||
 | 
			
		||||
  public CommentLinkInfo(String name, String match, String link, String html) {
 | 
			
		||||
    checkArgument(name != null, "invalid commentlink.name");
 | 
			
		||||
    checkArgument(!Strings.isNullOrEmpty(match),
 | 
			
		||||
        "invalid commentlink.%s.match", name);
 | 
			
		||||
    link = Strings.emptyToNull(link);
 | 
			
		||||
    html = Strings.emptyToNull(html);
 | 
			
		||||
    checkArgument(
 | 
			
		||||
        (link != null && html == null) || (link == null && html != null),
 | 
			
		||||
        "commentlink.%s must have either link or html", name);
 | 
			
		||||
    this.name = name;
 | 
			
		||||
    this.match = match;
 | 
			
		||||
    this.link = link;
 | 
			
		||||
    this.html = html;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,47 @@
 | 
			
		||||
// Copyright (C) 2012 The Android Open Source Project
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
// http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
package com.google.gerrit.server.project;
 | 
			
		||||
 | 
			
		||||
import com.google.common.collect.ImmutableList;
 | 
			
		||||
import com.google.common.collect.Lists;
 | 
			
		||||
import com.google.gerrit.server.config.GerritServerConfig;
 | 
			
		||||
import com.google.gerrit.server.git.ProjectConfig;
 | 
			
		||||
import com.google.inject.Inject;
 | 
			
		||||
import com.google.inject.Provider;
 | 
			
		||||
 | 
			
		||||
import org.eclipse.jgit.lib.Config;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
public class CommentLinkProvider implements Provider<List<CommentLinkInfo>> {
 | 
			
		||||
  private final Config cfg;
 | 
			
		||||
 | 
			
		||||
  @Inject
 | 
			
		||||
  CommentLinkProvider(@GerritServerConfig Config cfg) {
 | 
			
		||||
    this.cfg = cfg;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public List<CommentLinkInfo> get() {
 | 
			
		||||
    Set<String> subsections = cfg.getSubsections(ProjectConfig.COMMENTLINK);
 | 
			
		||||
    List<CommentLinkInfo> cls =
 | 
			
		||||
        Lists.newArrayListWithCapacity(subsections.size());
 | 
			
		||||
    for (String name : subsections) {
 | 
			
		||||
      cls.add(ProjectConfig.buildCommentLink(cfg, name));
 | 
			
		||||
    }
 | 
			
		||||
    return ImmutableList.copyOf(cls);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user