summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/CommandLineOptionsHandler.java
blob: 86ee58256db1a7e9cda32afeef8423e4dd279cb7 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 *
 * 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.configuration.store;

import static org.apache.qpid.transport.ConnectionSettings.WILDCARD_ADDRESS;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.qpid.server.BrokerOptions;
import org.apache.qpid.server.ProtocolExclusion;
import org.apache.qpid.server.ProtocolInclusion;
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.Port;
import org.apache.qpid.server.model.Protocol;
import org.apache.qpid.server.model.Protocol.ProtocolType;
import org.apache.qpid.server.model.Transport;
import org.apache.qpid.server.model.UUIDGenerator;
import org.apache.qpid.server.util.MapValueConverter;

public class CommandLineOptionsHandler implements ConfigurationEntryStore
{
    private static final String PORT_TYPE_NAME = Port.class.getSimpleName();
    private static final EnumSet<Protocol> AMQP_PROTOCOLS = EnumSet.of(Protocol.AMQP_0_8, Protocol.AMQP_0_9, Protocol.AMQP_0_9_1,
            Protocol.AMQP_0_10, Protocol.AMQP_1_0);

    private final ConfigurationEntryStore _store;
    private final Map<UUID, MutableConfigurationEntry> _cliEntries;
    private final ConfigurationEntry _root;

    public CommandLineOptionsHandler(BrokerOptions options, ConfigurationEntryStore store)
    {
        super();
        _store = store;
        List<ConfigurationAction> configActions = buildConfigurationActions(options);
        if (!configActions.isEmpty())
        {
            ConfigurationEntry root = store.getRootEntry();
            Set<UUID> rootChildren = new HashSet<UUID>(root.getChildrenIds());
            Collection<ConfigurationEntry> storePorts = root.getChildren().get(PORT_TYPE_NAME);
            Collection<MutableConfigurationEntry> mutableConfigurationEntries = new ArrayList<MutableConfigurationEntry>();
            for (ConfigurationEntry configurationEntry : storePorts)
            {
                mutableConfigurationEntries.add(new PortMutableConfigurationEntry(configurationEntry));
                rootChildren.remove(configurationEntry.getId());
            }
            for (ConfigurationAction configurationAction : configActions)
            {
                mutableConfigurationEntries = configurationAction.apply(mutableConfigurationEntries);
            }
            _cliEntries = new HashMap<UUID, MutableConfigurationEntry>();
            for (MutableConfigurationEntry mutableConfigurationEntry : mutableConfigurationEntries)
            {
                _cliEntries.put(mutableConfigurationEntry.getId(), mutableConfigurationEntry);
                if (!mutableConfigurationEntry.isDisabled())
                {
                    rootChildren.add(mutableConfigurationEntry.getId());
                }
            }
            _root = new ConfigurationEntry(root.getId(), root.getType(), root.getAttributes(), rootChildren, this);
        }
        else
        {
            _root = null;
            _cliEntries = null;
        }
    }

