summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/subjects/ConnectionLogSubject.java
blob: 3b08a172b65582f0401e08755a9c93691900ce44 (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
/*
 *
 * 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 org.apache.qpid.server.logging.subjects;

import org.apache.qpid.server.protocol.AMQProtocolSession;

import static org.apache.qpid.server.logging.subjects.LogSubjectFormat.CONNECTION_FORMAT;
import static org.apache.qpid.server.logging.subjects.LogSubjectFormat.SOCKET_FORMAT;
import static org.apache.qpid.server.logging.subjects.LogSubjectFormat.USER_FORMAT;

import java.text.MessageFormat;

/** The Connection LogSubject */
public class ConnectionLogSubject extends AbstractLogSubject
{

    public ConnectionLogSubject(AMQProtocolSession session)
    {
        _session = session;
    }

    // The Session this Actor is representing
    private AMQProtocolSession _session;

    // Used to stop re-creating the _logString when we reach our final format
    private boolean _upToDate = false;

    /**
     * Update the LogString as the Connection process proceeds.
     * 
     * When the Session has an authorized ID add that to the string.
     * 
     * When the Session then gains a Vhost add that to the string, at this point
     * we can set upToDate = true as the _logString will not need to be updated
     * from this point onwards.
     */
    private void updateLogString()
    {
        if (!_upToDate)
        {
            if (_session.getAuthorizedPrincipal() != null)
            {
                if (_session.getVirtualHost() != null)
                {
                    /**
                     * LOG FORMAT used by the AMQPConnectorActor follows
                     * ConnectionLogSubject.CONNECTION_FORMAT :
                     * con:{0}({1}@{2}/{3})
                     * 
                     * Uses a MessageFormat call to insert the required values
                     * according to these indices:
                     * 
                     * 0 - Connection ID 1 - User ID 2 - IP 3 - Virtualhost
                     */
                    setLogString("[" + MessageFormat.format(CONNECTION_FORMAT,
                                                            _session.getSessionID(),
                                                            _session.getAuthorizedPrincipal().getName(),
                                                            _session.getRemoteAddress(),
                                                            _session.getVirtualHost().getName())
                                 + "] ");

                    _upToDate = true;
                } 
                else
                {
                    setLogString("[" + MessageFormat.format(USER_FORMAT,
                                                            _session.getSessionID(),
                                                            _session.getAuthorizedPrincipal().getName(),
                                                            _session.getRemoteAddress())
                                 + "] ");

                }
            } 
            else
            {
                    setLogString("[" + MessageFormat.format(SOCKET_FORMAT,
                                                            _session.getSessionID(),
                                                            _session.getRemoteAddress())
                                 + "] ");
            }
        }
    }

    public String toLogString()
    {
        updateLogString();
        return super.toLogString();
    }
}