summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/logging/actors/ManagementActor.java
blob: ba5ea47fc13dd3ac1a1f9a9a12673e83ab1a940f (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
/*
 *
 * 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.actors;

import org.apache.qpid.server.logging.RootMessageLogger;
import org.apache.qpid.server.logging.subjects.LogSubjectFormat;

import java.text.MessageFormat;

/**
 * Management actor to use in {@link MBeanInvocationHandlerImpl} to log all management operational logging.
 */
public class ManagementActor extends AbstractManagementActor
{
    private String _lastThreadName = null;

    /**
     * The logString to be used for logging
     */
    private String _logStringContainingPrincipal;

    /** @param rootLogger The RootLogger to use for this Actor */
    public ManagementActor(RootMessageLogger rootLogger)
    {
        super(rootLogger, UNKNOWN_PRINCIPAL);
    }

    public ManagementActor(RootMessageLogger rootLogger, String principalName)
    {
        super(rootLogger, principalName);
    }

    private synchronized String getAndCacheLogString()
    {
        String currentName = Thread.currentThread().getName();

        String actor;
        String logString = _logStringContainingPrincipal;

        // Record the last thread name so we don't have to recreate the log string
        if (_logStringContainingPrincipal == null || !currentName.equals(_lastThreadName))
        {
            _lastThreadName = currentName;
            String principalName = getPrincipalName();

            // Management Thread names have this format.
            // RMI TCP Connection(2)-169.24.29.116
            // This is true for both LocalAPI and JMX Connections
            // However to be defensive lets test.
            String[] split = currentName.split("\\(");
            if (split.length == 2)
            {
                String ip = currentName.split("-")[1];
                actor = MessageFormat.format(LogSubjectFormat.MANAGEMENT_FORMAT, principalName, ip);
            }
            else
            {
                // This is a precautionary path as it is not expected to occur
                // however rather than adjusting the thread name of the two
                // tests that will use this it is safer all round to do this.
                // it is also currently used by tests :
                // AMQBrokerManagerMBeanTest
                // ExchangeMBeanTest
                actor = currentName;
            }

            logString = "[" + actor + "] ";
            if(principalName != UNKNOWN_PRINCIPAL )
            {
                _logStringContainingPrincipal = logString;
            }

        }
        return logString;
    }

    public String getLogMessage()
    {
        return getAndCacheLogString();
    }
}