    public List<ConfigurationAction> buildConfigurationActions(BrokerOptions options)
    {
        List<ConfigurationAction> actions = new ArrayList<ConfigurationAction>();

        Set<Integer> optionsAmqpPorts = options.getPorts();
        for (Integer port : optionsAmqpPorts)
        {
            actions.add(new AddAmqpPortAction(port, Transport.TCP, AMQP_PROTOCOLS, this));
        }
        Set<Integer> optionsAmqpSslPorts = options.getSSLPorts();
        for (Integer port : optionsAmqpSslPorts)
        {
            actions.add(new AddAmqpPortAction(port, Transport.SSL, AMQP_PROTOCOLS, this));
        }
        Integer jmxConnectorPort = options.getJmxPortConnectorServer();
        if (jmxConnectorPort != null)
        {
            actions.add(new AddJmxPortAction(jmxConnectorPort, Protocol.JMX_RMI, this));
        }
        Integer jmxRegistryPort = options.getJmxPortRegistryServer();
        if (jmxRegistryPort != null)
        {
            actions.add(new AddJmxPortAction(jmxRegistryPort, Protocol.RMI, this));
        }

        Set<Integer> exclude_1_0 = options.getExcludedPorts(ProtocolExclusion.v1_0);
        if (!exclude_1_0.isEmpty())
        {
            actions.add(new ProtocolExcludeAction(Protocol.AMQP_1_0, exclude_1_0));
        }
        Set<Integer> include_1_0 = options.getIncludedPorts(ProtocolInclusion.v1_0);
        if (!include_1_0.isEmpty())
        {
            actions.add(new ProtocolIncludeAction(Protocol.AMQP_1_0, include_1_0));
        }
        Set<Integer> exclude_0_10 = options.getExcludedPorts(ProtocolExclusion.v0_10);
        if (!exclude_0_10.isEmpty())
        {
            actions.add(new ProtocolExcludeAction(Protocol.AMQP_0_10, exclude_0_10));
        }
        Set<Integer> include_0_10 = options.getIncludedPorts(ProtocolInclusion.v0_10);
        if (!include_0_10.isEmpty())
        {
            actions.add(new ProtocolIncludeAction(Protocol.AMQP_0_10, include_0_10));
        }
        Set<Integer> exclude_0_9_1 = options.getExcludedPorts(ProtocolExclusion.v0_9_1);
        if (!exclude_0_9_1.isEmpty())
        {
            actions.add(new ProtocolExcludeAction(Protocol.AMQP_0_9_1, exclude_0_9_1));
        }
        Set<Integer> include_0_9_1 = options.getIncludedPorts(ProtocolInclusion.v0_9_1);
        if (!include_0_9_1.isEmpty())
        {
            actions.add(new ProtocolIncludeAction(Protocol.AMQP_0_9_1, include_0_9_1));
        }
        Set<Integer> exclude_0_9 = options.getExcludedPorts(ProtocolExclusion.v0_9);
        if (!exclude_0_9.isEmpty())
        {
            actions.add(new ProtocolExcludeAction(Protocol.AMQP_0_9, exclude_0_9));
        }
        Set<Integer> include_0_9 = options.getIncludedPorts(ProtocolInclusion.v0_9);
        if (!include_0_9.isEmpty())
        {
            actions.add(new ProtocolIncludeAction(Protocol.AMQP_0_9, include_0_9));
        }
        Set<Integer> exclude_0_8 = options.getExcludedPorts(ProtocolExclusion.v0_8);
        if (!exclude_0_8.isEmpty())
        {
            actions.add(new ProtocolExcludeAction(Protocol.AMQP_0_8, exclude_0_8));
        }
        Set<Integer> include_0_8 = options.getIncludedPorts(ProtocolInclusion.v0_8);
        if (!include_0_8.isEmpty())
        {
            actions.add(new ProtocolIncludeAction(Protocol.AMQP_0_8, include_0_8));
        }

        String bindingAddress = options.getBind();
        if (bindingAddress != null)
        {
            actions.add(new BindingAddressAction(bindingAddress));
        }
        return actions;
    }

    @Override
    public synchronized ConfigurationEntry getRootEntry()
    {
        if (_root == null)
        {
            return _store.getRootEntry();
        }
        return _root;
    }

    @Override
    public synchronized ConfigurationEntry getEntry(UUID id)
    {
        if (_cliEntries != null)
        {
            if (id == _root.getId())
            {
                return _root;
            }
            MutableConfigurationEntry entry = _cliEntries.get(id);
            if (entry != null && !entry.isDisabled())
            {
                return entry.toConfigurationEntry();
            }
        }
        return _store.getEntry(id);
    }

