Encapsulate access to global plugin config in own class
The global plugin configuration will need to be accessed from
different places and it would be bad if the knowledge about section
and key names is distributed in the code.
Rename the existing XDocConfig class to XDocProjectConfig to make
clear that this class deals with the project-specific plugin
configuration.
Change-Id: Iebabff1b64fdcb32f1686ba6ac6c9b1bc0305c10
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java
index accaec9..c313f4d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java
@@ -38,7 +38,7 @@
@Override
protected void configure() {
install(new XDocLoader.Module());
- factory(XDocConfig.Factory.class);
+ factory(XDocProjectConfig.Factory.class);
DynamicSet.bind(binder(), ProjectWebLink.class)
.to(XDocWebLink.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocFileWebLink.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocFileWebLink.java
index 2f14e06..32fb4e0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocFileWebLink.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocFileWebLink.java
@@ -30,7 +30,7 @@
@PluginName String pluginName,
GitRepositoryManager repoManager,
@Named(XDocLoader.Module.X_DOC_RESOURCES) LoadingCache<String, Resource> cache,
- XDocConfig.Factory cfgFactory,
+ XDocProjectConfig.Factory cfgFactory,
ProjectCache projectCache) {
super(pluginName, repoManager, cache, cfgFactory, projectCache);
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
new file mode 100644
index 0000000..df6ff5a
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
@@ -0,0 +1,37 @@
+// Copyright (C) 2014 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.googlesource.gerrit.plugins.xdocs;
+
+import org.eclipse.jgit.lib.Config;
+
+public class XDocGlobalConfig {
+ private static final String SECTION_FORMATTER = "formatter";
+ private static final String KEY_ALLOW_HTML = "allowHtml";
+
+ enum Formatter {
+ MARKDOWN;
+ }
+
+ private final Config cfg;
+
+ XDocGlobalConfig(Config cfg) {
+ this.cfg = cfg;
+ }
+
+ boolean isHtmlAllowed(Formatter formatter) {
+ return cfg.getBoolean(SECTION_FORMATTER, formatter.name(),
+ KEY_ALLOW_HTML, false);
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
index e4ed3d4..76cfe34 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
@@ -33,7 +33,8 @@
import com.google.inject.Provider;
import com.google.inject.Singleton;
-import org.eclipse.jgit.lib.Config;
+import com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.Formatter;
+
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
@@ -52,13 +53,6 @@
public class XDocLoader extends CacheLoader<String, Resource> {
private static final String DEFAULT_HOST = "review.example.com";
- private static final String SECTION_FORMATTER = "formatter";
- private static final String KEY_ALLOW_HTML = "allowHtml";
-
- private enum Formatter {
- MARKDOWN;
- }
-
private final GitRepositoryManager repoManager;
private final Provider<String> webUrl;
private final String pluginName;
@@ -78,7 +72,7 @@
@Override
public Resource load(String strKey) throws Exception {
XDocResourceKey key = XDocResourceKey.fromString(strKey);
- Config cfg = cfgFactory.getGlobalPluginConfig(pluginName);
+ XDocGlobalConfig cfg = new XDocGlobalConfig(cfgFactory.getGlobalPluginConfig(pluginName));
Repository repo = repoManager.openRepository(key.getProject());
try {
RevWalk rw = new RevWalk(repo);
@@ -109,12 +103,11 @@
}
}
- private Resource getMarkdownAsHtmlResource(Config cfg,
+ private Resource getMarkdownAsHtmlResource(XDocGlobalConfig cfg,
Project.NameKey project, String md, int lastModified)
throws IOException {
MarkdownFormatter f = new MarkdownFormatter();
- if (!cfg.getBoolean(SECTION_FORMATTER, Formatter.MARKDOWN.name(),
- KEY_ALLOW_HTML, false)) {
+ if (!cfg.isHtmlAllowed(Formatter.MARKDOWN)) {
f.suppressHtml();
}
byte[] html = f.markdownToDocHtml(replaceMacros(project, md), UTF_8.name());
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocConfig.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocProjectConfig.java
similarity index 89%
rename from src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocConfig.java
rename to src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocProjectConfig.java
index 8af2182..f3c931a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocProjectConfig.java
@@ -23,9 +23,9 @@
import org.eclipse.jgit.lib.Config;
-public class XDocConfig {
+public class XDocProjectConfig {
interface Factory {
- XDocConfig create(ProjectState project);
+ XDocProjectConfig create(ProjectState project);
}
private static final String SECTION_WEB = "web";
@@ -35,7 +35,7 @@
private final Config cfg;
@Inject
- XDocConfig(@PluginName String pluginName, PluginConfigFactory cfgFactory,
+ XDocProjectConfig(@PluginName String pluginName, PluginConfigFactory cfgFactory,
@Assisted ProjectState project) {
this.cfg = cfgFactory.getProjectPluginConfigWithInheritance(project, pluginName);
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
index 64e3d60..ab56e70 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
@@ -77,7 +77,7 @@
private final GitRepositoryManager repoManager;
private final LoadingCache<String, Resource> docCache;
private final FileTypeRegistry fileTypeRegistry;
- private final XDocConfig.Factory cfgFactory;
+ private final XDocProjectConfig.Factory cfgFactory;
@Inject
XDocServlet(
@@ -88,7 +88,7 @@
GitRepositoryManager repoManager,
@Named(XDocLoader.Module.X_DOC_RESOURCES) LoadingCache<String, Resource> cache,
FileTypeRegistry fileTypeRegistry,
- XDocConfig.Factory cfgFactory) {
+ XDocProjectConfig.Factory cfgFactory) {
this.db = db;
this.projectControlFactory = projectControlFactory;
this.projectCache = projectCache;
@@ -114,7 +114,7 @@
Resource.NOT_FOUND.send(req, res);
return;
}
- XDocConfig cfg = cfgFactory.create(state);
+ XDocProjectConfig cfg = cfgFactory.create(state);
if (key.file == null) {
res.sendRedirect(getRedirectUrl(req, key, cfg));
return;
@@ -251,7 +251,7 @@
}
private String getRedirectUrl(HttpServletRequest req, ResourceKey key,
- XDocConfig cfg) {
+ XDocProjectConfig cfg) {
StringBuilder redirectUrl = new StringBuilder();
redirectUrl.append(req.getRequestURL().substring(0,
req.getRequestURL().length() - req.getRequestURI().length()));
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocWebLink.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocWebLink.java
index 5355572..1838b0f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocWebLink.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocWebLink.java
@@ -44,7 +44,7 @@
private final String pluginName;
private final GitRepositoryManager repoManager;
private final LoadingCache<String, Resource> docCache;
- private final XDocConfig.Factory cfgFactory;
+ private final XDocProjectConfig.Factory cfgFactory;
private final ProjectCache projectCache;
@Inject
@@ -52,7 +52,7 @@
@PluginName String pluginName,
GitRepositoryManager repoManager,
@Named(XDocLoader.Module.X_DOC_RESOURCES) LoadingCache<String, Resource> cache,
- XDocConfig.Factory cfgFactory,
+ XDocProjectConfig.Factory cfgFactory,
ProjectCache projectCache) {
this.pluginName = pluginName;
this.repoManager = repoManager;