summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandler.java
blob: abf1537ef79fedbc5d071293c0a5a1bd369ae0f3 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package org.apache.qpid.server.configuration.store;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.qpid.server.BrokerOptions;
import org.apache.qpid.server.configuration.ConfigurationEntry;
import org.apache.qpid.server.configuration.ConfigurationEntryStore;
import org.apache.qpid.server.configuration.IllegalConfigurationException;
import org.apache.qpid.server.model.AccessControlProvider;
import org.apache.qpid.server.model.Port;
import org.apache.qpid.server.model.Protocol;
import org.apache.qpid.server.model.State;
import org.apache.qpid.server.model.VirtualHost;
import org.apache.qpid.server.util.MapValueConverter;

public class ManagementModeStoreHandler implements ConfigurationEntryStore
{
    private static final Logger LOGGER = Logger.getLogger(ManagementModeStoreHandler.class);

    private static final String MANAGEMENT_MODE_PORT_PREFIX = "MANAGEMENT-MODE-PORT-";
    private static final String PORT_TYPE = Port.class.getSimpleName();
    private static final String VIRTUAL_HOST_TYPE = VirtualHost.class.getSimpleName();
    private static final String ACCESS_CONTROL_PROVIDER_TYPE = AccessControlProvider.class.getSimpleName();
    private static final String ATTRIBUTE_STATE = VirtualHost.STATE;
    private static final Object MANAGEMENT_MODE_AUTH_PROVIDER = "mm-auth";

    private final ConfigurationEntryStore _store;
    private final Map<UUID, ConfigurationEntry> _cliEntries;
    private final Map<UUID, Object> _quiescedEntries;
    private final UUID _rootId;

    public ManagementModeStoreHandler(ConfigurationEntryStore store, BrokerOptions options)
    {
        ConfigurationEntry storeRoot = store.getRootEntry();
        _store = store;
        _rootId = storeRoot.getId();
        _cliEntries = createPortsFromCommadLineOptions(options);
        _quiescedEntries = quiesceEntries(storeRoot, options);
    }

    @Override
    public ConfigurationEntry getRootEntry()
    {
        return getEntry(_rootId);
    }

    @Override
    public ConfigurationEntry getEntry(UUID id)
    {
        synchronized (_store)
        {
            if (_cliEntries.containsKey(id))
            {
                return _cliEntries.get(id);
            }

            ConfigurationEntry entry = _store.getEntry(id);
            if (_quiescedEntries.containsKey(id))
            {
                entry = createEntryWithState(entry, State.QUIESCED);
            }
            else if (id == _rootId)
            {
                entry = createRootWithCLIEntries(entry);
            }
            return entry;
        }
    }

    @Override
    public void save(ConfigurationEntry... entries)
    {
        synchronized (_store)
        {
            ConfigurationEntry[] entriesToSave = new ConfigurationEntry[entries.length];

            for (int i = 0; i < entries.length; i++)
            {
                ConfigurationEntry entry = entries[i];
                UUID id = entry.getId();
                if (_cliEntries.containsKey(id))
                {
                    throw new IllegalConfigurationException("Cannot save configuration provided as command line argument:"
                            + entry);
                }
                else if (_quiescedEntries.containsKey(id))
                {
                    // save entry with the original state
                    entry = createEntryWithState(entry, _quiescedEntries.get(ATTRIBUTE_STATE));
                }
                else if (_rootId.equals(id))
                {
                    // save root without command line entries
                    Set<UUID> childrenIds = new HashSet<UUID>(entry.getChildrenIds());
                    if (!_cliEntries.isEmpty())
                    {
                        childrenIds.removeAll(_cliEntries.entrySet());
                    }
                    HashMap<String, Object> attributes = new HashMap<String, Object>(entry.getAttributes());
                    entry = new ConfigurationEntry(entry.getId(), entry.getType(), attributes, childrenIds, this);
                }
                entriesToSave[i] = entry;
            }

            _store.save(entriesToSave);
        }
    }

    @Override
    public UUID[] remove(UUID... entryIds)
    {
        synchronized (_store)
        {
            for (UUID id : entryIds)
            {
                if (_cliEntries.containsKey(id))
                {
                    throw new IllegalConfigurationException("Cannot change configuration for command line entry:"
                            + _cliEntries.get(id));
                }
            }
            UUID[] result = _store.remove(entryIds);
            for (UUID id : entryIds)
            {
                if (_quiescedEntries.containsKey(id))
                {
                    _quiescedEntries.remove(id);
                }
            }
            return result;
        }
    }

    @Override
    public void copyTo(String copyLocation)
    {
        synchronized (_store)
        {
            _store.copyTo(copyLocation);
        }
    }

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

    @Override
    public int getVersion()
    {
        return _store.getVersion();
    }

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