    @Override
    public synchronized void save(ConfigurationEntry... entries)
    {
        if (_root == null)
        {
            _store.save(entries);
        }
        else
        {
            List<ConfigurationEntry> storeEntries = new ArrayList<ConfigurationEntry>();
            List<ConfigurationEntry> nonStoreEntries = new ArrayList<ConfigurationEntry>();
            for (ConfigurationEntry entry : entries)
            {
                if (entry.getId() == _root.getId())
                {
                    // remove command line ports from broker children
                    Set<UUID> childrenIds = new HashSet<UUID>(entry.getChildrenIds());
                    for (MutableConfigurationEntry substitutedEntry : _cliEntries.values())
                    {
                        ConfigurationEntry original = substitutedEntry.getOriginal();
                        if (original == null)
                        {
                            childrenIds.remove(substitutedEntry.getId());
                        }
                        else
                        {
                            childrenIds.add(substitutedEntry.getId());
                        }
                    }
                    entry = new ConfigurationEntry(_root.getId(), _root.getType(), _root.getAttributes(), childrenIds, _store);
                    storeEntries.add(entry);
                }
                MutableConfigurationEntry override = _cliEntries.get(entry.getId());
                if (override == null)
                {
                    storeEntries.add(entry);
                }
                if (override != null)
                {
                    if (override.isDisabled())
                    {
                        throw new IllegalConfigurationException("Cannot store entry which was overridden by command line options: "  + entry);
                    }
                    nonStoreEntries.add(entry);
                }
            }
            _store.save(storeEntries.toArray(new ConfigurationEntry[storeEntries.size()]));
            for (ConfigurationEntry entry : nonStoreEntries)
            {
                MutableConfigurationEntry override = _cliEntries.get(entry.getId());
                override.setAttributes(entry.getAttributes());
            }
        }
    }

    @Override
    public synchronized UUID[] remove(UUID... entryIds)
    {
        if (_root == null)
        {
            return _store.remove(entryIds);
        }
        else
        {
            Set<UUID> deleted = new HashSet<UUID>();
            List<UUID> storeEntries = new ArrayList<UUID>();
            List<UUID> nonStoreEntries = new ArrayList<UUID>();
            for (UUID entryId : entryIds)
            {
                if (entryId == _root.getId())
                {
                    throw new IllegalConfigurationException("Cannot remove root entry");
                }
                MutableConfigurationEntry override = _cliEntries.get(entryId);
                if (override == null || override.isDisabled())
                {
                    storeEntries.add(entryId);
                }
                if (override != null)
                {
                    nonStoreEntries.add(entryId);
                }
            }
            UUID[] result = _store.remove(storeEntries.toArray(new UUID[storeEntries.size()]));
            if (result != null && result.length > 0)
            {
                deleted.addAll(Arrays.asList(result));
            }
            for (UUID entryId : nonStoreEntries)
            {
                MutableConfigurationEntry entry = _cliEntries.remove(entryId);
                if (entry != null)
                {
                    deleted.add(entryId);
                }
            }
            return deleted.toArray(new UUID[deleted.size()]);
        }
    }

    public static class MutableConfigurationEntry
    {
        private final UUID _id;
        private final String _type;
        private final Map<String, Object> _attributes;
        private final ConfigurationEntryStore _store;
        private final ConfigurationEntry _original;
        private boolean _disabled;

        private MutableConfigurationEntry(ConfigurationEntry original, UUID id, String type, ConfigurationEntryStore store)
        {
            super();
            _original = original;
            _attributes = new HashMap<String, Object>();
            _id = id;
            _type = type;
            _store = store;
            if (original != null)
            {
                Map<String, Object> originalAttributes = original.getAttributes();
                if (originalAttributes != null)
                {
                    _attributes.putAll(originalAttributes);
                }
            }
        }

        public MutableConfigurationEntry(ConfigurationEntry original)
        {
            this(original, original.getId(), original.getType(), original.getStore());
        }

        public MutableConfigurationEntry(UUID id, String type, ConfigurationEntryStore store)
        {
            this(null, id, type, store);
        }

        public ConfigurationEntry getOriginal()
        {
            return _original;
        }

        public void setAttribute(String name, Object value)
        {
            _attributes.put(name, value);
        }

        public void setAttributes(Map<String, Object> attributes)
        {
            for (Map.Entry<String, Object> attribute : attributes.entrySet())
            {
                _attributes.put(attribute.getKey(), attribute.getValue());
            }
        }

