diff options
| author | Alex Rudyy <orudyy@apache.org> | 2013-02-22 00:14:53 +0000 |
|---|---|---|
| committer | Alex Rudyy <orudyy@apache.org> | 2013-02-22 00:14:53 +0000 |
| commit | 35a5dcbeab7bd020dad9ad22c893b799341c444c (patch) | |
| tree | e3b33601d50c2bf40099ab5c51dd49e694506b33 /qpid/java | |
| parent | dc1aecebb61ab371dc5b94dc637612fe9c92ff44 (diff) | |
| download | qpid-python-35a5dcbeab7bd020dad9ad22c893b799341c444c.tar.gz | |
[QPID-4594] Add command line option to start java broker in a management mode and options to set JMX and HTTP ports in management mode
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1448866 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java')
16 files changed, 919 insertions, 15 deletions
diff --git a/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/HttpManagement.java b/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/HttpManagement.java index 59dbc6e530..c2ac675e20 100644 --- a/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/HttpManagement.java +++ b/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/HttpManagement.java @@ -222,6 +222,10 @@ public class HttpManagement extends AbstractPluginAdapter Server server = new Server(); for (Port port : ports) { + if (State.QUIESCED.equals(port.getActualState())) + { + continue; + } final Collection<Protocol> protocols = port.getProtocols(); Connector connector = null; diff --git a/qpid/java/broker-plugins/management-jmx/src/main/java/org/apache/qpid/server/jmx/JMXManagement.java b/qpid/java/broker-plugins/management-jmx/src/main/java/org/apache/qpid/server/jmx/JMXManagement.java index f307f5118a..45d642e4f7 100644 --- a/qpid/java/broker-plugins/management-jmx/src/main/java/org/apache/qpid/server/jmx/JMXManagement.java +++ b/qpid/java/broker-plugins/management-jmx/src/main/java/org/apache/qpid/server/jmx/JMXManagement.java @@ -134,6 +134,11 @@ public class JMXManagement extends AbstractPluginAdapter implements Configuratio Collection<Port> ports = _broker.getPorts(); for (Port port : ports) { + if (State.QUIESCED.equals(port.getActualState())) + { + continue; + } + if(isRegistryPort(port)) { registryPort = port; diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java index 32080e8e67..54dcf0543d 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java @@ -33,6 +33,7 @@ import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.configuration.ConfigurationEntryStore; import org.apache.qpid.server.configuration.BrokerConfigurationStoreCreator; +import org.apache.qpid.server.configuration.store.ManagementModeStoreHandler; import org.apache.qpid.server.logging.SystemOutMessageLogger; import org.apache.qpid.server.logging.actors.BrokerActor; import org.apache.qpid.server.logging.actors.CurrentActor; @@ -131,6 +132,11 @@ public class Broker ConfigurationEntryStore store = storeCreator.createStore(storeLocation, storeType, options.getInitialConfigurationStoreLocation(), options.getInitialConfigurationStoreLocation()); + if (options.isManagementMode()) + { + store = new ManagementModeStoreHandler(store, options); + } + _applicationRegistry = new ApplicationRegistry(store); try { diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java index 11eebf9865..57e401e608 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java @@ -35,6 +35,11 @@ public class BrokerOptions private String _initialConfigurationStoreLocation; private String _initialConfigurationStoreType = DEFAULT_STORE_TYPE; + private boolean _managementMode; + private int _managementModeRmiPort; + private int _managementModeConnectorPort; + private int _managementModeHttpPort; + public String getLogConfigFile() { return _logConfigFile; @@ -99,4 +104,43 @@ public class BrokerOptions return _initialConfigurationStoreType; } + public boolean isManagementMode() + { + return _managementMode; + } + + public void setManagementMode(boolean managementMode) + { + _managementMode = managementMode; + } + + public int getManagementModeRmiPort() + { + return _managementModeRmiPort; + } + + public void setManagementModeRmiPort(int managementModeRmiPort) + { + _managementModeRmiPort = managementModeRmiPort; + } + + public int getManagementModeConnectorPort() + { + return _managementModeConnectorPort; + } + + public void setManagementModeConnectorPort(int managementModeConnectorPort) + { + _managementModeConnectorPort = managementModeConnectorPort; + } + + public int getManagementModeHttpPort() + { + return _managementModeHttpPort; + } + + public void setManagementModeHttpPort(int managementModeHttpPort) + { + _managementModeHttpPort = managementModeHttpPort; + } }
\ No newline at end of file diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java index 245b0cf1fb..4927956c6c 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java @@ -65,6 +65,15 @@ public class Main .withDescription("monitor the log file configuration file for changes. Units are seconds. " + "Zero means do not check for changes.").withLongOpt("logwatch").create("w"); + private static final Option OPTION_MANAGEMENT_MODE = OptionBuilder.withDescription("start broker in a management mode") + .withLongOpt("management-mode").create("mm"); + private static final Option OPTION_RMI_PORT = OptionBuilder.withArgName("port").hasArg() + .withDescription("override jmx rmi port in management mode").withLongOpt("jmxregistryport").create("rmi"); + private static final Option OPTION_CONNECTOR_PORT = OptionBuilder.withArgName("port").hasArg() + .withDescription("override jmx connector port in management mode").withLongOpt("jmxconnectorport").create("jmxrmi"); + private static final Option OPTION_HTTP_PORT = OptionBuilder.withArgName("port").hasArg() + .withDescription("override web management port in management mode").withLongOpt("httpport").create("http"); + private static final Options OPTIONS = new Options(); static @@ -77,6 +86,10 @@ public class Main OPTIONS.addOption(OPTION_LOG_WATCH); OPTIONS.addOption(OPTION_INITIAL_CONFIGURATION_STORE_PATH); OPTIONS.addOption(OPTION_INITIAL_CONFIGURATION_STORE_TYPE); + OPTIONS.addOption(OPTION_MANAGEMENT_MODE); + OPTIONS.addOption(OPTION_RMI_PORT); + OPTIONS.addOption(OPTION_CONNECTOR_PORT); + OPTIONS.addOption(OPTION_HTTP_PORT); } protected CommandLine _commandLine; @@ -192,6 +205,26 @@ public class Main options.setInitialConfigurationStoreType(initailConfigurationStoreType); } + boolean managmentMode = _commandLine.hasOption(OPTION_MANAGEMENT_MODE.getOpt()); + if (managmentMode) + { + options.setManagementMode(true); + String rmiPort = _commandLine.getOptionValue(OPTION_RMI_PORT.getOpt()); + if (rmiPort != null) + { + options.setManagementModeRmiPort(Integer.parseInt(rmiPort)); + } + String connectorPort = _commandLine.getOptionValue(OPTION_CONNECTOR_PORT.getOpt()); + if (connectorPort != null) + { + options.setManagementModeConnectorPort(Integer.parseInt(connectorPort)); + } + String httpPort = _commandLine.getOptionValue(OPTION_HTTP_PORT.getOpt()); + if (httpPort != null) + { + options.setManagementModeHttpPort(Integer.parseInt(httpPort)); + } + } setExceptionHandler(); startBroker(options); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntry.java index 4771c4af6d..8afb1af24d 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntry.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntry.java @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -197,17 +196,8 @@ public class ConfigurationEntry @Override public String toString() { - return "ConfigurationEntry [_id=" + _id + ", _type=" + _type + ", _attributes=" + _attributes + ", _childrenIds=" + return "ConfigurationEntry [id=" + _id + ", type=" + _type + ", attributes=" + _attributes + ", childrenIds=" + _childrenIds + "]"; } - public Object setAttribute(String name, Object value) - { - return _attributes.put(name, value); - } - - public ConfigurationEntry clone() - { - return new ConfigurationEntry(_id, _type, new HashMap<String, Object>(_attributes), new HashSet<UUID>(_childrenIds), _store); - } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandler.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandler.java new file mode 100644 index 0000000000..e7c474bf55 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandler.java @@ -0,0 +1,327 @@ +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.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 ATTRIBUTE_STATE = VirtualHost.STATE; + + 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 void open(String storeLocation) + { + throw new IllegalStateException("The store should be already opened"); + } + + @Override + public void open(String storeLocation, String initialStoreLocation) + { + throw new IllegalStateException("The store should be already opened"); + } + + @Override + public void open(String storeLocation, ConfigurationEntryStore initialStore) + { + throw new IllegalStateException("The store should be already opened"); + } + + @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); + } + } + + 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()); + 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)) + { + 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()); + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractAdapter.java index 387b1b6b58..73e1f1e970 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractAdapter.java @@ -286,7 +286,7 @@ abstract class AbstractAdapter implements ConfiguredObject @Override public String toString() { - return getClass().getSimpleName() + " [id=" + _id + "]"; + return getClass().getSimpleName() + " [id=" + _id + ", name=" + getName() + "]"; } @SuppressWarnings("unchecked") diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java index d6c18a1141..533ecfe937 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java @@ -694,6 +694,14 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, Configurat Collection<? extends ConfiguredObject> adapters = configuredObjectMap.values(); for (ConfiguredObject configuredObject : adapters) { + if (State.ACTIVE.equals(desiredState) && State.QUIESCED.equals(configuredObject.getActualState())) + { + if (LOGGER.isDebugEnabled()) + { + LOGGER.debug(configuredObject + " cannot be activated as it is " +State.QUIESCED); + } + continue; + } try { configuredObject.setDesiredState(currentState, desiredState); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortAdapter.java index cb166d220e..c4a531c923 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortAdapter.java @@ -155,7 +155,12 @@ public class PortAdapter extends AbstractAdapter implements Port @Override public State getActualState() { - return State.ACTIVE; + State state = (State)super.getAttribute(STATE); + if (state == null) + { + return State.ACTIVE; + } + return state; } @Override diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortFactory.java index a1f72c8119..b7441b9f3b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortFactory.java @@ -35,6 +35,7 @@ import org.apache.qpid.server.model.Broker; 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.State; import org.apache.qpid.server.model.Transport; import org.apache.qpid.server.util.MapValueConverter; @@ -179,6 +180,12 @@ public class PortFactory String binding = MapValueConverter.getStringAttribute(Port.BINDING_ADDRESS, objectAttributes); attributes.put(Port.BINDING_ADDRESS, binding); } + + if (objectAttributes.containsKey(Port.STATE)) + { + State state = MapValueConverter.getEnumAttribute(State.class, Port.STATE, objectAttributes); + attributes.put(Port.STATE, state); + } return attributes; } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAdapter.java index 3e6f73d23e..1d50be279f 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAdapter.java @@ -88,6 +88,7 @@ public final class VirtualHostAdapter extends AbstractAdapter implements Virtual put(STORE_PATH, String.class); put(STORE_TYPE, String.class); put(CONFIG_PATH, String.class); + put(STATE, State.class); }}); private org.apache.qpid.server.virtualhost.VirtualHost _virtualHost; @@ -404,11 +405,17 @@ public final class VirtualHostAdapter extends AbstractAdapter implements Virtual throw new IllegalStateException(); } + @Override public State getActualState() { if (_virtualHost == null) { - return State.INITIALISING; + State state = (State)super.getAttribute(STATE); + if (state == null) + { + return State.INITIALISING; + } + return state; } else { @@ -757,7 +764,7 @@ public final class VirtualHostAdapter extends AbstractAdapter implements Virtual } else if(STATE.equals(name)) { - return State.ACTIVE; + return getActualState(); } else if(DURABLE.equals(name)) { diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/util/MapValueConverter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/util/MapValueConverter.java index 4732900c60..aa7b4afcae 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/util/MapValueConverter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/util/MapValueConverter.java @@ -116,6 +116,24 @@ public class MapValueConverter return getEnumAttribute(clazz, name, attributes, null); } + @SuppressWarnings({ "unchecked" }) + public static <T extends Enum<T>> T toEnum(String name, Object rawValue, Class<T> enumType) + { + if (enumType.isInstance(rawValue)) + { + return (T) rawValue; + } + else if (rawValue instanceof String) + { + return (T) Enum.valueOf(enumType, (String) rawValue); + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type " + + enumType.getSimpleName()); + } + } + public static Boolean getBooleanAttribute(String name, Map<String,Object> attributes, Boolean defaultValue) { Object obj = attributes.get(name); @@ -296,6 +314,7 @@ public class MapValueConverter return set; } + @SuppressWarnings("unchecked") public static Map<String, Object> convert(Map<String, Object> configurationAttributes, Map<String, Class<?>> attributeTypes) { Map<String, Object> attributes = new HashMap<String, Object>(); @@ -323,6 +342,12 @@ public class MapValueConverter { value = toString(rawValue); } + else if (Enum.class.isAssignableFrom(classObject)) + { + @SuppressWarnings("rawtypes") + Class<Enum> enumType = (Class<Enum>)classObject; + value = toEnum(attributeName, rawValue, enumType); + } else { throw new IllegalArgumentException("Cannot convert '" + rawValue + "' into " + classObject); @@ -332,4 +357,5 @@ public class MapValueConverter } return attributes; } + } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java index 1352ea5164..16b459b5d4 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java @@ -102,4 +102,49 @@ public class BrokerOptionsTest extends QpidTestCase _options.setInitialConfigurationStoreLocation(testConfigFile); assertEquals(testConfigFile, _options.getInitialConfigurationStoreLocation()); } + + public void testDefaultManagementMode() + { + assertEquals(false, _options.isManagementMode()); + } + + public void testOverriddenDefaultManagementMode() + { + _options.setManagementMode(true); + assertEquals(true, _options.isManagementMode()); + } + + public void testDefaultManagementModeRmiPort() + { + assertEquals(0, _options.getManagementModeRmiPort()); + } + + public void testOverriddenDefaultManagementModeRmiPort() + { + _options.setManagementModeRmiPort(5555); + assertEquals(5555, _options.getManagementModeRmiPort()); + } + + public void testDefaultManagementModeConnectorPort() + { + assertEquals(0, _options.getManagementModeConnectorPort()); + } + + public void testOverriddenDefaultManagementModeConnectorPort() + { + _options.setManagementModeConnectorPort(5555); + assertEquals(5555, _options.getManagementModeConnectorPort()); + } + + public void testDefaultManagementModeHttpPort() + { + assertEquals(0, _options.getManagementModeHttpPort()); + } + + public void testOverriddenDefaultManagementModeHttpPort() + { + _options.setManagementModeHttpPort(5555); + assertEquals(5555, _options.getManagementModeHttpPort()); + } + } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java index bef9b8a78b..cab54b1310 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java @@ -40,6 +40,11 @@ public class MainTest extends QpidTestCase assertEquals(0, options.getLogWatchFrequency()); assertEquals("json", options.getInitialConfigurationStoreType()); assertEquals(null, options.getInitialConfigurationStoreLocation()); + + assertFalse(options.isManagementMode()); + assertEquals(0, options.getManagementModeConnectorPort()); + assertEquals(0, options.getManagementModeRmiPort()); + assertEquals(0, options.getManagementModeHttpPort()); } public void testConfigurationStoreLocation() @@ -109,6 +114,57 @@ public class MainTest extends QpidTestCase } + public void testManagementMode() + { + BrokerOptions options = startDummyMain("-mm"); + assertTrue(options.isManagementMode()); + + options = startDummyMain("--management-mode"); + assertTrue(options.isManagementMode()); + } + + public void testManagementModeRmiPort() + { + BrokerOptions options = startDummyMain("-mm -rmi 7777"); + assertTrue(options.isManagementMode()); + assertEquals(7777, options.getManagementModeRmiPort()); + + options = startDummyMain("-mm --jmxregistryport 7777"); + assertTrue(options.isManagementMode()); + assertEquals(7777, options.getManagementModeRmiPort()); + + options = startDummyMain("-rmi 7777"); + assertEquals(0, options.getManagementModeRmiPort()); + } + + public void testManagementModeConnectorPort() + { + BrokerOptions options = startDummyMain("-mm -jmxrmi 8888"); + assertTrue(options.isManagementMode()); + assertEquals(8888, options.getManagementModeConnectorPort()); + + options = startDummyMain("-mm --jmxconnectorport 8888"); + assertTrue(options.isManagementMode()); + assertEquals(8888, options.getManagementModeConnectorPort()); + + options = startDummyMain("-jmxrmi 8888"); + assertEquals(0, options.getManagementModeConnectorPort()); + } + + public void testManagementModeHttpPort() + { + BrokerOptions options = startDummyMain("-mm -http 9999"); + assertTrue(options.isManagementMode()); + assertEquals(9999, options.getManagementModeHttpPort()); + + options = startDummyMain("-mm --httpport 9999"); + assertTrue(options.isManagementMode()); + assertEquals(9999, options.getManagementModeHttpPort()); + + options = startDummyMain("-http 9999"); + assertEquals(0, options.getManagementModeHttpPort()); + } + private BrokerOptions startDummyMain(String commandLine) { return (new TestMain(commandLine.split("\\s"))).getOptions(); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandlerTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandlerTest.java new file mode 100644 index 0000000000..a67daac610 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/ManagementModeStoreHandlerTest.java @@ -0,0 +1,341 @@ +package org.apache.qpid.server.configuration.store; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.any; + +import java.io.File; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.BrokerOptions; +import org.apache.qpid.server.configuration.BrokerConfigurationStoreCreator; +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.Broker; +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.test.utils.QpidTestCase; + +public class ManagementModeStoreHandlerTest extends QpidTestCase +{ + private ManagementModeStoreHandler _handler; + private BrokerOptions _options; + private ConfigurationEntryStore _store; + private ConfigurationEntry _root; + private ConfigurationEntry _portEntry; + private UUID _rootId, _portEntryId; + + protected void setUp() throws Exception + { + super.setUp(); + _rootId = UUID.randomUUID(); + _portEntryId = UUID.randomUUID(); + _store = mock(ConfigurationEntryStore.class); + _root = mock(ConfigurationEntry.class); + _portEntry = mock(ConfigurationEntry.class); + when(_store.getRootEntry()).thenReturn(_root); + when(_root.getId()).thenReturn(_rootId); + when(_portEntry.getId()).thenReturn(_portEntryId); + when(_store.getEntry(_portEntryId)).thenReturn(_portEntry); + when(_store.getEntry(_rootId)).thenReturn(_root); + when(_root.getChildrenIds()).thenReturn(Collections.singleton(_portEntryId)); + when(_portEntry.getType()).thenReturn(Port.class.getSimpleName()); + _options = new BrokerOptions(); + _handler = new ManagementModeStoreHandler(_store, _options); + } + + public void testOpenString() + { + try + { + _handler.open(TMP_FOLDER + File.separator + getTestName()); + fail("Exception should be thrown on attempt to call open method on a handler"); + } + catch (IllegalStateException e) + { + // pass + } + } + + public void testOpenStringString() + { + try + { + _handler.open(TMP_FOLDER + File.separator + getTestName(), + BrokerConfigurationStoreCreator.DEFAULT_INITIAL_STORE_LOCATION); + fail("Exception should be thrown on attempt to call open method on a handler"); + } + catch (IllegalStateException e) + { + // pass + } + } + + public void testOpenStringConfigurationEntryStore() + { + try + { + _handler.open(TMP_FOLDER + File.separator + getTestName(), _store); + fail("Exception should be thrown on attempt to call open method on a handler"); + } + catch (IllegalStateException e) + { + // pass + } + } + + public void testGetRootEntryWithEmptyOptions() + { + ConfigurationEntry root = _handler.getRootEntry(); + assertEquals("Unexpected root id", _rootId, root.getId()); + assertEquals("Unexpected children", Collections.singleton(_portEntryId), root.getChildrenIds()); + } + + public void testGetRootEntryWithHttpPortOverriden() + { + _options.setManagementModeHttpPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + ConfigurationEntry root = _handler.getRootEntry(); + assertEquals("Unexpected root id", _rootId, root.getId()); + Collection<UUID> childrenIds = root.getChildrenIds(); + assertEquals("Unexpected children size", 2, childrenIds.size()); + assertTrue("Store port entry id is not found", childrenIds.contains(_portEntryId)); + } + + public void testGetRootEntryWithRmiPortOverriden() + { + _options.setManagementModeRmiPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + ConfigurationEntry root = _handler.getRootEntry(); + assertEquals("Unexpected root id", _rootId, root.getId()); + Collection<UUID> childrenIds = root.getChildrenIds(); + assertEquals("Unexpected children size", 3, childrenIds.size()); + assertTrue("Store port entry id is not found", childrenIds.contains(_portEntryId)); + } + + public void testGetRootEntryWithConnectorPortOverriden() + { + _options.setManagementModeConnectorPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + ConfigurationEntry root = _handler.getRootEntry(); + assertEquals("Unexpected root id", _rootId, root.getId()); + Collection<UUID> childrenIds = root.getChildrenIds(); + assertEquals("Unexpected children size", 2, childrenIds.size()); + assertTrue("Store port entry id is not found", childrenIds.contains(_portEntryId)); + } + + public void testGetRootEntryWithManagementPortsOverriden() + { + _options.setManagementModeHttpPort(1000); + _options.setManagementModeRmiPort(2000); + _options.setManagementModeConnectorPort(3000); + _handler = new ManagementModeStoreHandler(_store, _options); + ConfigurationEntry root = _handler.getRootEntry(); + assertEquals("Unexpected root id", _rootId, root.getId()); + Collection<UUID> childrenIds = root.getChildrenIds(); + assertEquals("Unexpected children size", 4, childrenIds.size()); + assertTrue("Store port entry id is not found", childrenIds.contains(_portEntryId)); + } + + public void testGetEntryByRootId() + { + ConfigurationEntry root = _handler.getEntry(_rootId); + assertEquals("Unexpected root id", _rootId, root.getId()); + assertEquals("Unexpected children", Collections.singleton(_portEntryId), root.getChildrenIds()); + } + + public void testGetEntryByPortId() + { + ConfigurationEntry portEntry = _handler.getEntry(_portEntryId); + assertEquals("Unexpected entry id", _portEntryId, portEntry.getId()); + assertTrue("Unexpected children", portEntry.getChildrenIds().isEmpty()); + assertEquals("Unexpected state", State.QUIESCED, portEntry.getAttributes().get(Port.STATE)); + } + + public void testGetEntryByCLIConnectorPortId() + { + _options.setManagementModeConnectorPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + + UUID optionsPort = getOptionsPortId(); + ConfigurationEntry portEntry = _handler.getEntry(optionsPort); + assertCLIPortEntry(portEntry, optionsPort, Protocol.JMX_RMI); + } + + public void testGetEntryByCLIHttpPortId() + { + _options.setManagementModeHttpPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + + UUID optionsPort = getOptionsPortId(); + ConfigurationEntry portEntry = _handler.getEntry(optionsPort); + assertCLIPortEntry(portEntry, optionsPort, Protocol.HTTP); + } + + public void testHttpPortEntryIsQuiesced() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Port.PROTOCOLS, Collections.singleton(Protocol.HTTP)); + when(_portEntry.getAttributes()).thenReturn(attributes); + _options.setManagementModeHttpPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + + ConfigurationEntry portEntry = _handler.getEntry(_portEntryId); + assertEquals("Unexpected state", State.QUIESCED, portEntry.getAttributes().get(Port.STATE)); + } + + public void testRmiPortEntryIsQuiesced() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Port.PROTOCOLS, Collections.singleton(Protocol.RMI)); + when(_portEntry.getAttributes()).thenReturn(attributes); + _options.setManagementModeRmiPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + + ConfigurationEntry portEntry = _handler.getEntry(_portEntryId); + assertEquals("Unexpected state", State.QUIESCED, portEntry.getAttributes().get(Port.STATE)); + } + + public void testConnectorPortEntryIsQuiesced() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Port.PROTOCOLS, Collections.singleton(Protocol.JMX_RMI)); + when(_portEntry.getAttributes()).thenReturn(attributes); + _options.setManagementModeRmiPort(9090); + _handler = new ManagementModeStoreHandler(_store, _options); + + ConfigurationEntry portEntry = _handler.getEntry(_portEntryId); + assertEquals("Unexpected state", State.QUIESCED, portEntry.getAttributes().get(Port.STATE)); + } + + public void testVirtualHostEntryIsQuiesced() + { + UUID virtualHostId = UUID.randomUUID(); + ConfigurationEntry virtualHost = mock(ConfigurationEntry.class); + when(virtualHost.getId()).thenReturn(virtualHostId); + when(virtualHost.getType()).thenReturn(VirtualHost.class.getSimpleName()); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(VirtualHost.CONFIG_PATH, "/path/to/host.xml"); + when(virtualHost.getAttributes()).thenReturn(attributes); + when(_store.getEntry(virtualHostId)).thenReturn(virtualHost); + when(_root.getChildrenIds()).thenReturn(new HashSet<UUID>(Arrays.asList(_portEntryId, virtualHostId))); + + _handler = new ManagementModeStoreHandler(_store, _options); + + ConfigurationEntry hostEntry = _handler.getEntry(virtualHostId); + Map<String, Object> hostAttributes = hostEntry.getAttributes(); + assertEquals("Unexpected state", State.QUIESCED, hostAttributes.get(VirtualHost.STATE)); + hostAttributes.remove(VirtualHost.STATE); + assertEquals("Unexpected attributes", attributes, hostAttributes); + } + + @SuppressWarnings("unchecked") + private void assertCLIPortEntry(ConfigurationEntry portEntry, UUID optionsPort, Protocol protocol) + { + assertEquals("Unexpected entry id", optionsPort, portEntry.getId()); + assertTrue("Unexpected children", portEntry.getChildrenIds().isEmpty()); + Map<String, Object> attributes = portEntry.getAttributes(); + assertEquals("Unexpected name", "MANAGEMENT-MODE-PORT-" + protocol.name(), attributes.get(Port.NAME)); + assertEquals("Unexpected protocol", Collections.singleton(protocol), new HashSet<Protocol>( + (Collection<Protocol>) attributes.get(Port.PROTOCOLS))); + } + + public void testSavePort() + { + _options.setManagementModeHttpPort(1000); + _options.setManagementModeRmiPort(2000); + _options.setManagementModeConnectorPort(3000); + _handler = new ManagementModeStoreHandler(_store, _options); + + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Port.NAME, "TEST"); + ConfigurationEntry configurationEntry = new ConfigurationEntry(_portEntryId, Port.class.getSimpleName(), attributes, + Collections.<UUID> emptySet(), null); + _handler.save(configurationEntry); + verify(_store).save(any(ConfigurationEntry.class)); + } + + public void testSaveRoot() + { + _options.setManagementModeHttpPort(1000); + _options.setManagementModeRmiPort(2000); + _options.setManagementModeConnectorPort(3000); + _handler = new ManagementModeStoreHandler(_store, _options); + + ConfigurationEntry root = _handler.getRootEntry(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Broker.NAME, "TEST"); + ConfigurationEntry configurationEntry = new ConfigurationEntry(_rootId, Broker.class.getSimpleName(), attributes, + root.getChildrenIds(), null); + _handler.save(configurationEntry); + verify(_store).save(any(ConfigurationEntry.class)); + } + + public void testSaveCLIHttpPort() + { + _options.setManagementModeHttpPort(1000); + _handler = new ManagementModeStoreHandler(_store, _options); + + UUID portId = getOptionsPortId(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Port.NAME, "TEST"); + ConfigurationEntry configurationEntry = new ConfigurationEntry(portId, Port.class.getSimpleName(), attributes, + Collections.<UUID> emptySet(), null); + try + { + _handler.save(configurationEntry); + fail("Exception should be thrown on trying to save CLI port"); + } + catch (IllegalConfigurationException e) + { + // pass + } + } + + public void testRemove() + { + _options.setManagementModeHttpPort(1000); + _handler = new ManagementModeStoreHandler(_store, _options); + + _handler.remove(_portEntryId); + verify(_store).remove(_portEntryId); + } + + public void testRemoveCLIPort() + { + _options.setManagementModeHttpPort(1000); + _handler = new ManagementModeStoreHandler(_store, _options); + UUID portId = getOptionsPortId(); + try + { + _handler.remove(portId); + fail("Exception should be thrown on trying to remove CLI port"); + } + catch (IllegalConfigurationException e) + { + // pass + } + } + + private UUID getOptionsPortId() + { + ConfigurationEntry root = _handler.getRootEntry(); + assertEquals("Unexpected root id", _rootId, root.getId()); + Collection<UUID> childrenIds = root.getChildrenIds(); + + childrenIds.remove(_portEntryId); + UUID optionsPort = childrenIds.iterator().next(); + return optionsPort; + } + +} |
