aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/templates/LogExceptionUserTemplateDelegator.java
blob: 7696790897e5f4129742214906ca1a6a752796ce (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.templates;

import com.yahoo.log.LogLevel;
import com.yahoo.yolean.Exceptions;

import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Delegates to another UserTemplate, but handles any exceptions(except IOException) by logging them.
 * 
 * @author Tony Vaagenes
 * @deprecated use a renderer instead
 */
@SuppressWarnings("deprecation")
// TODO: Remove on Vespa 7
@Deprecated // OK (But wait for deprecated handlers in vespaclient-container-plugin to be removed)
public class LogExceptionUserTemplateDelegator<T extends Writer> extends UserTemplate<T> {

    private static Logger log = Logger.getLogger(LogExceptionUserTemplateDelegator.class.getName());
    private final UserTemplate<T> delegate;

    public LogExceptionUserTemplateDelegator(UserTemplate<T> delegate) {
        super(LogExceptionUserTemplateDelegator.class.getSimpleName());
        this.delegate = delegate;
    }

    @Override
    public Context createContext() {
        return delegate.createContext();
    }

    @Override
    public T wrapWriter(Writer writer) {
        return delegate.wrapWriter(writer);
    }

    @Override
    public boolean isDefaultTemplateSet() {
        return delegate.isDefaultTemplateSet();
    }

    @Override
    public String getSummaryClass() {
        return delegate.getSummaryClass();
    }

    @Override
    public String getBoldOpenTag() {
        return delegate.getBoldOpenTag();
    }

    @Override
    public String getBoldCloseTag() {
        return delegate.getBoldCloseTag();
    }

    @Override
    public String getSeparatorTag() {
        return delegate.getSeparatorTag();
    }

    @Override
    public void setSummaryClass(String summaryClass) {
        delegate.setSummaryClass(summaryClass);
    }

    @Override
    public void setHighlightTags(String start, String end, String sep) {
        delegate.setHighlightTags(start, end, sep);
    }

    @Override
    public String getName() {
        return delegate.getName();
    }

    @Override
    public String getMimeType() {
        return delegate.getMimeType();
    }

    @Override
    public String getEncoding() {
        return delegate.getEncoding();
    }

    @Override
    public Template<T> getTemplate(String templateName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setTemplate(String templateName, Template<? extends Writer> template) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setTemplateNotNull(String templateName, Template<? extends Writer> template) {
        throw new UnsupportedOperationException();
    }

    /*** Template

    @Override
    public void <methodName>(Context context, T writer) throws IOException {
        try {
            delegate.<methodName>(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    ***/

    /*** Begin expanded template for
         header, footer, hit, hitFooter, error, noHits, queryContext,
         Thanks java, for giving me the opportunely to use copy-paste ***/


    @Override
    public void header(Context context, T writer) throws IOException {
        try {
            delegate.header(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    @Override
    public void footer(Context context, T writer) throws IOException {
        try {
            delegate.footer(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    @Override
    public void hit(Context context, T writer) throws IOException {
        try {
            delegate.hit(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    @Override
    public void hitFooter(Context context, T writer) throws IOException {
        try {
            delegate.hitFooter(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    @Override
    public void error(Context context, T writer) throws IOException {
        try {
            delegate.error(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    @Override
    public void noHits(Context context, T writer) throws IOException {
        try {
            delegate.noHits(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    @Override
    public void queryContext(Context context, T writer) throws IOException {
        try {
            delegate.queryContext(context, writer);
        } catch (Exception e) {
            handleException(e);
        }
    }

    /*** End expanded template. ***/

    private void handleException(Exception e) throws IOException {
        if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            log.log(LogLevel.WARNING, "Exception thrown in " + getName()
                    + ": " + Exceptions.toMessageString(e), e);
        }
    }

    UserTemplate<T> getDelegate() {
        return delegate;
    }
}