        public Map<String, Object> getAttributes()
        {
            return _attributes;
        }

        public ConfigurationEntry toConfigurationEntry()
        {
            if (_original == null)
            {
                return new ConfigurationEntry(_id, _type, _attributes, Collections.<UUID> emptySet(), _store);
            }
            return new ConfigurationEntry(_original.getId(), _original.getType(), _attributes, _original.getChildrenIds(),
                    _original.getStore());
        }

        public String getType()
        {
            return _type;
        }

        public void disable()
        {
            _disabled = true;
        }

        public boolean isDisabled()
        {
            return _disabled;
        }

        public UUID getId()
        {
            return _id;
        }

        @Override
        public String toString()
        {
            return "MutableConfigurationEntry [_id=" + _id + ", _type=" + _type + ", _attributes=" + _attributes + ", _disabled="
                    + _disabled + ", _store=" + _store + ", _original=" + _original + "]";
        }


    }

    public static class PortMutableConfigurationEntry extends MutableConfigurationEntry
    {
        public PortMutableConfigurationEntry(ConfigurationEntry original)
        {
            super(original);
            if (!PORT_TYPE_NAME.equals(original.getType()))
            {
                throw new IllegalConfigurationException("Not a valid port entry");
            }
        }

        public PortMutableConfigurationEntry(UUID id, String type, ConfigurationEntryStore store)
        {
            super(id, type, store);
            if (!PORT_TYPE_NAME.equals(type))
            {
                throw new IllegalConfigurationException("Not a valid port entry");
            }
        }

        public int getPortAttribute()
        {
            Map<String, Object> attributes = getAttributes();
            Object portAttribute = attributes.get(Port.PORT);
            if (portAttribute == null || "".equals(portAttribute))
            {
                throw new IllegalConfigurationException("Port attribute is not set for port entry " + attributes);
            }
            int port = 0;
            if (portAttribute instanceof String)
            {
                try
                {
                    port = Integer.parseInt((String) portAttribute);
                }
                catch (Exception e)
                {
                    throw new IllegalConfigurationException("Port attribute is not an integer: " + portAttribute);
                }
            }
            else if (portAttribute instanceof Number)
            {
                port = ((Number) portAttribute).intValue();
            }
            return port;
        }

        public Set<Protocol> getProtocolsAttribute()
        {
            Map<String, Object> attributes = getAttributes();
            Set<Protocol> protocols = MapValueConverter.getEnumSetAttribute(Port.PROTOCOLS, attributes, Protocol.class);
            if (protocols == null || protocols.isEmpty())
            {
                throw new IllegalConfigurationException("Protocols attribute is not set for port entry " + attributes);
            }
            return protocols;
        }

        public Set<Transport> getTransportsAttribute()
        {
            Map<String, Object> attributes = getAttributes();
            Set<Transport> transports = MapValueConverter.getEnumSetAttribute(Port.TRANSPORTS, attributes, Transport.class);
            if (transports == null || transports.isEmpty())
            {
                throw new IllegalConfigurationException("Transports attribute is not set for port entry " + attributes);
            }
            return transports;
        }

        public void setProtocolsAttribute(Set<Protocol> protocols)
        {
            setAttribute(Port.PROTOCOLS, protocols);
        }
    }

    public interface ConfigurationAction
    {
        Collection<MutableConfigurationEntry> apply(Collection<MutableConfigurationEntry> entries);
    }

    public static abstract class PortConfigurationAction implements ConfigurationAction
    {
        @Override
        public Collection<MutableConfigurationEntry> apply(Collection<MutableConfigurationEntry> entries)
        {
            for (MutableConfigurationEntry configurationEntry : entries)
            {
                if (!configurationEntry.isDisabled() && configurationEntry instanceof PortMutableConfigurationEntry)
                {
                    onPortConfigurationEntry((PortMutableConfigurationEntry)configurationEntry);
                }
            }
            return entries;
        }

        public abstract void onPortConfigurationEntry(PortMutableConfigurationEntry configurationEntry);

    }

