summaryrefslogtreecommitdiff
path: root/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAMessageStoreManagerMBean.java
blob: cd4e990607d8909a429a8f5c4c4ca3a750b9909a (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
/*
 * 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.store.berkeleydb;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.management.JMException;
import javax.management.NotCompliantMBeanException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQStoreException;
import org.apache.qpid.server.management.AMQManagedObject;

public class BDBHAMessageStoreManagerMBean extends AMQManagedObject implements ManagedBDBHAMessageStore
{
    private static final Logger LOGGER = Logger.getLogger(BDBHAMessageStoreManagerMBean.class);

    private static  final TabularType GROUP_MEMBERS_TABLE;
    private static final CompositeType GROUP_MEMBER_ROW;
    private static final OpenType<?>[] GROUP_MEMBER_ATTRIBUTE_TYPES;

    static
    {
        try
        {
            GROUP_MEMBER_ATTRIBUTE_TYPES = new OpenType<?>[] {SimpleType.STRING, SimpleType.STRING};
            final String[] itemNames = new String[] {BDBHAMessageStore.GRP_MEM_COL_NODE_NAME, BDBHAMessageStore.GRP_MEM_COL_NODE_HOST_PORT};
            final String[] itemDescriptions = new String[] {"Unique node name", "Node host / port "};
            GROUP_MEMBER_ROW = new CompositeType("GroupMember", "Replication group member",
                                                itemNames,
                                                itemDescriptions,
                                                GROUP_MEMBER_ATTRIBUTE_TYPES );
            GROUP_MEMBERS_TABLE = new TabularType("GroupMembers", "Replication group memebers",
                                                GROUP_MEMBER_ROW,
                                                new String[] {BDBHAMessageStore.GRP_MEM_COL_NODE_NAME});
        }
        catch (final OpenDataException ode)
        {
            throw new ExceptionInInitializerError(ode);
        }
    }

    private final BDBHAMessageStore _store;

    protected BDBHAMessageStoreManagerMBean(BDBHAMessageStore store) throws NotCompliantMBeanException
    {
        super(ManagedBDBHAMessageStore.class, ManagedBDBHAMessageStore.TYPE);
        _store = store;
    }

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

    @Override
    public String getGroupName() throws IOException
    {
        return _store.getGroupName();
    }

    @Override
    public String getNodeName() throws IOException
    {
        return _store.getNodeName();
    }

    @Override
    public String getNodeHostPort() throws IOException
    {
        return _store.getNodeHostPort();
    }

    @Override
    public String getHelperHostPort() throws IOException
    {
        return _store.getHelperHostPort();
    }

    @Override
    public String getReplicationPolicy() throws IOException
    {
        return _store.getReplicationPolicy();
    }

    @Override
    public String getNodeState() throws IOException
    {
        return _store.getNodeState();
    }

    @Override
    public boolean getDesignatedPrimary() throws IOException
    {
        return _store.isDesignatedPrimary();
    }

    @Override
    public TabularData getAllNodesInGroup() throws IOException, JMException
    {
        final TabularDataSupport data = new TabularDataSupport(GROUP_MEMBERS_TABLE);
        final List<Map<String, String>> members = _store.getGroupMembers();

        for (Map<String, String> map : members)
        {
            CompositeData memberData = new CompositeDataSupport(GROUP_MEMBER_ROW, map);
            data.put(memberData);
        }
        return data;
    }

    @Override
    public void removeNodeFromGroup(String nodeName) throws JMException
    {
        try
        {
            _store.removeNodeFromGroup(nodeName);
        }
        catch (AMQStoreException e)
        {
            LOGGER.error("Failed to remove node " + nodeName + " from group", e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public void setDesignatedPrimary(boolean primary) throws JMException
    {
        try
        {
            _store.setDesignatedPrimary(primary);
        }
        catch (AMQStoreException e)
        {
            LOGGER.error("Failed to set node " + _store.getNodeName() + " as designated primary", e);
            throw new JMException(e.getMessage());
        }
    }

    @Override
    public void updateAddress(String nodeName, String newHostName, int newPort) throws JMException
    {
        try
        {
            _store.updateAddress(nodeName, newHostName, newPort);
        }
        catch(AMQStoreException e)
        {
            LOGGER.error("Failed to update address for node " + nodeName + " to " + newHostName + ":" + newPort, e);
            throw new JMException(e.getMessage());
        }
    }


}