blob: 783045babf46d225b25b966916f606bb9cbd7e8d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.rendering;
import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.processing.rendering.Renderer;
import com.yahoo.search.Result;
import com.yahoo.search.pagetemplates.result.PageTemplatesXmlRenderer;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.Executor;
/**
* Holds all configured and built-in renderers.
* This registry is always frozen.
*
* @author bratseth
*/
public final class RendererRegistry extends ComponentRegistry<com.yahoo.processing.rendering.Renderer<Result>> {
public static final ComponentId xmlRendererId = ComponentId.fromString("XmlRenderer");
public static final ComponentId pageRendererId = ComponentId.fromString("PageTemplatesXmlRenderer");
public static final ComponentId jsonRendererId = ComponentId.fromString("JsonRenderer");
public static final ComponentId defaultRendererId = jsonRendererId;
/** Creates a registry containing the built-in renderers only */
public RendererRegistry() {
this(Collections.emptyList());
}
/**
* Creates a registry containing the built-in renderers only, using a custom executor.
* Using a custom executor is useful for tests to avoid creating new threads for each renderer registry:
* Use MoreExecutors.directExecutor().
*/
public RendererRegistry(Executor executor) {
this(Collections.emptyList(), executor);
}
/** Creates a registry of the given renderers plus the built-in ones */
public RendererRegistry(Collection<Renderer> renderers) {
this(renderers, null);
}
/**
* Creates a registry of the given renderers plus the built-in ones, using a custom executor.
* Using a custom executor is useful for tests to avoid creating new threads for each renderer registry.
*/
public RendererRegistry(Collection<Renderer> renderers, Executor executor) {
// add json renderer
Renderer jsonRenderer = new JsonRenderer(executor);
jsonRenderer.initId(RendererRegistry.jsonRendererId);
register(jsonRenderer.getId(), jsonRenderer);
// Add xml renderer
Renderer xmlRenderer = new XmlRenderer(executor);
xmlRenderer.initId(xmlRendererId);
register(xmlRenderer.getId(), xmlRenderer);
// Add page templates renderer
Renderer pageRenderer = new PageTemplatesXmlRenderer(executor);
pageRenderer.initId(pageRendererId);
register(pageRenderer.getId(), pageRenderer);
// add application renderers
for (Renderer renderer : renderers)
register(renderer.getId(), renderer);
freeze();
}
/** Must be called when use of this is discontinued to free the resources it has allocated */
public void deconstruct() {
// deconstruct the renderers which was created by this
getRenderer(jsonRendererId.toSpecification()).deconstruct();
getRenderer(xmlRendererId.toSpecification()).deconstruct();
getRenderer(pageRendererId.toSpecification()).deconstruct();
}
/**
* Returns the default JSON renderer
*
* @return the default built-in result renderer
*/
public com.yahoo.processing.rendering.Renderer<Result> getDefaultRenderer() {
return getComponent(jsonRendererId);
}
/**
* Returns the requested renderer.
*
* @param format the id or format alias of the renderer to return. If null is passed the default renderer
* is returned
* @throws IllegalArgumentException if the renderer cannot be resolved
*/
public com.yahoo.processing.rendering.Renderer<Result> getRenderer(ComponentSpecification format) {
if (format == null || format.stringValue().equals("default")) return getDefaultRenderer();
if (format.stringValue().equals("json")) return getComponent(jsonRendererId);
if (format.stringValue().equals("xml")) return getComponent(xmlRendererId);
if (format.stringValue().equals("page")) return getComponent(pageRendererId);
com.yahoo.processing.rendering.Renderer<Result> renderer = getComponent(format);
if (renderer == null)
throw new IllegalArgumentException("No renderer with id or alias '" + format + "'. " +
"Available renderers are: [" + rendererNames() + "].");
return renderer;
}
private String rendererNames() {
StringBuilder r = new StringBuilder();
for (Renderer<Result> c : allComponents()) {
if (r.length() > 0)
r.append(", ");
r.append(c.getId().stringValue());
}
return r.toString();
}
}
|