    public static class BindingAddressAction extends PortConfigurationAction
    {
        private String _bindingAddress;
        private String _bindAddressOverride;

        public BindingAddressAction(String bindingAddress)
        {
            _bindingAddress = bindingAddress;
            if (WILDCARD_ADDRESS.equals(bindingAddress))
            {
                _bindAddressOverride = null;
            }
            else
            {
                _bindAddressOverride = _bindingAddress;
            }
        }

        @Override
        public void onPortConfigurationEntry(PortMutableConfigurationEntry configurationEntry)
        {
            Set<Protocol> protocols = configurationEntry.getProtocolsAttribute();
            if (protocols.size() > 0)
            {
                Protocol protocol = protocols.iterator().next();
                if (protocol.isAMQP())
                {
                    configurationEntry.setAttribute(Port.BINDING_ADDRESS, _bindAddressOverride);
                }
            }
        }
    }

    public static class ProtocolExcludeAction extends PortConfigurationAction
    {
        private Protocol _protocol;
        private Collection<Integer> _excludedPorts;

        public ProtocolExcludeAction(Protocol protocol, Collection<Integer> excludedPorts)
        {
            super();
            _protocol = protocol;
            _excludedPorts = excludedPorts;
        }

        @Override
        public void onPortConfigurationEntry(PortMutableConfigurationEntry configurationEntry)
        {
            int port = configurationEntry.getPortAttribute();
            if (_excludedPorts.contains(port))
            {
                Set<Protocol> protocols = configurationEntry.getProtocolsAttribute();
                if (protocols.contains(_protocol))
                {
                    protocols.remove(_protocol);
                    configurationEntry.setProtocolsAttribute(protocols);
                }
            }
        }
    }

    public static class ProtocolIncludeAction extends PortConfigurationAction
    {
        private Protocol _protocol;
        private Collection<Integer> _includedPorts;

        public ProtocolIncludeAction(Protocol protocol, Collection<Integer> includedPorts)
        {
            super();
            _protocol = protocol;
            _includedPorts = includedPorts;
        }

        @Override
        public void onPortConfigurationEntry(PortMutableConfigurationEntry configurationEntry)
        {
            if (!configurationEntry.isDisabled())
            {
                int port = configurationEntry.getPortAttribute();
                if (_includedPorts.contains(port))
                {
                    Set<Protocol> protocols = configurationEntry.getProtocolsAttribute();
                    if (!protocols.contains(_protocol))
                    {
                        protocols.add(_protocol);
                        configurationEntry.setProtocolsAttribute(protocols);
                    }
                }
            }
        }
    }

    public static class AddAmqpPortAction extends PortConfigurationAction
    {
        private int _port;
        private Transport _transport;
        private Set<Protocol> _protocols;
        private ConfigurationEntryStore _store;

        public AddAmqpPortAction(int port, Transport transport, Set<Protocol> protocols,
                ConfigurationEntryStore store)
        {
            super();
            _port = port;
            _transport = transport;
            _protocols = protocols;
            _store = store;
        }

        @Override
        public Collection<MutableConfigurationEntry> apply(Collection<MutableConfigurationEntry> entries)
        {
            MutableConfigurationEntry entry = findPortEntryWithTheSamePort(entries, _port);
            if (entry == null)
            {
                // disable all store port entries with the same protocol type
                // and transport
                super.apply(entries);
            }
            else
            {
                entry.disable();
            }
            String portName = getPortName(_port);
            UUID id = UUIDGenerator.generateBrokerChildUUID(PORT_TYPE_NAME, portName);
            PortMutableConfigurationEntry newEntry = new PortMutableConfigurationEntry(id, PORT_TYPE_NAME, _store);
            if (entry != null)
            {
                newEntry.setAttributes(entry.getAttributes());
            }
            newEntry.setAttribute(Port.NAME, portName);
            newEntry.setAttribute(Port.TRANSPORTS, Collections.singleton(_transport));
            newEntry.setAttribute(Port.PROTOCOLS, _protocols);
            newEntry.setAttribute(Port.PORT, _port);
            List<MutableConfigurationEntry> newEntries = new ArrayList<MutableConfigurationEntry>(entries);
            newEntries.add(newEntry);
            return newEntries;
        }

