summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm
blob: 02bf155c447bfd7925c784305c500a9cdc08821e (plain)
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
202
203
204
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 ${package};

import static org.apache.qpid.server.logging.AbstractRootMessageLogger.DEFAULT_LOG_HIERARCHY_PREFIX;

import org.apache.log4j.Logger;
import org.apache.qpid.server.logging.LogMessage;
import org.apache.qpid.server.registry.ApplicationRegistry;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Generated Using GeneratedLogMessages and LogMessages.vm
 *
 * This file is based on the content of LogMessages.properties
 *
 * It is generated so that we can provide compile time validation of the
 * message parameters.
 *
 * DO NOT EDIT DIRECTLY THIS FILE IS GENERATED.
 *
 */
public class ${type.name}Messages
{
    private static ResourceBundle _messages;
    private static Locale _currentLocale;
    
    public static final String ${type.name.toUpperCase()}_LOG_HIERARCHY = DEFAULT_LOG_HIERARCHY_PREFIX + "${type.name.toLowerCase()}";
#foreach( $message in ${type.list} )
    public static final String ${message.methodName.toUpperCase()}_LOG_HIERARCHY = DEFAULT_LOG_HIERARCHY_PREFIX + "${type.name.toLowerCase()}.${message.methodName.toLowerCase()}";
#end

    static
    {
        Logger.getLogger(${type.name.toUpperCase()}_LOG_HIERARCHY);
#foreach( $message in ${type.list} )
        Logger.getLogger(${message.methodName.toUpperCase()}_LOG_HIERARCHY);
#end

        reload();
    }

    public static void reload()
    {
        if (ApplicationRegistry.isConfigured())
        {
            _currentLocale = ApplicationRegistry.getInstance().getConfiguration().getLocale();
        }
        else
        {
            _currentLocale = Locale.getDefault();
        }

        _messages = ResourceBundle.getBundle("${resource}", _currentLocale);
    }


##
## The list stored under key 'list' in the 'type' HashMap contains all the
## log messages that this class should contain. So for each entry in the list
## this template will create a new log method.
##
#foreach( $message in ${type.list} )
    /**
     * Log a ${type.name} message of the Format:
     * <pre>${message.format}</pre>
     * Optional values are contained in [square brackets] and are numbered
     * sequentially in the method call.
     *
     */
## There is a lot in the method header here. To make it more readable/understandable
##  This is the same text laid out to be easier to read:
##  public static LogMessage ${message.methodName} (
##          #foreach($parameter in ${message.parameters})
##                ${parameter.type} ${parameter.name}
##                #if (${velocityCount} != ${message.parameters.size()} )
##                   ,
##                #end
##          #end
##          #if(${message.parameters.size()} > 0 && ${message.options.size()} > 0)
##          ,
##          #end
##          #foreach($option in ${message.options})
##          boolean ${option.name}
##          #if (${velocityCount} != ${message.options.size()} )
##             ,
##          #end
##          #end
##         )
##
## What is going on is that we set the method name based on the HashMap lookup
## for 'methodName' Then for each entry(another HashMap) in the list stored
## in the HashMap under 'parameters' build the argument list of from the 'type'
## and 'name' values. Ensuring that we add a ', ' between each entry.
##
## However, check that we are not at the last entry of the list as we don't
## want to add ', ' at then end.
##
## Before we go on to the options we perform a check to see if we had any
## parameters. If we did and we have options to add then we add ', ' to keep
## the syntax correct.
##
## We then go on to the options that are again retrieved from the top HashMap
## with key 'options'. For each option a boolean argument is created and the
## name is retrieved from the option HashMap with key 'name'
##
    public static LogMessage ${message.methodName}(#foreach($parameter in ${message.parameters})${parameter.type} ${parameter.name}#if (${velocityCount} != ${message.parameters.size()} ), #end
#end#if(${message.parameters.size()} > 0 && ${message.options.size()} > 0), #end#foreach($option in ${message.options})boolean ${option.name}#if (${velocityCount} != ${message.options.size()} ), #end#end)
    {
        String rawMessage = _messages.getString("${message.name}");
## If we have some options then we need to build the message based
## on those values so only provide that logic if we need it.
#if(${message.options.size()} > 0)
        StringBuffer msg = new StringBuffer();

        // Split the formatted message up on the option values so we can
        // rebuild the message based on the configured options.
        String[] parts = rawMessage.split("\\[");
        msg.append(parts[0]);

        int end;
        if (parts.length > 1)
        {
## For Each Optional value check if it has been enabled and then
## append it to the log.
#foreach($option in ${message.options})

            // Add Option : ${option.value}.
            end = parts[${velocityCount}].indexOf(']');
            if (${option.name})
            {
                msg.append(parts[${velocityCount}].substring(0, end));
            }

            // Use 'end + 1' to remove the ']' from the output
            msg.append(parts[${velocityCount}].substring(end + 1));
#end
        }

        rawMessage = msg.toString();
#end

##
## If we don't have any parameters then we don't need the overhead of using the
## message formatter so we can just set our return message to the retreived
## fixed string. So we don't need to create a new MessageFormat
##
## Here we setup rawMessage to be the formatted message ready for direct return
## with the message.name or further processing to remove options.
##
#if(${message.parameters.size()} > 0)
        final Object[] messageArguments = {#foreach($parameter in ${message.parameters})${parameter.name}#if (${velocityCount} != ${message.parameters.size()} ), #end#end};
        // Create a new MessageFormat to ensure thread safety.
        // Sharing a MessageFormat and using applyPattern is not thread safe
        MessageFormat formatter = new MessageFormat(rawMessage, _currentLocale);

        final String message = formatter.format(messageArguments);
#else
## If we have no parameters then we can skip the formating and set the log
        final String message = rawMessage;
#end

        return new LogMessage()
        {
            public String toString()
            {
                return message;
            }
            
            public String getLogHierarchy()
            {
                return ${message.methodName.toUpperCase()}_LOG_HIERARCHY;
            }
        };
    }

#end

    private ${type.name}Messages()
    {
    }

}