    private Map<UUID, ConfigurationEntry> createPortsFromCommadLineOptions(BrokerOptions options)
    {
        int managementModeRmiPort = options.getManagementModeRmiPort();
        if (managementModeRmiPort < 0)
        {
            throw new IllegalConfigurationException("Invalid rmi port is specified: " + managementModeRmiPort);
        }
        int managementModeConnectorPort = options.getManagementModeConnectorPort();
        if (managementModeConnectorPort < 0)
        {
            throw new IllegalConfigurationException("Invalid connector port is specified: " + managementModeConnectorPort);
        }
        int managementModeHttpPort = options.getManagementModeHttpPort();
        if (managementModeHttpPort < 0)
        {
            throw new IllegalConfigurationException("Invalid http port is specified: " + managementModeHttpPort);
        }
        Map<UUID, ConfigurationEntry> cliEntries = new HashMap<UUID, ConfigurationEntry>();
        if (managementModeRmiPort != 0)
        {
            ConfigurationEntry entry = createCLIPortEntry(managementModeRmiPort, Protocol.RMI);
            cliEntries.put(entry.getId(), entry);
            if (managementModeConnectorPort == 0)
            {
                ConfigurationEntry connectorEntry = createCLIPortEntry(managementModeRmiPort + 100, Protocol.JMX_RMI);
                cliEntries.put(connectorEntry.getId(), connectorEntry);
            }
        }
        if (managementModeConnectorPort != 0)
        {
            ConfigurationEntry entry = createCLIPortEntry(managementModeConnectorPort, Protocol.JMX_RMI);
            cliEntries.put(entry.getId(), entry);
        }
        if (managementModeHttpPort != 0)
        {
            ConfigurationEntry entry = createCLIPortEntry(managementModeHttpPort, Protocol.HTTP);
            cliEntries.put(entry.getId(), entry);
        }
        return cliEntries;
    }

    private ConfigurationEntry createCLIPortEntry(int port, Protocol protocol)
    {
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(Port.PORT, port);
        attributes.put(Port.PROTOCOLS, Collections.singleton(protocol));
        attributes.put(Port.NAME, MANAGEMENT_MODE_PORT_PREFIX + protocol.name());
        if (protocol != Protocol.RMI)
        {
            attributes.put(Port.AUTHENTICATION_PROVIDER, MANAGEMENT_MODE_AUTH_PROVIDER);
        }
        ConfigurationEntry portEntry = new ConfigurationEntry(UUID.randomUUID(), PORT_TYPE, attributes,
                Collections.<UUID> emptySet(), this);
        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("Add management mode port configuration " + portEntry + " for port " + port + " and protocol "
                    + protocol);
        }
        return portEntry;
    }

    private ConfigurationEntry createRootWithCLIEntries(ConfigurationEntry storeRoot)
    {
        Set<UUID> childrenIds = new HashSet<UUID>(storeRoot.getChildrenIds());
        if (!_cliEntries.isEmpty())
        {
            childrenIds.addAll(_cliEntries.keySet());
        }
        ConfigurationEntry root = new ConfigurationEntry(storeRoot.getId(), storeRoot.getType(), new HashMap<String, Object>(
                storeRoot.getAttributes()), childrenIds, this);
        return root;
    }

    private Map<UUID, Object> quiesceEntries(ConfigurationEntry storeRoot, BrokerOptions options)
    {
        Map<UUID, Object> quiescedEntries = new HashMap<UUID, Object>();
        Set<UUID> childrenIds;
        int managementModeRmiPort = options.getManagementModeRmiPort();
        int managementModeConnectorPort = options.getManagementModeConnectorPort();
        int managementModeHttpPort = options.getManagementModeHttpPort();
        childrenIds = storeRoot.getChildrenIds();
        for (UUID id : childrenIds)
        {
            ConfigurationEntry entry = _store.getEntry(id);
            String entryType = entry.getType();
            Map<String, Object> attributes = entry.getAttributes();
            boolean quiesce = false;
            if (VIRTUAL_HOST_TYPE.equals(entryType) && options.isManagementModeQuiesceVirtualHosts())
            {
                quiesce = true;
            }
            else if (ACCESS_CONTROL_PROVIDER_TYPE.equals(entryType))
            {
                quiesce = true;
            }
            else if (PORT_TYPE.equalsIgnoreCase(entryType))
            {
                if (attributes == null)
                {
                    throw new IllegalConfigurationException("Port attributes are not set in " + entry);
                }
                Set<Protocol> protocols = getPortProtocolsAttribute(attributes);
                if (protocols == null)
                {
                    quiesce = true;
                }
                else
                {
                    for (Protocol protocol : protocols)
                    {
                        switch (protocol)
                        {
                        case JMX_RMI:
                            quiesce = managementModeConnectorPort > 0 || managementModeRmiPort > 0;
                            break;
                        case RMI:
                            quiesce = managementModeRmiPort > 0;
                            break;
                        case HTTP:
                        case HTTPS:
                            quiesce = managementModeHttpPort > 0;
                            break;
                        default:
                            quiesce = true;
                        }
                    }
                }
            }
            if (quiesce)
            {
                if (LOGGER.isDebugEnabled())
                {
                    LOGGER.debug("Management mode quiescing entry " + entry);
                }

                // save original state
                quiescedEntries.put(entry.getId(), attributes.get(ATTRIBUTE_STATE));
            }
        }
        return quiescedEntries;
    }

    private Set<Protocol> getPortProtocolsAttribute(Map<String, Object> attributes)
    {
        Object object = attributes.get(Port.PROTOCOLS);
        if (object == null)
        {
            return null;
        }
        return MapValueConverter.getEnumSetAttribute(Port.PROTOCOLS, attributes, Protocol.class);
    }

    private ConfigurationEntry createEntryWithState(ConfigurationEntry entry, Object state)
    {
        Map<String, Object> attributes = new HashMap<String, Object>(entry.getAttributes());
        if (state == null)
        {
            attributes.remove(ATTRIBUTE_STATE);
        }
        else
        {
            attributes.put(ATTRIBUTE_STATE, state);
        }
        Set<UUID> originalChildren = entry.getChildrenIds();
        Set<UUID> children = null;
        if (originalChildren != null)
        {
            children = new HashSet<UUID>(originalChildren);
        }
        return new ConfigurationEntry(entry.getId(), entry.getType(), attributes, children, entry.getStore());
    }

}