        private MutableConfigurationEntry findPortEntryWithTheSamePort(Collection<MutableConfigurationEntry> entries, int port)
        {
            MutableConfigurationEntry entry = null;
            for (MutableConfigurationEntry configurationEntry : entries)
            {
                if (configurationEntry instanceof PortMutableConfigurationEntry)
                {
                    int entryPort = ((PortMutableConfigurationEntry)configurationEntry).getPortAttribute();
                    if (port == entryPort)
                    {
                        entry = configurationEntry;
                        break;
                    }
                }
            }
            return entry;
        }

        private String getPortName(Integer amqpPort)
        {
            return "cliAmqpPort" + amqpPort;
        }

        @Override
        public void onPortConfigurationEntry(PortMutableConfigurationEntry configurationEntry)
        {
            // disable only configuration entry if it has original attached
            if (!configurationEntry.isDisabled() && configurationEntry.getOriginal() != null)
            {
                Set<Transport> transports = configurationEntry.getTransportsAttribute();

                // disable only configuration entry with the same transports
                if (transports.contains(_transport))
                {
                    Set<Protocol> protocols = configurationEntry.getProtocolsAttribute();
                    for (Protocol protocol : protocols)
                    {
                        if (protocol.getProtocolType() == ProtocolType.AMQP)
                        {
                            // disable only configuration entry with the same protocol type
                            configurationEntry.disable();
                            break;
                        }
                    }
                }
            }
        }
    }

    public static class AddJmxPortAction extends PortConfigurationAction
    {
        private int _port;
        private ConfigurationEntryStore _store;
        private Protocol _protocol;

        public AddJmxPortAction(int port, Protocol protocol, ConfigurationEntryStore store)
        {
            super();
            _port = port;
            _protocol = protocol;
            _store = store;
        }

        @Override
        public Collection<MutableConfigurationEntry> apply(Collection<MutableConfigurationEntry> entries)
        {
            Collection<MutableConfigurationEntry> sameProtocolEntries = findPortEntriesWithTheSameProtocol(entries, _protocol);
            super.apply(sameProtocolEntries);
            String portName = getPortName(_port);
            UUID id = UUIDGenerator.generateBrokerChildUUID(PORT_TYPE_NAME, portName);
            PortMutableConfigurationEntry newEntry = new PortMutableConfigurationEntry(id, PORT_TYPE_NAME, _store);
            newEntry.setAttribute(Port.NAME, portName);
            newEntry.setAttribute(Port.TRANSPORTS, Collections.singleton(Transport.TCP));
            newEntry.setAttribute(Port.PROTOCOLS, Collections.singleton(_protocol));
            newEntry.setAttribute(Port.PORT, _port);
            List<MutableConfigurationEntry> newEntries = new ArrayList<MutableConfigurationEntry>(entries);
            newEntries.add(newEntry);
            return newEntries;
        }

        private Collection<MutableConfigurationEntry> findPortEntriesWithTheSameProtocol(Collection<MutableConfigurationEntry> entries, Protocol protocol)
        {
            List<MutableConfigurationEntry> foundEntries = new ArrayList<MutableConfigurationEntry>();
            for (MutableConfigurationEntry configurationEntry : entries)
            {
                if (configurationEntry instanceof PortMutableConfigurationEntry)
                {
                    Set<Protocol> protocols = ((PortMutableConfigurationEntry)configurationEntry).getProtocolsAttribute();
                    if (protocols.contains(protocol))
                    {
                        foundEntries.add(configurationEntry);
                    }
                }
            }
            return foundEntries;
        }

        private String getPortName(Integer amqpPort)
        {
            return "cliJmxPort" + amqpPort;
        }

        @Override
        public void onPortConfigurationEntry(PortMutableConfigurationEntry configurationEntry)
        {
            if (!configurationEntry.isDisabled())
            {
                configurationEntry.disable();
            }
        }
    }
}