diff options
| author | Alex Rudyy <orudyy@apache.org> | 2013-02-19 09:35:28 +0000 |
|---|---|---|
| committer | Alex Rudyy <orudyy@apache.org> | 2013-02-19 09:35:28 +0000 |
| commit | a973713561140fe7395368ae53def8f7edfa18a3 (patch) | |
| tree | 7bda80afada592df681fb73908400e7a189f015f /qpid/java/broker/src | |
| parent | 1b0f1d06188e73e9440dc1789c28ee65e24d539d (diff) | |
| download | qpid-python-a973713561140fe7395368ae53def8f7edfa18a3.tar.gz | |
QPID-4390: Introduce a configuration store in java broker allowing runtime modifications and replace existing xml file configuration with json configuration store
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1447646 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker/src')
204 files changed, 10894 insertions, 8132 deletions
diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/AMQChannel.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/AMQChannel.java index a05b53f7d4..ab4ca81d05 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/AMQChannel.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/AMQChannel.java @@ -54,6 +54,7 @@ import org.apache.qpid.protocol.AMQConstant; import org.apache.qpid.server.TransactionTimeoutHelper.CloseAction; import org.apache.qpid.server.ack.UnacknowledgedMessageMap; import org.apache.qpid.server.ack.UnacknowledgedMessageMapImpl; +import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.flow.FlowCreditManager; import org.apache.qpid.server.flow.Pre0_10CreditManager; @@ -78,7 +79,6 @@ import org.apache.qpid.server.queue.BaseQueue; import org.apache.qpid.server.queue.InboundMessageAdapter; import org.apache.qpid.server.queue.IncomingMessage; import org.apache.qpid.server.queue.QueueEntry; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.MessageStore; import org.apache.qpid.server.store.StoreFuture; import org.apache.qpid.server.store.StoredMessage; @@ -99,9 +99,8 @@ public class AMQChannel implements AMQSessionModel, AsyncAutoCommitTransaction.F private static final Logger _logger = Logger.getLogger(AMQChannel.class); - private static final boolean MSG_AUTH = - ApplicationRegistry.getInstance().getConfiguration().getMsgAuth(); - + //TODO use Broker property to configure message authorization requirements + private boolean _messageAuthorizationRequired = Boolean.getBoolean(BrokerProperties.PROPERTY_MSG_AUTH); private final int _channelId; @@ -1121,7 +1120,7 @@ public class AMQChannel implements AMQSessionModel, AsyncAutoCommitTransaction.F ? ((BasicContentHeaderProperties) header.getProperties()).getUserId() : null; - return (!MSG_AUTH || _session.getAuthorizedPrincipal().getName().equals(userID == null? "" : userID.toString())); + return (!_messageAuthorizationRequired || _session.getAuthorizedPrincipal().getName().equals(userID == null? "" : userID.toString())); } 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 3848c97a99..e4a95363ef 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 @@ -23,37 +23,30 @@ package org.apache.qpid.server; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.util.*; -import javax.net.ssl.SSLContext; +import java.util.List; +import java.util.Properties; +import java.util.Set; + import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.qpid.framing.AMQShortString; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.configuration.ServerNetworkTransportConfiguration; +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.logging.SystemOutMessageLogger; import org.apache.qpid.server.logging.actors.BrokerActor; import org.apache.qpid.server.logging.actors.CurrentActor; -import org.apache.qpid.server.logging.actors.GenericActor; import org.apache.qpid.server.logging.log4j.LoggingManagementFacade; import org.apache.qpid.server.logging.messages.BrokerMessages; -import org.apache.qpid.server.protocol.AmqpProtocolVersion; -import org.apache.qpid.server.protocol.MultiVersionProtocolEngineFactory; import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.transport.QpidAcceptor; -import org.apache.qpid.ssl.SSLContextFactory; -import org.apache.qpid.transport.NetworkTransportConfiguration; -import org.apache.qpid.transport.network.IncomingNetworkTransport; -import org.apache.qpid.transport.network.Transport; - -import static org.apache.qpid.transport.ConnectionSettings.WILDCARD_ADDRESS; +import org.apache.qpid.server.registry.IApplicationRegistry; public class Broker { private static final Logger LOGGER = Logger.getLogger(Broker.class); private volatile Thread _shutdownHookThread; + private volatile IApplicationRegistry _applicationRegistry; protected static class InitException extends RuntimeException { @@ -75,7 +68,10 @@ public class Broker { try { - ApplicationRegistry.remove(); + if (_applicationRegistry != null) + { + _applicationRegistry.close(); + } } finally { @@ -112,260 +108,49 @@ public class Broker private void startupImpl(final BrokerOptions options) throws Exception { - final String qpidHome = options.getQpidHome(); - final File configFile = getConfigFile(options.getConfigFile(), - BrokerOptions.DEFAULT_CONFIG_FILE, qpidHome, true); - - CurrentActor.get().message(BrokerMessages.CONFIG(configFile.getAbsolutePath())); + final String qpidHome = System.getProperty(BrokerProperties.PROPERTY_QPID_HOME); + String storeLocation = options.getConfigurationStoreLocation(); + String storeType = options.getConfigurationStoreType(); - File logConfigFile = getConfigFile(options.getLogConfigFile(), - BrokerOptions.DEFAULT_LOG_CONFIG_FILE, qpidHome, false); - - configureLogging(logConfigFile, options.getLogWatchFrequency()); - - ServerConfiguration serverConfig = new ServerConfiguration(configFile); - ApplicationRegistry config = new ApplicationRegistry(serverConfig); - if (options.getQpidWork() != null) - { - serverConfig.setQpidWork(options.getQpidWork()); - } - if (options.getQpidHome() != null) + if (storeLocation == null) { - serverConfig.setQpidHome(options.getQpidHome()); + String qpidWork = System.getProperty(BrokerProperties.PROPERTY_QPID_WORK); + if (qpidWork == null) + { + qpidWork = new File(System.getProperty("user.dir"), "work").getAbsolutePath(); + } + storeLocation = new File(qpidWork, BrokerOptions.DEFAULT_CONFIG_FILE + "." + storeType).getAbsolutePath(); } - updateManagementPorts(serverConfig, options.getJmxPortRegistryServer(), options.getJmxPortConnectorServer()); - ApplicationRegistry.initialise(config); + CurrentActor.get().message(BrokerMessages.CONFIG(storeLocation)); - // We have already loaded the BrokerMessages class by this point so we - // need to refresh the locale setting incase we had a different value in - // the configuration. - BrokerMessages.reload(); + File logConfigFile = getConfigFile(options.getLogConfigFile(), BrokerOptions.DEFAULT_LOG_CONFIG_FILE, qpidHome, false); + configureLogging(logConfigFile, options.getLogWatchFrequency()); - // AR.initialise() sets and removes its own actor so we now need to set the actor - // for the remainder of the startup, and the default actor if the stack is empty - CurrentActor.set(new BrokerActor(config.getCompositeStartupMessageLogger())); - CurrentActor.setDefault(new BrokerActor(config.getRootMessageLogger())); - GenericActor.setDefaultMessageLogger(config.getRootMessageLogger()); + BrokerConfigurationStoreCreator storeCreator = new BrokerConfigurationStoreCreator(); + ConfigurationEntryStore store = storeCreator.createStore(storeLocation, storeType, options); + _applicationRegistry = new ApplicationRegistry(store); try { - Set<Integer> ports = new HashSet<Integer>(options.getPorts()); - if(ports.isEmpty()) - { - parsePortList(ports, serverConfig.getPorts()); - } - - Set<Integer> sslPorts = new HashSet<Integer>(options.getSSLPorts()); - if(sslPorts.isEmpty()) - { - parsePortList(sslPorts, serverConfig.getSSLPorts()); - } - - //1-0 excludes and includes - Set<Integer> exclude_1_0 = new HashSet<Integer>(options.getExcludedPorts(ProtocolExclusion.v1_0)); - if(exclude_1_0.isEmpty()) - { - parsePortList(exclude_1_0, serverConfig.getPortExclude10()); - } - - Set<Integer> include_1_0 = new HashSet<Integer>(options.getIncludedPorts(ProtocolInclusion.v1_0)); - if(include_1_0.isEmpty()) - { - parsePortList(include_1_0, serverConfig.getPortInclude10()); - } - - //0-10 excludes and includes - Set<Integer> exclude_0_10 = new HashSet<Integer>(options.getExcludedPorts(ProtocolExclusion.v0_10)); - if(exclude_0_10.isEmpty()) - { - parsePortList(exclude_0_10, serverConfig.getPortExclude010()); - } - - Set<Integer> include_0_10 = new HashSet<Integer>(options.getIncludedPorts(ProtocolInclusion.v0_10)); - if(include_0_10.isEmpty()) - { - parsePortList(include_0_10, serverConfig.getPortInclude010()); - } - - //0-9-1 excludes and includes - Set<Integer> exclude_0_9_1 = new HashSet<Integer>(options.getExcludedPorts(ProtocolExclusion.v0_9_1)); - if(exclude_0_9_1.isEmpty()) - { - parsePortList(exclude_0_9_1, serverConfig.getPortExclude091()); - } - - Set<Integer> include_0_9_1 = new HashSet<Integer>(options.getIncludedPorts(ProtocolInclusion.v0_9_1)); - if(include_0_9_1.isEmpty()) - { - parsePortList(include_0_9_1, serverConfig.getPortInclude091()); - } - - //0-9 excludes and includes - Set<Integer> exclude_0_9 = new HashSet<Integer>(options.getExcludedPorts(ProtocolExclusion.v0_9)); - if(exclude_0_9.isEmpty()) - { - parsePortList(exclude_0_9, serverConfig.getPortExclude09()); - } - - Set<Integer> include_0_9 = new HashSet<Integer>(options.getIncludedPorts(ProtocolInclusion.v0_9)); - if(include_0_9.isEmpty()) - { - parsePortList(include_0_9, serverConfig.getPortInclude09()); - } - - //0-8 excludes and includes - Set<Integer> exclude_0_8 = new HashSet<Integer>(options.getExcludedPorts(ProtocolExclusion.v0_8)); - if(exclude_0_8.isEmpty()) - { - parsePortList(exclude_0_8, serverConfig.getPortExclude08()); - } - - Set<Integer> include_0_8 = new HashSet<Integer>(options.getIncludedPorts(ProtocolInclusion.v0_8)); - if(include_0_8.isEmpty()) - { - parsePortList(include_0_8, serverConfig.getPortInclude08()); - } - - String bindAddr = options.getBind(); - if (bindAddr == null) - { - bindAddr = serverConfig.getBind(); - } - - InetAddress bindAddress; - if (bindAddr.equals(WILDCARD_ADDRESS)) - { - bindAddress = null; - } - else + _applicationRegistry.initialise(); + } + catch(Exception e) + { + try { - bindAddress = InetAddress.getByName(bindAddr); + _applicationRegistry.close(); } - - final AmqpProtocolVersion defaultSupportedProtocolReply = serverConfig.getDefaultSupportedProtocolReply(); - - if (!serverConfig.getSSLOnly()) + catch(Exception ce) { - for(int port : ports) - { - final InetSocketAddress inetSocketAddress = new InetSocketAddress(bindAddress, port); - - final Set<AmqpProtocolVersion> supported = - getSupportedVersions(port, exclude_1_0, exclude_0_10, exclude_0_9_1, exclude_0_9, exclude_0_8, - include_1_0, include_0_10, include_0_9_1, include_0_9, include_0_8,serverConfig); - - final NetworkTransportConfiguration settings = - new ServerNetworkTransportConfiguration(serverConfig, inetSocketAddress, Transport.TCP); - - final IncomingNetworkTransport transport = Transport.getIncomingTransportInstance(); - final MultiVersionProtocolEngineFactory protocolEngineFactory = - new MultiVersionProtocolEngineFactory(supported, defaultSupportedProtocolReply); - - transport.accept(settings, protocolEngineFactory, null); - - ApplicationRegistry.getInstance().addAcceptor(inetSocketAddress, - new QpidAcceptor(transport,QpidAcceptor.Transport.TCP, supported)); - CurrentActor.get().message(BrokerMessages.LISTENING("TCP", port)); - } + LOGGER.debug("An error occured when closing the registry following initialization failure", ce); } - - if (serverConfig.getEnableSSL()) - { - final String keystorePath = serverConfig.getConnectorKeyStorePath(); - final String keystorePassword = serverConfig.getConnectorKeyStorePassword(); - final String keystoreType = serverConfig.getConnectorKeyStoreType(); - final String keyManagerFactoryAlgorithm = serverConfig.getConnectorKeyManagerFactoryAlgorithm(); - final SSLContext sslContext; - if(serverConfig.getConnectorTrustStorePath()!=null) - { - sslContext = SSLContextFactory.buildClientContext(serverConfig.getConnectorTrustStorePath(), - serverConfig.getConnectorTrustStorePassword(), - serverConfig.getConnectorTrustStoreType(), - serverConfig.getConnectorTrustManagerFactoryAlgorithm(), - keystorePath, - keystorePassword, keystoreType, keyManagerFactoryAlgorithm, - serverConfig.getCertAlias()); - } - else - { - sslContext = SSLContextFactory.buildServerContext(keystorePath, keystorePassword, keystoreType, keyManagerFactoryAlgorithm); - } - - for(int sslPort : sslPorts) - { - final InetSocketAddress inetSocketAddress = new InetSocketAddress(bindAddress, sslPort); - - final Set<AmqpProtocolVersion> supported = - getSupportedVersions(sslPort, exclude_1_0, exclude_0_10, exclude_0_9_1, exclude_0_9, exclude_0_8, - include_1_0, include_0_10, include_0_9_1, include_0_9, include_0_8, serverConfig); - final NetworkTransportConfiguration settings = - new ServerNetworkTransportConfiguration(serverConfig, inetSocketAddress, Transport.TCP); - - final IncomingNetworkTransport transport = Transport.getIncomingTransportInstance(); - final MultiVersionProtocolEngineFactory protocolEngineFactory = - new MultiVersionProtocolEngineFactory(supported, defaultSupportedProtocolReply); - - transport.accept(settings, protocolEngineFactory, sslContext); - - ApplicationRegistry.getInstance().addAcceptor(inetSocketAddress, - new QpidAcceptor(transport,QpidAcceptor.Transport.SSL, supported)); - CurrentActor.get().message(BrokerMessages.LISTENING("TCP/SSL", sslPort)); - } - } - - CurrentActor.get().message(BrokerMessages.READY()); - } - finally - { - // Startup is complete so remove the AR initialised Startup actor - CurrentActor.remove(); + throw e; } - } - - private static Set<AmqpProtocolVersion> getSupportedVersions(final int port, - final Set<Integer> exclude_1_0, - final Set<Integer> exclude_0_10, - final Set<Integer> exclude_0_9_1, - final Set<Integer> exclude_0_9, - final Set<Integer> exclude_0_8, - final Set<Integer> include_1_0, - final Set<Integer> include_0_10, - final Set<Integer> include_0_9_1, - final Set<Integer> include_0_9, - final Set<Integer> include_0_8, - final ServerConfiguration serverConfig) - { - final EnumSet<AmqpProtocolVersion> supported = EnumSet.allOf(AmqpProtocolVersion.class); - if((exclude_1_0.contains(port) || !serverConfig.isAmqp10enabled()) && !include_1_0.contains(port)) - { - supported.remove(AmqpProtocolVersion.v1_0_0); - } - - if((exclude_0_10.contains(port) || !serverConfig.isAmqp010enabled()) && !include_0_10.contains(port)) - { - supported.remove(AmqpProtocolVersion.v0_10); - } - - if((exclude_0_9_1.contains(port) || !serverConfig.isAmqp091enabled()) && !include_0_9_1.contains(port)) - { - supported.remove(AmqpProtocolVersion.v0_9_1); - } - - if((exclude_0_9.contains(port) || !serverConfig.isAmqp09enabled()) && !include_0_9.contains(port)) - { - supported.remove(AmqpProtocolVersion.v0_9); - } - - if((exclude_0_8.contains(port) || !serverConfig.isAmqp08enabled()) && !include_0_8.contains(port)) - { - supported.remove(AmqpProtocolVersion.v0_8); - } - - return supported; } + private File getConfigFile(final String fileName, final String defaultFileName, final String qpidHome, boolean throwOnFileNotFound) throws InitException @@ -386,7 +171,7 @@ public class Broker if (qpidHome == null) { - error = error + "\nNote: " + BrokerOptions.QPID_HOME + " is not set."; + error = error + "\nNote: " + BrokerProperties.PROPERTY_QPID_HOME + " is not set."; } throw new InitException(error, null); @@ -413,37 +198,6 @@ public class Broker } } - /** - * Update the configuration data with the management port. - * @param configuration - * @param registryServerPort The string from the command line - */ - private void updateManagementPorts(ServerConfiguration configuration, Integer registryServerPort, Integer connectorServerPort) - { - if (registryServerPort != null) - { - try - { - configuration.setJMXPortRegistryServer(registryServerPort); - } - catch (NumberFormatException e) - { - throw new InitException("Invalid management (registry server) port: " + registryServerPort, null); - } - } - if (connectorServerPort != null) - { - try - { - configuration.setJMXPortConnectorServer(connectorServerPort); - } - catch (NumberFormatException e) - { - throw new InitException("Invalid management (connector server) port: " + connectorServerPort, null); - } - } - } - private void configureLogging(File logConfigFile, int logWatchTime) throws InitException, IOException { if (logConfigFile.exists() && logConfigFile.canRead()) @@ -545,6 +299,15 @@ public class Broker LOGGER.debug("Skipping shutdown hook removal as there either isnt one, or we are it."); } } + /** + * Workaround that prevents AMQShortStrings cache from being left in the thread local. This is important + * when embedding the Broker in containers where the starting thread may not belong to Qpid. + * The long term solution here is to stop our use of AMQShortString outside the AMQP transport layer. + */ + private void clearAMQShortStringCache() + { + AMQShortString.clearLocalCache(); + } private class ShutdownService implements Runnable { @@ -555,13 +318,4 @@ public class Broker } } - /** - * Workaround that prevents AMQShortStrings cache from being left in the thread local. This is important - * when embedding the Broker in containers where the starting thread may not belong to Qpid. - * The long term solution here is to stop our use of AMQShortString outside the AMQP transport layer. - */ - private void clearAMQShortStringCache() - { - AMQShortString.clearLocalCache(); - } } 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 1a00467c3d..f85cf202a1 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 @@ -20,63 +20,17 @@ */ package org.apache.qpid.server; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - public class BrokerOptions { - public static final String DEFAULT_CONFIG_FILE = "etc/config.xml"; + public static final String DEFAULT_STORE_TYPE = "json"; + public static final String DEFAULT_CONFIG_FILE = "config"; public static final String DEFAULT_LOG_CONFIG_FILE = "etc/log4j.xml"; - public static final String QPID_HOME = "QPID_HOME"; - public static final String QPID_WORK = "QPID_WORK"; - - private final Set<Integer> _ports = new HashSet<Integer>(); - private final Set<Integer> _sslPorts = new HashSet<Integer>(); - private final Map<ProtocolExclusion,Set<Integer>> _exclusionMap = new HashMap<ProtocolExclusion, Set<Integer>>(); - private final Map<ProtocolInclusion,Set<Integer>> _inclusionMap = new HashMap<ProtocolInclusion, Set<Integer>>(); - private String _configFile; private String _logConfigFile; - private String _bind; - private Integer _jmxPortRegistryServer; - private Integer _jmxPortConnectorServer; - private Integer _logWatchFrequency = 0; - private String _qpidWorkFolder; - private String _qpidHomeFolder; - public void addPort(final int port) - { - _ports.add(port); - } - - public void addSSLPort(final int sslPort) - { - _sslPorts.add(sslPort); - } - - public Set<Integer> getPorts() - { - return Collections.unmodifiableSet(_ports); - } - - public Set<Integer> getSSLPorts() - { - return Collections.unmodifiableSet(_sslPorts); - } - - public String getConfigFile() - { - return _configFile; - } - - public void setConfigFile(final String configFile) - { - _configFile = configFile; - } + private String _configurationStoreLocation; + private String _configurationStoreType = DEFAULT_STORE_TYPE; public String getLogConfigFile() { @@ -88,57 +42,6 @@ public class BrokerOptions _logConfigFile = logConfigFile; } - public Integer getJmxPortRegistryServer() - { - return _jmxPortRegistryServer; - } - - public void setJmxPortRegistryServer(final int jmxPortRegistryServer) - { - _jmxPortRegistryServer = jmxPortRegistryServer; - } - - public Integer getJmxPortConnectorServer() - { - return _jmxPortConnectorServer; - } - - public void setJmxPortConnectorServer(final int jmxPortConnectorServer) - { - _jmxPortConnectorServer = jmxPortConnectorServer; - } - public String getQpidHome() - { - return _qpidHomeFolder == null? System.getProperty(QPID_HOME): _qpidHomeFolder; - } - - public Set<Integer> getExcludedPorts(final ProtocolExclusion excludeProtocol) - { - final Set<Integer> excludedPorts = _exclusionMap.get(excludeProtocol); - return excludedPorts == null ? Collections.<Integer>emptySet() : excludedPorts; - } - - public void addExcludedPort(final ProtocolExclusion excludeProtocol, final int port) - { - if (!_exclusionMap.containsKey(excludeProtocol)) - { - _exclusionMap.put(excludeProtocol, new HashSet<Integer>()); - } - - Set<Integer> ports = _exclusionMap.get(excludeProtocol); - ports.add(port); - } - - public String getBind() - { - return _bind; - } - - public void setBind(final String bind) - { - _bind = bind; - } - public int getLogWatchFrequency() { return _logWatchFrequency; @@ -153,35 +56,24 @@ public class BrokerOptions _logWatchFrequency = logWatchFrequency; } - public Set<Integer> getIncludedPorts(final ProtocolInclusion includeProtocol) + public String getConfigurationStoreLocation() { - final Set<Integer> includedPorts = _inclusionMap.get(includeProtocol); - return includedPorts == null ? Collections.<Integer>emptySet() : includedPorts; + return _configurationStoreLocation; } - public void addIncludedPort(final ProtocolInclusion includeProtocol, final int port) + public void setConfigurationStoreLocation(String cofigurationStore) { - if (!_inclusionMap.containsKey(includeProtocol)) - { - _inclusionMap.put(includeProtocol, new HashSet<Integer>()); - } - - Set<Integer> ports = _inclusionMap.get(includeProtocol); - ports.add(port); + _configurationStoreLocation = cofigurationStore; } - public String getQpidWork() + public String getConfigurationStoreType() { - return _qpidWorkFolder; + return _configurationStoreType; } - public void setQpidWork(String qpidWorkFolder) + public void setConfigurationStoreType(String cofigurationStoreType) { - _qpidWorkFolder = qpidWorkFolder; + _configurationStoreType = cofigurationStoreType; } - public void setQpidHome(String qpidHomeFolder) - { - _qpidHomeFolder = qpidHomeFolder; - } }
\ 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 9fe7a6619f..d834dd0b1e 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 @@ -30,9 +30,6 @@ import org.apache.commons.cli.PosixParser; import org.apache.log4j.Logger; import org.apache.qpid.common.QpidProperties; import org.apache.qpid.framing.ProtocolVersion; -import org.apache.qpid.server.Broker.InitException; -import org.apache.qpid.server.registry.ApplicationRegistry; - /** * Main entry point for AMQPD. @@ -45,86 +42,11 @@ public class Main private static final Option OPTION_VERSION = new Option("v", "version", false, "print the version information and exit"); - private static final Option OPTION_CONFIG_FILE = - OptionBuilder.withArgName("file").hasArg().withDescription("use given configuration file").withLongOpt("config") - .create("c"); - - private static final Option OPTION_PORT = - OptionBuilder.withArgName("port").hasArg() - .withDescription("listen on the specified port. Overrides any value in the config file") - .withLongOpt("port").create("p"); - - private static final Option OPTION_SSLPORT = - OptionBuilder.withArgName("port").hasArg() - .withDescription("SSL port. Overrides any value in the config file") - .withLongOpt("sslport").create("s"); - - - private static final Option OPTION_EXCLUDE_1_0 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("when listening on the specified port do not accept AMQP1-0 connections. The specified port must be one specified on the command line") - .withLongOpt("exclude-1-0").create(); - - private static final Option OPTION_EXCLUDE_0_10 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("when listening on the specified port do not accept AMQP0-10 connections. The specified port must be one specified on the command line") - .withLongOpt("exclude-0-10").create(); - - private static final Option OPTION_EXCLUDE_0_9_1 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("when listening on the specified port do not accept AMQP0-9-1 connections. The specified port must be one specified on the command line") - .withLongOpt("exclude-0-9-1").create(); - - private static final Option OPTION_EXCLUDE_0_9 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("when listening on the specified port do not accept AMQP0-9 connections. The specified port must be one specified on the command line") - .withLongOpt("exclude-0-9").create(); - - private static final Option OPTION_EXCLUDE_0_8 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("when listening on the specified port do not accept AMQP0-8 connections. The specified port must be one specified on the command line") - .withLongOpt("exclude-0-8").create(); - - private static final Option OPTION_INCLUDE_1_0 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("accept AMQP1-0 connections on this port, overriding configuration to the contrary. The specified port must be one specified on the command line") - .withLongOpt("include-1-0").create(); + private static final Option OPTION_CONFIGURATION_STORE_PATH = OptionBuilder.withArgName("path").hasArg() + .withDescription("use given configuration store location").withLongOpt("store-path").create("sp"); -private static final Option OPTION_INCLUDE_0_10 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("accept AMQP0-10 connections on this port, overriding configuration to the contrary. The specified port must be one specified on the command line") - .withLongOpt("include-0-10").create(); - -private static final Option OPTION_INCLUDE_0_9_1 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("accept AMQP0-9-1 connections on this port, overriding configuration to the contrary. The specified port must be one specified on the command line") - .withLongOpt("include-0-9-1").create(); - -private static final Option OPTION_INCLUDE_0_9 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("accept AMQP0-9 connections on this port, overriding configuration to the contrary. The specified port must be one specified on the command line") - .withLongOpt("include-0-9").create(); - -private static final Option OPTION_INCLUDE_0_8 = - OptionBuilder.withArgName("port").hasArg() - .withDescription("accept AMQP0-8 connections on this port, overriding configuration to the contrary. The specified port must be one specified on the command line") - .withLongOpt("include-0-8").create(); - - - private static final Option OPTION_JMX_PORT_REGISTRY_SERVER = - OptionBuilder.withArgName("port").hasArg() - .withDescription("listen on the specified management (registry server) port. Overrides any value in the config file") - .withLongOpt("jmxregistryport").create("m"); - - private static final Option OPTION_JMX_PORT_CONNECTOR_SERVER = - OptionBuilder.withArgName("port").hasArg() - .withDescription("listen on the specified management (connector server) port. Overrides any value in the config file") - .withLongOpt("jmxconnectorport").create(); - - private static final Option OPTION_BIND = - OptionBuilder.withArgName("address").hasArg() - .withDescription("bind to the specified address. Overrides any value in the config file") - .withLongOpt("bind").create("b"); + private static final Option OPTION_CONFIGURATION_STORE_TYPE = OptionBuilder.withArgName("type").hasArg() + .withDescription("use given store type").withLongOpt("store-type").create("st"); private static final Option OPTION_LOG_CONFIG_FILE = OptionBuilder.withArgName("file").hasArg() @@ -143,25 +65,10 @@ private static final Option OPTION_INCLUDE_0_8 = { OPTIONS.addOption(OPTION_HELP); OPTIONS.addOption(OPTION_VERSION); - OPTIONS.addOption(OPTION_CONFIG_FILE); + OPTIONS.addOption(OPTION_CONFIGURATION_STORE_PATH); + OPTIONS.addOption(OPTION_CONFIGURATION_STORE_TYPE); OPTIONS.addOption(OPTION_LOG_CONFIG_FILE); OPTIONS.addOption(OPTION_LOG_WATCH); - OPTIONS.addOption(OPTION_PORT); - OPTIONS.addOption(OPTION_SSLPORT); - OPTIONS.addOption(OPTION_EXCLUDE_1_0); - OPTIONS.addOption(OPTION_EXCLUDE_0_10); - OPTIONS.addOption(OPTION_EXCLUDE_0_9_1); - OPTIONS.addOption(OPTION_EXCLUDE_0_9); - OPTIONS.addOption(OPTION_EXCLUDE_0_8); - OPTIONS.addOption(OPTION_INCLUDE_1_0); - OPTIONS.addOption(OPTION_INCLUDE_0_10); - OPTIONS.addOption(OPTION_INCLUDE_0_9_1); - OPTIONS.addOption(OPTION_INCLUDE_0_9); - OPTIONS.addOption(OPTION_INCLUDE_0_8); - OPTIONS.addOption(OPTION_BIND); - - OPTIONS.addOption(OPTION_JMX_PORT_REGISTRY_SERVER); - OPTIONS.addOption(OPTION_JMX_PORT_CONNECTOR_SERVER); } protected CommandLine _commandLine; @@ -243,10 +150,15 @@ private static final Option OPTION_INCLUDE_0_8 = else { BrokerOptions options = new BrokerOptions(); - String configFile = _commandLine.getOptionValue(OPTION_CONFIG_FILE.getOpt()); - if(configFile != null) + String configurationStore = _commandLine.getOptionValue(OPTION_CONFIGURATION_STORE_PATH.getOpt()); + if (configurationStore != null) + { + options.setConfigurationStoreLocation(configurationStore); + } + String configurationStoreType = _commandLine.getOptionValue(OPTION_CONFIGURATION_STORE_TYPE.getOpt()); + if (configurationStoreType != null) { - options.setConfigFile(configFile); + options.setConfigurationStoreType(configurationStoreType); } String logWatchConfig = _commandLine.getOptionValue(OPTION_LOG_WATCH.getOpt()); @@ -261,52 +173,6 @@ private static final Option OPTION_INCLUDE_0_8 = options.setLogConfigFile(logConfig); } - String jmxPortRegistryServer = _commandLine.getOptionValue(OPTION_JMX_PORT_REGISTRY_SERVER.getOpt()); - if(jmxPortRegistryServer != null) - { - options.setJmxPortRegistryServer(Integer.parseInt(jmxPortRegistryServer)); - } - - String jmxPortConnectorServer = _commandLine.getOptionValue(OPTION_JMX_PORT_CONNECTOR_SERVER.getLongOpt()); - if(jmxPortConnectorServer != null) - { - options.setJmxPortConnectorServer(Integer.parseInt(jmxPortConnectorServer)); - } - - String bindAddr = _commandLine.getOptionValue(OPTION_BIND.getOpt()); - if (bindAddr != null) - { - options.setBind(bindAddr); - } - - String[] portStr = _commandLine.getOptionValues(OPTION_PORT.getOpt()); - if(portStr != null) - { - parsePortArray(options, portStr, false); - for(ProtocolExclusion pe : ProtocolExclusion.values()) - { - parsePortArray(options, _commandLine.getOptionValues(pe.getExcludeName()), pe); - } - for(ProtocolInclusion pe : ProtocolInclusion.values()) - { - parseProtocolInclusions(options, _commandLine.getOptionValues(pe.getIncludeName()), pe); - } - } - - String[] sslPortStr = _commandLine.getOptionValues(OPTION_SSLPORT.getOpt()); - if(sslPortStr != null) - { - parsePortArray(options, sslPortStr, true); - for(ProtocolExclusion pe : ProtocolExclusion.values()) - { - parsePortArray(options, _commandLine.getOptionValues(pe.getExcludeName()), pe); - } - for(ProtocolInclusion pe : ProtocolInclusion.values()) - { - parseProtocolInclusions(options, _commandLine.getOptionValues(pe.getIncludeName()), pe); - } - } - setExceptionHandler(); startBroker(options); @@ -389,72 +255,7 @@ private static final Option OPTION_INCLUDE_0_8 = protected void shutdown(final int status) { - ApplicationRegistry.remove(); System.exit(status); } - private static void parsePortArray(final BrokerOptions options,final Object[] ports, - final boolean ssl) throws InitException - { - if(ports != null) - { - for(int i = 0; i < ports.length; i++) - { - try - { - if(ssl) - { - options.addSSLPort(Integer.parseInt(String.valueOf(ports[i]))); - } - else - { - options.addPort(Integer.parseInt(String.valueOf(ports[i]))); - } - } - catch (NumberFormatException e) - { - throw new InitException("Invalid port: " + ports[i], e); - } - } - } - } - - private static void parsePortArray(final BrokerOptions options, final Object[] ports, - final ProtocolExclusion excludedProtocol) throws InitException - { - if(ports != null) - { - for(int i = 0; i < ports.length; i++) - { - try - { - options.addExcludedPort(excludedProtocol, - Integer.parseInt(String.valueOf(ports[i]))); - } - catch (NumberFormatException e) - { - throw new InitException("Invalid port for exclusion: " + ports[i], e); - } - } - } - } - - private static void parseProtocolInclusions(final BrokerOptions options, final Object[] ports, - final ProtocolInclusion includedProtocol) throws InitException - { - if(ports != null) - { - for(int i = 0; i < ports.length; i++) - { - try - { - options.addIncludedPort(includedProtocol, Integer.parseInt(String.valueOf(ports[i]))); - } - catch (NumberFormatException e) - { - throw new InitException("Invalid port for inclusion: " + ports[i], e); - } - } - } - } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/ProtocolExclusion.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/ProtocolExclusion.java deleted file mode 100644 index fe6e32173f..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/ProtocolExclusion.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * 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; - -import java.util.HashMap; -import java.util.Map; - -public enum ProtocolExclusion -{ - v0_8("exclude-0-8","--exclude-0-8"), - v0_9("exclude-0-9", "--exclude-0-9"), - v0_9_1("exclude-0-9-1", "--exclude-0-9-1"), - v0_10("exclude-0-10", "--exclude-0-10"), - v1_0("exclude-1-0", "--exclude-1-0"); - - private static final Map<String, ProtocolExclusion> MAP = new HashMap<String,ProtocolExclusion>(); - - static - { - for(ProtocolExclusion pe : ProtocolExclusion.values()) - { - MAP.put(pe.getArg(), pe); - } - } - - private String _arg; - private String _excludeName; - - private ProtocolExclusion(final String excludeName, final String arg) - { - _excludeName = excludeName; - _arg = arg; - } - - public String getArg() - { - return _arg; - } - - public String getExcludeName() - { - return _excludeName; - } - - public static ProtocolExclusion lookup(final String arg) - { - ProtocolExclusion ex = MAP.get(arg); - - if(ex == null) - { - throw new IllegalArgumentException(arg + " is not a valid protocol exclusion"); - } - - return ex; - } -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/ProtocolInclusion.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/ProtocolInclusion.java deleted file mode 100644 index 85fbe2e02e..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/ProtocolInclusion.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * 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; - -import java.util.HashMap; -import java.util.Map; - -public enum ProtocolInclusion -{ - v0_8("include-0-8","--include-0-8"), - v0_9("include-0-9", "--include-0-9"), - v0_9_1("include-0-9-1", "--include-0-9-1"), - v0_10("include-0-10", "--include-0-10"), - v1_0("include-1-0", "--include-1-0"); - - private static final Map<String, ProtocolInclusion> MAP = new HashMap<String,ProtocolInclusion>(); - - static - { - for(ProtocolInclusion pe : ProtocolInclusion.values()) - { - MAP.put(pe.getArg(), pe); - } - } - - private String _arg; - private String _includeName; - - private ProtocolInclusion(final String includeName, final String arg) - { - _includeName = includeName; - _arg = arg; - } - - public String getArg() - { - return _arg; - } - - public String getIncludeName() - { - return _includeName; - } - - public static ProtocolInclusion lookup(final String arg) - { - ProtocolInclusion ex = MAP.get(arg); - - if(ex == null) - { - throw new IllegalArgumentException(arg + " is not a valid protocol inclusion"); - } - - return ex; - } -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java new file mode 100644 index 0000000000..efae100c9d --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java @@ -0,0 +1,58 @@ +/* + * + * 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; + +import org.apache.qpid.server.BrokerOptions; +import org.apache.qpid.server.plugin.QpidServiceLoader; + +public class BrokerConfigurationStoreCreator +{ + /** + * Path to resource containing broker default configuration + */ + public static final String INITIAL_STORE_LOCATION = "initial-store.json"; + + /** + * Create broker configuration store for given store location, store type + * and command line options + */ + public ConfigurationEntryStore createStore(String storeLocation, String storeType, BrokerOptions options) + { + ConfigurationEntryStore store = null; + QpidServiceLoader<ConfigurationStoreFactory> serviceLoader = new QpidServiceLoader<ConfigurationStoreFactory>(); + Iterable<ConfigurationStoreFactory> configurationStoreFactories = serviceLoader.instancesOf(ConfigurationStoreFactory.class); + for (ConfigurationStoreFactory storeFactory : configurationStoreFactories) + { + if (storeFactory.getStoreType().equals(storeType)) + { + store = storeFactory.createStore(); + break; + } + } + if (store == null) + { + throw new IllegalConfigurationException("Cannot create store for the type " + storeType); + } + store.open(storeLocation); + return store; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerProperties.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerProperties.java new file mode 100644 index 0000000000..179d4a5640 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerProperties.java @@ -0,0 +1,54 @@ +package org.apache.qpid.server.configuration; + +import java.util.Locale; + +/** + * Declares broker system property names + */ +public class BrokerProperties +{ + public static final int DEFAULT_HEART_BEAT_TIMEOUT_FACTOR = 2; + + public static final String PROPERTY_DEAD_LETTER_EXCHANGE_SUFFIX = "qpid.dead_letter_exchange_suffix"; + public static final String PROPERTY_DEAD_LETTER_QUEUE_SUFFIX = "qpid.dead_letter_queue_suffix"; + + public static final String PROPERTY_FRAME_SIZE = "qpid.frame_size"; + public static final String PROPERTY_MSG_AUTH = "qpid.msg_auth"; + public static final String PROPERTY_STATUS_UPDATES = "qpid.status_updates"; + public static final String PROPERTY_LOCALE = "qpid.locale"; + public static final String PROPERTY_DEFAULT_SUPPORTED_PROTOCOL_REPLY = "qpid.default_supported_protocol_version_reply"; + public static final String PROPERTY_DISABLED_FEATURES = "qpid.broker_disabled_features"; + + public static final int DEFAULT_FRAME_SIZE = Integer.getInteger(PROPERTY_FRAME_SIZE, 65535); + public static final String PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_EXCLUDES = "qpid.broker_default_amqp_protocol_excludes"; + public static final String PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_INCLUDES = "qpid.broker_default_amqp_protocol_includes"; + + public static final String PROPERTY_MANAGEMENT_RIGHTS_INFER_ALL_ACCESS = "qpid.broker_jmx_method_rights_infer_all_access"; + public static final String PROPERTY_USE_CUSTOM_RMI_SOCKET_FACTORY = "qpid.broker_jmx_use_custom_rmi_socket_factory"; + + public static final String PROPERTY_QPID_HOME = "QPID_HOME"; + public static final String PROPERTY_QPID_WORK = "QPID_WORK"; + + private BrokerProperties() + { + } + + public static Locale getLocale() + { + Locale locale = Locale.US; + String localeSetting = System.getProperty(BrokerProperties.PROPERTY_LOCALE); + if (localeSetting != null) + { + String[] localeParts = localeSetting.split("_"); + String language = (localeParts.length > 0 ? localeParts[0] : ""); + String country = (localeParts.length > 1 ? localeParts[1] : ""); + String variant = ""; + if (localeParts.length > 2) + { + variant = localeSetting.substring(language.length() + 1 + country.length() + 1); + } + locale = new Locale(language, country, variant); + } + return locale; + } +} 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 new file mode 100644 index 0000000000..4771c4af6d --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntry.java @@ -0,0 +1,213 @@ +/* + * + * 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; + +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; + +public class ConfigurationEntry +{ + public static final String ATTRIBUTE_NAME = "name"; + + private final UUID _id; + private final String _type; + private final Map<String, Object> _attributes; + private final Set<UUID> _childrenIds; + private final ConfigurationEntryStore _store; + + public ConfigurationEntry(UUID id, String type, Map<String, Object> attributes, Set<UUID> childrenIds, + ConfigurationEntryStore store) + { + super(); + _id = id; + _type = type; + _attributes = attributes; + _childrenIds = childrenIds; + _store = store; + } + + public UUID getId() + { + return _id; + } + + public String getType() + { + return _type; + } + + public Map<String, Object> getAttributes() + { + return _attributes; + } + + public Set<UUID> getChildrenIds() + { + return _childrenIds; + } + + public ConfigurationEntryStore getStore() + { + return _store; + } + + /** + * Returns this entry's children. The collection should not be modified. + */ + public Map<String, Collection<ConfigurationEntry>> getChildren() + { + Map<String, Collection<ConfigurationEntry>> children = null; + if (_childrenIds == null) + { + children = Collections.emptyMap(); + } + else + { + children = new HashMap<String, Collection<ConfigurationEntry>>(); + for (UUID childId : _childrenIds) + { + ConfigurationEntry entry = _store.getEntry(childId); + String type = entry.getType(); + Collection<ConfigurationEntry> childrenOfType = children.get(type); + if (childrenOfType == null) + { + childrenOfType = new ArrayList<ConfigurationEntry>(); + children.put(type, childrenOfType); + } + childrenOfType.add(entry); + } + } + return Collections.unmodifiableMap(children); + } + + public boolean hasChild(UUID id) + { + return _childrenIds.contains(id); + } + + @Override + public int hashCode() + { + return _id.hashCode(); + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + + ConfigurationEntry other = (ConfigurationEntry) obj; + if (_id == null) + { + if (other._id != null) + { + return false; + } + } + else if (!_id.equals(other._id)) + { + return false; + } + + if (_type == null) + { + if (other._type != null) + { + return false; + } + } + else if (!_type.equals(other._type)) + { + return false; + } + + if (_store == null) + { + if (other._store != null) + { + return false; + } + } + else if (!_store.equals(other._store)) + { + return false; + } + + if (_childrenIds == null) + { + if (other._childrenIds != null) + { + return false; + } + } + else if (!_childrenIds.equals(other._childrenIds)) + { + return false; + } + + if (_attributes == null) + { + if (other._attributes != null) + { + return false; + } + } + else if (!_attributes.equals(other._attributes)) + { + return false; + } + return true; + } + + @Override + public String toString() + { + 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/ConfigurationEntryStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStore.java new file mode 100644 index 0000000000..08736c36f1 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStore.java @@ -0,0 +1,37 @@ +/* + * + * 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; + +import java.util.UUID; + +public interface ConfigurationEntryStore +{ + void open(String storeLocation); + + ConfigurationEntry getRootEntry(); + + ConfigurationEntry getEntry(UUID id); + + void save(ConfigurationEntry... entries); + + UUID[] remove(UUID... entryIds); + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationStoreFactory.java new file mode 100644 index 0000000000..dced38d260 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationStoreFactory.java @@ -0,0 +1,35 @@ +/* + * + * 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; + + +public interface ConfigurationStoreFactory +{ + /** + * Returns the type of the store this factory can create + */ + public String getStoreType(); + + /** + * Creates the store instance. + */ + public ConfigurationEntryStore createStore(); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfiguredObjectRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfiguredObjectRecoverer.java new file mode 100644 index 0000000000..65d97e6db1 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfiguredObjectRecoverer.java @@ -0,0 +1,28 @@ +/* + * + * 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; + +import org.apache.qpid.server.model.ConfiguredObject; + +public interface ConfiguredObjectRecoverer<T extends ConfiguredObject> +{ + T create(RecovererProvider recovererProvider, ConfigurationEntry entry, ConfiguredObject... parents); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/IllegalConfigurationException.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/IllegalConfigurationException.java new file mode 100644 index 0000000000..bedd470ddf --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/IllegalConfigurationException.java @@ -0,0 +1,37 @@ +/* + * + * 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; + +public class IllegalConfigurationException extends RuntimeException +{ + private static final long serialVersionUID = 1130064756291179812L; + + public IllegalConfigurationException(String message) + { + super(message); + } + + public IllegalConfigurationException(String message, Throwable cause) + { + super(message, cause); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/RecovererProvider.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/RecovererProvider.java new file mode 100644 index 0000000000..963d019ec3 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/RecovererProvider.java @@ -0,0 +1,28 @@ +/* + * + * 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; + +import org.apache.qpid.server.model.ConfiguredObject; + +public interface RecovererProvider +{ + ConfiguredObjectRecoverer<? extends ConfiguredObject> getRecoverer(String type); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java deleted file mode 100644 index 62ed2e44f4..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java +++ /dev/null @@ -1,994 +0,0 @@ -/* - * 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; - -import java.io.File; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Map.Entry; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManagerFactory; -import org.apache.commons.configuration.CompositeConfiguration; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.ConfigurationFactory; -import org.apache.commons.configuration.HierarchicalConfiguration; -import org.apache.commons.configuration.SystemConfiguration; -import org.apache.commons.configuration.XMLConfiguration; -import org.apache.log4j.Logger; -import org.apache.qpid.server.configuration.plugins.AbstractConfiguration; -import org.apache.qpid.server.exchange.DefaultExchangeFactory; -import org.apache.qpid.server.protocol.AmqpProtocolVersion; -import org.apache.qpid.server.queue.AMQQueueFactory; -import org.apache.qpid.server.registry.ApplicationRegistry; -import static org.apache.qpid.transport.ConnectionSettings.WILDCARD_ADDRESS; - -public class ServerConfiguration extends AbstractConfiguration -{ - protected static final Logger _logger = Logger.getLogger(ServerConfiguration.class); - - // Default Configuration values - public static final int DEFAULT_BUFFER_SIZE = 262144; - public static final String DEFAULT_STATUS_UPDATES = "on"; - public static final String SECURITY_CONFIG_RELOADED = "SECURITY CONFIGURATION RELOADED"; - - public static final int DEFAULT_FRAME_SIZE = 65536; - public static final int DEFAULT_PORT = 5672; - public static final int DEFAULT_SSL_PORT = 5671; - public static final long DEFAULT_HOUSEKEEPING_PERIOD = 30000L; - public static final int DEFAULT_JMXPORT_REGISTRYSERVER = 8999; - public static final int JMXPORT_CONNECTORSERVER_OFFSET = 100; - public static final int DEFAULT_HTTP_MANAGEMENT_PORT = 8080; - public static final int DEFAULT_HTTPS_MANAGEMENT_PORT = 8443; - public static final long DEFAULT_MINIMUM_ALERT_REPEAT_GAP = 30000l; - public static final String SKIP_SIGHUP_HANDLER_REGISTRATION = "qpid.skip_sighup_handler_registration"; - public static final String USE_CUSTOM_RMI_SOCKET_FACTORY = "qpid.use_custom_rmi_socket_factory"; - - public static final String QPID_HOME = "QPID_HOME"; - public static final String QPID_WORK = "QPID_WORK"; - public static final String LIB_DIR = "lib"; - - private Map<String, VirtualHostConfiguration> _virtualHosts = new HashMap<String, VirtualHostConfiguration>(); - - private File _configFile; - private File _vhostsFile; - private String _qpidWork; - private String _qpidHome; - - // Map of environment variables to config items - private static final Map<String, String> envVarMap = new HashMap<String, String>(); - - // Configuration values to be read from the configuration file - //todo Move all properties to static values to ensure system testing can be performed. - public static final String MGMT_CUSTOM_REGISTRY_SOCKET = "management.custom-registry-socket"; - public static final String MGMT_JMXPORT_REGISTRYSERVER = "management.jmxport.registryServer"; - public static final String MGMT_JMXPORT_CONNECTORSERVER = "management.jmxport.connectorServer"; - public static final String SECURITY_DEFAULT_AUTH_MANAGER = "security.default-auth-manager"; - public static final String SECURITY_PORT_MAPPINGS_PORT_MAPPING_AUTH_MANAGER = "security.port-mappings.port-mapping.auth-manager"; - public static final String SECURITY_PORT_MAPPINGS_PORT_MAPPING_PORT = "security.port-mappings.port-mapping.port"; - public static final String STATUS_UPDATES = "status-updates"; - public static final String ADVANCED_LOCALE = "advanced.locale"; - public static final String CONNECTOR_AMQP10ENABLED = "connector.amqp10enabled"; - public static final String CONNECTOR_AMQP010ENABLED = "connector.amqp010enabled"; - public static final String CONNECTOR_AMQP091ENABLED = "connector.amqp091enabled"; - public static final String CONNECTOR_AMQP09ENABLED = "connector.amqp09enabled"; - public static final String CONNECTOR_AMQP08ENABLED = "connector.amqp08enabled"; - public static final String CONNECTOR_AMQP_SUPPORTED_REPLY = "connector.amqpDefaultSupportedProtocolReply"; - public static final String CONNECTOR_INCLUDE_10 = "connector.include10"; - public static final String CONNECTOR_INCLUDE_010 = "connector.include010"; - public static final String CONNECTOR_INCLUDE_091 = "connector.include091"; - public static final String CONNECTOR_INCLUDE_09 = "connector.include09"; - public static final String CONNECTOR_INCLUDE_08 = "connector.include08"; - - { - envVarMap.put("QPID_PORT", "connector.port"); - envVarMap.put("QPID_SSLPORT", "connector.ssl.port"); - envVarMap.put("QPID_JMXPORT_REGISTRYSERVER", MGMT_JMXPORT_REGISTRYSERVER); - envVarMap.put("QPID_JMXPORT_CONNECTORSERVER", MGMT_JMXPORT_CONNECTORSERVER); - envVarMap.put("QPID_FRAMESIZE", "advanced.framesize"); - envVarMap.put("QPID_MSGAUTH", "security.msg-auth"); - envVarMap.put("QPID_AUTOREGISTER", "auto_register"); - envVarMap.put("QPID_MANAGEMENTENABLED", "management.enabled"); - envVarMap.put("QPID_HTTPMANAGEMENTENABLED", "management.http.enabled"); - envVarMap.put("QPID_HTTPMANAGEMENTPORT", "management.http.port"); - envVarMap.put("QPID_HEARTBEATDELAY", "heartbeat.delay"); - envVarMap.put("QPID_HEARTBEATTIMEOUTFACTOR", "heartbeat.timeoutFactor"); - envVarMap.put("QPID_MAXIMUMMESSAGEAGE", "maximumMessageAge"); - envVarMap.put("QPID_MAXIMUMMESSAGECOUNT", "maximumMessageCount"); - envVarMap.put("QPID_MAXIMUMQUEUEDEPTH", "maximumQueueDepth"); - envVarMap.put("QPID_MAXIMUMMESSAGESIZE", "maximumMessageSize"); - envVarMap.put("QPID_MAXIMUMCHANNELCOUNT", "maximumChannelCount"); - envVarMap.put("QPID_MINIMUMALERTREPEATGAP", "minimumAlertRepeatGap"); - envVarMap.put("QPID_QUEUECAPACITY", "capacity"); - envVarMap.put("QPID_FLOWRESUMECAPACITY", "flowResumeCapacity"); - envVarMap.put("QPID_SOCKETRECEIVEBUFFER", "connector.socketReceiveBuffer"); - envVarMap.put("QPID_SOCKETWRITEBUFFER", "connector.socketWriteBuffer"); - envVarMap.put("QPID_TCPNODELAY", "connector.tcpNoDelay"); - envVarMap.put("QPID_STATUS-UPDATES", "status-updates"); - } - - /** - * Loads the given file and sets up the HUP signal handler. - * - * This will load the file and present the root level properties but will - * not perform any virtualhost configuration. - * <p> - * To perform this {@link #initialise()} must be called. - * <p> - * This has been made a two step process to allow the Plugin Manager and - * Configuration Manager to be initialised in the Application Registry. - * <p> - * If using this ServerConfiguration via an ApplicationRegistry there is no - * need to explicitly call {@link #initialise()} as this is done via the - * {@link ApplicationRegistry#initialise()} method. - * - * @param configurationURL - * @throws org.apache.commons.configuration.ConfigurationException - */ - public ServerConfiguration(File configurationURL) throws ConfigurationException - { - this(parseConfig(configurationURL)); - _configFile = configurationURL; - } - - /** - * Wraps the given Commons Configuration as a ServerConfiguration. - * - * Mainly used during testing and in locations where configuration is not - * desired but the interface requires configuration. - * <p> - * If the given configuration has VirtualHost configuration then - * {@link #initialise()} must be called to perform the required setup. - * <p> - * This has been made a two step process to allow the Plugin Manager and - * Configuration Manager to be initialised in the Application Registry. - * <p> - * If using this ServerConfiguration via an ApplicationRegistry there is no - * need to explicitly call {@link #initialise()} as this is done via the - * {@link ApplicationRegistry#initialise()} method. - * - * @param conf - */ - public ServerConfiguration(Configuration conf) - { - setConfig(conf); - } - - /** - * Processes this configuration and setups any VirtualHosts defined in the - * configuration. - * - * <p> - * Called by {@link ApplicationRegistry#initialise()}. - * <p> - * NOTE: A DEFAULT ApplicationRegistry must exist when using this method - * or a new ApplicationRegistry will be created. - * - * @throws ConfigurationException - */ - public void initialise() throws ConfigurationException - { - setConfiguration("", getConfig()); - setupVirtualHosts(getConfig()); - } - - public String[] getElementsProcessed() - { - return new String[] { "" }; - } - - @Override - public void validateConfiguration() throws ConfigurationException - { - // Support for security.jmx.access was removed when JMX access rights were incorporated into the main ACL. - // This ensure that users remove the element from their configuration file. - - if (getListValue("security.jmx.access").size() > 0) - { - String message = "Validation error : security/jmx/access is no longer a supported element within the configuration xml." - + (_configFile == null ? "" : " Configuration file : " + _configFile); - throw new ConfigurationException(message); - } - - if (getListValue("security.jmx.principal-database").size() > 0) - { - String message = "Validation error : security/jmx/principal-database is no longer a supported element within the configuration xml." - + (_configFile == null ? "" : " Configuration file : " + _configFile); - throw new ConfigurationException(message); - } - - if (getListValue("security.principal-databases.principal-database(0).class").size() > 0) - { - String message = "Validation error : security/principal-databases is no longer supported within the configuration xml." - + (_configFile == null ? "" : " Configuration file : " + _configFile); - throw new ConfigurationException(message); - } - - // QPID-3266. Tidy up housekeeping configuration option for scheduling frequency - if (contains("housekeeping.expiredMessageCheckPeriod")) - { - String message = "Validation error : housekeeping/expiredMessageCheckPeriod must be replaced by housekeeping/checkPeriod." - + (_configFile == null ? "" : " Configuration file : " + _configFile); - throw new ConfigurationException(message); - } - - String[] ports = getConfig().getStringArray(SECURITY_PORT_MAPPINGS_PORT_MAPPING_PORT); - String[] authManagers = getConfig().getStringArray(SECURITY_PORT_MAPPINGS_PORT_MAPPING_AUTH_MANAGER); - if (ports.length != authManagers.length) - { - throw new ConfigurationException("Validation error: Each port-mapping must have exactly one port and exactly one auth-manager."); - } - - // QPID-3517: Inconsistency in capitalisation in the SSL configuration keys used within the connector and management configuration - // sections. For the moment, continue to understand both but generate a deprecated warning if the less preferred keystore is used. - for (String key : new String[] {"management.ssl.keystorePath", - "management.ssl.keystorePassword," + - "connector.ssl.keystorePath", - "connector.ssl.keystorePassword"}) - { - if (contains(key)) - { - final String deprecatedXpath = key.replaceAll("\\.", "/"); - final String preferredXpath = deprecatedXpath.replaceAll("keystore", "keyStore"); - _logger.warn("Validation warning: " + deprecatedXpath + " is deprecated and must be replaced by " + preferredXpath - + (_configFile == null ? "" : " Configuration file : " + _configFile)); - } - } - - // QPID-3739 certType was a misleading name. - if (contains("connector.ssl.certType")) - { - _logger.warn("Validation warning: connector/ssl/certType is deprecated and must be replaced by connector/ssl/keyManagerFactoryAlgorithm" - + (_configFile == null ? "" : " Configuration file : " + _configFile)); - } - } - - /* - * Modified to enforce virtualhosts configuration in external file or main file, but not - * both, as a fix for QPID-2360 and QPID-2361. - */ - @SuppressWarnings("unchecked") - protected void setupVirtualHosts(Configuration conf) throws ConfigurationException - { - List<String> vhostFiles = (List) conf.getList("virtualhosts"); - Configuration vhostConfig = conf.subset("virtualhosts"); - - // Only one configuration mechanism allowed - if (!vhostFiles.isEmpty() && !vhostConfig.subset("virtualhost").isEmpty()) - { - throw new ConfigurationException("Only one of external or embedded virtualhosts configuration allowed."); - } - - // We can only have one vhosts XML file included - if (vhostFiles.size() > 1) - { - throw new ConfigurationException("Only one external virtualhosts configuration file allowed, multiple filenames found."); - } - - // Virtualhost configuration object - Configuration vhostConfiguration = new HierarchicalConfiguration(); - - // Load from embedded configuration if possible - if (!vhostConfig.subset("virtualhost").isEmpty()) - { - vhostConfiguration = vhostConfig; - } - else - { - // Load from the external configuration if possible - for (String fileName : vhostFiles) - { - // Open the vhosts XML file and copy values from it to our config - _vhostsFile = new File(fileName); - if (!_vhostsFile.exists()) - { - throw new ConfigurationException("Virtualhosts file does not exist"); - } - vhostConfiguration = parseConfig(new File(fileName)); - - // save the default virtualhost name - String defaultVirtualHost = vhostConfiguration.getString("default"); - getConfig().setProperty("virtualhosts.default", defaultVirtualHost); - } - } - - // Now extract the virtual host names from the configuration object - List hosts = vhostConfiguration.getList("virtualhost.name"); - for (int j = 0; j < hosts.size(); j++) - { - String name = (String) hosts.get(j); - - // Add the virtual hosts to the server configuration - VirtualHostConfiguration virtualhost = new VirtualHostConfiguration(name, vhostConfiguration.subset("virtualhost." + escapeTagName(name))); - _virtualHosts.put(virtualhost.getName(), virtualhost); - } - } - - private static void substituteEnvironmentVariables(Configuration conf) - { - for (Entry<String, String> var : envVarMap.entrySet()) - { - String val = System.getenv(var.getKey()); - if (val != null) - { - conf.setProperty(var.getValue(), val); - } - } - } - - private static Configuration parseConfig(File file) throws ConfigurationException - { - ConfigurationFactory factory = new ConfigurationFactory(); - factory.setConfigurationFileName(file.getAbsolutePath()); - Configuration conf = factory.getConfiguration(); - - Iterator<?> keys = conf.getKeys(); - if (!keys.hasNext()) - { - keys = null; - conf = flatConfig(file); - } - - substituteEnvironmentVariables(conf); - - return conf; - } - - /** - * Check the configuration file to see if status updates are enabled. - * - * @return true if status updates are enabled - */ - public boolean getStatusUpdatesEnabled() - { - // Retrieve the setting from configuration but default to on. - String value = getStringValue(STATUS_UPDATES, DEFAULT_STATUS_UPDATES); - - return value.equalsIgnoreCase("on"); - } - - /** - * The currently defined {@see Locale} for this broker - * - * @return the configuration defined locale - */ - public Locale getLocale() - { - String localeString = getStringValue(ADVANCED_LOCALE); - // Expecting locale of format langauge_country_variant - - // If the configuration does not have a defined locale use the JVM default - if (localeString == null) - { - return Locale.getDefault(); - } - - String[] parts = localeString.split("_"); - - Locale locale; - switch (parts.length) - { - case 1: - locale = new Locale(localeString); - break; - case 2: - locale = new Locale(parts[0], parts[1]); - break; - default: - StringBuilder variant = new StringBuilder(parts[2]); - // If we have a variant such as the Java doc suggests for Spanish - // Traditional_WIN we may end up with more than 3 parts on a - // split with '_'. So we should recombine the variant. - if (parts.length > 3) - { - for (int index = 3; index < parts.length; index++) - { - variant.append('_').append(parts[index]); - } - } - - locale = new Locale(parts[0], parts[1], variant.toString()); - } - - return locale; - } - - // Our configuration class needs to make the interpolate method - // public so it can be called below from the config method. - public static class MyConfiguration extends CompositeConfiguration - { - public String interpolate(String obj) - { - return super.interpolate(obj); - } - } - - public final static Configuration flatConfig(File file) throws ConfigurationException - { - // We have to override the interpolate methods so that - // interpolation takes place across the entirety of the - // composite configuration. Without doing this each - // configuration object only interpolates variables defined - // inside itself. - final MyConfiguration conf = new MyConfiguration(); - conf.addConfiguration(new SystemConfiguration() - { - protected String interpolate(String o) - { - return conf.interpolate(o); - } - }); - conf.addConfiguration(new XMLConfiguration(file) - { - protected String interpolate(String o) - { - return conf.interpolate(o); - } - }); - return conf; - } - - public String getConfigurationURL() - { - return _configFile == null ? "" : _configFile.getAbsolutePath(); - } - - - public String getQpidWork() - { - if ( _qpidWork == null ) - { - return System.getProperty(QPID_WORK, System.getProperty("java.io.tmpdir")); - } - else - { - return _qpidWork; - } - } - - public String getQpidHome() - { - if ( _qpidHome == null ) - { - return System.getProperty(QPID_HOME); - } - else - { - return _qpidHome; - } - } - - public void setJMXPortRegistryServer(int registryServerPort) - { - getConfig().setProperty(MGMT_JMXPORT_REGISTRYSERVER, registryServerPort); - } - - public int getJMXPortRegistryServer() - { - return getIntValue(MGMT_JMXPORT_REGISTRYSERVER, DEFAULT_JMXPORT_REGISTRYSERVER); - } - - public void setJMXPortConnectorServer(int connectorServerPort) - { - getConfig().setProperty(MGMT_JMXPORT_CONNECTORSERVER, connectorServerPort); - } - - public int getJMXConnectorServerPort() - { - return getIntValue(MGMT_JMXPORT_CONNECTORSERVER, getJMXPortRegistryServer() + JMXPORT_CONNECTORSERVER_OFFSET); - } - - public boolean getUseCustomRMISocketFactory() - { - return getBooleanValue(MGMT_CUSTOM_REGISTRY_SOCKET, - Boolean.parseBoolean(System.getProperty(USE_CUSTOM_RMI_SOCKET_FACTORY, "true"))); - } - - public void setUseCustomRMISocketFactory(boolean bool) - { - getConfig().setProperty(MGMT_CUSTOM_REGISTRY_SOCKET, bool); - } - - public boolean getPlatformMbeanserver() - { - return getBooleanValue("management.platform-mbeanserver", true); - } - - public boolean getHTTPManagementEnabled() - { - return getBooleanValue("management.http.enabled", true); - } - - public int getHTTPManagementPort() - { - return getIntValue("management.http.port", DEFAULT_HTTP_MANAGEMENT_PORT); - } - - public boolean getHTTPManagementBasicAuth() - { - return getBooleanValue("management.http.basic-auth", false); - } - - /** - * @return value in seconds - */ - public int getHTTPManagementSessionTimeout() - { - return getIntValue("management.http.session-timeout", 60 * 15); - } - - public boolean getHTTPSManagementEnabled() - { - return getBooleanValue("management.https.enabled", false); - } - - public int getHTTPSManagementPort() - { - return getIntValue("management.https.port", DEFAULT_HTTPS_MANAGEMENT_PORT); - } - - public boolean getHTTPSManagementBasicAuth() - { - return getBooleanValue("management.https.basic-auth", true); - } - - public boolean getHTTPManagementSaslAuthEnabled() - { - return getBooleanValue("management.http.sasl-auth", true); - } - - public boolean getHTTPSManagementSaslAuthEnabled() - { - return getBooleanValue("management.https.sasl-auth", true); - } - - public String[] getVirtualHosts() - { - return _virtualHosts.keySet().toArray(new String[_virtualHosts.size()]); - } - - public VirtualHostConfiguration getVirtualHostConfig(String name) - { - return _virtualHosts.get(name); - } - - public void setVirtualHostConfig(VirtualHostConfiguration config) - { - _virtualHosts.put(config.getName(), config); - } - - public int getFrameSize() - { - return getIntValue("advanced.framesize", DEFAULT_FRAME_SIZE); - } - - public boolean getSynchedClocks() - { - return getBooleanValue("advanced.synced-clocks"); - } - - public boolean getMsgAuth() - { - return getBooleanValue("security.msg-auth"); - } - - public String getDefaultAuthenticationManager() - { - return getStringValue(SECURITY_DEFAULT_AUTH_MANAGER); - } - - public Map<Integer, String> getPortAuthenticationMappings() - { - String[] ports = getConfig().getStringArray(SECURITY_PORT_MAPPINGS_PORT_MAPPING_PORT); - String[] authManagers = getConfig().getStringArray(SECURITY_PORT_MAPPINGS_PORT_MAPPING_AUTH_MANAGER); - - Map<Integer,String> portMappings = new HashMap<Integer, String>(); - for(int i = 0; i < ports.length; i++) - { - portMappings.put(Integer.valueOf(ports[i]), authManagers[i]); - } - - return portMappings; - } - - - public String getManagementKeyStorePath() - { - // note difference in capitalisation used in fallback - final String fallback = getStringValue("management.ssl.keystorePath"); - return getStringValue("management.ssl.keyStorePath", fallback); - } - - public boolean getManagementSSLEnabled() - { - return getBooleanValue("management.ssl.enabled", false); - } - - public String getManagementKeyStorePassword() - { - // note difference in capitalisation used in fallback - final String fallback = getStringValue("management.ssl.keystorePassword"); - return getStringValue("management.ssl.keyStorePassword", fallback); - } - - public boolean getQueueAutoRegister() - { - return getBooleanValue("queue.auto_register", true); - } - - public boolean getJMXManagementEnabled() - { - return getBooleanValue("management.enabled", true); - } - - public int getHeartBeatDelay() - { - return getIntValue("heartbeat.delay", 5); - } - - public double getHeartBeatTimeout() - { - return getDoubleValue("heartbeat.timeoutFactor", 2.0); - } - - public long getMaximumMessageAge() - { - return getLongValue("maximumMessageAge"); - } - - public long getMaximumMessageCount() - { - return getLongValue("maximumMessageCount"); - } - - public long getMaximumQueueDepth() - { - return getLongValue("maximumQueueDepth"); - } - - public long getMaximumMessageSize() - { - return getLongValue("maximumMessageSize"); - } - - public long getMinimumAlertRepeatGap() - { - return getLongValue("minimumAlertRepeatGap", DEFAULT_MINIMUM_ALERT_REPEAT_GAP); - } - - public long getCapacity() - { - return getLongValue("capacity"); - } - - public long getFlowResumeCapacity() - { - return getLongValue("flowResumeCapacity", getCapacity()); - } - - public int getConnectorProcessors() - { - return getIntValue("connector.processors", 4); - } - - public List getPorts() - { - return getListValue("connector.port", Collections.<Integer>singletonList(DEFAULT_PORT)); - } - - public List getPortExclude10() - { - return getListValue("connector.non10port"); - } - - public List getPortExclude010() - { - return getListValue("connector.non010port"); - } - - public List getPortExclude091() - { - return getListValue("connector.non091port"); - } - - public List getPortExclude09() - { - return getListValue("connector.non09port"); - } - - public List getPortExclude08() - { - return getListValue("connector.non08port"); - } - - public List getPortInclude08() - { - return getListValue(CONNECTOR_INCLUDE_08); - } - - public List getPortInclude09() - { - return getListValue(CONNECTOR_INCLUDE_09); - } - - public List getPortInclude091() - { - return getListValue(CONNECTOR_INCLUDE_091); - } - - public List getPortInclude010() - { - return getListValue(CONNECTOR_INCLUDE_010); - } - - public List getPortInclude10() - { - return getListValue(CONNECTOR_INCLUDE_10); - } - - public String getBind() - { - return getStringValue("connector.bind", WILDCARD_ADDRESS); - } - - public int getReceiveBufferSize() - { - return getIntValue("connector.socketReceiveBuffer", DEFAULT_BUFFER_SIZE); - } - - public int getWriteBufferSize() - { - return getIntValue("connector.socketWriteBuffer", DEFAULT_BUFFER_SIZE); - } - - public boolean getTcpNoDelay() - { - return getBooleanValue("connector.tcpNoDelay", true); - } - - public boolean getEnableSSL() - { - return getBooleanValue("connector.ssl.enabled"); - } - - public boolean getSSLOnly() - { - return getBooleanValue("connector.ssl.sslOnly"); - } - - public List getSSLPorts() - { - return getListValue("connector.ssl.port", Collections.<Integer>singletonList(DEFAULT_SSL_PORT)); - } - - public String getConnectorKeyStorePath() - { - final String fallback = getStringValue("connector.ssl.keystorePath"); // pre-0.13 broker supported this name. - return getStringValue("connector.ssl.keyStorePath", fallback); - } - - public String getConnectorKeyStorePassword() - { - final String fallback = getStringValue("connector.ssl.keystorePassword"); // pre-0.13 brokers supported this name. - return getStringValue("connector.ssl.keyStorePassword", fallback); - } - - public String getConnectorKeyStoreType() - { - return getStringValue("connector.ssl.keyStoreType", "JKS"); - } - - public String getConnectorKeyManagerFactoryAlgorithm() - { - final String systemFallback = KeyManagerFactory.getDefaultAlgorithm(); - // deprecated, pre-0.17 brokers supported this name. - final String fallback = getStringValue("connector.ssl.certType", systemFallback); - return getStringValue("connector.ssl.keyManagerFactoryAlgorithm", fallback); - } - - public String getConnectorTrustStorePath() - { - return getStringValue("connector.ssl.trustStorePath", null); - } - - public String getConnectorTrustStorePassword() - { - return getStringValue("connector.ssl.trustStorePassword", null); - } - - public String getConnectorTrustStoreType() - { - return getStringValue("connector.ssl.trustStoreType", "JKS"); - } - - public String getConnectorTrustManagerFactoryAlgorithm() - { - return getStringValue("connector.ssl.trustManagerFactoryAlgorithm", TrustManagerFactory.getDefaultAlgorithm()); - } - - public String getCertAlias() - { - return getStringValue("connector.ssl.certAlias", null); - } - - public boolean needClientAuth() - { - return getConfig().getBoolean("connector.ssl.needClientAuth", false); - } - - public boolean wantClientAuth() - { - return getConfig().getBoolean("connector.ssl.wantClientAuth", false); - } - - public String getDefaultVirtualHost() - { - return getStringValue("virtualhosts.default"); - } - - public void setDefaultVirtualHost(String vhost) - { - getConfig().setProperty("virtualhosts.default", vhost); - } - - public void setHousekeepingCheckPeriod(long value) - { - getConfig().setProperty("housekeeping.checkPeriod", value); - } - - public long getHousekeepingCheckPeriod() - { - return getLongValue("housekeeping.checkPeriod", DEFAULT_HOUSEKEEPING_PERIOD); - } - - public long getStatisticsSamplePeriod() - { - return getConfig().getLong("statistics.sample.period", 5000L); - } - - public boolean isStatisticsGenerationBrokerEnabled() - { - return getConfig().getBoolean("statistics.generation.broker", false); - } - - public boolean isStatisticsGenerationVirtualhostsEnabled() - { - return getConfig().getBoolean("statistics.generation.virtualhosts", false); - } - - public boolean isStatisticsGenerationConnectionsEnabled() - { - return getConfig().getBoolean("statistics.generation.connections", false); - } - - public long getStatisticsReportingPeriod() - { - return getConfig().getLong("statistics.reporting.period", 0L); - } - - public boolean isStatisticsReportResetEnabled() - { - return getConfig().getBoolean("statistics.reporting.reset", false); - } - - public int getMaxChannelCount() - { - return getIntValue("maximumChannelCount", 256); - } - - /** - * List of Broker features that have been disabled within configuration. Disabled - * features won't be advertised to the clients on connection. - * - * @return list of disabled features, or empty list if no features are disabled. - */ - public List<String> getDisabledFeatures() - { - final List<String> disabledFeatures = getListValue("disabledFeatures", Collections.emptyList()); - return disabledFeatures; - } - - public boolean getManagementRightsInferAllAccess() - { - return getBooleanValue("management.managementRightsInferAllAccess", true); - } - - public int getMaxDeliveryCount() - { - return getConfig().getInt("maximumDeliveryCount", 0); - } - - /** - * Check if dead letter queue delivery is enabled, defaults to disabled if not set. - */ - public boolean isDeadLetterQueueEnabled() - { - return getConfig().getBoolean("deadLetterQueues", false); - } - - /** - * String to affix to end of queue name when generating an alternate exchange for DLQ purposes. - */ - public String getDeadLetterExchangeSuffix() - { - return getConfig().getString("deadLetterExchangeSuffix", DefaultExchangeFactory.DEFAULT_DLE_NAME_SUFFIX); - } - - /** - * String to affix to end of queue name when generating a queue for DLQ purposes. - */ - public String getDeadLetterQueueSuffix() - { - return getConfig().getString("deadLetterQueueSuffix", AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX); - } - - public boolean isAmqp10enabled() - { - return getConfig().getBoolean(CONNECTOR_AMQP10ENABLED, true); - } - - public boolean isAmqp010enabled() - { - return getConfig().getBoolean(CONNECTOR_AMQP010ENABLED, true); - } - - public boolean isAmqp091enabled() - { - return getConfig().getBoolean(CONNECTOR_AMQP091ENABLED, true); - } - - public boolean isAmqp09enabled() - { - return getConfig().getBoolean(CONNECTOR_AMQP09ENABLED, true); - } - - public boolean isAmqp08enabled() - { - return getConfig().getBoolean(CONNECTOR_AMQP08ENABLED, true); - } - - /** - * Returns the configured default reply to an unsupported AMQP protocol initiation, or null if there is none - */ - public AmqpProtocolVersion getDefaultSupportedProtocolReply() - { - String reply = getConfig().getString(CONNECTOR_AMQP_SUPPORTED_REPLY, null); - - return reply == null ? null : AmqpProtocolVersion.valueOf(reply); - } - - public void setQpidWork(String path) - { - _qpidWork = path; - } - - public void setQpidHome(String path) - { - _qpidHome = path; - } - - - -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerNetworkTransportConfiguration.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerNetworkTransportConfiguration.java deleted file mode 100644 index 51dcc38c47..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerNetworkTransportConfiguration.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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; - -import java.net.InetSocketAddress; -import org.apache.qpid.transport.NetworkTransportConfiguration; - -public class ServerNetworkTransportConfiguration implements NetworkTransportConfiguration -{ - private final ServerConfiguration _serverConfig; - private final String _transport; - private InetSocketAddress _address; - - public ServerNetworkTransportConfiguration(final ServerConfiguration serverConfig, - final InetSocketAddress address, - final String transport) - { - _serverConfig = serverConfig; - _address = address; - _transport = transport; - } - - public Boolean getTcpNoDelay() - { - return _serverConfig.getTcpNoDelay(); - } - - public Integer getSendBufferSize() - { - return _serverConfig.getWriteBufferSize(); - } - - public Integer getReceiveBufferSize() - { - return _serverConfig.getReceiveBufferSize(); - } - - public Integer getPort() - { - return _address.getPort(); - } - - public String getHost() - { - return _address.getHostName(); - } - - public String getTransport() - { - return _transport; - } - - public Integer getConnectorProcessors() - { - return _serverConfig.getConnectorProcessors(); - } - - public InetSocketAddress getAddress() - { - return _address; - } - - public boolean needClientAuth() - { - return _serverConfig.needClientAuth(); - } - - @Override - public boolean wantClientAuth() - { - return _serverConfig.wantClientAuth(); - } -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/VirtualHostConfiguration.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/VirtualHostConfiguration.java index a14977ec1b..eada676b65 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/VirtualHostConfiguration.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/VirtualHostConfiguration.java @@ -25,9 +25,10 @@ import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.qpid.server.configuration.plugins.AbstractConfiguration; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.store.MemoryMessageStore; +import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -38,13 +39,50 @@ public class VirtualHostConfiguration extends AbstractConfiguration private final String _name; private final Map<String, QueueConfiguration> _queues = new HashMap<String, QueueConfiguration>(); private final Map<String, ExchangeConfiguration> _exchanges = new HashMap<String, ExchangeConfiguration>(); + private final Broker _broker; + private final long _defaultHouseKeepingCheckPeriod; - public VirtualHostConfiguration(String name, Configuration config) throws ConfigurationException + public VirtualHostConfiguration(String name, Configuration config, Broker broker) throws ConfigurationException { _name = name; + _broker = broker; + + // store value of this attribute for running life of virtual host since updating of this value has no run-time effect + _defaultHouseKeepingCheckPeriod = ((Number)_broker.getAttribute(Broker.HOUSEKEEPING_CHECK_PERIOD)).longValue(); setConfiguration(config); } + public VirtualHostConfiguration(String name, File configurationFile, Broker broker) throws ConfigurationException + { + this(name, loadConfiguration(name, configurationFile), broker); + } + + private static Configuration loadConfiguration(String name, File configurationFile) throws ConfigurationException + { + Configuration configuration = null; + if (configurationFile == null) + { + throw new IllegalConfigurationException("Virtualhost configuration file must be supplied!"); + } + else + { + Configuration virtualHostConfig = XmlConfigurationUtilities.parseConfig(configurationFile, null); + + // check if it is an old virtual host configuration file which has an element of the same name as virtual host + Configuration config = virtualHostConfig.subset("virtualhost." + XmlConfigurationUtilities.escapeTagName(name)); + if (config.isEmpty()) + { + // assume it is a new configuration which does not have an element of the same name as the virtual host + configuration = virtualHostConfig; + } + else + { + configuration = config; + } + } + return configuration; + } + /** * Apply the given configuration to this VirtualHostConfiguration * @@ -82,7 +120,7 @@ public class VirtualHostConfiguration extends AbstractConfiguration public long getHousekeepingCheckPeriod() { - return getLongValue("housekeeping.checkPeriod", ApplicationRegistry.getInstance().getConfiguration().getHousekeepingCheckPeriod()); + return getLongValue("housekeeping.checkPeriod", _defaultHouseKeepingCheckPeriod); } public Configuration getStoreConfiguration() @@ -139,37 +177,37 @@ public class VirtualHostConfiguration extends AbstractConfiguration public int getMaximumMessageAge() { - return getIntValue("queues.maximumMessageAge"); + return getIntValue("queues.maximumMessageAge", getBrokerAttributeAsInt(Broker.ALERT_THRESHOLD_MESSAGE_AGE)); } public Long getMaximumQueueDepth() { - return getLongValue("queues.maximumQueueDepth"); + return getLongValue("queues.maximumQueueDepth", getBrokerAttributeAsLong(Broker.ALERT_THRESHOLD_QUEUE_DEPTH)); } public Long getMaximumMessageSize() { - return getLongValue("queues.maximumMessageSize"); + return getLongValue("queues.maximumMessageSize", getBrokerAttributeAsLong(Broker.ALERT_THRESHOLD_MESSAGE_SIZE)); } public Long getMaximumMessageCount() { - return getLongValue("queues.maximumMessageCount"); + return getLongValue("queues.maximumMessageCount", getBrokerAttributeAsLong(Broker.ALERT_THRESHOLD_MESSAGE_COUNT)); } public Long getMinimumAlertRepeatGap() { - return getLongValue("queues.minimumAlertRepeatGap", ApplicationRegistry.getInstance().getConfiguration().getMinimumAlertRepeatGap()); + return getLongValue("queues.minimumAlertRepeatGap", getBrokerAttributeAsLong(Broker.ALERT_REPEAT_GAP)); } public long getCapacity() { - return getLongValue("queues.capacity"); + return getLongValue("queues.capacity", getBrokerAttributeAsLong(Broker.FLOW_CONTROL_SIZE_BYTES)); } public long getFlowResumeCapacity() { - return getLongValue("queues.flowResumeCapacity", getCapacity()); + return getLongValue("queues.flowResumeCapacity", getBrokerAttributeAsLong(Broker.FLOW_CONTROL_RESUME_SIZE_BYTES)); } public String[] getElementsProcessed() @@ -225,7 +263,7 @@ public class VirtualHostConfiguration extends AbstractConfiguration public int getMaxDeliveryCount() { - return getIntValue("queues.maximumDeliveryCount", ApplicationRegistry.getInstance().getConfiguration().getMaxDeliveryCount()); + return getIntValue("queues.maximumDeliveryCount", getBrokerAttributeAsInt(Broker.MAXIMUM_DELIVERY_ATTEMPTS)); } /** @@ -233,7 +271,25 @@ public class VirtualHostConfiguration extends AbstractConfiguration */ public boolean isDeadLetterQueueEnabled() { - return getBooleanValue("queues.deadLetterQueues", ApplicationRegistry.getInstance().getConfiguration().isDeadLetterQueueEnabled()); + return getBooleanValue("queues.deadLetterQueues", getBrokerAttributeAsBoolean(Broker.DEAD_LETTER_QUEUE_ENABLED)); + } + + private long getBrokerAttributeAsLong(String name) + { + Number brokerValue = (Number)_broker.getAttribute(name); + return brokerValue == null? 0 : brokerValue.longValue(); + } + + private int getBrokerAttributeAsInt(String name) + { + Number brokerValue = (Number)_broker.getAttribute(name); + return brokerValue == null? 0 : brokerValue.intValue(); + } + + private boolean getBrokerAttributeAsBoolean(String name) + { + Boolean brokerValue = (Boolean)_broker.getAttribute(name); + return brokerValue == null? false : brokerValue.booleanValue(); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/XmlConfigurationUtilities.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/XmlConfigurationUtilities.java new file mode 100644 index 0000000000..c0cff2c109 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/XmlConfigurationUtilities.java @@ -0,0 +1,111 @@ +/* + * + * 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; + +import java.io.File; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.ConfigurationFactory; +import org.apache.commons.configuration.SystemConfiguration; +import org.apache.commons.configuration.XMLConfiguration; + +public class XmlConfigurationUtilities +{ + + // Our configuration class needs to make the interpolate method + // public so it can be called below from the config method. + public static class MyConfiguration extends CompositeConfiguration + { + public String interpolate(String obj) + { + return super.interpolate(obj); + } + } + + public static Configuration parseConfig(File file, Map<String, String> envVarMap) throws ConfigurationException + { + ConfigurationFactory factory = new ConfigurationFactory(); + factory.setConfigurationFileName(file.getAbsolutePath()); + Configuration conf = factory.getConfiguration(); + + Iterator<?> keys = conf.getKeys(); + if (!keys.hasNext()) + { + keys = null; + conf = flatConfig(file); + } + + XmlConfigurationUtilities.substituteEnvironmentVariables(conf, envVarMap); + return conf; + } + + public final static Configuration flatConfig(File file) throws ConfigurationException + { + // We have to override the interpolate methods so that + // interpolation takes place across the entirety of the + // composite configuration. Without doing this each + // configuration object only interpolates variables defined + // inside itself. + final MyConfiguration conf = new MyConfiguration(); + conf.addConfiguration(new SystemConfiguration() + { + protected String interpolate(String o) + { + return conf.interpolate(o); + } + }); + conf.addConfiguration(new XMLConfiguration(file) + { + protected String interpolate(String o) + { + return conf.interpolate(o); + } + }); + return conf; + } + + static void substituteEnvironmentVariables(Configuration conf, Map<String, String> envVarMap) + { + if (envVarMap == null || envVarMap.isEmpty()) + { + return; + } + for (Entry<String, String> var : envVarMap.entrySet()) + { + String val = System.getenv(var.getKey()); + if (val != null) + { + conf.setProperty(var.getValue(), val); + } + } + } + + + public static String escapeTagName(String name) + { + return name.replaceAll("\\.", "\\.\\."); + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/plugins/AbstractConfiguration.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/plugins/AbstractConfiguration.java index 0e7c19df87..3c17faef75 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/plugins/AbstractConfiguration.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/plugins/AbstractConfiguration.java @@ -315,7 +315,7 @@ public abstract class AbstractConfiguration } } - public String escapeTagName(String name) + public static String escapeTagName(String name) { return name.replaceAll("\\.", "\\.\\."); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/AuthenticationProviderRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/AuthenticationProviderRecoverer.java new file mode 100644 index 0000000000..9b06a2b499 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/AuthenticationProviderRecoverer.java @@ -0,0 +1,55 @@ +/* + * + * 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.startup; + +import java.util.Map; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.adapter.AuthenticationProviderFactory; + +public class AuthenticationProviderRecoverer implements ConfiguredObjectRecoverer<AuthenticationProvider> +{ + private final AuthenticationProviderFactory _authenticationProviderFactory; + + public AuthenticationProviderRecoverer(AuthenticationProviderFactory authenticationProviderFactory) + { + _authenticationProviderFactory = authenticationProviderFactory; + } + + @Override + public AuthenticationProvider create(RecovererProvider recovererProvider, ConfigurationEntry configurationEntry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + Map<String, Object> attributes = configurationEntry.getAttributes(); + AuthenticationProvider authenticationProvider = _authenticationProviderFactory.create( + configurationEntry.getId(), + broker, + attributes, null); + + return authenticationProvider; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/BrokerRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/BrokerRecoverer.java new file mode 100644 index 0000000000..4bfa0ca7a3 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/BrokerRecoverer.java @@ -0,0 +1,139 @@ +package org.apache.qpid.server.configuration.startup; + +import java.util.Collection; +import java.util.Map; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.adapter.AuthenticationProviderFactory; +import org.apache.qpid.server.model.adapter.BrokerAdapter; +import org.apache.qpid.server.model.adapter.PortFactory; +import org.apache.qpid.server.configuration.store.StoreConfigurationChangeListener; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; + +public class BrokerRecoverer implements ConfiguredObjectRecoverer<Broker> +{ + private final StatisticsGatherer _statisticsGatherer; + private final VirtualHostRegistry _virtualHostRegistry; + private final LogRecorder _logRecorder; + private final RootMessageLogger _rootMessageLogger; + private final AuthenticationProviderFactory _authenticationProviderFactory; + private final PortFactory _portFactory; + private final TaskExecutor _taskExecutor; + + public BrokerRecoverer(AuthenticationProviderFactory authenticationProviderFactory, PortFactory portFactory, + StatisticsGatherer statisticsGatherer, VirtualHostRegistry virtualHostRegistry, LogRecorder logRecorder, + RootMessageLogger rootMessageLogger, TaskExecutor taskExecutor) + { + _portFactory = portFactory; + _authenticationProviderFactory = authenticationProviderFactory; + _statisticsGatherer = statisticsGatherer; + _virtualHostRegistry = virtualHostRegistry; + _logRecorder = logRecorder; + _rootMessageLogger = rootMessageLogger; + _taskExecutor = taskExecutor; + } + + @Override + public Broker create(RecovererProvider recovererProvider, ConfigurationEntry entry, ConfiguredObject... parents) + { + StoreConfigurationChangeListener storeChangeListener = new StoreConfigurationChangeListener(entry.getStore()); + BrokerAdapter broker = new BrokerAdapter(entry.getId(), entry.getAttributes(), _statisticsGatherer, _virtualHostRegistry, + _logRecorder, _rootMessageLogger, _authenticationProviderFactory, _portFactory, _taskExecutor); + broker.addChangeListener(storeChangeListener); + Map<String, Collection<ConfigurationEntry>> childEntries = entry.getChildren(); + for (String type : childEntries.keySet()) + { + ConfiguredObjectRecoverer<?> recoverer = recovererProvider.getRecoverer(type); + if (recoverer == null) + { + throw new IllegalConfigurationException("Cannot recover entry for the type '" + type + "' from broker"); + } + Collection<ConfigurationEntry> entries = childEntries.get(type); + for (ConfigurationEntry childEntry : entries) + { + ConfiguredObject object = recoverer.create(recovererProvider, childEntry, broker); + if (object == null) + { + throw new IllegalConfigurationException("Cannot create configured object for the entry " + childEntry); + } + broker.recoverChild(object); + object.addChangeListener(storeChangeListener); + } + } + wireUpConfiguredObjects(broker, entry.getAttributes()); + + return broker; + } + + private void wireUpConfiguredObjects(BrokerAdapter broker, Map<String, Object> brokerAttributes) + { + AuthenticationProvider defaultAuthenticationProvider = null; + Collection<AuthenticationProvider> authenticationProviders = broker.getAuthenticationProviders(); + int numberOfAuthenticationProviders = authenticationProviders.size(); + if (numberOfAuthenticationProviders == 0) + { + throw new IllegalConfigurationException("No authentication provider was configured"); + } + else if (numberOfAuthenticationProviders == 1) + { + defaultAuthenticationProvider = authenticationProviders.iterator().next(); + } + else + { + String name = (String) brokerAttributes.get(Broker.DEFAULT_AUTHENTICATION_PROVIDER); + if (name == null) + { + throw new IllegalConfigurationException("Multiple authentication providers defined, but no default was configured."); + } + + defaultAuthenticationProvider = getAuthenticationProviderByName(broker, name); + } + broker.setDefaultAuthenticationProvider(defaultAuthenticationProvider); + + GroupPrincipalAccessor groupPrincipalAccessor = new GroupPrincipalAccessor(broker.getGroupProviders()); + for (AuthenticationProvider authenticationProvider : authenticationProviders) + { + authenticationProvider.setGroupAccessor(groupPrincipalAccessor); + } + + Collection<Port> ports = broker.getPorts(); + for (Port port : ports) + { + String authenticationProviderName = (String) port.getAttribute(Port.AUTHENTICATION_MANAGER); + AuthenticationProvider provider = null; + if (authenticationProviderName != null) + { + provider = getAuthenticationProviderByName(broker, authenticationProviderName); + } + else + { + provider = defaultAuthenticationProvider; + } + port.setAuthenticationProvider(provider); + } + } + + private AuthenticationProvider getAuthenticationProviderByName(BrokerAdapter broker, String authenticationProviderName) + { + AuthenticationProvider provider = broker.getAuthenticationProviderByName(authenticationProviderName); + if (provider == null) + { + throw new IllegalConfigurationException("Cannot find the authentication provider with name: " + + authenticationProviderName); + } + return provider; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/DefaultRecovererProvider.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/DefaultRecovererProvider.java new file mode 100644 index 0000000000..15cb229d8a --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/DefaultRecovererProvider.java @@ -0,0 +1,112 @@ +/* + * + * 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.startup; + +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.Plugin; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.model.adapter.AuthenticationProviderFactory; +import org.apache.qpid.server.model.adapter.PortFactory; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.plugin.GroupManagerFactory; +import org.apache.qpid.server.plugin.PluginFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; + +public class DefaultRecovererProvider implements RecovererProvider +{ + + private final StatisticsGatherer _brokerStatisticsGatherer; + private final VirtualHostRegistry _virtualHostRegistry; + private final LogRecorder _logRecorder; + private final RootMessageLogger _rootMessageLogger; + private final AuthenticationProviderFactory _authenticationProviderFactory; + private final PortFactory _portFactory; + private final QpidServiceLoader<GroupManagerFactory> _groupManagerServiceLoader; + private final QpidServiceLoader<PluginFactory> _pluginFactoryServiceLoader; + private final TaskExecutor _taskExecutor; + + public DefaultRecovererProvider(StatisticsGatherer brokerStatisticsGatherer, VirtualHostRegistry virtualHostRegistry, + LogRecorder logRecorder, RootMessageLogger rootMessageLogger, TaskExecutor taskExecutor) + { + _authenticationProviderFactory = new AuthenticationProviderFactory(new QpidServiceLoader<AuthenticationManagerFactory>()); + _portFactory = new PortFactory(); + _brokerStatisticsGatherer = brokerStatisticsGatherer; + _virtualHostRegistry = virtualHostRegistry; + _logRecorder = logRecorder; + _rootMessageLogger = rootMessageLogger; + _groupManagerServiceLoader = new QpidServiceLoader<GroupManagerFactory>(); + _pluginFactoryServiceLoader = new QpidServiceLoader<PluginFactory>(); + _taskExecutor = taskExecutor; + } + + @Override + public ConfiguredObjectRecoverer<?> getRecoverer(String type) + { + if (Broker.class.getSimpleName().equals(type)) + { + return new BrokerRecoverer(_authenticationProviderFactory, _portFactory, _brokerStatisticsGatherer, _virtualHostRegistry, + _logRecorder, _rootMessageLogger, _taskExecutor); + } + else if(VirtualHost.class.getSimpleName().equals(type)) + { + return new VirtualHostRecoverer(_brokerStatisticsGatherer); + } + else if(AuthenticationProvider.class.getSimpleName().equals(type)) + { + return new AuthenticationProviderRecoverer(_authenticationProviderFactory); + } + else if(Port.class.getSimpleName().equals(type)) + { + return new PortRecoverer(_portFactory); + } + else if(GroupProvider.class.getSimpleName().equals(type)) + { + return new GroupProviderRecoverer(_groupManagerServiceLoader); + } + else if(KeyStore.class.getSimpleName().equals(type)) + { + return new KeyStoreRecoverer(); + } + else if(TrustStore.class.getSimpleName().equals(type)) + { + return new TrustStoreRecoverer(); + } + else if(Plugin.class.getSimpleName().equals(type)) + { + return new PluginRecoverer(_pluginFactoryServiceLoader); + } + + return null; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/GroupProviderRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/GroupProviderRecoverer.java new file mode 100644 index 0000000000..275a0c736c --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/GroupProviderRecoverer.java @@ -0,0 +1,74 @@ +/* + * + * 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.startup; + +import java.util.Map; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.adapter.GroupProviderAdapter; +import org.apache.qpid.server.plugin.GroupManagerFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; +import org.apache.qpid.server.security.group.GroupManager; + +public class GroupProviderRecoverer implements ConfiguredObjectRecoverer<GroupProvider> +{ + private QpidServiceLoader<GroupManagerFactory> _groupManagerServiceLoader; + + public GroupProviderRecoverer(QpidServiceLoader<GroupManagerFactory> groupManagerServiceLoader) + { + super(); + _groupManagerServiceLoader = groupManagerServiceLoader; + } + + @Override + public GroupProvider create(RecovererProvider recovererProvider, ConfigurationEntry configurationEntry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + Map<String, Object> attributes = configurationEntry.getAttributes(); + GroupManager groupManager = createGroupManager(attributes); + if (groupManager == null) + { + throw new IllegalConfigurationException("Cannot create GroupManager from attributes : " + attributes); + } + GroupProviderAdapter groupProviderAdapter = new GroupProviderAdapter(configurationEntry.getId(), groupManager, broker); + return groupProviderAdapter; + } + + private GroupManager createGroupManager(Map<String, Object> attributes) + { + for(GroupManagerFactory factory : _groupManagerServiceLoader.instancesOf(GroupManagerFactory.class)) + { + GroupManager groupManager = factory.createInstance(attributes); + if (groupManager != null) + { + return groupManager; + } + } + return null; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/KeyStoreRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/KeyStoreRecoverer.java new file mode 100644 index 0000000000..8efedd37b5 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/KeyStoreRecoverer.java @@ -0,0 +1,40 @@ +/* + * + * 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.startup; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.adapter.KeyStoreAdapter; + +public class KeyStoreRecoverer implements ConfiguredObjectRecoverer<KeyStore> +{ + @Override + public KeyStore create(RecovererProvider recovererProvider, ConfigurationEntry entry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + return new KeyStoreAdapter(entry.getId(), broker, entry.getAttributes()); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/PluginRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/PluginRecoverer.java new file mode 100644 index 0000000000..ddc4482953 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/PluginRecoverer.java @@ -0,0 +1,66 @@ +/* + * + * 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.startup; + +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.plugin.PluginFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; + +public class PluginRecoverer implements ConfiguredObjectRecoverer<ConfiguredObject> +{ + private QpidServiceLoader<PluginFactory> _serviceLoader; + + public PluginRecoverer(QpidServiceLoader<PluginFactory> serviceLoader) + { + _serviceLoader = serviceLoader; + } + + @Override + public ConfiguredObject create(RecovererProvider recovererProvider, ConfigurationEntry configurationEntry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + Map<String, Object> attributes = configurationEntry.getAttributes(); + Iterable<PluginFactory> factories = _serviceLoader.instancesOf(PluginFactory.class); + for (PluginFactory pluginFactory : factories) + { + UUID configurationId = configurationEntry.getId(); + ConfiguredObject pluginObject = pluginFactory.createInstance(configurationId, attributes, broker); + if (pluginObject != null) + { + UUID pluginId = pluginObject.getId(); + if (!configurationId.equals(pluginId)) + { + throw new IllegalStateException("Plugin object id '" + pluginId + "' does not equal expected id " + configurationId); + } + return pluginObject; + } + } + throw new IllegalConfigurationException("Cannot create a plugin object for " + attributes + " with factories " + factories); + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/PortRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/PortRecoverer.java new file mode 100644 index 0000000000..147e835a8d --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/PortRecoverer.java @@ -0,0 +1,52 @@ +/* + * + * 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.startup; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.adapter.BrokerAdapter; +import org.apache.qpid.server.model.adapter.PortFactory; + +public class PortRecoverer implements ConfiguredObjectRecoverer<Port> +{ + /** + * delegates to a {@link PortFactory} so that the logic can be shared by + * {@link BrokerAdapter} + */ + private final PortFactory _portFactory; + + public PortRecoverer(PortFactory portFactory) + { + _portFactory = portFactory; + } + + @Override + public Port create(RecovererProvider recovererProvider, ConfigurationEntry configurationEntry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + return _portFactory.createPort(configurationEntry.getId(), broker, configurationEntry.getAttributes()); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/RecovererHelper.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/RecovererHelper.java new file mode 100644 index 0000000000..b60c9c289f --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/RecovererHelper.java @@ -0,0 +1,44 @@ +/* + * + * 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.startup; + +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; + +public class RecovererHelper +{ + public static Broker verifyOnlyBrokerIsParent(ConfiguredObject... parents) + { + if (parents == null || parents.length == 0) + { + throw new IllegalArgumentException("Broker parent is not passed!"); + } + if (parents.length != 1) + { + throw new IllegalArgumentException("Only one parent is expected!"); + } + if (!(parents[0] instanceof Broker)) + { + throw new IllegalArgumentException("Parent is not a broker"); + } + return (Broker)parents[0]; + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/TrustStoreRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/TrustStoreRecoverer.java new file mode 100644 index 0000000000..7e9428a4d6 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/TrustStoreRecoverer.java @@ -0,0 +1,40 @@ +/* + * + * 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.startup; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.server.model.adapter.TrustStoreAdapter; + +public class TrustStoreRecoverer implements ConfiguredObjectRecoverer<TrustStore> +{ + @Override + public TrustStore create(RecovererProvider recovererProvider, ConfigurationEntry entry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + return new TrustStoreAdapter(entry.getId(), broker, entry.getAttributes()); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/VirtualHostRecoverer.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/VirtualHostRecoverer.java new file mode 100644 index 0000000000..4f863adfb5 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/startup/VirtualHostRecoverer.java @@ -0,0 +1,51 @@ +/* + * + * 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.startup; + + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.model.adapter.VirtualHostAdapter; +import org.apache.qpid.server.stats.StatisticsGatherer; + +public class VirtualHostRecoverer implements ConfiguredObjectRecoverer<VirtualHost> +{ + private StatisticsGatherer _brokerStatisticsGatherer; + + public VirtualHostRecoverer(StatisticsGatherer brokerStatisticsGatherer) + { + super(); + _brokerStatisticsGatherer = brokerStatisticsGatherer; + } + + @Override + public VirtualHost create(RecovererProvider recovererProvider, ConfigurationEntry entry, ConfiguredObject... parents) + { + Broker broker = RecovererHelper.verifyOnlyBrokerIsParent(parents); + + return new VirtualHostAdapter(entry.getId(), entry.getAttributes(), broker, _brokerStatisticsGatherer, broker.getTaskExecutor()); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java new file mode 100644 index 0000000000..b8481de2cc --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java @@ -0,0 +1,528 @@ +package org.apache.qpid.server.configuration.store; + +import static org.apache.qpid.server.configuration.ConfigurationEntry.ATTRIBUTE_NAME; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.configuration.BrokerConfigurationStoreCreator; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.Model; +import org.apache.qpid.server.model.UUIDGenerator; +import org.apache.qpid.util.FileUtils; +import org.apache.qpid.util.Strings; +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; +import org.codehaus.jackson.node.ArrayNode; + +public class JsonConfigurationEntryStore implements ConfigurationEntryStore +{ + private static final String DEFAULT_BROKER_NAME = "Broker"; + private static final String ID = "id"; + private static final String TYPE = "@type"; + + private ObjectMapper _objectMapper; + private Map<UUID, ConfigurationEntry> _entries; + private File _storeFile; + private UUID _rootId; + private String _initialStoreLocation; + private Map<String, Class<? extends ConfiguredObject>> _relationshipClasses; + + public JsonConfigurationEntryStore() + { + this(BrokerConfigurationStoreCreator.INITIAL_STORE_LOCATION); + } + + public JsonConfigurationEntryStore(String initialStore) + { + _initialStoreLocation = initialStore; + _objectMapper = new ObjectMapper(); + _objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + _objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); + _entries = new HashMap<UUID, ConfigurationEntry>(); + _relationshipClasses = buildRelationshipClassMap(); + } + + private Map<String, Class<? extends ConfiguredObject>> buildRelationshipClassMap() + { + Map<String, Class<? extends ConfiguredObject>> relationships = new HashMap<String, Class<? extends ConfiguredObject>>(); + + Collection<Class<? extends ConfiguredObject>> children = Model.getInstance().getChildTypes(Broker.class); + for (Class<? extends ConfiguredObject> childClass : children) + { + String name = childClass.getSimpleName().toLowerCase(); + String relationshipName = name + (name.endsWith("s") ? "es" : "s"); + relationships.put(relationshipName, childClass); + } + return relationships; + } + + public void load(URL storeURL) + { + if (_rootId != null) + { + throw new IllegalStateException("Cannot load the store from"); + } + JsonNode node = load(storeURL, _objectMapper); + ConfigurationEntry brokerEntry = toEntry(node, Broker.class, _entries); + _rootId = brokerEntry.getId(); + } + + @Override + public void open(String storeLocation) + { + _storeFile = new File(storeLocation); + if (!_storeFile.exists() || _storeFile.length() == 0) + { + copyInitialStore(); + } + + load(fileToURL(_storeFile)); + } + + private void copyInitialStore() + { + InputStream in = null; + try + { + in = JsonConfigurationEntryStore.class.getClassLoader().getResourceAsStream(_initialStoreLocation); + FileUtils.copy(in, _storeFile); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot create store file by copying initial store", e); + } + finally + { + if (in != null) + { + try + { + in.close(); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot close initial store input stream", e); + } + } + } + } + + @Override + public synchronized UUID[] remove(UUID... entryIds) + { + List<UUID> removedIds = new ArrayList<UUID>(); + boolean anyRemoved = false; + for (UUID uuid : entryIds) + { + if (_rootId.equals(uuid)) + { + throw new IllegalConfigurationException("Cannot remove root entry"); + } + } + for (UUID uuid : entryIds) + { + if (removeInternal(uuid)) + { + anyRemoved = true; + + // remove references to the entry from parent entries + for (ConfigurationEntry entry : _entries.values()) + { + if (entry.hasChild(uuid)) + { + Set<UUID> children = new HashSet<UUID>(entry.getChildrenIds()); + children.remove(uuid); + ConfigurationEntry referal = new ConfigurationEntry(entry.getId(), entry.getType(), + entry.getAttributes(), children, this); + _entries.put(entry.getId(), referal); + } + } + removedIds.add(uuid); + } + } + if (anyRemoved) + { + saveAsTree(); + } + return removedIds.toArray(new UUID[removedIds.size()]); + } + + @Override + public synchronized void save(ConfigurationEntry... entries) + { + boolean anySaved = false; + for (ConfigurationEntry entry : entries) + { + ConfigurationEntry oldEntry = _entries.put(entry.getId(), entry); + if (!entry.equals(oldEntry)) + { + anySaved = true; + } + } + if (anySaved) + { + saveAsTree(); + } + } + + @Override + public ConfigurationEntry getRootEntry() + { + return getEntry(_rootId); + } + + @Override + public synchronized ConfigurationEntry getEntry(UUID id) + { + return _entries.get(id); + } + + public void saveTo(File file) + { + saveAsTree(_rootId, _entries, _objectMapper, file); + } + + private URL fileToURL(File storeFile) + { + URL storeURL = null; + try + { + storeURL = storeFile.toURI().toURL(); + } + catch (MalformedURLException e) + { + throw new IllegalConfigurationException("Cannot create URL for file " + storeFile, e); + } + return storeURL; + } + + private boolean removeInternal(UUID entryId) + { + ConfigurationEntry oldEntry = _entries.remove(entryId); + if (oldEntry != null) + { + Set<UUID> children = oldEntry.getChildrenIds(); + if (children != null && !children.isEmpty()) + { + for (UUID childId : children) + { + removeInternal(childId); + } + } + return true; + } + return false; + } + + private void saveAsTree() + { + if (_storeFile != null) + { + saveAsTree(_rootId, _entries, _objectMapper, _storeFile); + } + } + + private void saveAsTree(UUID rootId, Map<UUID, ConfigurationEntry> entries, ObjectMapper mapper, File file) + { + Map<String, Object> tree = toTree(rootId, entries); + try + { + mapper.writeValue(file, tree); + } + catch (JsonGenerationException e) + { + throw new IllegalConfigurationException("Cannot generate json!", e); + } + catch (JsonMappingException e) + { + throw new IllegalConfigurationException("Cannot map objects for json serialization!", e); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot save configuration into " + file + "!", e); + } + } + + private Map<String, Object> toTree(UUID rootId, Map<UUID, ConfigurationEntry> entries) + { + ConfigurationEntry entry = entries.get(rootId); + if (entry == null || !entry.getId().equals(rootId)) + { + throw new IllegalConfigurationException("Cannot find entry with id " + rootId + "!"); + } + Map<String, Object> tree = new TreeMap<String, Object>(); + Map<String, Object> attributes = entry.getAttributes(); + if (attributes != null) + { + tree.putAll( attributes); + } + tree.put(ID, entry.getId()); + tree.put(TYPE, entry.getType()); + Set<UUID> childrenIds = entry.getChildrenIds(); + if (childrenIds != null && !childrenIds.isEmpty()) + { + for (UUID relationship : childrenIds) + { + ConfigurationEntry child = entries.get(relationship); + if (child != null) + { + String relationshipName = child.getType().toLowerCase() + "s"; + + @SuppressWarnings("unchecked") + Collection<Map<String, Object>> children = (Collection<Map<String, Object>>) tree.get(relationshipName); + if (children == null) + { + children = new ArrayList<Map<String, Object>>(); + tree.put(relationshipName, children); + } + Map<String, Object> childAsMap = toTree(relationship, entries); + children.add(childAsMap); + } + } + } + return tree; + } + + private JsonNode load(URL url, ObjectMapper mapper) + { + JsonNode root = null; + try + { + root = mapper.readTree(url); + } + catch (JsonProcessingException e) + { + throw new IllegalConfigurationException("Cannot parse json from '" + url + "'", e); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot read from '" + url + "'", e); + } + return root; + } + + private ConfigurationEntry toEntry(JsonNode parent, Class<? extends ConfiguredObject> expectedConfiguredObjectClass, Map<UUID, ConfigurationEntry> entries) + { + Map<String, Object> attributes = null; + Set<UUID> childrenIds = new TreeSet<UUID>(); + Iterator<String> fieldNames = parent.getFieldNames(); + String type = null; + String idAsString = null; + while (fieldNames.hasNext()) + { + String fieldName = fieldNames.next(); + JsonNode fieldNode = parent.get(fieldName); + if (fieldName.equals(ID)) + { + idAsString = fieldNode.asText(); + } + else if (fieldName.equals(TYPE)) + { + type = fieldNode.asText(); + } + else if (fieldNode.isArray()) + { + // array containing either broker children or attribute values + Iterator<JsonNode> elements = fieldNode.getElements(); + List<Object> fieldValues = null; + while (elements.hasNext()) + { + JsonNode element = elements.next(); + if (element.isObject()) + { + Class<? extends ConfiguredObject> expectedChildConfiguredObjectClass = _relationshipClasses.get(fieldName); + // assuming it is a child node + ConfigurationEntry entry = toEntry(element, expectedChildConfiguredObjectClass, entries); + childrenIds.add(entry.getId()); + } + else + { + if (fieldValues == null) + { + fieldValues = new ArrayList<Object>(); + } + fieldValues.add(toObject(element)); + } + } + if (fieldValues != null) + { + Object[] array = fieldValues.toArray(new Object[fieldValues.size()]); + attributes.put(fieldName, array); + } + } + else if (fieldNode.isObject()) + { + // ignore, in-line objects are not supported yet + } + else + { + // primitive attribute + Object value = toObject(fieldNode); + if (attributes == null) + { + attributes = new HashMap<String, Object>(); + } + attributes.put(fieldName, value); + } + } + + if (type == null) + { + if (expectedConfiguredObjectClass == null) + { + throw new IllegalConfigurationException("Type attribute is not provided for configuration entry " + parent); + } + else + { + type = expectedConfiguredObjectClass.getSimpleName(); + } + } + String name = null; + if (attributes != null) + { + name = (String) attributes.get(ATTRIBUTE_NAME); + } + if ((name == null || "".equals(name))) + { + if (expectedConfiguredObjectClass == Broker.class) + { + name = DEFAULT_BROKER_NAME; + } + else + { + throw new IllegalConfigurationException("Name attribute is not provided for configuration entry " + parent); + } + } + UUID id = null; + if (idAsString == null) + { + if (expectedConfiguredObjectClass == Broker.class) + { + id = UUIDGenerator.generateRandomUUID(); + } + else + { + id = UUIDGenerator.generateBrokerChildUUID(type, name); + } + } + else + { + try + { + id = UUID.fromString(idAsString); + } + catch (Exception e) + { + throw new IllegalConfigurationException( + "ID attribute value does not conform to UUID format for configuration entry " + parent); + } + } + ConfigurationEntry entry = new ConfigurationEntry(id, type, attributes, childrenIds, this); + if (entries.containsKey(id)) + { + throw new IllegalConfigurationException("Duplicate id is found: " + id + + "! The following configuration entries have the same id: " + entries.get(id) + ", " + entry); + } + entries.put(id, entry); + return entry; + } + + private Object toObject(JsonNode node) + { + if (node.isValueNode()) + { + if (node.isBoolean()) + { + return node.asBoolean(); + } + else if (node.isDouble()) + { + return node.asDouble(); + } + else if (node.isInt()) + { + return node.asInt(); + } + else if (node.isLong()) + { + return node.asLong(); + } + else if (node.isNull()) + { + return null; + } + else + { + return Strings.expand(node.asText()); + } + } + else if (node.isArray()) + { + return toArray(node); + } + else if (node.isObject()) + { + return toMap(node); + } + else + { + throw new IllegalConfigurationException("Unexpected node: " + node); + } + } + + private Map<String, Object> toMap(JsonNode node) + { + Map<String, Object> object = new TreeMap<String, Object>(); + Iterator<String> fieldNames = node.getFieldNames(); + while (fieldNames.hasNext()) + { + String name = fieldNames.next(); + Object value = toObject(node.get(name)); + object.put(name, value); + } + return object; + } + + private Object toArray(JsonNode node) + { + ArrayNode arrayNode = (ArrayNode) node; + Object[] array = new Object[arrayNode.size()]; + Iterator<JsonNode> elements = arrayNode.getElements(); + for (int i = 0; i < array.length; i++) + { + array[i] = toObject(elements.next()); + } + return array; + } + + @Override + public String toString() + { + return "JsonConfigurationEntryStore [_storeFile=" + _storeFile + ", _rootId=" + _rootId + ", _initialStoreLocation=" + + _initialStoreLocation + "]"; + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/StoreConfigurationChangeListener.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/StoreConfigurationChangeListener.java new file mode 100644 index 0000000000..813702d0a6 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/StoreConfigurationChangeListener.java @@ -0,0 +1,205 @@ +/* + * + * 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 java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.TreeSet; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfigurationChangeListener; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.Model; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.VirtualHost; + +public class StoreConfigurationChangeListener implements ConfigurationChangeListener +{ + private ConfigurationEntryStore _store; + + public StoreConfigurationChangeListener(ConfigurationEntryStore store) + { + super(); + _store = store; + } + + @Override + public void stateChanged(ConfiguredObject object, State oldState, State newState) + { + if (newState == State.DELETED) + { + _store.remove(object.getId()); + object.removeChangeListener(this); + } + } + + @Override + public void childAdded(ConfiguredObject object, ConfiguredObject child) + { + // exclude VirtualHost children from storing in broker store + if (!(object instanceof VirtualHost)) + { + child.addChangeListener(this); + ConfigurationEntry parentEntry = toConfigurationEntry(object); + ConfigurationEntry childEntry = toConfigurationEntry(child); + _store.save(parentEntry, childEntry); + } + + } + + @Override + public void childRemoved(ConfiguredObject object, ConfiguredObject child) + { + _store.save(toConfigurationEntry(object)); + } + + @Override + public void attributeSet(ConfiguredObject object, String attrinuteName, Object oldAttributeValue, Object newAttributeValue) + { + _store.save(toConfigurationEntry(object)); + } + + private ConfigurationEntry toConfigurationEntry(ConfiguredObject object) + { + Class<? extends ConfiguredObject> objectType = getConfiguredObjectType(object); + Set<UUID> childrenIds = getChildernIds(object, objectType); + ConfigurationEntry entry = new ConfigurationEntry(object.getId(), objectType.getSimpleName(), + object.getActualAttributes(), childrenIds, _store); + return entry; + } + + private Set<UUID> getChildernIds(ConfiguredObject object, Class<? extends ConfiguredObject> objectType) + { + // Virtual Host children's IDs should not be stored in broker store + if (object instanceof VirtualHost) + { + return Collections.emptySet(); + } + Set<UUID> childrenIds = new TreeSet<UUID>(); + Collection<Class<? extends ConfiguredObject>> childClasses = Model.getInstance().getChildTypes(objectType); + if (childClasses != null) + { + for (Class<? extends ConfiguredObject> childClass : childClasses) + { + Collection<? extends ConfiguredObject> children = object.getChildren(childClass); + if (children != null) + { + for (ConfiguredObject childObject : children) + { + childrenIds.add(childObject.getId()); + } + } + } + } + return childrenIds; + } + + private Class<? extends ConfiguredObject> getConfiguredObjectType(ConfiguredObject object) + { + if (object instanceof Broker) + { + return Broker.class; + } + return getConfiguredObjectTypeFromImplementedInterfaces(object.getClass()); + } + + @SuppressWarnings("unchecked") + private Class<? extends ConfiguredObject> getConfiguredObjectTypeFromImplementedInterfaces(Class<?> objectClass) + { + // get all implemented interfaces extending ConfiguredObject + Set<Class<?>> interfaces = getImplementedInterfacesExtendingSuper(objectClass, ConfiguredObject.class); + + if (interfaces.size() == 0) + { + throw new RuntimeException("Can not identify the configured object type"); + } + + if (interfaces.size() == 1) + { + return (Class<? extends ConfiguredObject>)interfaces.iterator().next(); + } + + Set<Class<?>> superInterfaces = new HashSet<Class<?>>(); + + // find all super interfaces + for (Class<?> interfaceClass : interfaces) + { + for (Class<?> interfaceClass2 : interfaces) + { + if (interfaceClass != interfaceClass2) + { + if (interfaceClass.isAssignableFrom(interfaceClass2)) + { + superInterfaces.add(interfaceClass); + } + } + } + } + + // remove super interfaces + for (Class<?> superInterface : superInterfaces) + { + interfaces.remove(superInterface); + } + + if (interfaces.size() == 1) + { + return (Class<? extends ConfiguredObject>)interfaces.iterator().next(); + } + else + { + throw new RuntimeException("Can not identify the configured object type as an it implements" + + " more than one configured object interfaces: " + interfaces); + } + + } + + private Set<Class<?>> getImplementedInterfacesExtendingSuper(Class<?> classInstance, Class<?> superInterface) + { + Set<Class<?>> interfaces = new HashSet<Class<?>>(); + Class<?>[] classInterfaces = classInstance.getInterfaces(); + for (Class<?> interfaceClass : classInterfaces) + { + if (interfaceClass!= superInterface && superInterface.isAssignableFrom(interfaceClass)) + { + interfaces.add(interfaceClass); + } + } + Class<?> superClass = classInstance.getSuperclass(); + if (superClass != null) + { + Set<Class<?>> superClassInterfaces = getImplementedInterfacesExtendingSuper(superClass, superInterface); + interfaces.addAll(superClassInterfaces); + } + return interfaces; + } + + @Override + public String toString() + { + return "StoreConfigurationChangeListener [store=" + _store + "]"; + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java new file mode 100644 index 0000000000..a2664219bc --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java @@ -0,0 +1,43 @@ +/* + * + * 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.factory; + +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.configuration.ConfigurationStoreFactory; +import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; + +public class JsonConfigurationStoreFactory implements ConfigurationStoreFactory +{ + private static final String STORE_TYPE = "json"; + + @Override + public ConfigurationEntryStore createStore() + { + return new JsonConfigurationEntryStore(); + } + + @Override + public String getStoreType() + { + return STORE_TYPE; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/ChangeStateTask.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/ChangeStateTask.java new file mode 100644 index 0000000000..b6de1e136a --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/ChangeStateTask.java @@ -0,0 +1,67 @@ +/* + * + * 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.updater; + +import java.util.concurrent.Callable; + +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.State; + +public final class ChangeStateTask implements Callable<State> +{ + private ConfiguredObject _object; + private State _expectedState; + private State _desiredState; + + public ChangeStateTask(ConfiguredObject object, State expectedState, State desiredState) + { + _object = object; + _expectedState = expectedState; + _desiredState = desiredState; + } + + public ConfiguredObject getObject() + { + return _object; + } + + public State getExpectedState() + { + return _expectedState; + } + + public State getDesiredState() + { + return _desiredState; + } + + @Override + public State call() + { + return _object.setDesiredState(_expectedState, _desiredState); + } + + @Override + public String toString() + { + return "ChangeStateTask [object=" + _object + ", expectedState=" + _expectedState + ", desiredState=" + _desiredState + "]"; + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/CreateChildTask.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/CreateChildTask.java new file mode 100644 index 0000000000..d3a8f5b797 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/CreateChildTask.java @@ -0,0 +1,78 @@ +/* + * + * 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.updater; + +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.Callable; + +import org.apache.qpid.server.model.ConfiguredObject; + +public final class CreateChildTask implements Callable<ConfiguredObject> +{ + private ConfiguredObject _object; + private Class<? extends ConfiguredObject> _childClass; + private Map<String, Object> _attributes; + private ConfiguredObject[] _otherParents; + + public CreateChildTask(ConfiguredObject object, Class<? extends ConfiguredObject> childClass, Map<String, Object> attributes, + ConfiguredObject... otherParents) + { + _object = object; + _childClass = childClass; + _attributes = attributes; + _otherParents = otherParents; + } + + public ConfiguredObject getObject() + { + return _object; + } + + public Class<? extends ConfiguredObject> getChildClass() + { + return _childClass; + } + + public Map<String, Object> getAttributes() + { + return _attributes; + } + + public ConfiguredObject[] getOtherParents() + { + return _otherParents; + } + + @Override + public ConfiguredObject call() + { + return _object.createChild(_childClass, _attributes, _otherParents); + } + + @Override + public String toString() + { + return "CreateChildTask [object=" + _object + ", childClass=" + _childClass + ", attributes=" + _attributes + + ", otherParents=" + Arrays.toString(_otherParents) + "]"; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/SetAttributeTask.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/SetAttributeTask.java new file mode 100644 index 0000000000..94649434e6 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/SetAttributeTask.java @@ -0,0 +1,74 @@ +/* + * + * 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.updater; + +import java.util.concurrent.Callable; + +import org.apache.qpid.server.model.ConfiguredObject; + +public final class SetAttributeTask implements Callable<Object> +{ + private ConfiguredObject _object; + private String _attributeName; + private Object _expectedValue; + private Object _desiredValue; + + public SetAttributeTask(ConfiguredObject object, String attributeName, Object expectedValue, Object desiredValue) + { + _object = object; + _attributeName = attributeName; + _expectedValue = expectedValue; + _desiredValue = desiredValue; + } + + public ConfiguredObject getObject() + { + return _object; + } + + public String getAttributeName() + { + return _attributeName; + } + + public Object getExpectedValue() + { + return _expectedValue; + } + + public Object getDesiredValue() + { + return _desiredValue; + } + + @Override + public Object call() + { + return _object.setAttribute(_attributeName, _expectedValue, _desiredValue); + } + + @Override + public String toString() + { + return "SetAttributeTask [object=" + _object + ", attributeName=" + _attributeName + ", expectedValue=" + _expectedValue + + ", desiredValue=" + _desiredValue + "]"; + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/TaskExecutor.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/TaskExecutor.java new file mode 100644 index 0000000000..671104d413 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/updater/TaskExecutor.java @@ -0,0 +1,324 @@ +/* + * + * 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.updater; + +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import javax.security.auth.Subject; + +import org.apache.log4j.Logger; +import org.apache.qpid.server.logging.LogActor; +import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.security.SecurityManager; + +public class TaskExecutor +{ + private static final String TASK_EXECUTION_THREAD_NAME = "Broker-Configuration-Thread"; + private static final Logger LOGGER = Logger.getLogger(TaskExecutor.class); + + private volatile Thread _taskThread; + private final AtomicReference<State> _state; + private volatile ExecutorService _executor; + + public TaskExecutor() + { + _state = new AtomicReference<State>(State.INITIALISING); + } + + public State getState() + { + return _state.get(); + } + + public void start() + { + if (_state.compareAndSet(State.INITIALISING, State.ACTIVE)) + { + LOGGER.debug("Starting task executor"); + _executor = Executors.newFixedThreadPool(1, new ThreadFactory() + { + @Override + public Thread newThread(Runnable r) + { + _taskThread = new Thread(r, TASK_EXECUTION_THREAD_NAME); + return _taskThread; + } + }); + LOGGER.debug("Task executor is started"); + } + } + + public void stopImmediately() + { + if (_state.compareAndSet(State.ACTIVE, State.STOPPED)) + { + ExecutorService executor = _executor; + if (executor != null) + { + LOGGER.debug("Stopping task executor immediately"); + List<Runnable> cancelledTasks = executor.shutdownNow(); + if (cancelledTasks != null) + { + for (Runnable runnable : cancelledTasks) + { + if (runnable instanceof RunnableFuture<?>) + { + ((RunnableFuture<?>) runnable).cancel(true); + } + } + } + _executor = null; + _taskThread = null; + LOGGER.debug("Task executor was stopped immediately. Number of unfinished tasks: " + cancelledTasks.size()); + } + } + } + + public void stop() + { + if (_state.compareAndSet(State.ACTIVE, State.STOPPED)) + { + ExecutorService executor = _executor; + if (executor != null) + { + LOGGER.debug("Stopping task executor"); + executor.shutdown(); + _executor = null; + _taskThread = null; + LOGGER.debug("Task executor is stopped"); + } + } + } + + Future<?> submit(Callable<?> task) + { + checkState(); + if (LOGGER.isDebugEnabled()) + { + LOGGER.debug("Submitting task: " + task); + } + Future<?> future = null; + if (isTaskExecutorThread()) + { + Object result = executeTaskAndHandleExceptions(task); + return new ImmediateFuture(result); + } + else + { + future = _executor.submit(new CallableWrapper(task)); + } + return future; + } + + public Object submitAndWait(Callable<?> task) throws CancellationException + { + try + { + Future<?> future = submit(task); + return future.get(); + } + catch (InterruptedException e) + { + throw new RuntimeException("Task execution was interrupted: " + task, e); + } + catch (ExecutionException e) + { + Throwable cause = e.getCause(); + if (cause instanceof RuntimeException) + { + throw (RuntimeException) cause; + } + else if (cause instanceof Exception) + { + throw new RuntimeException("Failed to execute user task: " + task, cause); + } + else if (cause instanceof Error) + { + throw (Error) cause; + } + else + { + throw new RuntimeException("Failed to execute user task: " + task, cause); + } + } + } + + public boolean isTaskExecutorThread() + { + return Thread.currentThread() == _taskThread; + } + + private void checkState() + { + if (_state.get() != State.ACTIVE) + { + throw new IllegalStateException("Task executor is not in ACTIVE state"); + } + } + + private Object executeTaskAndHandleExceptions(Callable<?> userTask) + { + try + { + return executeTask(userTask); + } + catch (Exception e) + { + if (e instanceof RuntimeException) + { + throw (RuntimeException) e; + } + throw new RuntimeException("Failed to execute user task: " + userTask, e); + } + } + + private Object executeTask(Callable<?> userTask) throws Exception + { + if (LOGGER.isDebugEnabled()) + { + LOGGER.debug("Performing task " + userTask); + } + Object result = userTask.call(); + if (LOGGER.isDebugEnabled()) + { + LOGGER.debug("Task " + userTask + " is performed successfully with result:" + result); + } + return result; + } + + private class CallableWrapper implements Callable<Object> + { + private Callable<?> _userTask; + private Subject _securityManagerSubject; + private LogActor _actor; + private Subject _contextSubject; + + public CallableWrapper(Callable<?> userWork) + { + _userTask = userWork; + _securityManagerSubject = SecurityManager.getThreadSubject(); + _actor = CurrentActor.get(); + _contextSubject = Subject.getSubject(AccessController.getContext()); + } + + @Override + public Object call() throws Exception + { + SecurityManager.setThreadSubject(_securityManagerSubject); + CurrentActor.set(_actor); + + try + { + Object result = null; + try + { + result = Subject.doAs(_contextSubject, new PrivilegedExceptionAction<Object>() + { + @Override + public Object run() throws Exception + { + return executeTask(_userTask); + } + }); + } + catch (PrivilegedActionException e) + { + throw e.getException(); + } + return result; + } + finally + { + try + { + CurrentActor.remove(); + } + catch (Exception e) + { + LOGGER.warn("Unxpected exception on current actor removal", e); + } + try + { + SecurityManager.setThreadSubject(null); + } + catch (Exception e) + { + LOGGER.warn("Unxpected exception on nullifying of subject for a security manager", e); + } + } + } + } + + private class ImmediateFuture implements Future<Object> + { + private Object _result; + + public ImmediateFuture(Object result) + { + super(); + this._result = result; + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) + { + return false; + } + + @Override + public boolean isCancelled() + { + return false; + } + + @Override + public boolean isDone() + { + return true; + } + + @Override + public Object get() + { + return _result; + } + + @Override + public Object get(long timeout, TimeUnit unit) + { + return get(); + } + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java index e65fe10eb5..0339287e38 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java @@ -30,8 +30,9 @@ import org.apache.qpid.framing.ConnectionSecureOkBody; import org.apache.qpid.framing.ConnectionTuneBody; import org.apache.qpid.framing.MethodRegistry; import org.apache.qpid.protocol.AMQConstant; +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.AMQProtocolSession; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.SubjectCreator; import org.apache.qpid.server.security.auth.SubjectAuthenticationResult; import org.apache.qpid.server.state.AMQState; @@ -58,6 +59,7 @@ public class ConnectionSecureOkMethodHandler implements StateAwareMethodListener public void methodReceived(AMQStateManager stateManager, ConnectionSecureOkBody body, int channelId) throws AMQException { + Broker broker = stateManager.getBroker(); AMQProtocolSession session = stateManager.getProtocolSession(); SubjectCreator subjectCreator = stateManager.getSubjectCreator(); @@ -96,9 +98,9 @@ public class ConnectionSecureOkMethodHandler implements StateAwareMethodListener stateManager.changeState(AMQState.CONNECTION_NOT_TUNED); ConnectionTuneBody tuneBody = - methodRegistry.createConnectionTuneBody(ApplicationRegistry.getInstance().getConfiguration().getMaxChannelCount(), - ConnectionStartOkMethodHandler.getConfiguredFrameSize(), - ApplicationRegistry.getInstance().getConfiguration().getHeartBeatDelay()); + methodRegistry.createConnectionTuneBody((Integer)broker.getAttribute(Broker.SESSION_COUNT_LIMIT), + BrokerProperties.DEFAULT_FRAME_SIZE, + (Integer)broker.getAttribute(Broker.HEART_BEAT_DELAY)); session.writeFrame(tuneBody.generateFrame(0)); session.setAuthorizedSubject(authResult.getSubject()); disposeSaslServer(session); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java index 9f596a4637..e70fa6a37b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java @@ -29,9 +29,9 @@ import org.apache.qpid.framing.ConnectionStartOkBody; import org.apache.qpid.framing.ConnectionTuneBody; import org.apache.qpid.framing.MethodRegistry; import org.apache.qpid.protocol.AMQConstant; -import org.apache.qpid.server.configuration.ServerConfiguration; +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.AMQProtocolSession; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.SubjectCreator; import org.apache.qpid.server.security.auth.SubjectAuthenticationResult; import org.apache.qpid.server.state.AMQState; @@ -59,6 +59,7 @@ public class ConnectionStartOkMethodHandler implements StateAwareMethodListener< public void methodReceived(AMQStateManager stateManager, ConnectionStartOkBody body, int channelId) throws AMQException { + Broker broker = stateManager.getBroker(); AMQProtocolSession session = stateManager.getProtocolSession(); _logger.info("SASL Mechanism selected: " + body.getMechanism()); @@ -111,9 +112,9 @@ public class ConnectionStartOkMethodHandler implements StateAwareMethodListener< stateManager.changeState(AMQState.CONNECTION_NOT_TUNED); - ConnectionTuneBody tuneBody = methodRegistry.createConnectionTuneBody(ApplicationRegistry.getInstance().getConfiguration().getMaxChannelCount(), - getConfiguredFrameSize(), - ApplicationRegistry.getInstance().getConfiguration().getHeartBeatDelay()); + ConnectionTuneBody tuneBody = methodRegistry.createConnectionTuneBody((Integer)broker.getAttribute(Broker.SESSION_COUNT_LIMIT), + BrokerProperties.DEFAULT_FRAME_SIZE, + (Integer)broker.getAttribute(Broker.HEART_BEAT_DELAY)); session.writeFrame(tuneBody.generateFrame(0)); break; case CONTINUE: @@ -147,13 +148,6 @@ public class ConnectionStartOkMethodHandler implements StateAwareMethodListener< } } - static int getConfiguredFrameSize() - { - final ServerConfiguration config = ApplicationRegistry.getInstance().getConfiguration(); - final int framesize = config.getFrameSize(); - _logger.info("Framesize set to " + framesize); - return framesize; - } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/QueueDeclareHandler.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/QueueDeclareHandler.java index ae725b9ec1..194c3d6351 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/QueueDeclareHandler.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/QueueDeclareHandler.java @@ -38,7 +38,6 @@ import org.apache.qpid.server.protocol.AMQSessionModel; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.AMQQueueFactory; import org.apache.qpid.server.queue.QueueRegistry; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.state.AMQStateManager; import org.apache.qpid.server.state.StateAwareMethodListener; import org.apache.qpid.server.store.DurableConfigurationStore; @@ -59,8 +58,6 @@ public class QueueDeclareHandler implements StateAwareMethodListener<QueueDeclar return _instance; } - private boolean autoRegister = ApplicationRegistry.getInstance().getConfiguration().getQueueAutoRegister(); - public void methodReceived(AMQStateManager stateManager, QueueDeclareBody body, int channelId) throws AMQException { final AMQProtocolSession protocolConnection = stateManager.getProtocolSession(); @@ -148,13 +145,11 @@ public class QueueDeclareHandler implements StateAwareMethodListener<QueueDeclar }); } } - if (autoRegister) - { - Exchange defaultExchange = exchangeRegistry.getDefaultExchange(); + Exchange defaultExchange = exchangeRegistry.getDefaultExchange(); - virtualHost.getBindingFactory().addBinding(String.valueOf(queueName), queue, defaultExchange, Collections.EMPTY_MAP); - _logger.info("Queue " + queueName + " bound to default exchange(" + defaultExchange.getNameShortString() + ")"); - } + virtualHost.getBindingFactory().addBinding(String.valueOf(queueName), queue, defaultExchange, + Collections.<String, Object> emptyMap()); + _logger.info("Queue " + queueName + " bound to default exchange(" + defaultExchange.getNameShortString() + ")"); } } else if (queue.isExclusive() && !queue.isDurable() && (owningSession == null || owningSession.getConnectionModel() != protocolConnection)) diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/AbstractRootMessageLogger.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/AbstractRootMessageLogger.java index 545f2adea2..98da9074ef 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/AbstractRootMessageLogger.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/AbstractRootMessageLogger.java @@ -20,7 +20,6 @@ */ package org.apache.qpid.server.logging; -import org.apache.qpid.server.configuration.ServerConfiguration; public abstract class AbstractRootMessageLogger implements RootMessageLogger { @@ -33,9 +32,9 @@ public abstract class AbstractRootMessageLogger implements RootMessageLogger } - public AbstractRootMessageLogger(ServerConfiguration config) + public AbstractRootMessageLogger(boolean statusUpdatesEnabled) { - _enabled = config.getStatusUpdatesEnabled(); + _enabled = statusUpdatesEnabled; } public boolean isEnabled() diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/Log4jMessageLogger.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/Log4jMessageLogger.java index 62f0e75ceb..b4e9f2f333 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/Log4jMessageLogger.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/Log4jMessageLogger.java @@ -21,7 +21,6 @@ package org.apache.qpid.server.logging; import org.apache.log4j.Logger; -import org.apache.qpid.server.configuration.ServerConfiguration; public class Log4jMessageLogger extends AbstractRootMessageLogger { @@ -30,9 +29,9 @@ public class Log4jMessageLogger extends AbstractRootMessageLogger super(); } - public Log4jMessageLogger(ServerConfiguration config) + public Log4jMessageLogger(boolean statusUpdatesEnabled) { - super(config); + super(statusUpdatesEnabled); } @Override diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/AuthenticationProvider.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/AuthenticationProvider.java index 6000886956..417f6036ab 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/AuthenticationProvider.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/AuthenticationProvider.java @@ -24,6 +24,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import org.apache.qpid.server.security.SubjectCreator; +import org.apache.qpid.server.security.group.GroupPrincipalAccessor; + public interface AuthenticationProvider extends ConfiguredObject { @@ -52,4 +55,15 @@ public interface AuthenticationProvider extends ConfiguredObject TYPE)); //children Collection<VirtualHostAlias> getVirtualHostPortBindings(); + + String getName(); + + /** + * A temporary method to create SubjectCreator. + * + * TODO: move all the functionality from SubjectCreator into AuthenticationProvider + */ + SubjectCreator getSubjectCreator(); + + void setGroupAccessor(GroupPrincipalAccessor groupPrincipalAccessor); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Broker.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Broker.java index 08b01a1b65..fbecf1965b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Broker.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Broker.java @@ -20,12 +20,20 @@ */ package org.apache.qpid.server.model; +import java.net.SocketAddress; import java.security.AccessControlException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Map; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.security.SecurityManager; +import org.apache.qpid.server.security.SubjectCreator; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; + public interface Broker extends ConfiguredObject { @@ -44,9 +52,49 @@ public interface Broker extends ConfiguredObject String STATE = "state"; String TIME_TO_LIVE = "timeToLive"; String UPDATED = "updated"; + String DEFAULT_AUTHENTICATION_PROVIDER = "defaultAuthenticationProvider"; + String DEFAULT_VIRTUAL_HOST = "defaultVirtualHost"; + + String ALERT_THRESHOLD_MESSAGE_AGE = "alertThresholdMessageAge"; + String ALERT_THRESHOLD_MESSAGE_COUNT = "alertThresholdMessageCount"; + String ALERT_THRESHOLD_QUEUE_DEPTH = "alertThresholdQueueDepth"; + String ALERT_THRESHOLD_MESSAGE_SIZE = "alertThresholdMessageSize"; + String ALERT_REPEAT_GAP = "alertRepeatGap"; + String FLOW_CONTROL_SIZE_BYTES = "queueFlowControlSizeBytes"; + String FLOW_CONTROL_RESUME_SIZE_BYTES = "queueFlowResumeSizeBytes"; + String MAXIMUM_DELIVERY_ATTEMPTS = "maximumDeliveryAttempts"; + String DEAD_LETTER_QUEUE_ENABLED = "deadLetterQueueEnabled"; + String HOUSEKEEPING_CHECK_PERIOD = "housekeepingCheckPeriod"; + + String SESSION_COUNT_LIMIT = "sessionCountLimit"; + String HEART_BEAT_DELAY = "heartBeatDelay"; + String STATISTICS_REPORTING_PERIOD = "statisticsReportingPeriod"; + String STATISTICS_REPORTING_RESET_ENABLED = "statisticsReportingResetEnabled"; + + /* + * A temporary attribute to pass the path to ACL file. + * TODO: It should be a part of AuthorizationProvider. + */ + String ACL_FILE = "aclFile"; + + /* + * A temporary attributes to set the broker default key/trust stores. + * TODO: Remove them after adding a full support to configure KeyStore/TrustStore via management layers. + */ + String KEY_STORE_PATH = "keyStorePath"; + String KEY_STORE_PASSWORD = "keyStorePassword"; + String KEY_STORE_CERT_ALIAS = "keyStoreCertAlias"; + String TRUST_STORE_PATH = "trustStorePath"; + String TRUST_STORE_PASSWORD = "trustStorePassword"; + + /* + * A temporary attributes to set the broker group file. + * TODO: Remove them after adding a full support to configure authorization providers via management layers. + */ + String GROUP_FILE = "groupFile"; // Attributes - public static final Collection<String> AVAILABLE_ATTRIBUTES = + Collection<String> AVAILABLE_ATTRIBUTES = Collections.unmodifiableList( Arrays.asList(BUILD_VERSION, BYTES_RETAINED, @@ -62,7 +110,32 @@ public interface Broker extends ConfiguredObject NAME, STATE, TIME_TO_LIVE, - UPDATED)); + UPDATED, + DEFAULT_AUTHENTICATION_PROVIDER, + DEFAULT_VIRTUAL_HOST, + ALERT_THRESHOLD_MESSAGE_AGE, + ALERT_THRESHOLD_MESSAGE_COUNT, + ALERT_THRESHOLD_QUEUE_DEPTH, + ALERT_THRESHOLD_MESSAGE_SIZE, + ALERT_REPEAT_GAP, + FLOW_CONTROL_SIZE_BYTES, + FLOW_CONTROL_RESUME_SIZE_BYTES, + MAXIMUM_DELIVERY_ATTEMPTS, + DEAD_LETTER_QUEUE_ENABLED, + HOUSEKEEPING_CHECK_PERIOD, + SESSION_COUNT_LIMIT, + HEART_BEAT_DELAY, + STATISTICS_REPORTING_PERIOD, + STATISTICS_REPORTING_RESET_ENABLED, + + ACL_FILE, + KEY_STORE_PATH, + KEY_STORE_PASSWORD, + KEY_STORE_CERT_ALIAS, + TRUST_STORE_PATH, + TRUST_STORE_PASSWORD, + GROUP_FILE + )); //children Collection < VirtualHost > getVirtualHosts(); @@ -75,6 +148,49 @@ public interface Broker extends ConfiguredObject LifetimePolicy lifetime, long ttl, Map<String, Object> attributes) throws AccessControlException, IllegalArgumentException; - void deleteVirtualHost(VirtualHost virtualHost) - throws AccessControlException, IllegalStateException; + AuthenticationProvider getDefaultAuthenticationProvider(); + + Collection<GroupProvider> getGroupProviders(); + + /** + * A temporary hack to expose root message logger via broker instance. + * TODO We need a better way to do operational logging, for example, via logging listeners + */ + RootMessageLogger getRootMessageLogger(); + + /** + * A temporary hack to expose security manager via broker instance. + * TODO We need to add and implement an authorization provider configured object instead + */ + SecurityManager getSecurityManager(); + + /** + * TODO: A temporary hack to expose log recorder via broker instance. + */ + LogRecorder getLogRecorder(); + + VirtualHost findVirtualHostByName(String name); + + /** + * Get the SubjectCreator for the given socket address. + * TODO: move the authentication related functionality into host aliases and AuthenticationProviders + * + * @param address The (listening) socket address for which the AuthenticationManager is required + */ + SubjectCreator getSubjectCreator(SocketAddress localAddress); + + Collection<KeyStore> getKeyStores(); + + Collection<TrustStore> getTrustStores(); + + /* + * TODO: Remove this method. Eventually the broker will become a registry. + */ + VirtualHostRegistry getVirtualHostRegistry(); + + KeyStore getDefaultKeyStore(); + + TrustStore getDefaultTrustStore(); + + TaskExecutor getTaskExecutor(); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfigurationChangeListener.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfigurationChangeListener.java index 78b98faffe..bd7da962ba 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfigurationChangeListener.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfigurationChangeListener.java @@ -36,4 +36,5 @@ public interface ConfigurationChangeListener void childRemoved(ConfiguredObject object, ConfiguredObject child); + void attributeSet(ConfiguredObject object, String attributeName, Object oldAttributeValue, Object newAttributeValue); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java index 414b2d083a..d567a3aa44 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java @@ -25,6 +25,9 @@ import java.util.Collection; import java.util.Map; import java.util.UUID; +/** + * An object that can be "managed" (eg via the web interface) and usually read from configuration. + */ public interface ConfiguredObject { @@ -47,7 +50,7 @@ public interface ConfiguredObject * Attempt to change the name of the object * * Request a change to the name of the object. The caller must pass in the name it believes the object currently - * has. If the current name differes from this expected value, then no name change will occur + * has. If the current name differs from this expected value, then no name change will occur * * @param currentName the name the caller believes the object to have * @param desiredName the name the caller would like the object to have @@ -198,14 +201,25 @@ public interface ConfiguredObject /** - * Return the value for the given attribute + * Return the value for the given attribute name. The actual attribute value + * is returned if the configured object has such attribute set. If not, the + * value is looked default attributes. * - * @param name the name of the attribute - * @return the value of the attribute at the object (or null if the attribute is not set + * @param name + * the name of the attribute + * @return the value of the attribute at the object (or null if the + * attribute value is set neither on object itself no in defaults) */ Object getAttribute(String name); /** + * Return the map containing only explicitly set attributes + * + * @return the map with the attributes + */ + Map<String, Object> getActualAttributes(); + + /** * Set the value of an attribute * * @param name the name of the attribute to be set diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/GroupProvider.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/GroupProvider.java index 76fa379c1b..9016f97927 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/GroupProvider.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/GroupProvider.java @@ -19,9 +19,11 @@ */ package org.apache.qpid.server.model; +import java.security.Principal; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Set; public interface GroupProvider extends ConfiguredObject { @@ -48,4 +50,6 @@ public interface GroupProvider extends ConfiguredObject CREATED, UPDATED, TYPE)); + + Set<Principal> getGroupPrincipalsForUser(String username); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/KeyStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/KeyStore.java new file mode 100644 index 0000000000..959714656b --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/KeyStore.java @@ -0,0 +1,51 @@ +/* + * + * 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.model; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public interface KeyStore extends TrustStore +{ + + String CERTIFICATE_ALIAS = "certificateAlias"; + + public static final Collection<String> AVAILABLE_ATTRIBUTES = + Collections.unmodifiableList( + Arrays.asList( + ID, + NAME, + STATE, + DURABLE, + LIFETIME_POLICY, + TIME_TO_LIVE, + CREATED, + UPDATED, + DESCRIPTION, + PATH, + PASSWORD, + TYPE, + KEY_MANAGER_FACTORY_ALGORITHM, + CERTIFICATE_ALIAS + )); + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Model.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Model.java index 329574f017..2c05dce9cb 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Model.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Model.java @@ -48,6 +48,9 @@ public class Model addRelationship(Broker.class, Port.class); addRelationship(Broker.class, AuthenticationProvider.class); addRelationship(Broker.class, GroupProvider.class); + addRelationship(Broker.class, TrustStore.class); + addRelationship(Broker.class, KeyStore.class); + addRelationship(Broker.class, Plugin.class); addRelationship(VirtualHost.class, Exchange.class); addRelationship(VirtualHost.class, Queue.class); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Plugin.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Plugin.java new file mode 100644 index 0000000000..b9503a5841 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Plugin.java @@ -0,0 +1,52 @@ +/* + * + * 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.model; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public interface Plugin extends ConfiguredObject +{ + //Hack, using it for the class name only for consistency with the other things. + String CREATED = "created"; + String DURABLE = "durable"; + String ID = "id"; + String LIFETIME_POLICY = "lifetimePolicy"; + String NAME = "name"; + String STATE = "state"; + String TIME_TO_LIVE = "timeToLive"; + String UPDATED = "updated"; + + // Attributes + public static final Collection<String> AVAILABLE_ATTRIBUTES = + Collections.unmodifiableList( + Arrays.asList( + ID, + NAME, + STATE, + DURABLE, + LIFETIME_POLICY, + TIME_TO_LIVE, + CREATED, + UPDATED + )); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Port.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Port.java index 50c0ebcd14..2f94c3cab7 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Port.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Port.java @@ -39,6 +39,16 @@ public interface Port extends ConfiguredObject String PORT = "port"; String PROTOCOLS = "protocols"; String TRANSPORTS = "transports"; + String TCP_NO_DELAY = "tcpNoDelay"; + String SEND_BUFFER_SIZE = "sendBufferSize"; + String RECEIVE_BUFFER_SIZE = "receiveBufferSize"; + String NEED_CLIENT_AUTH = "needClientAuth"; + String WANT_CLIENT_AUTH = "wantClientAuth"; + + /** + * TODO: rename it to AUTHENTICATION_MANAGER_ID or introduce relationships + */ + String AUTHENTICATION_MANAGER = "authenticationManager"; // Attributes public static final Collection<String> AVAILABLE_ATTRIBUTES = @@ -55,7 +65,13 @@ public interface Port extends ConfiguredObject BINDING_ADDRESS, PORT, PROTOCOLS, - TRANSPORTS + TRANSPORTS, + TCP_NO_DELAY, + SEND_BUFFER_SIZE, + RECEIVE_BUFFER_SIZE, + NEED_CLIENT_AUTH, + WANT_CLIENT_AUTH, + AUTHENTICATION_MANAGER )); @@ -88,4 +104,8 @@ public interface Port extends ConfiguredObject //children Collection<VirtualHostAlias> getVirtualHostBindings(); Collection<Connection> getConnections(); + + AuthenticationProvider getAuthenticationProvider(); + + void setAuthenticationProvider(AuthenticationProvider authenticationProvider); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Protocol.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Protocol.java index 5d9de69f9a..6cd5eb23a4 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Protocol.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Protocol.java @@ -20,14 +20,95 @@ */ package org.apache.qpid.server.model; +import java.util.Collection; +import java.util.EnumSet; + +import org.apache.qpid.server.protocol.AmqpProtocolVersion; + public enum Protocol { - AMQP_0_8, - AMQP_0_9, - AMQP_0_9_1, - AMQP_0_10, - AMQP_1_0, - JMX, - HTTP, - HTTPS + AMQP_0_8(ProtocolType.AMQP), + AMQP_0_9(ProtocolType.AMQP), + AMQP_0_9_1(ProtocolType.AMQP), + AMQP_0_10(ProtocolType.AMQP), + AMQP_1_0(ProtocolType.AMQP), + JMX_RMI(ProtocolType.JMX), + HTTP(ProtocolType.HTTP), + HTTPS(ProtocolType.HTTP), + RMI(ProtocolType.RMI); + + private final ProtocolType _protocolType; + + private Protocol(ProtocolType type) + { + _protocolType = type; + } + + public ProtocolType getProtocolType() + { + return _protocolType; + } + + public boolean isAMQP() + { + return _protocolType == ProtocolType.AMQP; + } + + public AmqpProtocolVersion toAmqpProtocolVersion() + { + switch(this) + { + case AMQP_0_8: + return AmqpProtocolVersion.v0_8; + case AMQP_0_9: + return AmqpProtocolVersion.v0_9; + case AMQP_0_9_1: + return AmqpProtocolVersion.v0_9_1; + case AMQP_0_10: + return AmqpProtocolVersion.v0_10; + case AMQP_1_0: + return AmqpProtocolVersion.v1_0_0; + default: + throw new IllegalArgumentException(this + " is not an known AMQP protocol"); + } + } + + public static Protocol valueOfObject(Object protocolObject) + { + Protocol protocol; + if (protocolObject instanceof Protocol) + { + protocol = (Protocol) protocolObject; + } + else + { + try + { + protocol = Protocol.valueOf(String.valueOf(protocolObject)); + } + catch (Exception e) + { + throw new IllegalArgumentException("Can't convert '" + protocolObject + + "' to one of the supported protocols: " + EnumSet.allOf(Protocol.class), e); + } + } + return protocol; + } + + public static boolean hasAmqpProtocol(Collection<Protocol> protocols) + { + for (Protocol protocol : protocols) + { + if (protocol.isAMQP()) + { + return true; + } + } + return false; + } + + public static enum ProtocolType + { + AMQP, HTTP, JMX, RMI; + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Transport.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Transport.java index 03cd46be01..ae6e5ac43a 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Transport.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/Transport.java @@ -20,8 +20,32 @@ */ package org.apache.qpid.server.model; +import java.util.EnumSet; + public enum Transport { TCP, - SSL + SSL; + + public static Transport valueOfObject(Object transportObject) + { + Transport transport; + if (transportObject instanceof Transport) + { + transport = (Transport) transportObject; + } + else + { + try + { + transport = Transport.valueOf(String.valueOf(transportObject)); + } + catch (Exception e) + { + throw new IllegalArgumentException("Can't convert '" + transportObject + + "' to one of the supported transports: " + EnumSet.allOf(Transport.class), e); + } + } + return transport; + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/TrustStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/TrustStore.java new file mode 100644 index 0000000000..0c322ae02f --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/TrustStore.java @@ -0,0 +1,65 @@ +/* + * + * 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.model; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public interface TrustStore extends ConfiguredObject +{ + String ID = "id"; + String NAME = "name"; + String DURABLE = "durable"; + String LIFETIME_POLICY = "lifetimePolicy"; + String STATE = "state"; + String TIME_TO_LIVE = "timeToLive"; + String CREATED = "created"; + String UPDATED = "updated"; + String DESCRIPTION = "description"; + + String PATH = "path"; + String PASSWORD = "password"; + String TYPE = "type"; + String KEY_MANAGER_FACTORY_ALGORITHM = "keyManagerFactoryAlgorithm"; + + public static final Collection<String> AVAILABLE_ATTRIBUTES = + Collections.unmodifiableList( + Arrays.asList( + ID, + NAME, + STATE, + DURABLE, + LIFETIME_POLICY, + TIME_TO_LIVE, + CREATED, + UPDATED, + DESCRIPTION, + PATH, + PASSWORD, + TYPE, + KEY_MANAGER_FACTORY_ALGORITHM + )); + + public String getPassword(); + + public void setPassword(String password); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/UUIDGenerator.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/UUIDGenerator.java index 6606944dc5..bcedd91596 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/UUIDGenerator.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/UUIDGenerator.java @@ -87,4 +87,9 @@ public class UUIDGenerator { return createUUID(GroupMember.class.getName(), groupProviderName, groupName, groupMemberName); } + + public static UUID generateBrokerChildUUID(String type, String childName) + { + return createUUID(type, childName); + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/VirtualHost.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/VirtualHost.java index 01257d588f..5f4ec1d3a8 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/VirtualHost.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/VirtualHost.java @@ -21,6 +21,9 @@ package org.apache.qpid.server.model; import org.apache.qpid.server.queue.QueueEntry; +import org.apache.qpid.server.security.SecurityManager; +import org.apache.qpid.server.store.MessageStore; + import java.security.AccessControlException; import java.util.Arrays; import java.util.Collection; @@ -64,12 +67,12 @@ public interface VirtualHost extends ConfiguredObject String MAXIMUM_DELIVERY_ATTEMPTS = "maximumDeliveryAttempts"; String QUEUE_FLOW_CONTROL_SIZE_BYTES = "queueFlowControlSizeBytes"; String QUEUE_FLOW_RESUME_SIZE_BYTES = "queueFlowResumeSizeBytes"; - String STORE_CONFIGURATION = "storeConfiguration"; String STORE_TRANSACTION_IDLE_TIMEOUT_CLOSE = "storeTransactionIdleTimeoutClose"; String STORE_TRANSACTION_IDLE_TIMEOUT_WARN = "storeTransactionIdleTimeoutWarn"; String STORE_TRANSACTION_OPEN_TIMEOUT_CLOSE = "storeTransactionOpenTimeoutClose"; String STORE_TRANSACTION_OPEN_TIMEOUT_WARN = "storeTransactionOpenTimeoutWarn"; String STORE_TYPE = "storeType"; + String STORE_PATH = "storePath"; String SUPPORTED_EXCHANGE_TYPES = "supportedExchangeTypes"; String SUPPORTED_QUEUE_TYPES = "supportedQueueTypes"; String CREATED = "created"; @@ -80,6 +83,8 @@ public interface VirtualHost extends ConfiguredObject String STATE = "state"; String TIME_TO_LIVE = "timeToLive"; String UPDATED = "updated"; + String CONFIG_PATH = "configPath"; + // Attributes public static final Collection<String> AVAILABLE_ATTRIBUTES = Collections.unmodifiableList( @@ -100,7 +105,7 @@ public interface VirtualHost extends ConfiguredObject QUEUE_FLOW_CONTROL_SIZE_BYTES, QUEUE_FLOW_RESUME_SIZE_BYTES, STORE_TYPE, - STORE_CONFIGURATION, + STORE_PATH, STORE_TRANSACTION_IDLE_TIMEOUT_CLOSE, STORE_TRANSACTION_IDLE_TIMEOUT_WARN, STORE_TRANSACTION_OPEN_TIMEOUT_CLOSE, @@ -109,7 +114,8 @@ public interface VirtualHost extends ConfiguredObject ALERT_THRESHOLD_MESSAGE_AGE, ALERT_THRESHOLD_MESSAGE_SIZE, ALERT_THRESHOLD_QUEUE_DEPTH_BYTES, - ALERT_THRESHOLD_QUEUE_DEPTH_MESSAGES)); + ALERT_THRESHOLD_QUEUE_DEPTH_MESSAGES, + CONFIG_PATH)); @@ -147,4 +153,12 @@ public interface VirtualHost extends ConfiguredObject } void executeTransaction(TransactionalOperation op); + + /** + * A temporary hack to expose host security manager. + * TODO We need to add and implement an authorization provider configured object instead + */ + SecurityManager getSecurityManager(); + + MessageStore getMessageStore(); } 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 7d6aa9b2cb..387b1b6b58 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 @@ -24,12 +24,18 @@ import java.security.AccessControlException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; + import org.apache.qpid.server.model.ConfigurationChangeListener; import org.apache.qpid.server.model.ConfiguredObject; import org.apache.qpid.server.model.IllegalStateTransitionException; import org.apache.qpid.server.model.State; +import org.apache.qpid.server.configuration.updater.ChangeStateTask; +import org.apache.qpid.server.configuration.updater.CreateChildTask; +import org.apache.qpid.server.configuration.updater.SetAttributeTask; +import org.apache.qpid.server.configuration.updater.TaskExecutor; abstract class AbstractAdapter implements ConfiguredObject { @@ -40,134 +46,78 @@ abstract class AbstractAdapter implements ConfiguredObject new ArrayList<ConfigurationChangeListener>(); private final UUID _id; + private final Map<String, Object> _defaultAttributes = new HashMap<String, Object>(); + private final TaskExecutor _taskExecutor; - protected AbstractAdapter(UUID id) + protected AbstractAdapter(UUID id, Map<String, Object> defaults, Map<String, Object> attributes, TaskExecutor taskExecutor) { + _taskExecutor = taskExecutor; _id = id; - } - - static String getStringAttribute(String name, Map<String,Object> attributes, String defaultVal) - { - final Object value = attributes.get(name); - return value == null ? defaultVal : String.valueOf(value); - } - - static Map getMapAttribute(String name, Map<String,Object> attributes, Map defaultVal) - { - final Object value = attributes.get(name); - if(value == null) - { - return defaultVal; - } - else if(value instanceof Map) + if (attributes != null) { - return (Map) value; + Collection<String> names = getAttributeNames(); + for (String name : names) + { + if (attributes.containsKey(name)) + { + _attributes.put(name, attributes.get(name)); + } + } } - else + if (defaults != null) { - throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Map"); + _defaultAttributes.putAll(defaults); } } - - static <E extends Enum> E getEnumAttribute(Class<E> clazz, String name, Map<String,Object> attributes, E defaultVal) + protected AbstractAdapter(UUID id, TaskExecutor taskExecutor) { - Object obj = attributes.get(name); - if(obj == null) - { - return defaultVal; - } - else if(clazz.isInstance(obj)) - { - return (E) obj; - } - else if(obj instanceof String) - { - return (E) Enum.valueOf(clazz, (String)obj); - } - else - { - throw new IllegalArgumentException("Value for attribute " + name + " is not of required type " + clazz.getSimpleName()); - } + this(id, null, null, taskExecutor); } - static Boolean getBooleanAttribute(String name, Map<String,Object> attributes, Boolean defaultValue) + public final UUID getId() { - Object obj = attributes.get(name); - if(obj == null) - { - return defaultValue; - } - else if(obj instanceof Boolean) - { - return (Boolean) obj; - } - else if(obj instanceof String) - { - return Boolean.parseBoolean((String) obj); - } - else - { - throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Boolean"); - } + return _id; } - static Integer getIntegerAttribute(String name, Map<String,Object> attributes, Integer defaultValue) + public State getDesiredState() { - Object obj = attributes.get(name); - if(obj == null) - { - return defaultValue; - } - else if(obj instanceof Number) - { - return ((Number) obj).intValue(); - } - else if(obj instanceof String) - { - return Integer.valueOf((String) obj); - } - else - { - throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Integer"); - } + return null; //TODO } - static Long getLongAttribute(String name, Map<String,Object> attributes, Long defaultValue) + @Override + public final State setDesiredState(final State currentState, final State desiredState) + throws IllegalStateTransitionException, AccessControlException { - Object obj = attributes.get(name); - if(obj == null) - { - return defaultValue; - } - else if(obj instanceof Number) + if (_taskExecutor.isTaskExecutorThread()) { - return ((Number) obj).longValue(); - } - else if(obj instanceof String) - { - return Long.valueOf((String) obj); + if (setState(currentState, desiredState)) + { + notifyStateChanged(currentState, desiredState); + } } else { - throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Long"); + _taskExecutor.submitAndWait(new ChangeStateTask(this, currentState, desiredState)); } + return getActualState(); } - public final UUID getId() - { - return _id; - } + /** + * @return true when the state has been successfully updated to desiredState or false otherwise + */ + protected abstract boolean setState(State currentState, State desiredState); - public State getDesiredState() + protected void notifyStateChanged(final State currentState, final State desiredState) { - return null; //TODO - } - - public State setDesiredState(final State currentState, final State desiredState) - throws IllegalStateTransitionException, AccessControlException - { - return null; //TODO + synchronized (_changeListeners) + { + List<ConfigurationChangeListener> copy = new ArrayList<ConfigurationChangeListener>(_changeListeners); + for(ConfigurationChangeListener listener : copy) + { + listener.stateChanged(this, currentState, desiredState); + } + } } public void addChangeListener(final ConfigurationChangeListener listener) @@ -176,7 +126,7 @@ abstract class AbstractAdapter implements ConfiguredObject { throw new NullPointerException("Cannot add a null listener"); } - synchronized (this) + synchronized (_changeListeners) { if(!_changeListeners.contains(listener)) { @@ -191,39 +141,76 @@ abstract class AbstractAdapter implements ConfiguredObject { throw new NullPointerException("Cannot remove a null listener"); } - synchronized (this) + synchronized (_changeListeners) { return _changeListeners.remove(listener); } } - protected void childAdded(ConfiguredObject child) { - synchronized (this) + synchronized (_changeListeners) { - for(ConfigurationChangeListener listener : _changeListeners) + List<ConfigurationChangeListener> copy = new ArrayList<ConfigurationChangeListener>(_changeListeners); + for(ConfigurationChangeListener listener : copy) { listener.childAdded(this, child); } } } - protected void childRemoved(ConfiguredObject child) { - synchronized (this) + synchronized (_changeListeners) { - for(ConfigurationChangeListener listener : _changeListeners) + List<ConfigurationChangeListener> copy = new ArrayList<ConfigurationChangeListener>(_changeListeners); + for(ConfigurationChangeListener listener : copy) { listener.childRemoved(this, child); } } } - public Object getAttribute(final String name) + protected void attributeSet(String attrinuteName, Object oldAttributeValue, Object newAttributeValue) { - synchronized (this) + synchronized (_changeListeners) + { + List<ConfigurationChangeListener> copy = new ArrayList<ConfigurationChangeListener>(_changeListeners); + for(ConfigurationChangeListener listener : copy) + { + listener.attributeSet(this, attrinuteName, oldAttributeValue, newAttributeValue); + } + } + } + + private final Object getDefaultAttribute(String name) + { + return _defaultAttributes.get(name); + } + + @Override + public Object getAttribute(String name) + { + Object value = getActualAttribute(name); + if (value == null) + { + value = getDefaultAttribute(name); + } + return value; + } + + @Override + public final Map<String, Object> getActualAttributes() + { + synchronized (_attributes) + { + return new HashMap<String, Object>(_attributes); + } + } + + private Object getActualAttribute(final String name) + { + synchronized (_attributes) { return _attributes.get(name); } @@ -232,25 +219,41 @@ abstract class AbstractAdapter implements ConfiguredObject public Object setAttribute(final String name, final Object expected, final Object desired) throws IllegalStateException, AccessControlException, IllegalArgumentException { - synchronized (this) + if (_taskExecutor.isTaskExecutorThread()) { - Object currentValue = _attributes.get(name); + if (changeAttribute(name, expected, desired)) + { + attributeSet(name, expected, desired); + } + } + else + { + _taskExecutor.submitAndWait(new SetAttributeTask(this, name, expected, desired)); + } + return getAttribute(name); + } + + protected boolean changeAttribute(final String name, final Object expected, final Object desired) + { + synchronized (_attributes) + { + Object currentValue = getAttribute(name); if((currentValue == null && expected == null) || (currentValue != null && currentValue.equals(expected))) { _attributes.put(name, desired); - return desired; + return true; } else { - return currentValue; + return false; } } } public <T extends ConfiguredObject> T getParent(final Class<T> clazz) { - synchronized (this) + synchronized (_parents) { return (T) _parents.get(clazz); } @@ -258,7 +261,7 @@ abstract class AbstractAdapter implements ConfiguredObject protected <T extends ConfiguredObject> void addParent(Class<T> clazz, T parent) { - synchronized (this) + synchronized (_parents) { _parents.put(clazz, parent); } @@ -280,4 +283,40 @@ abstract class AbstractAdapter implements ConfiguredObject } } + @Override + public String toString() + { + return getClass().getSimpleName() + " [id=" + _id + "]"; + } + + @SuppressWarnings("unchecked") + @Override + public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + { + if (_taskExecutor.isTaskExecutorThread()) + { + C child = addChild(childClass, attributes, otherParents); + if (child != null) + { + childAdded(child); + } + return child; + } + else + { + return (C)_taskExecutor.submitAndWait(new CreateChildTask(this, childClass, attributes, otherParents)); + } + } + + protected <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + { + throw new UnsupportedOperationException(); + } + + + protected TaskExecutor getTaskExecutor() + { + return _taskExecutor; + } + } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractKeyStoreAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractKeyStoreAdapter.java new file mode 100644 index 0000000000..ebd98f915d --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractKeyStoreAdapter.java @@ -0,0 +1,198 @@ +/* + * + * 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.model.adapter; + +import java.security.AccessControlException; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.Statistics; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.server.util.MapValueConverter; + +public abstract class AbstractKeyStoreAdapter extends AbstractAdapter +{ + private String _name; + private String _password; + + protected AbstractKeyStoreAdapter(UUID id, Broker broker, Map<String, Object> attributes) + { + super(id, broker.getTaskExecutor()); + addParent(Broker.class, broker); + _name = MapValueConverter.getStringAttribute(TrustStore.NAME, attributes); + _password = MapValueConverter.getStringAttribute(TrustStore.PASSWORD, attributes); + setMandatoryAttribute(TrustStore.PATH, attributes); + setOptionalAttribute(TrustStore.TYPE, attributes); + setOptionalAttribute(TrustStore.KEY_MANAGER_FACTORY_ALGORITHM, attributes); + setOptionalAttribute(TrustStore.DESCRIPTION, attributes); + } + + @Override + public String getName() + { + return _name; + } + + @Override + public String setName(String currentName, String desiredName) throws IllegalStateException, AccessControlException + { + throw new IllegalStateException(); + } + + @Override + public State getActualState() + { + return State.ACTIVE; + } + + @Override + public boolean isDurable() + { + return true; + } + + @Override + public void setDurable(boolean durable) throws IllegalStateException, AccessControlException, IllegalArgumentException + { + throw new IllegalStateException(); + } + + @Override + public LifetimePolicy getLifetimePolicy() + { + return LifetimePolicy.PERMANENT; + } + + @Override + public LifetimePolicy setLifetimePolicy(LifetimePolicy expected, LifetimePolicy desired) throws IllegalStateException, AccessControlException, + IllegalArgumentException + { + throw new IllegalStateException(); + } + + @Override + public long getTimeToLive() + { + return 0; + } + + @Override + public long setTimeToLive(long expected, long desired) throws IllegalStateException, AccessControlException, IllegalArgumentException + { + throw new IllegalStateException(); + } + + @Override + public Statistics getStatistics() + { + return NoStatistics.getInstance(); + } + + @Override + public <C extends ConfiguredObject> Collection<C> getChildren(Class<C> clazz) + { + return Collections.emptySet(); + } + + @Override + public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + { + throw new UnsupportedOperationException(); + } + + @Override + public Object getAttribute(String name) + { + if(KeyStore.ID.equals(name)) + { + return getId(); + } + else if(KeyStore.NAME.equals(name)) + { + return getName(); + } + else if(KeyStore.STATE.equals(name)) + { + return getActualState(); + } + else if(KeyStore.DURABLE.equals(name)) + { + return isDurable(); + } + else if(KeyStore.LIFETIME_POLICY.equals(name)) + { + return getLifetimePolicy(); + } + else if(KeyStore.TIME_TO_LIVE.equals(name)) + { + return getTimeToLive(); + } + else if(KeyStore.CREATED.equals(name)) + { + + } + else if(KeyStore.UPDATED.equals(name)) + { + + } + else if(KeyStore.PASSWORD.equals(name)) + { + return null; // for security reasons we don't expose the password + } + return super.getAttribute(name); + } + + @Override + protected boolean setState(State currentState, State desiredState) + { + return false; + } + + public String getPassword() + { + return _password; + } + + public void setPassword(String password) + { + _password = password; + } + + private void setMandatoryAttribute(String name, Map<String, Object> attributeValues) + { + changeAttribute(name, null, MapValueConverter.getStringAttribute(name, attributeValues)); + } + + private void setOptionalAttribute(String name, Map<String, Object> attributeValues) + { + if (attributeValues.get(name) != null) + { + changeAttribute(name, null, MapValueConverter.getStringAttribute(name, attributeValues)); + } + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractPluginAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractPluginAdapter.java new file mode 100644 index 0000000000..ed4af9881f --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AbstractPluginAdapter.java @@ -0,0 +1,152 @@ +/* + * + * 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.model.adapter; + +import java.security.AccessControlException; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.Plugin; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.Statistics; + +public abstract class AbstractPluginAdapter extends AbstractAdapter implements Plugin +{ + + protected AbstractPluginAdapter(UUID id, Map<String, Object> defaults, Map<String, Object> attributes, TaskExecutor taskExecutor) + { + super(id, defaults, attributes, taskExecutor); + } + + @Override + public String setName(String currentName, String desiredName) throws IllegalStateException, AccessControlException + { + throw new UnsupportedOperationException(); + } + + @Override + public State getActualState() + { + return null; + } + + @Override + public boolean isDurable() + { + return true; + } + + @Override + public void setDurable(boolean durable) throws IllegalStateException, AccessControlException, IllegalArgumentException + { + throw new UnsupportedOperationException(); + } + + @Override + public LifetimePolicy getLifetimePolicy() + { + return LifetimePolicy.PERMANENT; + } + + @Override + public LifetimePolicy setLifetimePolicy(LifetimePolicy expected, LifetimePolicy desired) throws IllegalStateException, + AccessControlException, IllegalArgumentException + { + throw new UnsupportedOperationException(); + } + + @Override + public long getTimeToLive() + { + return 0; + } + + @Override + public long setTimeToLive(long expected, long desired) throws IllegalStateException, AccessControlException, + IllegalArgumentException + { + throw new UnsupportedOperationException(); + } + + @Override + public Statistics getStatistics() + { + return null; + } + + @Override + public <C extends ConfiguredObject> Collection<C> getChildren(Class<C> clazz) + { + return Collections.emptyList(); + } + + @Override + public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, + ConfiguredObject... otherParents) + { + throw new UnsupportedOperationException(); + } + + @Override + public Collection<String> getAttributeNames() + { + return AVAILABLE_ATTRIBUTES; + } + + @Override + public Object getAttribute(String name) + { + if (ID.equals(name)) + { + return getId(); + } + else if (STATE.equals(name)) + { + return getActualState(); + } + else if (DURABLE.equals(name)) + { + return isDurable(); + } + else if (LIFETIME_POLICY.equals(name)) + { + return getLifetimePolicy(); + } + else if (TIME_TO_LIVE.equals(name)) + { + return getTimeToLive(); + } + else if (CREATED.equals(name)) + { + + } + else if (UPDATED.equals(name)) + { + + } + return super.getAttribute(name); + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AmqpPortAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AmqpPortAdapter.java new file mode 100644 index 0000000000..2f7e89bb2b --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AmqpPortAdapter.java @@ -0,0 +1,251 @@ +/* + * 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.model.adapter; + +import static org.apache.qpid.transport.ConnectionSettings.WILDCARD_ADDRESS; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.security.GeneralSecurityException; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import javax.net.ssl.SSLContext; + +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.logging.messages.BrokerMessages; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.Protocol; +import org.apache.qpid.server.model.Transport; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.protocol.AmqpProtocolVersion; +import org.apache.qpid.server.protocol.MultiVersionProtocolEngineFactory; +import org.apache.qpid.ssl.SSLContextFactory; +import org.apache.qpid.transport.NetworkTransportConfiguration; +import org.apache.qpid.transport.network.IncomingNetworkTransport; + +public class AmqpPortAdapter extends PortAdapter +{ + private final Broker _broker; + private IncomingNetworkTransport _transport; + + public AmqpPortAdapter(UUID id, Broker broker, Map<String, Object> attributes, Map<String, Object> defaultAttributes, TaskExecutor taskExecutor) + { + super(id, broker, attributes, defaultAttributes, taskExecutor); + _broker = broker; + } + + @Override + protected void onActivate() + { + Collection<Transport> transports = getTransports(); + Set<AmqpProtocolVersion> supported = convertFromModelProtocolsToAmqp(getProtocols()); + + SSLContext sslContext = null; + if (transports.contains(Transport.SSL)) + { + sslContext = createSslContext(); + } + + AmqpProtocolVersion defaultSupportedProtocolReply = getDefaultAmqpSupportedReply(); + + String bindingAddress = (String) getAttribute(Port.BINDING_ADDRESS); + if (WILDCARD_ADDRESS.equals(bindingAddress)) + { + bindingAddress = null; + } + Integer port = (Integer) getAttribute(Port.PORT); + InetSocketAddress bindingSocketAddress = null; + if ( bindingAddress == null ) + { + bindingSocketAddress = new InetSocketAddress(port); + } + else + { + bindingSocketAddress = new InetSocketAddress(bindingAddress, port); + } + + final NetworkTransportConfiguration settings = new ServerNetworkTransportConfiguration( + bindingSocketAddress, (Boolean)getAttribute(TCP_NO_DELAY), + (Integer)getAttribute(SEND_BUFFER_SIZE), (Integer)getAttribute(RECEIVE_BUFFER_SIZE), + (Boolean)getAttribute(NEED_CLIENT_AUTH), (Boolean)getAttribute(WANT_CLIENT_AUTH)); + + _transport = org.apache.qpid.transport.network.Transport.getIncomingTransportInstance(); + final MultiVersionProtocolEngineFactory protocolEngineFactory = new MultiVersionProtocolEngineFactory( + _broker, supported, defaultSupportedProtocolReply); + + _transport.accept(settings, protocolEngineFactory, sslContext); + CurrentActor.get().message(BrokerMessages.LISTENING(getTransports().toString(), getPort())); + } + + @Override + protected void onStop() + { + if (_transport != null) + { + CurrentActor.get().message(BrokerMessages.SHUTTING_DOWN(getTransports().toString(), getPort())); + _transport.close(); + } + } + + private Set<AmqpProtocolVersion> convertFromModelProtocolsToAmqp(Collection<Protocol> modelProtocols) + { + Set<AmqpProtocolVersion> amqpProtocols = new HashSet<AmqpProtocolVersion>(); + for (Protocol protocol : modelProtocols) + { + amqpProtocols.add(protocol.toAmqpProtocolVersion()); + } + return amqpProtocols; + } + + private SSLContext createSslContext() + { + KeyStore keyStore = _broker.getDefaultKeyStore(); + if (keyStore == null) + { + throw new IllegalConfigurationException("SSL was requested on AMQP port '" + + this.getName() + "' but no key store defined"); + } + + TrustStore trustStore = _broker.getDefaultTrustStore(); + if (((Boolean)getAttribute(NEED_CLIENT_AUTH) || (Boolean)getAttribute(WANT_CLIENT_AUTH)) && trustStore == null) + { + throw new IllegalConfigurationException("Client certificate authentication is enabled on AMQP port '" + + this.getName() + "' but no trust store defined"); + } + + String keystorePath = (String)keyStore.getAttribute(KeyStore.PATH); + String keystorePassword = keyStore.getPassword(); + String keystoreType = (String)keyStore.getAttribute(KeyStore.TYPE); + String keyManagerFactoryAlgorithm = (String)keyStore.getAttribute(KeyStore.KEY_MANAGER_FACTORY_ALGORITHM); + String certAlias = (String)keyStore.getAttribute(KeyStore.CERTIFICATE_ALIAS); + + final SSLContext sslContext; + try + { + if(trustStore != null) + { + String trustStorePassword = trustStore.getPassword(); + String trustStoreType = (String)trustStore.getAttribute(TrustStore.TYPE); + String trustManagerFactoryAlgorithm = (String)trustStore.getAttribute(TrustStore.KEY_MANAGER_FACTORY_ALGORITHM); + String trustStorePath = (String)trustStore.getAttribute(TrustStore.PATH); + + sslContext = SSLContextFactory.buildClientContext(trustStorePath, + trustStorePassword, + trustStoreType, + trustManagerFactoryAlgorithm, + keystorePath, + keystorePassword, keystoreType, keyManagerFactoryAlgorithm, + certAlias); + } + else + { + sslContext = SSLContextFactory.buildServerContext(keystorePath, keystorePassword, keystoreType, keyManagerFactoryAlgorithm); + } + } + catch (GeneralSecurityException e) + { + throw new RuntimeException("Unable to create SSLContext for key or trust store", e); + } + catch (IOException e) + { + throw new RuntimeException("Unable to create SSLContext - unable to load key/trust store", e); + } + return sslContext; + } + + private AmqpProtocolVersion getDefaultAmqpSupportedReply() + { + String defaultAmqpSupportedReply = System.getProperty(BrokerProperties.PROPERTY_DEFAULT_SUPPORTED_PROTOCOL_REPLY); + if (defaultAmqpSupportedReply != null) + { + return AmqpProtocolVersion.valueOf(defaultAmqpSupportedReply); + } + return null; + } + + + class ServerNetworkTransportConfiguration implements NetworkTransportConfiguration + { + private final InetSocketAddress _bindingSocketAddress; + private final Boolean _tcpNoDelay; + private final Integer _sendBufferSize; + private final Integer _receiveBufferSize; + private final boolean _needClientAuth; + private final boolean _wantClientAuth; + + public ServerNetworkTransportConfiguration( + InetSocketAddress bindingSocketAddress, boolean tcpNoDelay, + int sendBufferSize, int receiveBufferSize, + boolean needClientAuth, boolean wantClientAuth) + { + _bindingSocketAddress = bindingSocketAddress; + _tcpNoDelay = tcpNoDelay; + _sendBufferSize = sendBufferSize; + _receiveBufferSize = receiveBufferSize; + _needClientAuth = needClientAuth; + _wantClientAuth = wantClientAuth; + } + + @Override + public boolean wantClientAuth() + { + return _wantClientAuth; + } + + @Override + public boolean needClientAuth() + { + return _needClientAuth; + } + + @Override + public Boolean getTcpNoDelay() + { + return _tcpNoDelay; + } + + @Override + public Integer getSendBufferSize() + { + return _sendBufferSize; + } + + @Override + public Integer getReceiveBufferSize() + { + return _receiveBufferSize; + } + + @Override + public InetSocketAddress getAddress() + { + return _bindingSocketAddress; + } + }; +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java index adad2a355c..ac4b0255d5 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java @@ -29,38 +29,50 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; + import javax.security.auth.login.AccountNotFoundException; import org.apache.log4j.Logger; -import org.apache.qpid.server.model.*; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.IllegalStateTransitionException; +import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.PasswordCredentialManagingAuthenticationProvider; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.Statistics; +import org.apache.qpid.server.model.UUIDGenerator; +import org.apache.qpid.server.model.User; +import org.apache.qpid.server.model.VirtualHostAlias; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.security.SubjectCreator; import org.apache.qpid.server.security.access.Operation; +import org.apache.qpid.server.security.auth.UsernamePrincipal; import org.apache.qpid.server.security.auth.database.PrincipalDatabase; import org.apache.qpid.server.security.auth.manager.AuthenticationManager; import org.apache.qpid.server.security.auth.manager.PrincipalDatabaseAuthenticationManager; -import org.apache.qpid.server.security.auth.UsernamePrincipal; +import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.security.SecurityManager; public abstract class AuthenticationProviderAdapter<T extends AuthenticationManager> extends AbstractAdapter implements AuthenticationProvider { private static final Logger LOGGER = Logger.getLogger(AuthenticationProviderAdapter.class); - private final BrokerAdapter _broker; private final T _authManager; + protected final Broker _broker; - private AuthenticationProviderAdapter(BrokerAdapter brokerAdapter, - final T authManager) - { - super(UUIDGenerator.generateRandomUUID()); - _broker = brokerAdapter; - _authManager = authManager; - } + private GroupPrincipalAccessor _groupAccessor; + + private Object _type; - public static AuthenticationProviderAdapter createAuthenticationProviderAdapter(BrokerAdapter brokerAdapter, - final AuthenticationManager authManager) + private AuthenticationProviderAdapter(UUID id, Broker broker, final T authManager, Map<String, Object> attributes) { - return authManager instanceof PrincipalDatabaseAuthenticationManager - ? new PrincipalDatabaseAuthenticationManagerAdapter(brokerAdapter, (PrincipalDatabaseAuthenticationManager) authManager) - : new SimpleAuthenticationProviderAdapter(brokerAdapter, authManager); + super(id, null, attributes, broker.getTaskExecutor()); + _authManager = authManager; + _broker = broker; + _type = authManager instanceof PrincipalDatabaseAuthenticationManager? PrincipalDatabaseAuthenticationManager.class.getSimpleName() : AuthenticationManager.class.getSimpleName() ; + addParent(Broker.class, broker); } T getAuthManager() @@ -77,7 +89,7 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana @Override public String getName() { - return _authManager.getClass().getSimpleName(); + return (String)getAttribute(AuthenticationProvider.NAME); } @Override @@ -147,7 +159,7 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana { if(TYPE.equals(name)) { - return getName(); + return _type; } else if(CREATED.equals(name)) { @@ -165,10 +177,6 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana { return LifetimePolicy.PERMANENT; } - else if(NAME.equals(name)) - { - return getName(); - } else if(STATE.equals(name)) { return State.ACTIVE; // TODO @@ -191,31 +199,67 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana } @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, - Map<String, Object> attributes, - ConfiguredObject... otherParents) + public boolean setState(State currentState, State desiredState) + throws IllegalStateTransitionException, AccessControlException { - throw new IllegalArgumentException("This authentication provider does not support" + - " creating children of type: " + childClass); + if(desiredState == State.DELETED) + { + return true; + } + else if(desiredState == State.ACTIVE) + { + if (_groupAccessor == null) + { + throw new IllegalStateTransitionException("Cannot transit into ACTIVE state with null group accessor!"); + } + _authManager.initialise(); + return true; + } + else if(desiredState == State.STOPPED) + { + _authManager.close(); + return true; + } + return false; + } + + @Override + public SubjectCreator getSubjectCreator() + { + return new SubjectCreator(_authManager, _groupAccessor); + } + + public void setGroupAccessor(GroupPrincipalAccessor groupAccessor) + { + _groupAccessor = groupAccessor; } - private static class SimpleAuthenticationProviderAdapter extends AuthenticationProviderAdapter<AuthenticationManager> + public static class SimpleAuthenticationProviderAdapter extends AuthenticationProviderAdapter<AuthenticationManager> { + public SimpleAuthenticationProviderAdapter( - BrokerAdapter brokerAdapter, AuthenticationManager authManager) + UUID id, Broker broker, AuthenticationManager authManager, Map<String, Object> attributes) { - super(brokerAdapter,authManager); + super(id, broker,authManager, attributes); + } + + @Override + public <C extends ConfiguredObject> C createChild(Class<C> childClass, + Map<String, Object> attributes, + ConfiguredObject... otherParents) + { + throw new UnsupportedOperationException(); } } - private static class PrincipalDatabaseAuthenticationManagerAdapter + public static class PrincipalDatabaseAuthenticationManagerAdapter extends AuthenticationProviderAdapter<PrincipalDatabaseAuthenticationManager> implements PasswordCredentialManagingAuthenticationProvider { public PrincipalDatabaseAuthenticationManagerAdapter( - BrokerAdapter brokerAdapter, PrincipalDatabaseAuthenticationManager authManager) + UUID id, Broker broker, PrincipalDatabaseAuthenticationManager authManager, Map<String, Object> attributes) { - super(brokerAdapter, authManager); + super(id, broker, authManager, attributes); } @Override @@ -245,9 +289,9 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana } } - private org.apache.qpid.server.security.SecurityManager getSecurityManager() + private SecurityManager getSecurityManager() { - return ApplicationRegistry.getInstance().getSecurityManager(); + return _broker.getSecurityManager(); } private PrincipalDatabase getPrincipalDatabase() @@ -275,7 +319,7 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana Map<String, Map<String,String>> users = new HashMap<String, Map<String, String>>(); for(Principal principal : getPrincipalDatabase().getUsers()) { - users.put(principal.getName(), Collections.EMPTY_MAP); + users.put(principal.getName(), Collections.<String, String>emptyMap()); } return users; } @@ -286,7 +330,7 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana } @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { @@ -298,7 +342,9 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana if(createUser(username, password,null)) { - return (C) new PrincipalAdapter(p); + @SuppressWarnings("unchecked") + C pricipalAdapter = (C) new PrincipalAdapter(p, getTaskExecutor()); + return pricipalAdapter; } else { @@ -307,7 +353,7 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana } } - return super.createChild(childClass, attributes, otherParents); + return super.addChild(childClass, attributes, otherParents); } @Override @@ -319,9 +365,11 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana Collection<User> principals = new ArrayList<User>(users.size()); for(Principal user : users) { - principals.add(new PrincipalAdapter(user)); + principals.add(new PrincipalAdapter(user, getTaskExecutor())); } - return (Collection<C>) Collections.unmodifiableCollection(principals); + @SuppressWarnings("unchecked") + Collection<C> unmodifiablePrincipals = (Collection<C>) Collections.unmodifiableCollection(principals); + return unmodifiablePrincipals; } else { @@ -334,9 +382,9 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana private final Principal _user; - public PrincipalAdapter(Principal user) + public PrincipalAdapter(Principal user, TaskExecutor taskExecutor) { - super(UUIDGenerator.generateUserUUID(PrincipalDatabaseAuthenticationManagerAdapter.this.getName(), user.getName())); + super(UUIDGenerator.generateUserUUID(PrincipalDatabaseAuthenticationManagerAdapter.this.getName(), user.getName()), taskExecutor); _user = user; } @@ -457,20 +505,19 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana } @Override - public Object setAttribute(String name, Object expected, Object desired) + public boolean changeAttribute(String name, Object expected, Object desired) throws IllegalStateException, AccessControlException, IllegalArgumentException { if(name.equals(PASSWORD)) { setPassword((String)desired); + return true; } - return super.setAttribute(name, - expected, - desired); + return super.changeAttribute(name, expected, desired); } @Override - public State setDesiredState(State currentState, State desiredState) + protected boolean setState(State currentState, State desiredState) throws IllegalStateTransitionException, AccessControlException { if(desiredState == State.DELETED) @@ -483,9 +530,9 @@ public abstract class AuthenticationProviderAdapter<T extends AuthenticationMana { LOGGER.warn("Failed to delete user " + _user, e); } - return State.DELETED; + return true; } - return super.setDesiredState(currentState, desiredState); + return false; } } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactory.java new file mode 100644 index 0000000000..e5108ebbcf --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactory.java @@ -0,0 +1,77 @@ +/* + * + * 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.model.adapter; + +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; +import org.apache.qpid.server.security.auth.manager.AuthenticationManager; +import org.apache.qpid.server.security.auth.manager.PrincipalDatabaseAuthenticationManager; +import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.model.adapter.AuthenticationProviderAdapter.PrincipalDatabaseAuthenticationManagerAdapter; +import org.apache.qpid.server.model.adapter.AuthenticationProviderAdapter.SimpleAuthenticationProviderAdapter; + +public class AuthenticationProviderFactory +{ + private final Iterable<AuthenticationManagerFactory> _factories; + + public AuthenticationProviderFactory(QpidServiceLoader<AuthenticationManagerFactory> authManagerFactoryServiceLoader) + { + _factories = authManagerFactoryServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class); + } + + /** + * Creates {@link AuthenticationProvider} for given ID, {@link Broker} and attributes. + * <p> + * The configured {@link AuthenticationManagerFactory}'s are used to try to create the {@link AuthenticationProvider}. + * The first non-null instance is returned. The factories are used in non-deterministic order. + * @param groupPrincipalAccessor TODO + */ + public AuthenticationProvider create(UUID id, Broker broker, Map<String, Object> attributes, GroupPrincipalAccessor groupPrincipalAccessor) + { + for (AuthenticationManagerFactory factory : _factories) + { + AuthenticationManager manager = factory.createInstance(attributes); + if (manager != null) + { + AuthenticationProviderAdapter<?> authenticationProvider; + if (manager instanceof PrincipalDatabaseAuthenticationManager) + { + authenticationProvider = new PrincipalDatabaseAuthenticationManagerAdapter(id, broker, + (PrincipalDatabaseAuthenticationManager) manager, attributes); + } + else + { + authenticationProvider = new SimpleAuthenticationProviderAdapter(id, broker, manager, attributes); + } + authenticationProvider.setGroupAccessor(groupPrincipalAccessor); + return authenticationProvider; + } + } + + throw new IllegalArgumentException("No authentication provider factory found for configuration attributes " + attributes); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BindingAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BindingAdapter.java index abd3160686..eb2d0dd7e2 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BindingAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/BindingAdapter.java @@ -48,7 +48,7 @@ final class BindingAdapter extends AbstractAdapter implements Binding ExchangeAdapter exchangeAdapter, QueueAdapter queueAdapter) { - super(binding.getId()); + super(binding.getId(), queueAdapter.getTaskExecutor()); _binding = binding; _exchange = exchangeAdapter; _queue = queueAdapter; @@ -206,27 +206,20 @@ final class BindingAdapter extends AbstractAdapter implements Binding } @Override - public Object setAttribute(final String name, final Object expected, final Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - return super.setAttribute(name, expected, desired); //TODO - } - - @Override public Collection<String> getAttributeNames() { return Binding.AVAILABLE_ATTRIBUTES; } @Override - public State setDesiredState(State currentState, State desiredState) throws IllegalStateTransitionException, + protected boolean setState(State currentState, State desiredState) throws IllegalStateTransitionException, AccessControlException { if (desiredState == State.DELETED) { delete(); - return State.DELETED; + return true; } - return super.setDesiredState(currentState, desiredState); + return false; } } 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 276dc83540..d6c18a1141 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 @@ -21,89 +21,211 @@ package org.apache.qpid.server.model.adapter; import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.security.AccessControlException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; +import java.util.UUID; + +import javax.net.ssl.KeyManagerFactory; + +import org.apache.log4j.Logger; import org.apache.qpid.common.QpidProperties; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.logging.actors.BrokerActor; +import org.apache.qpid.server.logging.actors.CurrentActor; import org.apache.qpid.server.model.AuthenticationProvider; import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfigurationChangeListener; import org.apache.qpid.server.model.ConfiguredObject; import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.KeyStore; import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.Plugin; 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.Statistics; -import org.apache.qpid.server.model.Transport; +import org.apache.qpid.server.model.TrustStore; import org.apache.qpid.server.model.UUIDGenerator; import org.apache.qpid.server.model.VirtualHost; -import org.apache.qpid.server.registry.IApplicationRegistry; -import org.apache.qpid.server.security.auth.manager.AuthenticationManager; -import org.apache.qpid.server.security.auth.manager.IAuthenticationManagerRegistry; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.security.group.FileGroupManager; import org.apache.qpid.server.security.group.GroupManager; -import org.apache.qpid.server.transport.QpidAcceptor; +import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.security.SecurityManager; +import org.apache.qpid.server.security.SubjectCreator; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.util.MapValueConverter; import org.apache.qpid.server.virtualhost.VirtualHostRegistry; -public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHostRegistry.RegistryChangeListener, - IApplicationRegistry.PortBindingListener, - IAuthenticationManagerRegistry.RegistryChangeListener, - IApplicationRegistry.GroupManagerChangeListener +public class BrokerAdapter extends AbstractAdapter implements Broker, ConfigurationChangeListener { - - private final IApplicationRegistry _applicationRegistry; - private String _name; - private final Map<org.apache.qpid.server.virtualhost.VirtualHost, VirtualHostAdapter> _vhostAdapters = - new HashMap<org.apache.qpid.server.virtualhost.VirtualHost, VirtualHostAdapter>(); - private final StatisticsAdapter _statistics; - private final Map<QpidAcceptor, PortAdapter> _portAdapters = new HashMap<QpidAcceptor, PortAdapter>(); - private Collection<HTTPPortAdapter> _httpManagementPorts; - - private final Map<AuthenticationManager, AuthenticationProviderAdapter> _authManagerAdapters = - new HashMap<AuthenticationManager, AuthenticationProviderAdapter>(); - private final Map<GroupManager, GroupProviderAdapter> _groupManagerAdapters = - new HashMap<GroupManager, GroupProviderAdapter>(); - - - public BrokerAdapter(final IApplicationRegistry instance) - { - super(UUIDGenerator.generateRandomUUID()); - _applicationRegistry = instance; - _name = "Broker"; - _statistics = new StatisticsAdapter(instance); - - instance.getVirtualHostRegistry().addRegistryChangeListener(this); - populateVhosts(); - instance.addPortBindingListener(this); - populatePorts(); - instance.addAuthenticationManagerRegistryChangeListener(this); - populateAuthenticationManagers(); - instance.addGroupManagerChangeListener(this); - populateGroupManagers(); - } - - private void populateVhosts() - { - synchronized(_vhostAdapters) - { - Collection<org.apache.qpid.server.virtualhost.VirtualHost> actualVhosts = - _applicationRegistry.getVirtualHostRegistry().getVirtualHosts(); - for(org.apache.qpid.server.virtualhost.VirtualHost vh : actualVhosts) - { - if(!_vhostAdapters.containsKey(vh)) - { - _vhostAdapters.put(vh, new VirtualHostAdapter(this, vh)); - } - } - + private static final Logger LOGGER = Logger.getLogger(BrokerAdapter.class); + + @SuppressWarnings("serial") + public static final Map<String, Class<?>> ATTRIBUTE_TYPES = Collections.unmodifiableMap(new HashMap<String, Class<?>>(){{ + put(ALERT_THRESHOLD_MESSAGE_AGE, Long.class); + put(ALERT_THRESHOLD_MESSAGE_COUNT, Long.class); + put(ALERT_THRESHOLD_QUEUE_DEPTH, Long.class); + put(ALERT_THRESHOLD_MESSAGE_SIZE, Long.class); + put(ALERT_REPEAT_GAP, Long.class); + put(FLOW_CONTROL_SIZE_BYTES, Long.class); + put(FLOW_CONTROL_RESUME_SIZE_BYTES, Long.class); + put(HOUSEKEEPING_CHECK_PERIOD, Long.class); + + put(DEAD_LETTER_QUEUE_ENABLED, Boolean.class); + put(STATISTICS_REPORTING_RESET_ENABLED, Boolean.class); + + put(MAXIMUM_DELIVERY_ATTEMPTS, Integer.class); + put(SESSION_COUNT_LIMIT, Integer.class); + put(HEART_BEAT_DELAY, Integer.class); + put(STATISTICS_REPORTING_PERIOD, Integer.class); + + put(ACL_FILE, String.class); + put(NAME, String.class); + put(DEFAULT_VIRTUAL_HOST, String.class); + put(DEFAULT_AUTHENTICATION_PROVIDER, String.class); + + put(KEY_STORE_PATH, String.class); + put(KEY_STORE_PASSWORD, String.class); + put(KEY_STORE_CERT_ALIAS, String.class); + put(TRUST_STORE_PATH, String.class); + put(TRUST_STORE_PASSWORD, String.class); + put(GROUP_FILE, String.class); + }}); + + public static final int DEFAULT_STATISTICS_REPORTING_PERIOD = 0; + public static final boolean DEFAULT_STATISTICS_REPORTING_RESET_ENABLED = false; + public static final long DEFAULT_ALERT_REPEAT_GAP = 30000l; + public static final long DEFAULT_ALERT_THRESHOLD_MESSAGE_AGE = 0l; + public static final long DEFAULT_ALERT_THRESHOLD_MESSAGE_COUNT = 0l; + public static final long DEFAULT_ALERT_THRESHOLD_MESSAGE_SIZE = 0l; + public static final long DEFAULT_ALERT_THRESHOLD_QUEUE_DEPTH = 0l; + public static final boolean DEFAULT_DEAD_LETTER_QUEUE_ENABLED = false; + public static final int DEFAULT_MAXIMUM_DELIVERY_ATTEMPTS = 0; + public static final long DEFAULT_FLOW_CONTROL_RESUME_SIZE_BYTES = 0l; + public static final long DEFAULT_FLOW_CONTROL_SIZE_BYTES = 0l; + public static final long DEFAULT_HOUSEKEEPING_CHECK_PERIOD = 30000l; + public static final int DEFAULT_HEART_BEAT_DELAY = 0; + public static final int DEFAULT_SESSION_COUNT_LIMIT = 256; + public static final String DEFAULT_NAME = "QpidBroker"; + private static final String DEFAULT_KEY_STORE_NAME = "defaultKeyStore"; + private static final String DEFAULT_TRUST_STORE_NAME = "defaultTrustStore"; + private static final String DEFAULT_GROUP_PROFIDER_NAME = "defaultGroupProvider"; + + private static final String DUMMY_PASSWORD_MASK = "********"; + + @SuppressWarnings("serial") + private static final Map<String, Object> DEFAULTS = Collections.unmodifiableMap(new HashMap<String, Object>(){{ + put(Broker.STATISTICS_REPORTING_PERIOD, DEFAULT_STATISTICS_REPORTING_PERIOD); + put(Broker.STATISTICS_REPORTING_RESET_ENABLED, DEFAULT_STATISTICS_REPORTING_RESET_ENABLED); + put(Broker.ALERT_REPEAT_GAP, DEFAULT_ALERT_REPEAT_GAP); + put(Broker.ALERT_THRESHOLD_MESSAGE_AGE, DEFAULT_ALERT_THRESHOLD_MESSAGE_AGE); + put(Broker.ALERT_THRESHOLD_MESSAGE_COUNT, DEFAULT_ALERT_THRESHOLD_MESSAGE_COUNT); + put(Broker.ALERT_THRESHOLD_MESSAGE_SIZE, DEFAULT_ALERT_THRESHOLD_MESSAGE_SIZE); + put(Broker.ALERT_THRESHOLD_QUEUE_DEPTH, DEFAULT_ALERT_THRESHOLD_QUEUE_DEPTH); + put(Broker.DEAD_LETTER_QUEUE_ENABLED, DEFAULT_DEAD_LETTER_QUEUE_ENABLED); + put(Broker.MAXIMUM_DELIVERY_ATTEMPTS, DEFAULT_MAXIMUM_DELIVERY_ATTEMPTS); + put(Broker.FLOW_CONTROL_RESUME_SIZE_BYTES, DEFAULT_FLOW_CONTROL_RESUME_SIZE_BYTES); + put(Broker.FLOW_CONTROL_SIZE_BYTES, DEFAULT_FLOW_CONTROL_SIZE_BYTES); + put(Broker.HOUSEKEEPING_CHECK_PERIOD, DEFAULT_HOUSEKEEPING_CHECK_PERIOD); + put(Broker.HEART_BEAT_DELAY, DEFAULT_HEART_BEAT_DELAY); + put(Broker.SESSION_COUNT_LIMIT, DEFAULT_SESSION_COUNT_LIMIT); + put(Broker.NAME, DEFAULT_NAME); + }}); + + + + + private final StatisticsGatherer _statisticsGatherer; + private final VirtualHostRegistry _virtualHostRegistry; + private final LogRecorder _logRecorder; + private final RootMessageLogger _rootMessageLogger; + private StatisticsAdapter _statistics; + + private final Map<String, VirtualHost> _vhostAdapters = new HashMap<String, VirtualHost>(); + private final Map<Integer, Port> _portAdapters = new HashMap<Integer, Port>(); + private final Map<String, AuthenticationProvider> _authenticationProviders = new HashMap<String, AuthenticationProvider>(); + private final Map<String, GroupProvider> _groupProviders = new HashMap<String, GroupProvider>(); + private final Map<UUID, ConfiguredObject> _plugins = new HashMap<UUID, ConfiguredObject>(); + private final Map<UUID, KeyStore> _keyStores = new HashMap<UUID, KeyStore>(); + private final Map<UUID, TrustStore> _trustStores = new HashMap<UUID, TrustStore>(); + + private final AuthenticationProviderFactory _authenticationProviderFactory; + private AuthenticationProvider _defaultAuthenticationProvider; + + private final PortFactory _portFactory; + private final SecurityManager _securityManager; + private final UUID _defaultKeyStoreId; + private final UUID _defaultTrustStoreId; + + public BrokerAdapter(UUID id, Map<String, Object> attributes, StatisticsGatherer statisticsGatherer, VirtualHostRegistry virtualHostRegistry, + LogRecorder logRecorder, RootMessageLogger rootMessageLogger, AuthenticationProviderFactory authenticationProviderFactory, + PortFactory portFactory, TaskExecutor taskExecutor) + { + super(id, DEFAULTS, MapValueConverter.convert(attributes, ATTRIBUTE_TYPES), taskExecutor); + _statisticsGatherer = statisticsGatherer; + _virtualHostRegistry = virtualHostRegistry; + _logRecorder = logRecorder; + _rootMessageLogger = rootMessageLogger; + _statistics = new StatisticsAdapter(statisticsGatherer); + _authenticationProviderFactory = authenticationProviderFactory; + _portFactory = portFactory; + _securityManager = new SecurityManager((String)getAttribute(ACL_FILE)); + + _defaultKeyStoreId = UUIDGenerator.generateBrokerChildUUID(KeyStore.class.getSimpleName(), DEFAULT_KEY_STORE_NAME); + _defaultTrustStoreId = UUIDGenerator.generateBrokerChildUUID(TrustStore.class.getSimpleName(), DEFAULT_TRUST_STORE_NAME); + createBrokerChildrenFromAttributes(); + } + + /* + * A temporary method to create broker children that can be only configured via broker attributes + */ + private void createBrokerChildrenFromAttributes() + { + String groupFile = (String) getAttribute(GROUP_FILE); + if (groupFile != null) + { + GroupManager groupManager = new FileGroupManager(groupFile); + UUID groupProviderId = UUIDGenerator.generateBrokerChildUUID(GroupProvider.class.getSimpleName(), + DEFAULT_GROUP_PROFIDER_NAME); + GroupProviderAdapter groupProviderAdapter = new GroupProviderAdapter(groupProviderId, groupManager, this); + addGroupProvider(groupProviderAdapter); + } + Map<String, Object> actualAttributes = getActualAttributes(); + String keyStorePath = (String) getAttribute(KEY_STORE_PATH); + if (keyStorePath != null) + { + Map<String, Object> keyStoreAttributes = new HashMap<String, Object>(); + keyStoreAttributes.put(KeyStore.NAME, DEFAULT_KEY_STORE_NAME); + keyStoreAttributes.put(KeyStore.PATH, keyStorePath); + keyStoreAttributes.put(KeyStore.PASSWORD, (String) actualAttributes.get(KEY_STORE_PASSWORD)); + keyStoreAttributes.put(KeyStore.TYPE, java.security.KeyStore.getDefaultType()); + keyStoreAttributes.put(KeyStore.CERTIFICATE_ALIAS, getAttribute(KEY_STORE_CERT_ALIAS)); + keyStoreAttributes.put(KeyStore.KEY_MANAGER_FACTORY_ALGORITHM, KeyManagerFactory.getDefaultAlgorithm()); + KeyStoreAdapter KeyStoreAdapter = new KeyStoreAdapter(_defaultKeyStoreId, this, keyStoreAttributes); + addKeyStore(KeyStoreAdapter); + } + String trustStorePath = (String) getAttribute(TRUST_STORE_PATH); + if (trustStorePath != null) + { + Map<String, Object> trsustStoreAttributes = new HashMap<String, Object>(); + trsustStoreAttributes.put(TrustStore.NAME, DEFAULT_TRUST_STORE_NAME); + trsustStoreAttributes.put(TrustStore.PATH, trustStorePath); + trsustStoreAttributes.put(TrustStore.PASSWORD, (String) actualAttributes.get(TRUST_STORE_PASSWORD)); + trsustStoreAttributes.put(TrustStore.TYPE, java.security.KeyStore.getDefaultType()); + trsustStoreAttributes.put(TrustStore.KEY_MANAGER_FACTORY_ALGORITHM, KeyManagerFactory.getDefaultAlgorithm()); + TrustStoreAdapter trustStore = new TrustStoreAdapter(_defaultTrustStoreId, this, trsustStoreAttributes); + addTrustStore(trustStore); } } - public Collection<VirtualHost> getVirtualHosts() { synchronized(_vhostAdapters) @@ -112,107 +234,55 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos } } - private void populatePorts() - { - synchronized (_portAdapters) - { - Map<InetSocketAddress, QpidAcceptor> acceptors = _applicationRegistry.getAcceptors(); - - for(Map.Entry<InetSocketAddress, QpidAcceptor> entry : acceptors.entrySet()) - { - if(!_portAdapters.containsKey(entry.getValue())) - { - _portAdapters.put(entry.getValue(), new PortAdapter(this, entry.getValue(), entry.getKey())); - } - } - if(_applicationRegistry.useHTTPManagement() || _applicationRegistry.useHTTPSManagement()) - { - ArrayList<HTTPPortAdapter> httpPorts = new ArrayList<HTTPPortAdapter>(); - if (_applicationRegistry.useHTTPManagement()) - { - httpPorts.add(new HTTPPortAdapter(this, _applicationRegistry.getHTTPManagementPort())); - } - if (_applicationRegistry.useHTTPSManagement()) - { - httpPorts.add(new HTTPPortAdapter(this, _applicationRegistry.getHTTPSManagementPort(), Protocol.HTTPS, Transport.SSL)); - } - _httpManagementPorts = Collections.unmodifiableCollection(httpPorts); - } - } - } public Collection<Port> getPorts() { synchronized (_portAdapters) { final ArrayList<Port> ports = new ArrayList<Port>(_portAdapters.values()); - if(_httpManagementPorts != null) - { - ports.addAll(_httpManagementPorts); - } return ports; } } - private void populateAuthenticationManagers() + public Collection<AuthenticationProvider> getAuthenticationProviders() { - synchronized (_authManagerAdapters) + synchronized (_authenticationProviders) { - IAuthenticationManagerRegistry authenticationManagerRegistry = - _applicationRegistry.getAuthenticationManagerRegistry(); - if(authenticationManagerRegistry != null) - { - Map<String, AuthenticationManager> authenticationManagers = - authenticationManagerRegistry.getAvailableAuthenticationManagers(); - - for(Map.Entry<String, AuthenticationManager> entry : authenticationManagers.entrySet()) - { - if(!_authManagerAdapters.containsKey(entry.getValue())) - { - _authManagerAdapters.put(entry.getValue(), - AuthenticationProviderAdapter.createAuthenticationProviderAdapter(this, - entry.getValue())); - } - } - } + return new ArrayList<AuthenticationProvider>(_authenticationProviders.values()); } } - private void populateGroupManagers() + public AuthenticationProvider getAuthenticationProviderByName(String authenticationProviderName) { - synchronized (_groupManagerAdapters) + Collection<AuthenticationProvider> providers = getAuthenticationProviders(); + for (AuthenticationProvider authenticationProvider : providers) { - List<GroupManager> groupManagers = _applicationRegistry.getGroupManagers(); - if(groupManagers != null) + if (authenticationProvider.getName().equals(authenticationProviderName)) { - for (GroupManager groupManager : groupManagers) - { - if(!_groupManagerAdapters.containsKey(groupManager)) - { - _groupManagerAdapters.put(groupManager, - GroupProviderAdapter.createGroupProviderAdapter(this, groupManager)); - } - } + return authenticationProvider; } } + return null; } - public Collection<AuthenticationProvider> getAuthenticationProviders() + @Override + public AuthenticationProvider getDefaultAuthenticationProvider() { - synchronized (_authManagerAdapters) - { - final ArrayList<AuthenticationProvider> authManagers = - new ArrayList<AuthenticationProvider>(_authManagerAdapters.values()); - return authManagers; - } + return _defaultAuthenticationProvider; } + public void setDefaultAuthenticationProvider(AuthenticationProvider provider) + { + _defaultAuthenticationProvider = provider; + } + + @Override public Collection<GroupProvider> getGroupProviders() { - synchronized (_groupManagerAdapters) + synchronized (_groupProviders) { final ArrayList<GroupProvider> groupManagers = - new ArrayList<GroupProvider>(_groupManagerAdapters.values()); + new ArrayList<GroupProvider>(_groupProviders.values()); return groupManagers; } } @@ -228,22 +298,29 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos return null; //TODO } - public VirtualHost createVirtualHost(final Map<String, Object> attributes) + private VirtualHost createVirtualHost(final Map<String, Object> attributes) throws AccessControlException, IllegalArgumentException { - return null; //TODO + final VirtualHostAdapter virtualHostAdapter = new VirtualHostAdapter(UUID.randomUUID(), attributes, this, + _statisticsGatherer, getTaskExecutor()); + addVirtualHost(virtualHostAdapter); + virtualHostAdapter.setDesiredState(State.INITIALISING, State.ACTIVE); + return virtualHostAdapter; } - public void deleteVirtualHost(final VirtualHost vhost) - throws AccessControlException, IllegalStateException + private boolean deleteVirtualHost(final VirtualHost vhost) throws AccessControlException, IllegalStateException { - //TODO - throw new UnsupportedOperationException("Not yet implemented"); + synchronized (_vhostAdapters) + { + _vhostAdapters.remove(vhost); + } + vhost.removeChangeListener(this); + return true; } public String getName() { - return _name; + return (String)getAttribute(NAME); } public String setName(final String currentName, final String desiredName) @@ -297,6 +374,7 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos return _statistics; } + @SuppressWarnings("unchecked") @Override public <C extends ConfiguredObject> Collection<C> getChildren(Class<C> clazz) { @@ -316,12 +394,26 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos { return (Collection<C>) getGroupProviders(); } + else if(clazz == KeyStore.class) + { + return (Collection<C>) getKeyStores(); + } + else if(clazz == TrustStore.class) + { + return (Collection<C>) getTrustStores(); + } + else if(clazz == Plugin.class) + { + return (Collection<C>) getPlugins(); + } return Collections.emptySet(); } + //TODO: ACL + @SuppressWarnings("unchecked") @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { if(childClass == VirtualHost.class) { @@ -341,111 +433,107 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos } } - private Port createPort(Map<String, Object> attributes) + private void addPort(Port port) { - // TODO - return null; + synchronized (_portAdapters) + { + int portNumber = port.getPort(); + if(_portAdapters.containsKey(portNumber)) + { + throw new IllegalArgumentException("Cannot add port " + port + " because port number " + portNumber + " already configured"); + } + _portAdapters.put(portNumber, port); + } + port.addChangeListener(this); } - private AuthenticationProvider createAuthenticationProvider(Map<String,Object> attributes) + private Port createPort(Map<String, Object> attributes) { - // TODO - return null; + Port port = _portFactory.createPort(UUID.randomUUID(), this, attributes); + addPort(port); + return port; } + private AuthenticationProvider createAuthenticationProvider(Map<String, Object> attributes) + { + // it's cheap to create the groupPrincipalAccessor on the fly + GroupPrincipalAccessor groupPrincipalAccessor = new GroupPrincipalAccessor(_groupProviders.values()); + + AuthenticationProvider authenticationProvider = _authenticationProviderFactory.create(UUID.randomUUID(), this, attributes, groupPrincipalAccessor); + addAuthenticationProvider(authenticationProvider); + return authenticationProvider; + } - public void virtualHostRegistered(org.apache.qpid.server.virtualhost.VirtualHost virtualHost) + /** + * @throws IllegalConfigurationException if an AuthenticationProvider with the same name already exists + */ + private void addAuthenticationProvider(AuthenticationProvider authenticationProvider) { - VirtualHostAdapter adapter = null; - synchronized (_vhostAdapters) + String name = authenticationProvider.getName(); + synchronized (_authenticationProviders) { - if(!_vhostAdapters.containsKey(virtualHost)) + if(_authenticationProviders.containsKey(name)) { - adapter = new VirtualHostAdapter(this, virtualHost); - _vhostAdapters.put(virtualHost, adapter); + throw new IllegalConfigurationException("Cannot add AuthenticationProvider because one with name " + name + " already exists"); } + _authenticationProviders.put(name, authenticationProvider); } - if(adapter != null) - { - childAdded(adapter); - } + authenticationProvider.addChangeListener(this); } - public void virtualHostUnregistered(org.apache.qpid.server.virtualhost.VirtualHost virtualHost) + private void addGroupProvider(GroupProvider groupProvider) { - VirtualHostAdapter adapter = null; - - synchronized (_vhostAdapters) - { - adapter = _vhostAdapters.remove(virtualHost); - } - if(adapter != null) + synchronized (_groupProviders) { - childRemoved(adapter); + String name = groupProvider.getName(); + if(_groupProviders.containsKey(name)) + { + throw new IllegalConfigurationException("Cannot add GroupProvider because one with name " + name + " already exists"); + } + _groupProviders.put(name, groupProvider); } + groupProvider.addChangeListener(this); } - @Override - public void authenticationManagerRegistered(AuthenticationManager authenticationManager) + private boolean deleteGroupProvider(GroupProvider object) { - AuthenticationProviderAdapter adapter = null; - synchronized (_authManagerAdapters) + throw new UnsupportedOperationException("Not implemented yet!"); + } + + private void addKeyStore(KeyStore keyStore) + { + synchronized (_keyStores) { - if(!_authManagerAdapters.containsKey(authenticationManager)) + if(_keyStores.containsKey(keyStore.getId())) { - adapter = - AuthenticationProviderAdapter.createAuthenticationProviderAdapter(this, authenticationManager); - _authManagerAdapters.put(authenticationManager, adapter); + throw new IllegalConfigurationException("Cannot add KeyStore because one with id " + keyStore.getId() + " already exists"); } + _keyStores.put(keyStore.getId(), keyStore); } - if(adapter != null) - { - childAdded(adapter); - } + keyStore.addChangeListener(this); } - @Override - public void authenticationManagerUnregistered(AuthenticationManager authenticationManager) + private boolean deleteKeyStore(KeyStore object) { - AuthenticationProviderAdapter adapter; - synchronized (_authManagerAdapters) - { - adapter = _authManagerAdapters.remove(authenticationManager); - } - if(adapter != null) - { - childRemoved(adapter); - } + throw new UnsupportedOperationException("Not implemented yet!"); } - - @Override - public void bound(QpidAcceptor acceptor, InetSocketAddress bindAddress) + private void addTrustStore(TrustStore trustStore) { - synchronized (_portAdapters) + synchronized (_trustStores) { - if(!_portAdapters.containsKey(acceptor)) + if(_trustStores.containsKey(trustStore.getId())) { - PortAdapter adapter = new PortAdapter(this, acceptor, bindAddress); - _portAdapters.put(acceptor, adapter); - childAdded(adapter); + throw new IllegalConfigurationException("Cannot add TrustStore because one with id " + trustStore.getId() + " already exists"); } + _trustStores.put(trustStore.getId(), trustStore); } + trustStore.addChangeListener(this); } - @Override - public void unbound(QpidAcceptor acceptor) + private boolean deleteTrustStore(TrustStore object) { - PortAdapter adapter = null; - - synchronized (_portAdapters) - { - adapter = _portAdapters.remove(acceptor); - } - if(adapter != null) - { - childRemoved(adapter); - } + throw new UnsupportedOperationException("Not implemented yet!"); } @Override @@ -461,10 +549,6 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos { return getId(); } - else if(NAME.equals(name)) - { - return getName(); - } else if(STATE.equals(name)) { return State.ACTIVE; @@ -520,46 +604,308 @@ public class BrokerAdapter extends AbstractAdapter implements Broker, VirtualHos { // TODO } + else if (DEFAULT_AUTHENTICATION_PROVIDER.equals(name)) + { + return _defaultAuthenticationProvider == null ? null : _defaultAuthenticationProvider.getName(); + } + else if (KEY_STORE_PASSWORD.equals(name)) + { + return DUMMY_PASSWORD_MASK; + } + else if (TRUST_STORE_PASSWORD.equals(name)) + { + return DUMMY_PASSWORD_MASK; + } + return super.getAttribute(name); + } + + private boolean deletePort(Port portAdapter) + { + Port removedPort = null; + synchronized (_portAdapters) + { + removedPort = _portAdapters.remove(portAdapter.getPort()); + } + return removedPort != null; + } + + private boolean deleteAuthenticationProvider(AuthenticationProvider authenticationProvider) + { + AuthenticationProvider removedAuthenticationProvider = null; + synchronized (_authenticationProviders) + { + removedAuthenticationProvider = _authenticationProviders.remove(authenticationProvider.getName()); + } + return removedAuthenticationProvider != null; + } + + private void addVirtualHost(VirtualHost virtualHost) + { + synchronized (_vhostAdapters) + { + String name = virtualHost.getName(); + if (_vhostAdapters.containsKey(name)) + { + throw new IllegalConfigurationException("Virtual host with name " + name + " is already specified!"); + } + _vhostAdapters.put(name, virtualHost); + } + virtualHost.addChangeListener(this); + } + + @Override + public boolean setState(State currentState, State desiredState) + { + if (desiredState == State.ACTIVE) + { + changeState(_groupProviders, currentState, State.ACTIVE, false); + changeState(_authenticationProviders, currentState, State.ACTIVE, false); + + CurrentActor.set(new BrokerActor(getRootMessageLogger())); + try + { + changeState(_vhostAdapters, currentState, State.ACTIVE, false); + } + finally + { + CurrentActor.remove(); + } + + changeState(_portAdapters, currentState,State.ACTIVE, false); + changeState(_plugins, currentState,State.ACTIVE, false); + return true; + } + else if (desiredState == State.STOPPED) + { + changeState(_plugins, currentState,State.STOPPED, true); + changeState(_portAdapters, currentState, State.STOPPED, true); + changeState(_vhostAdapters,currentState, State.STOPPED, true); + changeState(_authenticationProviders, currentState, State.STOPPED, true); + changeState(_groupProviders, currentState, State.STOPPED, true); + return true; + } + return false; + } + + private void changeState(Map<?, ? extends ConfiguredObject> configuredObjectMap, State currentState, State desiredState, boolean swallowException) + { + synchronized(configuredObjectMap) + { + Collection<? extends ConfiguredObject> adapters = configuredObjectMap.values(); + for (ConfiguredObject configuredObject : adapters) + { + try + { + configuredObject.setDesiredState(currentState, desiredState); + } + catch(RuntimeException e) + { + if (swallowException) + { + LOGGER.error("Failed to stop " + configuredObject, e); + } + else + { + throw e; + } + } + } + } + } - return super.getAttribute(name); //TODO - Implement. + @Override + public void stateChanged(ConfiguredObject object, State oldState, State newState) + { + if(newState == State.DELETED) + { + boolean childDeleted = false; + if(object instanceof AuthenticationProvider) + { + childDeleted = deleteAuthenticationProvider((AuthenticationProvider)object); + } + else if(object instanceof Port) + { + childDeleted = deletePort((Port)object); + } + else if(object instanceof VirtualHost) + { + childDeleted = deleteVirtualHost((VirtualHost)object); + } + else if(object instanceof GroupProvider) + { + childDeleted = deleteGroupProvider((GroupProvider)object); + } + else if(object instanceof KeyStore) + { + childDeleted = deleteKeyStore((KeyStore)object); + } + else if(object instanceof TrustStore) + { + childDeleted = deleteTrustStore((TrustStore)object); + } + if(childDeleted) + { + childRemoved(object); + } + } } @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException + public void childAdded(ConfiguredObject object, ConfiguredObject child) + { + // no-op + } + + @Override + public void childRemoved(ConfiguredObject object, ConfiguredObject child) { - return super.setAttribute(name, expected, desired); //TODO - Implement. + // no-op } @Override - public void groupManagerRegistered(GroupManager groupManager) + public void attributeSet(ConfiguredObject object, String attributeName, Object oldAttributeValue, Object newAttributeValue) + { + // no-op + } + + private void addPlugin(ConfiguredObject plugin) { - GroupProviderAdapter adapter = null; - synchronized (_groupManagerAdapters) + synchronized(_plugins) { - if(!_groupManagerAdapters.containsKey(groupManager)) + if (_plugins.containsKey(plugin.getId())) { - adapter = GroupProviderAdapter.createGroupProviderAdapter(this, groupManager); - _groupManagerAdapters.put(groupManager, adapter); + throw new IllegalConfigurationException("Plugin with id '" + plugin.getId() + "' is already registered!"); } + _plugins.put(plugin.getId(), plugin); } - if(adapter != null) + plugin.addChangeListener(this); + } + + + private Collection<ConfiguredObject> getPlugins() + { + synchronized(_plugins) { - childAdded(adapter); + return Collections.unmodifiableCollection(_plugins.values()); } } + public void recoverChild(ConfiguredObject object) + { + if(object instanceof AuthenticationProvider) + { + addAuthenticationProvider((AuthenticationProvider)object); + } + else if(object instanceof Port) + { + addPort((Port)object); + } + else if(object instanceof VirtualHost) + { + addVirtualHost((VirtualHost)object); + } + else if(object instanceof GroupProvider) + { + addGroupProvider((GroupProvider)object); + } + else if(object instanceof KeyStore) + { + addKeyStore((KeyStore)object); + } + else if(object instanceof TrustStore) + { + addTrustStore((TrustStore)object); + } + else if(object instanceof Plugin) + { + addPlugin(object); + } + else + { + throw new IllegalArgumentException("Attempted to recover unexpected type of configured object: " + object.getClass().getName()); + } + } + + @Override + public RootMessageLogger getRootMessageLogger() + { + return _rootMessageLogger; + } + + @Override + public SecurityManager getSecurityManager() + { + return _securityManager; + } + + @Override + public LogRecorder getLogRecorder() + { + return _logRecorder; + } + @Override - public void groupManagerUnregistered(GroupManager groupManager) + public VirtualHost findVirtualHostByName(String name) { - GroupProviderAdapter adapter; - synchronized (_groupManagerAdapters) + return _vhostAdapters.get(name); + } + + @Override + public SubjectCreator getSubjectCreator(SocketAddress localAddress) + { + InetSocketAddress inetSocketAddress = (InetSocketAddress)localAddress; + AuthenticationProvider provider = _defaultAuthenticationProvider; + Collection<Port> ports = getPorts(); + for (Port p : ports) + { + if (inetSocketAddress.getPort() == p.getPort()) + { + provider = p.getAuthenticationProvider(); + break; + } + } + return provider.getSubjectCreator(); + } + + @Override + public Collection<KeyStore> getKeyStores() + { + synchronized(_trustStores) { - adapter = _groupManagerAdapters.remove(groupManager); + return Collections.unmodifiableCollection(_keyStores.values()); } - if(adapter != null) + } + + @Override + public Collection<TrustStore> getTrustStores() + { + synchronized(_trustStores) { - childRemoved(adapter); + return Collections.unmodifiableCollection(_trustStores.values()); } } + + @Override + public VirtualHostRegistry getVirtualHostRegistry() + { + return _virtualHostRegistry; + } + + @Override + public KeyStore getDefaultKeyStore() + { + return _keyStores.get(_defaultKeyStoreId); + } + + @Override + public TrustStore getDefaultTrustStore() + { + return _trustStores.get(_defaultTrustStoreId); + } + + @Override + public TaskExecutor getTaskExecutor() + { + return super.getTaskExecutor(); + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConnectionAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConnectionAdapter.java index 5439f6a560..84f99e1f17 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConnectionAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConnectionAdapter.java @@ -38,6 +38,7 @@ import org.apache.qpid.server.model.Session; import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.Statistics; import org.apache.qpid.server.model.UUIDGenerator; +import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.protocol.AMQConnectionModel; import org.apache.qpid.server.protocol.AMQSessionModel; import org.apache.qpid.server.stats.StatisticsGatherer; @@ -50,9 +51,9 @@ final class ConnectionAdapter extends AbstractAdapter implements Connection new HashMap<AMQSessionModel, SessionAdapter>(); private final Statistics _statistics; - public ConnectionAdapter(final AMQConnectionModel conn) + public ConnectionAdapter(final AMQConnectionModel conn, TaskExecutor taskExecutor) { - super(UUIDGenerator.generateRandomUUID()); + super(UUIDGenerator.generateRandomUUID(), taskExecutor); _connection = conn; _statistics = new ConnectionStatisticsAdapter(conn); } @@ -74,7 +75,7 @@ final class ConnectionAdapter extends AbstractAdapter implements Connection { if(!_sessionAdapters.containsKey(session)) { - _sessionAdapters.put(session, new SessionAdapter(session)); + _sessionAdapters.put(session, new SessionAdapter(session, getTaskExecutor())); } } return new ArrayList<Session>(_sessionAdapters.values()); @@ -199,52 +200,6 @@ final class ConnectionAdapter extends AbstractAdapter implements Connection } @Override - public Object setAttribute(String name, Object expected, Object desired) throws IllegalStateException, AccessControlException, IllegalArgumentException - { - if(name.equals(CLIENT_ID)) - { - - } - else if(name.equals(CLIENT_VERSION)) - { - - } - else if(name.equals(INCOMING)) - { - - } - else if(name.equals(LOCAL_ADDRESS)) - { - - } - else if(name.equals(PRINCIPAL)) - { - - } - else if(name.equals(PROPERTIES)) - { - - } - else if(name.equals(REMOTE_ADDRESS)) - { - - } - else if(name.equals(REMOTE_PROCESS_NAME)) - { - - } - else if(name.equals(REMOTE_PROCESS_PID)) - { - - } - else if(name.equals(SESSION_COUNT_LIMIT)) - { - - } - return super.setAttribute(name, expected, desired); - } - - @Override public Collection<String> getAttributeNames() { final HashSet<String> attrNames = new HashSet<String>(super.getAttributeNames()); @@ -270,7 +225,8 @@ final class ConnectionAdapter extends AbstractAdapter implements Connection } } - public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + @Override + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { if(childClass == Session.class) { @@ -310,4 +266,11 @@ final class ConnectionAdapter extends AbstractAdapter implements Connection return super.getStatistic(name); } } + + @Override + protected boolean setState(State currentState, State desiredState) + { + // TODO: add state management + return false; + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConsumerAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConsumerAdapter.java index 031d518670..e6d3fab2f8 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConsumerAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ConsumerAdapter.java @@ -45,7 +45,7 @@ public class ConsumerAdapter extends AbstractAdapter implements Consumer queueAdapter.getName(), subscription.getSessionModel().getConnectionModel().getRemoteAddressString(), String.valueOf(subscription.getSessionModel().getChannelId()), - subscription.getConsumerName())); + subscription.getConsumerName()), queueAdapter.getTaskExecutor()); _subscription = subscription; _queue = queueAdapter; _statistics = new ConsumerStatistics(); @@ -108,13 +108,6 @@ public class ConsumerAdapter extends AbstractAdapter implements Consumer } @Override - public Object setAttribute(final String name, final Object expected, final Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - return super.setAttribute(name, expected, desired); //TODO - } - - @Override public Object getAttribute(final String name) { if(ID.equals(name)) @@ -222,4 +215,11 @@ public class ConsumerAdapter extends AbstractAdapter implements Consumer return null; // TODO - Implement } } + + @Override + protected boolean setState(State currentState, State desiredState) + { + // TODO : Add state management + return false; + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ExchangeAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ExchangeAdapter.java index 266fdbd1ba..5d5f3f7378 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ExchangeAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/ExchangeAdapter.java @@ -35,14 +35,13 @@ import org.apache.qpid.server.binding.Binding; import org.apache.qpid.server.exchange.ExchangeRegistry; import org.apache.qpid.server.model.ConfiguredObject; import org.apache.qpid.server.model.Exchange; -import org.apache.qpid.server.model.IllegalStateTransitionException; import org.apache.qpid.server.model.LifetimePolicy; import org.apache.qpid.server.model.Publisher; import org.apache.qpid.server.model.Queue; import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.Statistics; -import org.apache.qpid.server.plugin.ExchangeType; import org.apache.qpid.server.queue.AMQQueue; +import org.apache.qpid.server.util.MapValueConverter; import org.apache.qpid.server.virtualhost.VirtualHost; final class ExchangeAdapter extends AbstractAdapter implements Exchange, org.apache.qpid.server.exchange.Exchange.BindingListener @@ -57,7 +56,7 @@ final class ExchangeAdapter extends AbstractAdapter implements Exchange, org.apa public ExchangeAdapter(final VirtualHostAdapter virtualHostAdapter, final org.apache.qpid.server.exchange.Exchange exchange) { - super(exchange.getId()); + super(exchange.getId(), virtualHostAdapter.getTaskExecutor()); _statistics = new ExchangeStatistics(); _vhost = virtualHostAdapter; _exchange = exchange; @@ -113,8 +112,8 @@ final class ExchangeAdapter extends AbstractAdapter implements Exchange, org.apa throws AccessControlException, IllegalStateException { attributes = new HashMap<String, Object>(attributes); - String bindingKey = getStringAttribute(org.apache.qpid.server.model.Binding.NAME, attributes, ""); - Map<String, Object> bindingArgs = getMapAttribute(org.apache.qpid.server.model.Binding.ARGUMENTS, attributes, Collections.EMPTY_MAP); + String bindingKey = MapValueConverter.getStringAttribute(org.apache.qpid.server.model.Binding.NAME, attributes, ""); + Map<String, Object> bindingArgs = MapValueConverter.getMapAttribute(org.apache.qpid.server.model.Binding.ARGUMENTS, attributes, Collections.<String,Object>emptyMap()); attributes.remove(org.apache.qpid.server.model.Binding.NAME); attributes.remove(org.apache.qpid.server.model.Binding.ARGUMENTS); @@ -257,7 +256,7 @@ final class ExchangeAdapter extends AbstractAdapter implements Exchange, org.apa } @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { if(childClass == org.apache.qpid.server.model.Binding.class) { @@ -369,28 +368,20 @@ final class ExchangeAdapter extends AbstractAdapter implements Exchange, org.apa } @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - return super.setAttribute(name, expected, desired); //TODO - Implement - } - - @Override public Collection<String> getAttributeNames() { return AVAILABLE_ATTRIBUTES; } @Override - public State setDesiredState(State currentState, State desiredState) throws IllegalStateTransitionException, - AccessControlException + protected boolean setState(State currentState, State desiredState) { if (desiredState == State.DELETED) { delete(); - return State.DELETED; + return true; } - return super.setDesiredState(currentState, desiredState); + return false; } private class ExchangeStatistics implements Statistics diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/GroupProviderAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/GroupProviderAdapter.java index adc98919d5..0fa834bc28 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/GroupProviderAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/GroupProviderAdapter.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Set; +import java.util.UUID; import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.ConfiguredObject; @@ -37,33 +38,27 @@ import org.apache.qpid.server.model.LifetimePolicy; import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.Statistics; import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.security.access.Operation; import org.apache.qpid.server.security.group.GroupManager; +import org.apache.qpid.server.security.SecurityManager; public class GroupProviderAdapter extends AbstractAdapter implements GroupProvider { private final GroupManager _groupManager; - - protected GroupProviderAdapter(GroupManager groupManager) + private final Broker _broker; + public GroupProviderAdapter(UUID id, GroupManager groupManager, Broker broker) { - super(UUIDGenerator.generateRandomUUID()); + super(id, broker.getTaskExecutor()); if (groupManager == null) { throw new IllegalArgumentException("GroupManager must not be null"); } _groupManager = groupManager; - } - - public static GroupProviderAdapter createGroupProviderAdapter( - BrokerAdapter brokerAdapter, GroupManager groupManager) - { - final GroupProviderAdapter groupProviderAdapter = new GroupProviderAdapter( - groupManager); - groupProviderAdapter.addParent(Broker.class, brokerAdapter); - return groupProviderAdapter; + _broker = broker; + addParent(Broker.class, broker); } @Override @@ -180,7 +175,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements } @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { if (childClass == Group.class) @@ -190,7 +185,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements if (getSecurityManager().authoriseGroupOperation(Operation.CREATE, groupName)) { _groupManager.createGroup(groupName); - return (C) new GroupAdapter(groupName); + return (C) new GroupAdapter(groupName, getTaskExecutor()); } else { @@ -214,7 +209,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements Collection<Group> principals = new ArrayList<Group>(groups.size()); for (Principal group : groups) { - principals.add(new GroupAdapter(group.getName())); + principals.add(new GroupAdapter(group.getName(), getTaskExecutor())); } return (Collection<C>) Collections .unmodifiableCollection(principals); @@ -225,19 +220,18 @@ public class GroupProviderAdapter extends AbstractAdapter implements } } - private org.apache.qpid.server.security.SecurityManager getSecurityManager() + private SecurityManager getSecurityManager() { - return ApplicationRegistry.getInstance().getSecurityManager(); + return _broker.getSecurityManager(); } private class GroupAdapter extends AbstractAdapter implements Group { private final String _group; - public GroupAdapter(String group) + public GroupAdapter(String group, TaskExecutor taskExecutor) { - super(UUIDGenerator.generateGroupUUID( - GroupProviderAdapter.this.getName(), group)); + super(UUIDGenerator.generateGroupUUID(GroupProviderAdapter.this.getName(), group), taskExecutor); _group = group; } @@ -319,7 +313,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements Collection<GroupMember> members = new ArrayList<GroupMember>(); for (Principal principal : usersInGroup) { - members.add(new GroupMemberAdapter(principal.getName())); + members.add(new GroupMemberAdapter(principal.getName(), getTaskExecutor())); } return (Collection<C>) Collections .unmodifiableCollection(members); @@ -332,7 +326,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements } @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { @@ -343,7 +337,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements if (getSecurityManager().authoriseGroupOperation(Operation.UPDATE, _group)) { _groupManager.addUserToGroup(memberName, _group); - return (C) new GroupMemberAdapter(memberName); + return (C) new GroupMemberAdapter(memberName, getTaskExecutor()); } else { @@ -378,15 +372,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements } @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, - IllegalArgumentException - { - return super.setAttribute(name, expected, desired); - } - - @Override - public State setDesiredState(State currentState, State desiredState) + protected boolean setState(State currentState, State desiredState) throws IllegalStateTransitionException, AccessControlException { if (desiredState == State.DELETED) @@ -394,16 +380,15 @@ public class GroupProviderAdapter extends AbstractAdapter implements if (getSecurityManager().authoriseGroupOperation(Operation.DELETE, _group)) { _groupManager.removeGroup(_group); - return State.DELETED; + return true; } else { - throw new AccessControlException("Do not have permission" + - " to delete group"); + throw new AccessControlException("Do not have permission to delete group"); } } - return super.setDesiredState(currentState, desiredState); + return false; } private class GroupMemberAdapter extends AbstractAdapter implements @@ -411,12 +396,9 @@ public class GroupProviderAdapter extends AbstractAdapter implements { private String _memberName; - public GroupMemberAdapter(String memberName) + public GroupMemberAdapter(String memberName, TaskExecutor taskExecutor) { - super(UUIDGenerator - .generateGroupMemberUUID( - GroupProviderAdapter.this.getName(), _group, - memberName)); + super(UUIDGenerator.generateGroupMemberUUID(GroupProviderAdapter.this.getName(), _group, memberName), taskExecutor); _memberName = memberName; } @@ -522,7 +504,7 @@ public class GroupProviderAdapter extends AbstractAdapter implements } @Override - public State setDesiredState(State currentState, State desiredState) + protected boolean setState(State currentState, State desiredState) throws IllegalStateTransitionException, AccessControlException { @@ -531,18 +513,38 @@ public class GroupProviderAdapter extends AbstractAdapter implements if (getSecurityManager().authoriseGroupOperation(Operation.UPDATE, _group)) { _groupManager.removeUserFromGroup(_memberName, _group); - return State.DELETED; + return true; } else { - throw new AccessControlException("Do not have permission" + - " to remove group member"); + throw new AccessControlException("Do not have permission to remove group member"); } } - - return super.setDesiredState(currentState, desiredState); + return false; } } } + + @Override + protected boolean setState(State currentState, State desiredState) + { + if (desiredState == State.ACTIVE) + { + return true; + } + else if (desiredState == State.STOPPED) + { + return true; + } + // TODO: DELETE state is ignored for now + // in case if we need to delete group provider, then we need AuthenticationProvider to be a change listener of it + // in order to remove deleted group provider from its group provider list + return false; + } + + public Set<Principal> getGroupPrincipalsForUser(String username) + { + return _groupManager.getGroupPrincipalsForUser(username); + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/HTTPPortAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/HTTPPortAdapter.java deleted file mode 100644 index 823d27160b..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/HTTPPortAdapter.java +++ /dev/null @@ -1,273 +0,0 @@ -/* - * - * 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.model.adapter; - -import java.security.AccessControlException; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import org.apache.qpid.server.model.ConfiguredObject; -import org.apache.qpid.server.model.Connection; -import org.apache.qpid.server.model.LifetimePolicy; -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.Statistics; -import org.apache.qpid.server.model.Transport; -import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.model.VirtualHostAlias; - -public class HTTPPortAdapter extends AbstractAdapter implements Port -{ - private final BrokerAdapter _broker; - private final int _port; - private final Protocol _protocol; - private final Transport _transport; - - public HTTPPortAdapter(BrokerAdapter brokerAdapter, int port) - { - this(brokerAdapter, port, Protocol.HTTP, Transport.TCP); - } - - public HTTPPortAdapter(BrokerAdapter brokerAdapter, int port, Protocol protocol, Transport transport) - { - super(UUIDGenerator.generateRandomUUID()); - _broker = brokerAdapter; - _port = port; - _protocol = protocol; - _transport = transport; - } - - @Override - public String getBindingAddress() - { - return "0.0.0.0"; - } - - @Override - public int getPort() - { - return _port; - } - - @Override - public Collection<Transport> getTransports() - { - return Collections.singleton(_transport); - } - - @Override - public void addTransport(Transport transport) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public Transport removeTransport(Transport transport) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public Collection<Protocol> getProtocols() - { - return Collections.singleton(_protocol); - } - - @Override - public void addProtocol(Protocol protocol) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public Protocol removeProtocol(Protocol protocol) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public Collection<VirtualHostAlias> getVirtualHostBindings() - { - return Collections.emptySet(); - } - - @Override - public Collection<Connection> getConnections() - { - return Collections.emptySet(); // TODO - Implement - } - - @Override - public String getName() - { - return getBindingAddress() + ":" + getPort(); // TODO - Implement - } - - @Override - public String setName(String currentName, String desiredName) throws IllegalStateException, AccessControlException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public State getActualState() - { - return State.ACTIVE; - } - - @Override - public boolean isDurable() - { - return false; // TODO - Implement - } - - @Override - public void setDurable(boolean durable) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); - } - - @Override - public LifetimePolicy getLifetimePolicy() - { - return LifetimePolicy.PERMANENT; - } - - @Override - public LifetimePolicy setLifetimePolicy(LifetimePolicy expected, LifetimePolicy desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public long getTimeToLive() - { - return 0; // TODO - Implement - } - - @Override - public long setTimeToLive(long expected, long desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - throw new IllegalStateException(); // TODO - Implement - } - - @Override - public Statistics getStatistics() - { - return NoStatistics.getInstance(); - } - - @Override - public <C extends ConfiguredObject> Collection<C> getChildren(Class<C> clazz) - { - if(clazz == Connection.class) - { - return (Collection<C>) getConnections(); - } - else - { - return Collections.emptySet(); - } - } - - @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) - { - throw new UnsupportedOperationException(); - } - - @Override - public Object getAttribute(String name) - { - if(ID.equals(name)) - { - return getId(); - } - else if(NAME.equals(name)) - { - return getName(); - } - else if(STATE.equals(name)) - { - return getActualState(); - } - else if(DURABLE.equals(name)) - { - return isDurable(); - } - else if(LIFETIME_POLICY.equals(name)) - { - return getLifetimePolicy(); - } - else if(TIME_TO_LIVE.equals(name)) - { - return getTimeToLive(); - } - else if(CREATED.equals(name)) - { - - } - else if(UPDATED.equals(name)) - { - - } - else if(BINDING_ADDRESS.equals(name)) - { - return getBindingAddress(); - } - else if(PORT.equals(name)) - { - return getPort(); - } - else if(PROTOCOLS.equals(name)) - { - return getProtocols(); - } - else if(TRANSPORTS.equals(name)) - { - return getTransports(); - } - - return super.getAttribute(name); //TODO - Implement - } - - @Override - public Collection<String> getAttributeNames() - { - return AVAILABLE_ATTRIBUTES; - } - - @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - return super.setAttribute(name, expected, desired); //TODO - Implement - } -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/KeyStoreAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/KeyStoreAdapter.java new file mode 100644 index 0000000000..113d895e62 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/KeyStoreAdapter.java @@ -0,0 +1,48 @@ +/* + * + * 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.model.adapter; + +import java.util.Collection; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.KeyStore; + +public class KeyStoreAdapter extends AbstractKeyStoreAdapter implements KeyStore +{ + + public KeyStoreAdapter(UUID id, Broker broker, Map<String, Object> attributes) + { + super(id, broker, attributes); + if (attributes.get(CERTIFICATE_ALIAS) != null) + { + changeAttribute(CERTIFICATE_ALIAS, null, attributes.get(CERTIFICATE_ALIAS)); + } + } + + @Override + public Collection<String> getAttributeNames() + { + return AVAILABLE_ATTRIBUTES; + } + +} 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 7653fcc9b9..cb166d220e 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 @@ -21,7 +21,16 @@ package org.apache.qpid.server.model.adapter; +import java.security.AccessControlException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.ConfiguredObject; import org.apache.qpid.server.model.Connection; import org.apache.qpid.server.model.LifetimePolicy; @@ -30,119 +39,82 @@ import org.apache.qpid.server.model.Protocol; import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.Statistics; import org.apache.qpid.server.model.Transport; -import org.apache.qpid.server.model.UUIDGenerator; import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.model.VirtualHostAlias; -import org.apache.qpid.server.protocol.AmqpProtocolVersion; -import org.apache.qpid.server.transport.QpidAcceptor; - -import java.net.InetSocketAddress; -import java.security.AccessControlException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import org.apache.qpid.server.configuration.updater.TaskExecutor; public class PortAdapter extends AbstractAdapter implements Port { - private final BrokerAdapter _broker; - private final QpidAcceptor _acceptor; - private final InetSocketAddress _address; - private final Collection<Protocol> _protocols; - - public PortAdapter(BrokerAdapter brokerAdapter, QpidAcceptor acceptor, InetSocketAddress address) - { - super(UUIDGenerator.generateRandomUUID()); - _broker = brokerAdapter; - _acceptor = acceptor; - _address = address; - List<Protocol> protocols = new ArrayList<Protocol>(); + private final Broker _broker; + private AuthenticationProvider _authenticationProvider; - for(AmqpProtocolVersion pv : _acceptor.getSupported()) - { - switch(pv) - { - case v0_8: - protocols.add(Protocol.AMQP_0_8); - break; - case v0_9: - protocols.add(Protocol.AMQP_0_9); - break; - case v0_9_1: - protocols.add(Protocol.AMQP_0_9_1); - break; - case v0_10: - protocols.add(Protocol.AMQP_0_10); - break; - case v1_0_0: - protocols.add(Protocol.AMQP_1_0); - break; - } - } + /* + * TODO register PortAceptor as a listener. For supporting multiple + * protocols on the same port we need to introduce a special entity like + * PortAceptor which will be responsible for port binding/unbinding + */ + public PortAdapter(UUID id, Broker broker, Map<String, Object> attributes, Map<String, Object> defaults, TaskExecutor taskExecutor) + { + super(id, defaults, attributes, taskExecutor); + _broker = broker; - _protocols = Collections.unmodifiableCollection(protocols); + addParent(Broker.class, broker); } @Override public String getBindingAddress() { - return _address.getHostName(); + return (String)getAttribute(BINDING_ADDRESS); } @Override public int getPort() { - return _address.getPort(); + return (Integer)getAttribute(PORT); } + @SuppressWarnings("unchecked") @Override public Collection<Transport> getTransports() { - switch (_acceptor.getTransport()) - { - case TCP: - return Collections.singleton(Transport.TCP); - case SSL: - return Collections.singleton(Transport.SSL); - } - - return null; // TODO - Implement + return (Collection<Transport>)getAttribute(TRANSPORTS); } @Override public void addTransport(Transport transport) throws IllegalStateException, AccessControlException, IllegalArgumentException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } @Override public Transport removeTransport(Transport transport) throws IllegalStateException, AccessControlException, IllegalArgumentException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } + @SuppressWarnings("unchecked") @Override public Collection<Protocol> getProtocols() { - return _protocols; + return (Collection<Protocol>)getAttribute(PROTOCOLS); } @Override public void addProtocol(Protocol protocol) throws IllegalStateException, AccessControlException, IllegalArgumentException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } @Override public Protocol removeProtocol(Protocol protocol) throws IllegalStateException, AccessControlException, IllegalArgumentException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } @Override @@ -165,19 +137,19 @@ public class PortAdapter extends AbstractAdapter implements Port @Override public Collection<Connection> getConnections() { - return null; // TODO - Implement + return null; } @Override public String getName() { - return getBindingAddress() + ":" + getPort(); // TODO - Implement + return (String)getAttribute(NAME); } @Override public String setName(String currentName, String desiredName) throws IllegalStateException, AccessControlException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } @Override @@ -189,7 +161,7 @@ public class PortAdapter extends AbstractAdapter implements Port @Override public boolean isDurable() { - return false; // TODO - Implement + return false; } @Override @@ -209,20 +181,20 @@ public class PortAdapter extends AbstractAdapter implements Port public LifetimePolicy setLifetimePolicy(LifetimePolicy expected, LifetimePolicy desired) throws IllegalStateException, AccessControlException, IllegalArgumentException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } @Override public long getTimeToLive() { - return 0; // TODO - Implement + return 0; } @Override public long setTimeToLive(long expected, long desired) throws IllegalStateException, AccessControlException, IllegalArgumentException { - throw new IllegalStateException(); // TODO - Implement + throw new IllegalStateException(); } @Override @@ -257,10 +229,6 @@ public class PortAdapter extends AbstractAdapter implements Port { return getId(); } - else if(NAME.equals(name)) - { - return getName(); - } else if(STATE.equals(name)) { return getActualState(); @@ -285,36 +253,54 @@ public class PortAdapter extends AbstractAdapter implements Port { } - else if(BINDING_ADDRESS.equals(name)) - { - return getBindingAddress(); - } - else if(PORT.equals(name)) + return super.getAttribute(name); + } + + @Override + public Collection<String> getAttributeNames() + { + return AVAILABLE_ATTRIBUTES; + } + + @Override + public boolean setState(State currentState, State desiredState) + { + if (desiredState == State.DELETED) { - return getPort(); + return true; } - else if(PROTOCOLS.equals(name)) + else if (desiredState == State.ACTIVE) { - return getProtocols(); + onActivate(); + return true; } - else if(TRANSPORTS.equals(name)) + else if (desiredState == State.STOPPED) { - return getTransports(); + onStop(); + return true; } + return false; + } - return super.getAttribute(name); //TODO - Implement + protected void onActivate() + { + // no-op: expected to be overridden by subclass } - @Override - public Collection<String> getAttributeNames() + protected void onStop() { - return AVAILABLE_ATTRIBUTES; + // no-op: expected to be overridden by subclass } @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException + public AuthenticationProvider getAuthenticationProvider() + { + return _authenticationProvider; + } + + public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) { - return super.setAttribute(name, expected, desired); //TODO - Implement + _authenticationProvider = authenticationProvider; } + } 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 new file mode 100644 index 0000000000..a1f72c8119 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/PortFactory.java @@ -0,0 +1,215 @@ +/* + * + * 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.model.adapter; + +import java.util.Collection; +import java.util.Collections; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.apache.qpid.server.configuration.BrokerProperties; +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.Protocol.ProtocolType; +import org.apache.qpid.server.model.Transport; +import org.apache.qpid.server.util.MapValueConverter; + +public class PortFactory +{ + public static final int DEFAULT_AMQP_SEND_BUFFER_SIZE = 262144; + public static final int DEFAULT_AMQP_RECEIVE_BUFFER_SIZE = 262144; + public static final boolean DEFAULT_AMQP_NEED_CLIENT_AUTH = false; + public static final boolean DEFAULT_AMQP_WANT_CLIENT_AUTH = false; + public static final boolean DEFAULT_AMQP_TCP_NO_DELAY = true; + public static final String DEFAULT_AMQP_BINDING = "*"; + public static final Transport DEFAULT_TRANSPORT = Transport.TCP; + + private final Collection<Protocol> _defaultProtocols; + + public PortFactory() + { + Set<Protocol> defaultProtocols = EnumSet.of(Protocol.AMQP_0_8, Protocol.AMQP_0_9, Protocol.AMQP_0_9_1, + Protocol.AMQP_0_10, Protocol.AMQP_1_0); + String excludedProtocols = System.getProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_EXCLUDES); + if (excludedProtocols != null) + { + String[] excludes = excludedProtocols.split(","); + for (String exclude : excludes) + { + Protocol protocol = Protocol.valueOf(exclude); + defaultProtocols.remove(protocol); + } + } + String includedProtocols = System.getProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_INCLUDES); + if (includedProtocols != null) + { + String[] includes = includedProtocols.split(","); + for (String include : includes) + { + Protocol protocol = Protocol.valueOf(include); + defaultProtocols.add(protocol); + } + } + _defaultProtocols = Collections.unmodifiableCollection(defaultProtocols); + } + + public Port createPort(UUID id, Broker broker, Map<String, Object> objectAttributes) + { + Map<String, Object> attributes = retrieveAttributes(objectAttributes); + + final Port port; + Map<String, Object> defaults = new HashMap<String, Object>(); + defaults.put(Port.TRANSPORTS, Collections.singleton(DEFAULT_TRANSPORT)); + Object portValue = attributes.get(Port.PORT); + if (portValue == null) + { + throw new IllegalConfigurationException("Port attribute is not specified for port: " + attributes); + } + if (isAmqpProtocol(attributes)) + { + Object binding = attributes.get(Port.BINDING_ADDRESS); + if (binding == null) + { + binding = DEFAULT_AMQP_BINDING; + defaults.put(Port.BINDING_ADDRESS, DEFAULT_AMQP_BINDING); + } + defaults.put(Port.NAME, binding + ":" + portValue); + defaults.put(Port.PROTOCOLS, _defaultProtocols); + defaults.put(Port.TCP_NO_DELAY, DEFAULT_AMQP_TCP_NO_DELAY); + defaults.put(Port.WANT_CLIENT_AUTH, DEFAULT_AMQP_WANT_CLIENT_AUTH); + defaults.put(Port.NEED_CLIENT_AUTH, DEFAULT_AMQP_NEED_CLIENT_AUTH); + defaults.put(Port.RECEIVE_BUFFER_SIZE, DEFAULT_AMQP_RECEIVE_BUFFER_SIZE); + defaults.put(Port.SEND_BUFFER_SIZE, DEFAULT_AMQP_SEND_BUFFER_SIZE); + port = new AmqpPortAdapter(id, broker, attributes, defaults, broker.getTaskExecutor()); + } + else + { + @SuppressWarnings("unchecked") + Collection<Protocol> protocols = (Collection<Protocol>)attributes.get(Port.PROTOCOLS); + if (protocols.size() > 1) + { + throw new IllegalConfigurationException("Only one protocol can be used on non AMQP port"); + } + Protocol protocol = protocols.iterator().next(); + defaults.put(Port.NAME, portValue + "-" + protocol.name()); + port = new PortAdapter(id, broker, attributes, defaults, broker.getTaskExecutor()); + } + return port; + } + + private Map<String, Object> retrieveAttributes(Map<String, Object> objectAttributes) + { + Map<String, Object> attributes = new HashMap<String, Object>(objectAttributes); + + if (objectAttributes.containsKey(Port.PROTOCOLS)) + { + final Set<Protocol> protocolSet = MapValueConverter.getEnumSetAttribute(Port.PROTOCOLS, objectAttributes, Protocol.class); + attributes.put(Port.PROTOCOLS, protocolSet); + } + + if (objectAttributes.containsKey(Port.TRANSPORTS)) + { + final Set<Transport> transportSet = MapValueConverter.getEnumSetAttribute(Port.TRANSPORTS, objectAttributes, + Transport.class); + attributes.put(Port.TRANSPORTS, transportSet); + } + + if (objectAttributes.containsKey(Port.PORT)) + { + Integer port = MapValueConverter.getIntegerAttribute(Port.PORT, objectAttributes); + attributes.put(Port.PORT, port); + } + + if (objectAttributes.containsKey(Port.TCP_NO_DELAY)) + { + boolean tcpNoDelay = MapValueConverter.getBooleanAttribute(Port.TCP_NO_DELAY, objectAttributes); + attributes.put(Port.TCP_NO_DELAY, tcpNoDelay); + } + + if (objectAttributes.containsKey(Port.RECEIVE_BUFFER_SIZE)) + { + int receiveBufferSize = MapValueConverter.getIntegerAttribute(Port.RECEIVE_BUFFER_SIZE, objectAttributes); + attributes.put(Port.RECEIVE_BUFFER_SIZE, receiveBufferSize); + } + + if (objectAttributes.containsKey(Port.SEND_BUFFER_SIZE)) + { + int sendBufferSize = MapValueConverter.getIntegerAttribute(Port.SEND_BUFFER_SIZE, objectAttributes); + attributes.put(Port.SEND_BUFFER_SIZE, sendBufferSize); + } + + if (objectAttributes.containsKey(Port.NEED_CLIENT_AUTH)) + { + boolean needClientAuth = MapValueConverter.getBooleanAttribute(Port.NEED_CLIENT_AUTH, objectAttributes); + attributes.put(Port.NEED_CLIENT_AUTH, needClientAuth); + } + + if (objectAttributes.containsKey(Port.WANT_CLIENT_AUTH)) + { + boolean wantClientAuth = MapValueConverter.getBooleanAttribute(Port.WANT_CLIENT_AUTH, objectAttributes); + attributes.put(Port.WANT_CLIENT_AUTH, wantClientAuth); + } + + if (objectAttributes.containsKey(Port.BINDING_ADDRESS)) + { + String binding = MapValueConverter.getStringAttribute(Port.BINDING_ADDRESS, objectAttributes); + attributes.put(Port.BINDING_ADDRESS, binding); + } + return attributes; + } + + private boolean isAmqpProtocol(Map<String, Object> portAttributes) + { + @SuppressWarnings("unchecked") + Set<Protocol> protocols = (Set<Protocol>) portAttributes.get(Port.PROTOCOLS); + if (protocols == null || protocols.isEmpty()) + { + // defaulting to AMQP if protocol is not specified + return true; + } + + Set<ProtocolType> protocolTypes = new HashSet<ProtocolType>(); + for (Protocol protocolObject : protocols) + { + protocolTypes.add(protocolObject.getProtocolType()); + } + + if (protocolTypes.size() > 1) + { + throw new IllegalConfigurationException("Found different protocol types '" + protocolTypes + + "' on port configuration: " + portAttributes); + } + + return protocolTypes.contains(ProtocolType.AMQP); + } + + public Collection<Protocol> getDefaultProtocols() + { + return _defaultProtocols; + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/QueueAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/QueueAdapter.java index b907d4177a..f3ddf32e5a 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/QueueAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/QueueAdapter.java @@ -43,6 +43,7 @@ import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.Statistics; import org.apache.qpid.server.queue.*; import org.apache.qpid.server.subscription.Subscription; +import org.apache.qpid.server.util.MapValueConverter; final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.SubscriptionRegistrationListener, AMQQueue.NotificationListener { @@ -79,7 +80,7 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs public QueueAdapter(final VirtualHostAdapter virtualHostAdapter, final AMQQueue queue) { - super(queue.getId()); + super(queue.getId(), virtualHostAdapter.getTaskExecutor()); _vhost = virtualHostAdapter; addParent(org.apache.qpid.server.model.VirtualHost.class, virtualHostAdapter); @@ -206,47 +207,47 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs } @Override - public Object setAttribute(String name, Object expected, Object desired) throws IllegalStateException, AccessControlException, IllegalArgumentException + public boolean changeAttribute(String name, Object expected, Object desired) throws IllegalStateException, AccessControlException, IllegalArgumentException { try { if(ALERT_REPEAT_GAP.equals(name)) { _queue.setMinimumAlertRepeatGap((Long)desired); - return desired; + return true; } else if(ALERT_THRESHOLD_MESSAGE_AGE.equals(name)) { _queue.setMaximumMessageAge((Long)desired); - return desired; + return true; } else if(ALERT_THRESHOLD_MESSAGE_SIZE.equals(name)) { _queue.setMaximumMessageSize((Long)desired); - return desired; + return true; } else if(ALERT_THRESHOLD_QUEUE_DEPTH_BYTES.equals(name)) { _queue.setMaximumQueueDepth((Long)desired); - return desired; + return true; } else if(ALERT_THRESHOLD_QUEUE_DEPTH_MESSAGES.equals(name)) { _queue.setMaximumMessageCount((Long)desired); - return desired; + return true; } else if(ALTERNATE_EXCHANGE.equals(name)) { // In future we may want to accept a UUID as an alternative way to identifying the exchange ExchangeAdapter alternateExchange = (ExchangeAdapter) desired; _queue.setAlternateExchange(alternateExchange == null ? null : alternateExchange.getExchange()); - return desired; + return true; } else if(EXCLUSIVE.equals(name)) { Boolean exclusiveFlag = (Boolean) desired; _queue.setExclusive(exclusiveFlag); - return desired; + return true; } else if(MESSAGE_GROUP_KEY.equals(name)) { @@ -267,7 +268,7 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs else if(MAXIMUM_DELIVERY_ATTEMPTS.equals(name)) { _queue.setMaximumDeliveryCount((Integer)desired); - return desired; + return true; } else if(NO_LOCAL.equals(name)) { @@ -280,12 +281,12 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs else if(QUEUE_FLOW_CONTROL_SIZE_BYTES.equals(name)) { _queue.setCapacity((Long)desired); - return desired; + return true; } else if(QUEUE_FLOW_RESUME_SIZE_BYTES.equals(name)) { _queue.setFlowResumeCapacity((Long)desired); - return desired; + return true; } else if(QUEUE_FLOW_STOPPED.equals(name)) { @@ -302,10 +303,10 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs else if (DESCRIPTION.equals(name)) { _queue.setDescription((String) desired); - return desired; + return true; } - return super.setAttribute(name, expected, desired); + return super.changeAttribute(name, expected, desired); } finally { @@ -496,8 +497,8 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs throws AccessControlException, IllegalStateException { attributes = new HashMap<String, Object>(attributes); - String bindingKey = getStringAttribute(org.apache.qpid.server.model.Binding.NAME, attributes, ""); - Map<String, Object> bindingArgs = getMapAttribute(org.apache.qpid.server.model.Binding.ARGUMENTS, attributes, Collections.EMPTY_MAP); + String bindingKey = MapValueConverter.getStringAttribute(org.apache.qpid.server.model.Binding.NAME, attributes, ""); + Map<String, Object> bindingArgs = MapValueConverter.getMapAttribute(org.apache.qpid.server.model.Binding.ARGUMENTS, attributes, Collections.<String,Object>emptyMap()); attributes.remove(org.apache.qpid.server.model.Binding.NAME); attributes.remove(org.apache.qpid.server.model.Binding.ARGUMENTS); @@ -509,7 +510,7 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { if(childClass == org.apache.qpid.server.model.Binding.class) { @@ -713,15 +714,15 @@ final class QueueAdapter extends AbstractAdapter implements Queue, AMQQueue.Subs } @Override - public State setDesiredState(State currentState, State desiredState) throws IllegalStateTransitionException, + protected boolean setState(State currentState, State desiredState) throws IllegalStateTransitionException, AccessControlException { if (desiredState == State.DELETED) { delete(); - return State.DELETED; + return true; } - return super.setDesiredState(currentState, desiredState); + return false; } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/SessionAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/SessionAdapter.java index d802697d67..2fffdb32f8 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/SessionAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/SessionAdapter.java @@ -34,6 +34,7 @@ import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.Statistics; import org.apache.qpid.server.model.Consumer; import org.apache.qpid.server.model.UUIDGenerator; +import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.protocol.AMQSessionModel; final class SessionAdapter extends AbstractAdapter implements Session @@ -44,9 +45,9 @@ final class SessionAdapter extends AbstractAdapter implements Session private AMQSessionModel _session; private SessionStatistics _statistics; - public SessionAdapter(final AMQSessionModel session) + public SessionAdapter(final AMQSessionModel session, TaskExecutor taskExecutor) { - super(UUIDGenerator.generateRandomUUID()); + super(UUIDGenerator.generateRandomUUID(), taskExecutor); _session = session; _statistics = new SessionStatistics(); } @@ -141,13 +142,6 @@ final class SessionAdapter extends AbstractAdapter implements Session return super.getAttribute(name); //TODO - Implement } - @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - return super.setAttribute(name, expected, desired); //TODO - Implement - } - public Statistics getStatistics() { return _statistics; @@ -237,4 +231,11 @@ final class SessionAdapter extends AbstractAdapter implements Session return null; // TODO - Implement } } + + @Override + protected boolean setState(State currentState, State desiredState) + { + // TODO : add state management + return false; + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/TrustStoreAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/TrustStoreAdapter.java new file mode 100644 index 0000000000..bdffe605ec --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/TrustStoreAdapter.java @@ -0,0 +1,43 @@ +/* + * + * 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.model.adapter; + +import java.util.Collection; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.TrustStore; + +public class TrustStoreAdapter extends AbstractKeyStoreAdapter implements TrustStore +{ + public TrustStoreAdapter(UUID id, Broker broker, Map<String, Object> attributes) + { + super(id, broker, attributes); + } + + @Override + public Collection<String> getAttributeNames() + { + return AVAILABLE_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 e564eabcdd..3e6f73d23e 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 @@ -20,6 +20,7 @@ */ package org.apache.qpid.server.model.adapter; +import java.io.File; import java.security.AccessControlException; import java.security.Principal; import java.util.ArrayList; @@ -31,16 +32,27 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.commons.configuration.SystemConfiguration; import org.apache.qpid.AMQException; import org.apache.qpid.framing.FieldTable; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.configuration.VirtualHostConfiguration; +import org.apache.qpid.server.configuration.XmlConfigurationUtilities.MyConfiguration; import org.apache.qpid.server.connection.IConnectionRegistry; import org.apache.qpid.server.exchange.ExchangeRegistry; import org.apache.qpid.server.message.ServerMessage; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.ConfiguredObject; import org.apache.qpid.server.model.Connection; import org.apache.qpid.server.model.Exchange; import org.apache.qpid.server.model.LifetimePolicy; import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.Protocol; import org.apache.qpid.server.model.Queue; import org.apache.qpid.server.model.QueueType; import org.apache.qpid.server.model.State; @@ -48,6 +60,7 @@ import org.apache.qpid.server.model.Statistics; import org.apache.qpid.server.model.UUIDGenerator; import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.model.VirtualHostAlias; +import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.plugin.ExchangeType; import org.apache.qpid.server.protocol.AMQConnectionModel; import org.apache.qpid.server.queue.AMQQueue; @@ -56,16 +69,28 @@ import org.apache.qpid.server.queue.QueueEntry; import org.apache.qpid.server.queue.QueueRegistry; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.security.auth.AuthenticatedPrincipal; +import org.apache.qpid.server.stats.StatisticsGatherer; import org.apache.qpid.server.store.MessageStore; import org.apache.qpid.server.txn.LocalTransaction; import org.apache.qpid.server.txn.ServerTransaction; +import org.apache.qpid.server.util.MapValueConverter; +import org.apache.qpid.server.virtualhost.VirtualHostImpl; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; -final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, ExchangeRegistry.RegistryChangeListener, +public final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, ExchangeRegistry.RegistryChangeListener, QueueRegistry.RegistryChangeListener, IConnectionRegistry.RegistryChangeListener { - private final org.apache.qpid.server.virtualhost.VirtualHost _virtualHost; + @SuppressWarnings("serial") + public static final Map<String, Class<?>> ATTRIBUTE_TYPES = Collections.unmodifiableMap(new HashMap<String, Class<?>>(){{ + put(NAME, String.class); + put(STORE_PATH, String.class); + put(STORE_TYPE, String.class); + put(CONFIG_PATH, String.class); + }}); + + private org.apache.qpid.server.virtualhost.VirtualHost _virtualHost; private final Map<AMQConnectionModel, ConnectionAdapter> _connectionAdapters = new HashMap<AMQConnectionModel, ConnectionAdapter>(); @@ -75,37 +100,52 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E private final Map<org.apache.qpid.server.exchange.Exchange, ExchangeAdapter> _exchangeAdapters = new HashMap<org.apache.qpid.server.exchange.Exchange, ExchangeAdapter>(); - - private final StatisticsAdapter _statistics; - - private final BrokerAdapter _broker; - + private StatisticsAdapter _statistics; + private final Broker _broker; private final List<VirtualHostAlias> _aliases = new ArrayList<VirtualHostAlias>(); + private StatisticsGatherer _brokerStatisticsGatherer; - - VirtualHostAdapter(BrokerAdapter brokerAdapter, - final org.apache.qpid.server.virtualhost.VirtualHost virtualHost) + public VirtualHostAdapter(UUID id, Map<String, Object> attributes, Broker broker, StatisticsGatherer brokerStatisticsGatherer, TaskExecutor taskExecutor) { - super(virtualHost.getId()); - _broker = brokerAdapter; - _virtualHost = virtualHost; - _statistics = new VirtualHostStatisticsAdapter(virtualHost); - virtualHost.getQueueRegistry().addRegistryChangeListener(this); - populateQueues(); - virtualHost.getExchangeRegistry().addRegistryChangeListener(this); - populateExchanges(); - virtualHost.getConnectionRegistry().addRegistryChangeListener(this); - populateConnections(); - + super(id, null, MapValueConverter.convert(attributes, ATTRIBUTE_TYPES), taskExecutor); + validateAttributes(); + _broker = broker; + _brokerStatisticsGatherer = brokerStatisticsGatherer; + addParent(Broker.class, broker); + } + private void validateAttributes() + { + String name = getName(); + if (name == null || "".equals(name.trim())) + { + throw new IllegalConfigurationException("Virtual host name must be specified"); + } - for(Port port :_broker.getPorts()) + String configurationFile = (String) getAttribute(CONFIG_PATH); + String storePath = (String) getAttribute(STORE_PATH); + String storeType = (String) getAttribute(STORE_TYPE); + boolean invalidAttributes = false; + if (configurationFile == null) { - _aliases.add(new VirtualHostAliasAdapter(this, port)); + if (storePath == null || storeType == null) + { + invalidAttributes = true; + } + } + else + { + if (storePath != null || storeType != null) + { + invalidAttributes = true; + } + } + if (invalidAttributes) + { + throw new IllegalConfigurationException("Please specify either the 'configPath' attribute or both 'storePath' and 'storeType' attributes"); } } - private void populateExchanges() { Collection<org.apache.qpid.server.exchange.Exchange> actualExchanges = @@ -127,37 +167,22 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E private void populateQueues() { Collection<AMQQueue> actualQueues = _virtualHost.getQueueRegistry().getQueues(); - - synchronized(_queueAdapters) - { - for(AMQQueue queue : actualQueues) - { - if(!_queueAdapters.containsKey(queue)) - { - _queueAdapters.put(queue, new QueueAdapter(this,queue)); - } - } - } - } - - private void populateConnections() - { - - List<AMQConnectionModel> actualConnections = _virtualHost.getConnectionRegistry().getConnections(); - - synchronized(_connectionAdapters) + if ( actualQueues != null ) { - for(AMQConnectionModel conn : actualConnections) + synchronized(_queueAdapters) { - if(!_connectionAdapters.containsKey(conn)) + for(AMQQueue queue : actualQueues) { - _connectionAdapters.put(conn, new ConnectionAdapter(conn)); + if(!_queueAdapters.containsKey(queue)) + { + _queueAdapters.put(queue, new QueueAdapter(this, queue)); + } } } } - } + @Override public String getReplicationGroupName() { return null; //TODO @@ -199,12 +224,12 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E { attributes = new HashMap<String, Object>(attributes); - String name = getStringAttribute(Exchange.NAME, attributes, null); - State state = getEnumAttribute(State.class, Exchange.STATE, attributes, State.ACTIVE); - boolean durable = getBooleanAttribute(Exchange.DURABLE, attributes, false); - LifetimePolicy lifetime = getEnumAttribute(LifetimePolicy.class, Exchange.LIFETIME_POLICY, attributes, LifetimePolicy.PERMANENT); - String type = getStringAttribute(Exchange.TYPE, attributes, null); - long ttl = getLongAttribute(Exchange.TIME_TO_LIVE, attributes, 0l); + String name = MapValueConverter.getStringAttribute(Exchange.NAME, attributes, null); + State state = MapValueConverter.getEnumAttribute(State.class, Exchange.STATE, attributes, State.ACTIVE); + boolean durable = MapValueConverter.getBooleanAttribute(Exchange.DURABLE, attributes, false); + LifetimePolicy lifetime = MapValueConverter.getEnumAttribute(LifetimePolicy.class, Exchange.LIFETIME_POLICY, attributes, LifetimePolicy.PERMANENT); + String type = MapValueConverter.getStringAttribute(Exchange.TYPE, attributes, null); + long ttl = MapValueConverter.getLongAttribute(Exchange.TIME_TO_LIVE, attributes, 0l); attributes.remove(Exchange.NAME); attributes.remove(Exchange.STATE); @@ -267,7 +292,7 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E if (attributes.containsKey(Queue.TYPE)) { - String typeAttribute = getStringAttribute(Queue.TYPE, attributes, null); + String typeAttribute = MapValueConverter.getStringAttribute(Queue.TYPE, attributes, null); QueueType queueType = null; try { @@ -290,12 +315,12 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E throw new IllegalArgumentException("Sort key is not specified for sorted queue"); } } - String name = getStringAttribute(Queue.NAME, attributes, null); - State state = getEnumAttribute(State.class, Queue.STATE, attributes, State.ACTIVE); - boolean durable = getBooleanAttribute(Queue.DURABLE, attributes, false); - LifetimePolicy lifetime = getEnumAttribute(LifetimePolicy.class, Queue.LIFETIME_POLICY, attributes, LifetimePolicy.PERMANENT); - long ttl = getLongAttribute(Queue.TIME_TO_LIVE, attributes, 0l); - boolean exclusive= getBooleanAttribute(Queue.EXCLUSIVE, attributes, false); + String name = MapValueConverter.getStringAttribute(Queue.NAME, attributes, null); + State state = MapValueConverter.getEnumAttribute(State.class, Queue.STATE, attributes, State.ACTIVE); + boolean durable = MapValueConverter.getBooleanAttribute(Queue.DURABLE, attributes, false); + LifetimePolicy lifetime = MapValueConverter.getEnumAttribute(LifetimePolicy.class, Queue.LIFETIME_POLICY, attributes, LifetimePolicy.PERMANENT); + long ttl = MapValueConverter.getLongAttribute(Queue.TIME_TO_LIVE, attributes, 0l); + boolean exclusive= MapValueConverter.getBooleanAttribute(Queue.EXCLUSIVE, attributes, false); attributes.remove(Queue.NAME); attributes.remove(Queue.STATE); @@ -370,7 +395,7 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E public String getName() { - return _virtualHost.getName(); + return (String)getAttribute(NAME); } public String setName(final String currentName, final String desiredName) @@ -381,7 +406,28 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E public State getActualState() { - return getDesiredState(); + if (_virtualHost == null) + { + return State.INITIALISING; + } + else + { + org.apache.qpid.server.virtualhost.State implementationState = _virtualHost.getState(); + switch(implementationState) + { + case INITIALISING: + return State.INITIALISING; + case ACTIVE: + return State.ACTIVE; + case PASSIVE: + return State.QUIESCED; + case STOPPED: + return State.STOPPED; + default: + // unexpected state + return null; + } + } } public boolean isDurable() @@ -448,7 +494,7 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E } @Override - public <C extends ConfiguredObject> C createChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) + public <C extends ConfiguredObject> C addChild(Class<C> childClass, Map<String, Object> attributes, ConfiguredObject... otherParents) { if(childClass == Exchange.class) { @@ -548,7 +594,7 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E { if(!_connectionAdapters.containsKey(connection)) { - adapter = new ConnectionAdapter(connection); + adapter = new ConnectionAdapter(connection, getTaskExecutor()); _connectionAdapters.put(connection, adapter); } @@ -709,10 +755,6 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E { return getId(); } - else if(NAME.equals(name)) - { - return getName(); - } else if(STATE.equals(name)) { return State.ACTIVE; @@ -737,10 +779,19 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E { // TODO } - else if(SUPPORTED_EXCHANGE_TYPES.equals(name)) + else if (_virtualHost != null) + { + return getAttributeFromVirtualHostImplementation(name); + } + return super.getAttribute(name); + } + + private Object getAttributeFromVirtualHostImplementation(String name) + { + if(SUPPORTED_EXCHANGE_TYPES.equals(name)) { List<String> types = new ArrayList<String>(); - for(ExchangeType type : _virtualHost.getExchangeFactory().getRegisteredTypes()) + for(@SuppressWarnings("rawtypes") ExchangeType type : _virtualHost.getExchangeFactory().getRegisteredTypes()) { types.add(type.getName().asString()); } @@ -774,9 +825,9 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E { return _virtualHost.getMessageStore().getStoreType(); } - else if(STORE_CONFIGURATION.equals(name)) + else if(STORE_PATH.equals(name)) { - // TODO + return _virtualHost.getMessageStore().getStoreLocation(); } else if(STORE_TRANSACTION_IDLE_TIMEOUT_CLOSE.equals(name)) { @@ -818,13 +869,6 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E } @Override - public Object setAttribute(String name, Object expected, Object desired) - throws IllegalStateException, AccessControlException, IllegalArgumentException - { - return super.setAttribute(name, expected, desired); //TODO - Implement - } - - @Override public Collection<String> getAttributeNames() { return AVAILABLE_ATTRIBUTES; @@ -885,4 +929,111 @@ final class VirtualHostAdapter extends AbstractAdapter implements VirtualHost, E } } + + @Override + protected boolean setState(State currentState, State desiredState) + { + if (desiredState == State.ACTIVE) + { + activate(); + return true; + } + else if (desiredState == State.STOPPED) + { + if (_virtualHost != null) + { + try + { + _virtualHost.close(); + } + finally + { + _broker.getVirtualHostRegistry().unregisterVirtualHost(_virtualHost); + } + } + return true; + } + else if (desiredState == State.DELETED) + { + //TODO: add ACL check to authorize the operation + if (_virtualHost != null && _virtualHost.getState() == org.apache.qpid.server.virtualhost.State.ACTIVE) + { + setDesiredState(currentState, State.STOPPED); + } + return true; + } + return false; + } + + private void activate() + { + VirtualHostRegistry virtualHostRegistry = _broker.getVirtualHostRegistry(); + String virtualHostName = getName(); + try + { + VirtualHostConfiguration configuration = createVirtualHostConfiguration(virtualHostName); + _virtualHost = new VirtualHostImpl(_broker.getVirtualHostRegistry(), _brokerStatisticsGatherer, _broker.getSecurityManager(), configuration); + } + catch (Exception e) + { + throw new RuntimeException("Failed to create virtual host " + virtualHostName, e); + } + + virtualHostRegistry.registerVirtualHost(_virtualHost); + + _statistics = new VirtualHostStatisticsAdapter(_virtualHost); + _virtualHost.getQueueRegistry().addRegistryChangeListener(this); + populateQueues(); + _virtualHost.getExchangeRegistry().addRegistryChangeListener(this); + populateExchanges(); + _virtualHost.getConnectionRegistry().addRegistryChangeListener(this); + + synchronized(_aliases) + { + for(Port port :_broker.getPorts()) + { + if (Protocol.hasAmqpProtocol(port.getProtocols())) + { + _aliases.add(new VirtualHostAliasAdapter(this, port)); + } + } + } + } + + private VirtualHostConfiguration createVirtualHostConfiguration(String virtualHostName) throws ConfigurationException + { + VirtualHostConfiguration configuration; + String configurationFile = (String)getAttribute(CONFIG_PATH); + if (configurationFile == null) + { + final MyConfiguration basicConfiguration = new MyConfiguration(); + PropertiesConfiguration config = new PropertiesConfiguration(); + config.addProperty("store.type", (String)getAttribute(STORE_TYPE)); + config.addProperty("store.environment-path", (String)getAttribute(STORE_PATH)); + basicConfiguration.addConfiguration(config); + + CompositeConfiguration compositeConfiguration = new CompositeConfiguration(); + compositeConfiguration.addConfiguration(new SystemConfiguration()); + compositeConfiguration.addConfiguration(basicConfiguration); + configuration = new VirtualHostConfiguration(virtualHostName, compositeConfiguration , _broker); + } + else + { + configuration = new VirtualHostConfiguration(virtualHostName, new File(configurationFile) , _broker); + } + return configuration; + } + + @Override + public SecurityManager getSecurityManager() + { + return _virtualHost.getSecurityManager(); + } + + @Override + public MessageStore getMessageStore() + { + return _virtualHost.getMessageStore(); + } + } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAliasAdapter.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAliasAdapter.java index 367d1ff518..91b705b004 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAliasAdapter.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/model/adapter/VirtualHostAliasAdapter.java @@ -43,7 +43,7 @@ public class VirtualHostAliasAdapter extends AbstractAdapter implements Virtual public VirtualHostAliasAdapter(VirtualHostAdapter virtualHostAdapter, Port port) { - super(UUIDGenerator.generateVhostAliasUUID(virtualHostAdapter.getName(), port.getName())); + super(UUIDGenerator.generateVhostAliasUUID(virtualHostAdapter.getName(), port.getName()), virtualHostAdapter.getTaskExecutor()); _vhost = virtualHostAdapter; _port = port; } @@ -140,4 +140,11 @@ public class VirtualHostAliasAdapter extends AbstractAdapter implements Virtual { throw new UnsupportedOperationException(); } + + @Override + protected boolean setState(State currentState, State desiredState) + { + // TODO: state is not supported at the moment + return false; + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AccessControlFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AccessControlFactory.java index 82f57c707b..7708b90efc 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AccessControlFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AccessControlFactory.java @@ -18,10 +18,11 @@ */ package org.apache.qpid.server.plugin; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.security.AccessControl; public interface AccessControlFactory { - AccessControl createInstance(Configuration securityConfig); + AccessControl createInstance(Map<String, Object> attributes); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AuthenticationManagerFactory.java index 014b9fb28f..95e6b4feb0 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AuthenticationManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/AuthenticationManagerFactory.java @@ -18,11 +18,14 @@ */ package org.apache.qpid.server.plugin; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.security.auth.manager.AuthenticationManager; public interface AuthenticationManagerFactory { - AuthenticationManager createInstance(Configuration securityConfiguration); + public static final String ATTRIBUTE_TYPE = "authenticationProviderType"; + + AuthenticationManager createInstance(Map<String, Object> attributes); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/GroupManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/GroupManagerFactory.java index b7f1eeaf29..5d80ca24fd 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/GroupManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/GroupManagerFactory.java @@ -18,10 +18,11 @@ */ package org.apache.qpid.server.plugin; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.security.group.GroupManager; public interface GroupManagerFactory { - GroupManager createInstance(Configuration securityConfiguration); + GroupManager createInstance(Map<String, Object> attributes); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/ManagementFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/PluginFactory.java index 92414b7127..af24f62e28 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/ManagementFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/PluginFactory.java @@ -18,11 +18,15 @@ */ package org.apache.qpid.server.plugin; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.management.plugin.ManagementPlugin; +import java.util.Map; +import java.util.UUID; + import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.Plugin; -public interface ManagementFactory +public interface PluginFactory { - ManagementPlugin createInstance(ServerConfiguration configuration, Broker broker); + static final String PLUGIN_TYPE = "pluginType"; + + Plugin createInstance(UUID id, Map<String, Object> attributes, Broker broker); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/AMQProtocolEngine.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/AMQProtocolEngine.java index f77f3a764a..ee1ef2418a 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/AMQProtocolEngine.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/AMQProtocolEngine.java @@ -52,6 +52,7 @@ import org.apache.qpid.protocol.AMQMethodEvent; import org.apache.qpid.protocol.AMQMethodListener; import org.apache.qpid.protocol.ServerProtocolEngine; import org.apache.qpid.server.AMQChannel; +import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.handler.ServerMethodDispatcherImpl; import org.apache.qpid.server.logging.LogActor; import org.apache.qpid.server.logging.LogSubject; @@ -60,10 +61,10 @@ import org.apache.qpid.server.logging.actors.CurrentActor; import org.apache.qpid.server.logging.actors.ManagementActor; import org.apache.qpid.server.logging.messages.ConnectionMessages; import org.apache.qpid.server.logging.subjects.ConnectionLogSubject; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.output.ProtocolOutputConverter; import org.apache.qpid.server.output.ProtocolOutputConverterRegistry; import org.apache.qpid.server.queue.QueueEntry; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.auth.AuthenticatedPrincipal; import org.apache.qpid.server.state.AMQState; import org.apache.qpid.server.state.AMQStateManager; @@ -72,7 +73,6 @@ import org.apache.qpid.server.subscription.ClientDeliveryMethod; import org.apache.qpid.server.subscription.Subscription; import org.apache.qpid.server.subscription.SubscriptionImpl; import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.server.virtualhost.VirtualHostRegistry; import org.apache.qpid.transport.Sender; import org.apache.qpid.transport.TransportException; import org.apache.qpid.transport.network.NetworkConnection; @@ -112,7 +112,7 @@ public class AMQProtocolEngine implements ServerProtocolEngine, AMQProtocolSessi private volatile boolean _closed; // maximum number of channels this session should have - private long _maxNoOfChannels = ApplicationRegistry.getInstance().getConfiguration().getMaxChannelCount(); + private long _maxNoOfChannels; /* AMQP Version for this session */ private ProtocolVersion _protocolVersion = ProtocolVersion.getLatestSupportedVersion(); @@ -152,18 +152,21 @@ public class AMQProtocolEngine implements ServerProtocolEngine, AMQProtocolSessi private final Lock _receivedLock; private AtomicLong _lastWriteTime = new AtomicLong(System.currentTimeMillis()); + private final Broker _broker; - public AMQProtocolEngine(VirtualHostRegistry virtualHostRegistry, NetworkConnection network, final long connectionId) + public AMQProtocolEngine(Broker broker, NetworkConnection network, final long connectionId) { + _broker = broker; + _maxNoOfChannels = (Integer)broker.getAttribute(Broker.SESSION_COUNT_LIMIT); _receivedLock = new ReentrantLock(); - _stateManager = new AMQStateManager(virtualHostRegistry, this); + _stateManager = new AMQStateManager(broker, this); _codecFactory = new AMQCodecFactory(true, this); setNetworkConnection(network); _connectionID = connectionId; - _actor = new AMQPConnectionActor(this, virtualHostRegistry.getApplicationRegistry().getRootMessageLogger()); + _actor = new AMQPConnectionActor(this, _broker.getRootMessageLogger()); _logSubject = new ConnectionLogSubject(this); @@ -370,7 +373,7 @@ public class AMQProtocolEngine implements ServerProtocolEngine, AMQProtocolSessi // This sets the protocol version (and hence framing classes) for this session. setProtocolVersion(pv); - String mechanisms = ApplicationRegistry.getInstance().getSubjectCreator(getLocalAddress()).getMechanisms(); + String mechanisms = _broker.getSubjectCreator(getLocalAddress()).getMechanisms(); String locales = "en_US"; @@ -761,7 +764,7 @@ public class AMQProtocolEngine implements ServerProtocolEngine, AMQProtocolSessi if (delay > 0) { _network.setMaxWriteIdle(delay); - _network.setMaxReadIdle((int) (ApplicationRegistry.getInstance().getConfiguration().getHeartBeatTimeout() * delay)); + _network.setMaxReadIdle(BrokerProperties.DEFAULT_HEART_BEAT_TIMEOUT_FACTOR * delay); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngine.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngine.java index b0465c731e..d9e5e1c473 100755 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngine.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngine.java @@ -28,7 +28,7 @@ import java.util.Set; import org.apache.log4j.Logger; import org.apache.qpid.protocol.ServerProtocolEngine; -import org.apache.qpid.server.registry.IApplicationRegistry; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.transport.ServerConnection; import org.apache.qpid.transport.ConnectionDelegate; import org.apache.qpid.transport.Sender; @@ -42,24 +42,24 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine private Set<AmqpProtocolVersion> _supported; private String _fqdn; - private IApplicationRegistry _appRegistry; + private final Broker _broker; private NetworkConnection _network; private Sender<ByteBuffer> _sender; private final AmqpProtocolVersion _defaultSupportedReply; private volatile ServerProtocolEngine _delegate = new SelfDelegateProtocolEngine(); - public MultiVersionProtocolEngine(final IApplicationRegistry appRegistry, + public MultiVersionProtocolEngine(final Broker broker, final Set<AmqpProtocolVersion> supported, final AmqpProtocolVersion defaultSupportedReply, final long id, final NetworkConnection network) { - this(appRegistry, supported, defaultSupportedReply, id); + this(broker, supported, defaultSupportedReply, id); setNetworkConnection(network); } - public MultiVersionProtocolEngine(final IApplicationRegistry appRegistry, + public MultiVersionProtocolEngine(final Broker broker, final Set<AmqpProtocolVersion> supported, final AmqpProtocolVersion defaultSupportedReply, final long id) @@ -71,7 +71,7 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine } _id = id; - _appRegistry = appRegistry; + _broker = broker; _supported = supported; _defaultSupportedReply = defaultSupportedReply; } @@ -252,7 +252,7 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine public ServerProtocolEngine getProtocolEngine() { - return new AMQProtocolEngine(_appRegistry.getVirtualHostRegistry(), _network, _id); + return new AMQProtocolEngine(_broker, _network, _id); } }; @@ -272,7 +272,7 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine public ServerProtocolEngine getProtocolEngine() { - return new AMQProtocolEngine(_appRegistry.getVirtualHostRegistry(), _network, _id); + return new AMQProtocolEngine(_broker, _network, _id); } }; @@ -292,7 +292,7 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine public ServerProtocolEngine getProtocolEngine() { - return new AMQProtocolEngine(_appRegistry.getVirtualHostRegistry(), _network, _id); + return new AMQProtocolEngine(_broker, _network, _id); } }; @@ -313,15 +313,15 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine public ServerProtocolEngine getProtocolEngine() { - final ConnectionDelegate connDelegate = - new org.apache.qpid.server.transport.ServerConnectionDelegate(_appRegistry, _fqdn, _appRegistry.getSubjectCreator(getLocalAddress())); + final ConnectionDelegate connDelegate = new org.apache.qpid.server.transport.ServerConnectionDelegate(_broker, + _fqdn, _broker.getSubjectCreator(getLocalAddress())); ServerConnection conn = new ServerConnection(_id); conn.setConnectionDelegate(connDelegate); conn.setRemoteAddress(_network.getRemoteAddress()); conn.setLocalAddress(_network.getLocalAddress()); - return new ProtocolEngine_0_10( conn, _network, _appRegistry); + return new ProtocolEngine_0_10( conn, _network); } }; @@ -341,7 +341,7 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine public ServerProtocolEngine getProtocolEngine() { - return new ProtocolEngine_1_0_0(_network, _appRegistry,_id); + return new ProtocolEngine_1_0_0(_network, _broker, _id); } }; @@ -361,7 +361,7 @@ public class MultiVersionProtocolEngine implements ServerProtocolEngine public ServerProtocolEngine getProtocolEngine() { - return new ProtocolEngine_1_0_0_SASL(_network, _appRegistry, _id); + return new ProtocolEngine_1_0_0_SASL(_network, _broker, _id); } }; diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactory.java index 552b1c7054..9f078c8999 100755 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactory.java @@ -22,8 +22,7 @@ package org.apache.qpid.server.protocol; import org.apache.qpid.protocol.ProtocolEngineFactory; import org.apache.qpid.protocol.ServerProtocolEngine; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; +import org.apache.qpid.server.model.Broker; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; @@ -32,11 +31,12 @@ public class MultiVersionProtocolEngineFactory implements ProtocolEngineFactory { private static final AtomicLong ID_GENERATOR = new AtomicLong(0); - private final IApplicationRegistry _appRegistry; + private final Broker _broker; private final Set<AmqpProtocolVersion> _supported; private final AmqpProtocolVersion _defaultSupportedReply; - public MultiVersionProtocolEngineFactory(final Set<AmqpProtocolVersion> supportedVersions, final AmqpProtocolVersion defaultSupportedReply) + public MultiVersionProtocolEngineFactory(Broker broker, + final Set<AmqpProtocolVersion> supportedVersions, final AmqpProtocolVersion defaultSupportedReply) { if(defaultSupportedReply != null && !supportedVersions.contains(defaultSupportedReply)) { @@ -44,14 +44,14 @@ public class MultiVersionProtocolEngineFactory implements ProtocolEngineFactory + ") to an unsupported protocol version initiation is itself not supported!"); } - _appRegistry = ApplicationRegistry.getInstance(); + _broker = broker; _supported = supportedVersions; _defaultSupportedReply = defaultSupportedReply; } public ServerProtocolEngine newProtocolEngine() { - return new MultiVersionProtocolEngine(_appRegistry, _supported, _defaultSupportedReply, ID_GENERATOR.getAndIncrement()); + return new MultiVersionProtocolEngine(_broker, _supported, _defaultSupportedReply, ID_GENERATOR.getAndIncrement()); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_0_10.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_0_10.java index 7d263b517c..d5f7fe486c 100755 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_0_10.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_0_10.java @@ -22,7 +22,6 @@ package org.apache.qpid.server.protocol; import org.apache.qpid.protocol.ServerProtocolEngine; import org.apache.qpid.server.logging.messages.ConnectionMessages; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.transport.ServerConnection; import org.apache.qpid.transport.Sender; import org.apache.qpid.transport.network.Assembler; @@ -32,7 +31,7 @@ import org.apache.qpid.transport.network.NetworkConnection; import java.net.SocketAddress; import java.nio.ByteBuffer; -import java.util.UUID; + public class ProtocolEngine_0_10 extends InputHandler implements ServerProtocolEngine { @@ -42,19 +41,17 @@ public class ProtocolEngine_0_10 extends InputHandler implements ServerProtocol private long _readBytes; private long _writtenBytes; private ServerConnection _connection; - private final IApplicationRegistry _appRegistry; + private long _createTime = System.currentTimeMillis(); private long _lastReadTime; private long _lastWriteTime; public ProtocolEngine_0_10(ServerConnection conn, - NetworkConnection network, - final IApplicationRegistry appRegistry) + NetworkConnection network) { super(new Assembler(conn)); _connection = conn; - _appRegistry = appRegistry; if(network != null) { diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0.java index 4dbb98cc4a..f6b8e1e5c9 100755 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0.java @@ -22,7 +22,6 @@ package org.apache.qpid.server.protocol; import java.net.SocketAddress; import java.nio.ByteBuffer; -import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; @@ -39,10 +38,10 @@ import org.apache.qpid.amqp_1_0.transport.FrameOutputHandler; import org.apache.qpid.amqp_1_0.type.Binary; import org.apache.qpid.amqp_1_0.type.FrameBody; import org.apache.qpid.protocol.ServerProtocolEngine; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.v1_0.Connection_1_0; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.auth.manager.AuthenticationManager; +import org.apache.qpid.server.virtualhost.VirtualHost; import org.apache.qpid.transport.Sender; import org.apache.qpid.transport.network.NetworkConnection; @@ -55,7 +54,7 @@ public class ProtocolEngine_1_0_0 implements ServerProtocolEngine, FrameOutputHa private long _writtenBytes; private long _lastReadTime; private long _lastWriteTime; - private final IApplicationRegistry _appRegistry; + private final Broker _broker; private long _createTime = System.currentTimeMillis(); private ConnectionEndpoint _conn; private final long _connectionId; @@ -99,9 +98,9 @@ public class ProtocolEngine_1_0_0 implements ServerProtocolEngine, FrameOutputHa - public ProtocolEngine_1_0_0(final NetworkConnection networkDriver, final IApplicationRegistry appRegistry, long id) + public ProtocolEngine_1_0_0(final NetworkConnection networkDriver, final Broker broker, long id) { - _appRegistry = appRegistry; + _broker = broker; _connectionId = id; if(networkDriver != null) { @@ -145,12 +144,14 @@ public class ProtocolEngine_1_0_0 implements ServerProtocolEngine, FrameOutputHa _network = network; _sender = sender; - Container container = new Container(_appRegistry.getBrokerId().toString()); + Container container = new Container(_broker.getId().toString()); - _conn = new ConnectionEndpoint(container, asSaslServerProvider(_appRegistry.getSubjectCreator( + VirtualHost virtualHost = _broker.getVirtualHostRegistry().getVirtualHost((String)_broker.getAttribute(Broker.DEFAULT_VIRTUAL_HOST)); + + _conn = new ConnectionEndpoint(container, asSaslServerProvider(_broker.getSubjectCreator( getLocalAddress()))); _conn.setRemoteAddress(_network.getRemoteAddress()); - _conn.setConnectionEventListener(new Connection_1_0(_appRegistry, _conn, _connectionId)); + _conn.setConnectionEventListener(new Connection_1_0(virtualHost, _conn, _connectionId)); _conn.setFrameOutputHandler(this); _frameWriter = new FrameWriter(_conn.getDescribedTypeRegistry()); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0_SASL.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0_SASL.java index 8e671a016f..3b02ef2e5b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0_SASL.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/ProtocolEngine_1_0_0_SASL.java @@ -23,7 +23,6 @@ package org.apache.qpid.server.protocol; import java.io.PrintWriter; import java.net.SocketAddress; import java.nio.ByteBuffer; -import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import javax.security.sasl.SaslException; @@ -40,10 +39,10 @@ import org.apache.qpid.amqp_1_0.transport.FrameOutputHandler; import org.apache.qpid.amqp_1_0.type.Binary; import org.apache.qpid.amqp_1_0.type.FrameBody; import org.apache.qpid.protocol.ServerProtocolEngine; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.v1_0.Connection_1_0; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.security.SubjectCreator; +import org.apache.qpid.server.virtualhost.VirtualHost; import org.apache.qpid.transport.Sender; import org.apache.qpid.transport.network.NetworkConnection; @@ -54,7 +53,7 @@ public class ProtocolEngine_1_0_0_SASL implements ServerProtocolEngine, FrameOut private long _lastReadTime; private long _lastWriteTime; - private final IApplicationRegistry _appRegistry; + private final Broker _broker; private long _createTime = System.currentTimeMillis(); private ConnectionEndpoint _conn; private long _connectionId; @@ -113,12 +112,11 @@ public class ProtocolEngine_1_0_0_SASL implements ServerProtocolEngine, FrameOut private State _state = State.A; - public ProtocolEngine_1_0_0_SASL(final NetworkConnection networkDriver, final IApplicationRegistry appRegistry, + public ProtocolEngine_1_0_0_SASL(final NetworkConnection networkDriver, final Broker broker, long id) { _connectionId = id; - _appRegistry = appRegistry; - + _broker = broker; if(networkDriver != null) { setNetworkConnection(networkDriver, networkDriver.getSender()); @@ -161,15 +159,12 @@ public class ProtocolEngine_1_0_0_SASL implements ServerProtocolEngine, FrameOut _network = network; _sender = sender; - Container container = new Container(_appRegistry.getBrokerId().toString()); + Container container = new Container(_broker.getId().toString()); - _conn = new ConnectionEndpoint(container, asSaslServerProvider(ApplicationRegistry.getInstance() - .getSubjectCreator(getLocalAddress()))); + VirtualHost virtualHost = _broker.getVirtualHostRegistry().getVirtualHost((String)_broker.getAttribute(Broker.DEFAULT_VIRTUAL_HOST)); + _conn = new ConnectionEndpoint(container, asSaslServerProvider(_broker.getSubjectCreator(getLocalAddress()))); _conn.setRemoteAddress(getRemoteAddress()); - _conn.setConnectionEventListener(new Connection_1_0(_appRegistry, _conn, _connectionId)); - - - + _conn.setConnectionEventListener(new Connection_1_0(virtualHost, _conn, _connectionId)); _conn.setFrameOutputHandler(this); _conn.setSaslFrameOutput(this); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Connection_1_0.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Connection_1_0.java index f429d8ba9f..cf4164c244 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Connection_1_0.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Connection_1_0.java @@ -31,7 +31,6 @@ import org.apache.qpid.protocol.AMQConstant; import org.apache.qpid.server.logging.LogSubject; import org.apache.qpid.server.protocol.AMQConnectionModel; import org.apache.qpid.server.protocol.AMQSessionModel; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.stats.StatisticsCounter; import org.apache.qpid.server.virtualhost.VirtualHost; @@ -44,7 +43,6 @@ import static org.apache.qpid.server.logging.subjects.LogSubjectFormat.CONNECTIO public class Connection_1_0 implements ConnectionEventListener { - private IApplicationRegistry _appRegistry; private VirtualHost _vhost; private final ConnectionEndpoint _conn; private final long _connectionId; @@ -62,10 +60,9 @@ public class Connection_1_0 implements ConnectionEventListener - public Connection_1_0(IApplicationRegistry appRegistry, ConnectionEndpoint conn, long connectionId) + public Connection_1_0(VirtualHost virtualHost, ConnectionEndpoint conn, long connectionId) { - _appRegistry = appRegistry; - _vhost = _appRegistry.getVirtualHostRegistry().getDefaultVirtualHost(); + _vhost = virtualHost; _conn = conn; _connectionId = connectionId; _vhost.getConnectionRegistry().registerConnection(_model); @@ -74,7 +71,7 @@ public class Connection_1_0 implements ConnectionEventListener public void remoteSessionCreation(SessionEndpoint endpoint) { - Session_1_0 session = new Session_1_0(_vhost, _appRegistry, this); + Session_1_0 session = new Session_1_0(_vhost, this); _sessions.add(session); endpoint.setSessionEventListener(session); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Session_1_0.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Session_1_0.java index a019a4b78d..a0ed824c58 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Session_1_0.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/protocol/v1_0/Session_1_0.java @@ -36,7 +36,6 @@ import org.apache.qpid.amqp_1_0.type.transport.*; import org.apache.qpid.amqp_1_0.type.transport.Error; import org.apache.qpid.AMQException; import org.apache.qpid.AMQSecurityException; -import org.apache.qpid.protocol.ProtocolEngine; import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.logging.LogSubject; import org.apache.qpid.server.message.InboundMessage; @@ -45,8 +44,6 @@ import org.apache.qpid.server.protocol.AMQConnectionModel; import org.apache.qpid.server.protocol.AMQSessionModel; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.AMQQueueFactory; -import org.apache.qpid.server.registry.IApplicationRegistry; -import org.apache.qpid.server.transport.ServerConnection; import org.apache.qpid.server.txn.AutoCommitTransaction; import org.apache.qpid.server.txn.ServerTransaction; import org.apache.qpid.server.virtualhost.VirtualHost; @@ -58,7 +55,6 @@ import static org.apache.qpid.server.logging.subjects.LogSubjectFormat.CHANNEL_F public class Session_1_0 implements SessionEventListener, AMQSessionModel, LogSubject { private static final Symbol LIFETIME_POLICY = Symbol.valueOf("lifetime-policy"); - private IApplicationRegistry _appRegistry; private VirtualHost _vhost; private AutoCommitTransaction _transaction; @@ -68,9 +64,8 @@ public class Session_1_0 implements SessionEventListener, AMQSessionModel, LogSu private UUID _id = UUID.randomUUID(); - public Session_1_0(VirtualHost vhost, IApplicationRegistry appRegistry, final Connection_1_0 connection) + public Session_1_0(VirtualHost vhost, final Connection_1_0 connection) { - _appRegistry = appRegistry; _vhost = vhost; _transaction = new AutoCommitTransaction(vhost.getMessageStore()); _connection = connection; diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/AMQQueueFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/AMQQueueFactory.java index 6a1c3bd893..a65a6a8eb2 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/AMQQueueFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/AMQQueueFactory.java @@ -30,13 +30,13 @@ import org.apache.qpid.AMQSecurityException; import org.apache.qpid.exchange.ExchangeDefaults; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; +import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.configuration.QueueConfiguration; -import org.apache.qpid.server.configuration.ServerConfiguration; +import org.apache.qpid.server.exchange.DefaultExchangeFactory; import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.exchange.ExchangeFactory; import org.apache.qpid.server.exchange.ExchangeRegistry; import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.virtualhost.VirtualHost; public class AMQQueueFactory @@ -426,9 +426,7 @@ public class AMQQueueFactory */ protected static String getDeadLetterQueueName(String name) { - ServerConfiguration serverConfig = ApplicationRegistry.getInstance().getConfiguration(); - String dlQueueName = name + serverConfig.getDeadLetterQueueSuffix(); - return dlQueueName; + return name + System.getProperty(BrokerProperties.PROPERTY_DEAD_LETTER_QUEUE_SUFFIX, DEFAULT_DLQ_NAME_SUFFIX); } /** @@ -440,9 +438,7 @@ public class AMQQueueFactory */ protected static String getDeadLetterExchangeName(String name) { - ServerConfiguration serverConfig = ApplicationRegistry.getInstance().getConfiguration(); - String dlExchangeName = name + serverConfig.getDeadLetterExchangeSuffix(); - return dlExchangeName; + return name + System.getProperty(BrokerProperties.PROPERTY_DEAD_LETTER_EXCHANGE_SUFFIX, DefaultExchangeFactory.DEFAULT_DLE_NAME_SUFFIX); } private static Map<String, Object> createQueueArgumentsFromConfig(QueueConfiguration config) diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/IncomingMessage.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/IncomingMessage.java index c5a610c7b6..18affc7161 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/IncomingMessage.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/IncomingMessage.java @@ -34,7 +34,6 @@ import org.apache.qpid.server.message.EnqueableMessage; import org.apache.qpid.server.message.InboundMessage; import org.apache.qpid.server.message.MessageContentSource; import org.apache.qpid.server.message.MessageMetaData; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.StoredMessage; import java.nio.ByteBuffer; @@ -47,9 +46,6 @@ public class IncomingMessage implements Filterable, InboundMessage, EnqueableMes /** Used for debugging purposes. */ private static final Logger _logger = Logger.getLogger(IncomingMessage.class); - private static final boolean SYNCHED_CLOCKS = - ApplicationRegistry.getInstance().getConfiguration().getSynchedClocks(); - private final MessagePublishInfo _messagePublishInfo; private ContentHeaderBody _contentHeaderBody; @@ -101,33 +97,7 @@ public class IncomingMessage implements Filterable, InboundMessage, EnqueableMes public void setExpiration() { - long expiration = - ((BasicContentHeaderProperties) _contentHeaderBody.getProperties()).getExpiration(); - long timestamp = - ((BasicContentHeaderProperties) _contentHeaderBody.getProperties()).getTimestamp(); - - if (SYNCHED_CLOCKS) - { - _expiration = expiration; - } - else - { - // Update TTL to be in broker time. - if (expiration != 0L) - { - if (timestamp != 0L) - { - // todo perhaps use arrival time - long diff = (System.currentTimeMillis() - timestamp); - - if ((diff > 1000L) || (diff < 1000L)) - { - _expiration = expiration + diff; - } - } - } - } - + _expiration = ((BasicContentHeaderProperties) _contentHeaderBody.getProperties()).getExpiration(); } public MessageMetaData headersReceived(long currentTime) diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/SimpleAMQQueue.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/SimpleAMQQueue.java index beb566f622..73c2870b9b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/SimpleAMQQueue.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/SimpleAMQQueue.java @@ -52,7 +52,6 @@ import org.apache.qpid.server.logging.messages.QueueMessages; import org.apache.qpid.server.logging.subjects.QueueLogSubject; import org.apache.qpid.server.message.ServerMessage; import org.apache.qpid.server.protocol.AMQSessionModel; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.AuthorizationHolder; import org.apache.qpid.server.subscription.AssignedSubscriptionMessageGroupManager; import org.apache.qpid.server.subscription.DefinedGroupMessageGroupManager; @@ -132,23 +131,23 @@ public class SimpleAMQQueue implements AMQQueue, Subscription.StateListener, Mes private final AtomicInteger _bindingCountHigh = new AtomicInteger(); /** max allowed size(KB) of a single message */ - private long _maximumMessageSize = ApplicationRegistry.getInstance().getConfiguration().getMaximumMessageSize(); + private long _maximumMessageSize; /** max allowed number of messages on a queue. */ - private long _maximumMessageCount = ApplicationRegistry.getInstance().getConfiguration().getMaximumMessageCount(); + private long _maximumMessageCount; /** max queue depth for the queue */ - private long _maximumQueueDepth = ApplicationRegistry.getInstance().getConfiguration().getMaximumQueueDepth(); + private long _maximumQueueDepth; /** maximum message age before alerts occur */ - private long _maximumMessageAge = ApplicationRegistry.getInstance().getConfiguration().getMaximumMessageAge(); + private long _maximumMessageAge; /** the minimum interval between sending out consecutive alerts of the same type */ - private long _minimumAlertRepeatGap = ApplicationRegistry.getInstance().getConfiguration().getMinimumAlertRepeatGap(); + private long _minimumAlertRepeatGap; - private long _capacity = ApplicationRegistry.getInstance().getConfiguration().getCapacity(); + private long _capacity; - private long _flowResumeCapacity = ApplicationRegistry.getInstance().getConfiguration().getFlowResumeCapacity(); + private long _flowResumeCapacity; private final Set<NotificationCheck> _notificationChecks = EnumSet.noneOf(NotificationCheck.class); @@ -185,7 +184,7 @@ public class SimpleAMQQueue implements AMQQueue, Subscription.StateListener, Mes private AbstractConfiguration _queueConfiguration; /** the maximum delivery count for each message on this queue or 0 if maximum delivery count is not to be enforced. */ - private int _maximumDeliveryCount = ApplicationRegistry.getInstance().getConfiguration().getMaxDeliveryCount(); + private int _maximumDeliveryCount; private final MessageGroupManager _messageGroupManager; private final Collection<SubscriptionRegistrationListener> _subscriptionListeners = diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java index c32e85b293..995951a462 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/ApplicationRegistry.java @@ -20,44 +20,39 @@ */ package org.apache.qpid.server.registry; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.log4j.Logger; -import org.apache.qpid.server.logging.*; +import java.util.Collection; +import java.util.Timer; +import java.util.TimerTask; +import org.apache.log4j.Logger; import org.apache.qpid.common.Closeable; import org.apache.qpid.common.QpidProperties; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.configuration.VirtualHostConfiguration; +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.configuration.startup.DefaultRecovererProvider; +import org.apache.qpid.server.logging.CompositeStartupMessageLogger; +import org.apache.qpid.server.logging.Log4jMessageLogger; +import org.apache.qpid.server.logging.LogActor; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.logging.SystemOutMessageLogger; import org.apache.qpid.server.logging.actors.AbstractActor; import org.apache.qpid.server.logging.actors.BrokerActor; import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.logging.actors.GenericActor; import org.apache.qpid.server.logging.messages.BrokerMessages; import org.apache.qpid.server.logging.messages.VirtualHostMessages; -import org.apache.qpid.server.management.plugin.ManagementPlugin; import org.apache.qpid.server.model.Broker; -import org.apache.qpid.server.model.adapter.BrokerAdapter; -import org.apache.qpid.server.plugin.GroupManagerFactory; -import org.apache.qpid.server.plugin.ManagementFactory; -import org.apache.qpid.server.plugin.QpidServiceLoader; -import org.apache.qpid.server.security.SecurityManager; -import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.auth.manager.AuthenticationManager; -import org.apache.qpid.server.security.auth.manager.AuthenticationManagerRegistry; -import org.apache.qpid.server.security.auth.manager.IAuthenticationManagerRegistry; -import org.apache.qpid.server.security.group.GroupManager; -import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.stats.StatisticsCounter; -import org.apache.qpid.server.transport.QpidAcceptor; +import org.apache.qpid.server.stats.StatisticsGatherer; import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.server.virtualhost.VirtualHostImpl; import org.apache.qpid.server.virtualhost.VirtualHostRegistry; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.util.*; -import java.util.concurrent.atomic.AtomicReference; - /** * An abstract application registry that provides access to configuration information and handles the @@ -69,307 +64,86 @@ public class ApplicationRegistry implements IApplicationRegistry { private static final Logger _logger = Logger.getLogger(ApplicationRegistry.class); - private static AtomicReference<IApplicationRegistry> _instance = new AtomicReference<IApplicationRegistry>(null); - - private final ServerConfiguration _configuration; - - private final Map<InetSocketAddress, QpidAcceptor> _acceptors = - Collections.synchronizedMap(new HashMap<InetSocketAddress, QpidAcceptor>()); - - private IAuthenticationManagerRegistry _authenticationManagerRegistry; - - private final VirtualHostRegistry _virtualHostRegistry = new VirtualHostRegistry(this); - - private SecurityManager _securityManager; + private final VirtualHostRegistry _virtualHostRegistry = new VirtualHostRegistry(); private volatile RootMessageLogger _rootMessageLogger; - private CompositeStartupMessageLogger _startupMessageLogger; - private Broker _broker; private Timer _reportingTimer; private StatisticsCounter _messagesDelivered, _dataDelivered, _messagesReceived, _dataReceived; - private final List<PortBindingListener> _portBindingListeners = new ArrayList<PortBindingListener>(); - - private int _httpManagementPort = -1, _httpsManagementPort = -1; - private LogRecorder _logRecorder; - private List<IAuthenticationManagerRegistry.RegistryChangeListener> _authManagerChangeListeners = - new ArrayList<IAuthenticationManagerRegistry.RegistryChangeListener>(); - - private List<GroupManagerChangeListener> _groupManagerChangeListeners = - new ArrayList<GroupManagerChangeListener>(); - - private List<GroupManager> _groupManagerList = new ArrayList<GroupManager>(); - - private QpidServiceLoader<GroupManagerFactory> _groupManagerServiceLoader = new QpidServiceLoader<GroupManagerFactory>(); - - private final List<ManagementPlugin> _managmentInstanceList = new ArrayList<ManagementPlugin>(); - - public Map<InetSocketAddress, QpidAcceptor> getAcceptors() - { - synchronized (_acceptors) - { - return new HashMap<InetSocketAddress, QpidAcceptor>(_acceptors); - } - } - - protected void setSecurityManager(SecurityManager securityManager) - { - _securityManager = securityManager; - } + private ConfigurationEntryStore _store; + private TaskExecutor _taskExecutor; protected void setRootMessageLogger(RootMessageLogger rootMessageLogger) { _rootMessageLogger = rootMessageLogger; } - protected CompositeStartupMessageLogger getStartupMessageLogger() - { - return _startupMessageLogger; - } - - protected void setStartupMessageLogger(CompositeStartupMessageLogger startupMessageLogger) + public ApplicationRegistry(ConfigurationEntryStore store) { - _startupMessageLogger = startupMessageLogger; - } - - public static void initialise(IApplicationRegistry instance) throws Exception - { - if(instance == null) - { - throw new IllegalArgumentException("ApplicationRegistry instance must not be null"); - } - - if(!_instance.compareAndSet(null, instance)) - { - throw new IllegalStateException("An ApplicationRegistry is already initialised"); - } - - _logger.info("Initialising Application Registry(" + instance + ")"); - - - try - { - instance.initialise(); - } - catch (Exception e) - { - _instance.set(null); - - //remove the Broker instance, then re-throw - - throw e; - } - } - - public static boolean isConfigured() - { - return _instance.get() != null; - } - - public static void remove() - { - IApplicationRegistry instance = _instance.getAndSet(null); - try - { - if (instance != null) - { - if (_logger.isInfoEnabled()) - { - _logger.info("Shutting down ApplicationRegistry(" + instance + ")"); - } - instance.close(); - } - } - catch (Exception e) - { - _logger.error("Error shutting down Application Registry(" + instance + "): " + e, e); - } - } - - public ApplicationRegistry(ServerConfiguration configuration) - { - _configuration = configuration; + _store = store; + initialiseStatistics(); } public void initialise() throws Exception { + // Create the RootLogger to be used during broker operation + boolean statusUpdatesEnabled = Boolean.parseBoolean(System.getProperty(BrokerProperties.PROPERTY_STATUS_UPDATES, "true")); + _rootMessageLogger = new Log4jMessageLogger(statusUpdatesEnabled); + _logRecorder = new LogRecorder(); - //Create the RootLogger to be used during broker operation - _rootMessageLogger = new Log4jMessageLogger(_configuration); //Create the composite (log4j+SystemOut MessageLogger to be used during startup RootMessageLogger[] messageLoggers = {new SystemOutMessageLogger(), _rootMessageLogger}; - _startupMessageLogger = new CompositeStartupMessageLogger(messageLoggers); + CompositeStartupMessageLogger startupMessageLogger = new CompositeStartupMessageLogger(messageLoggers); - BrokerActor actor = new BrokerActor(_startupMessageLogger); - CurrentActor.setDefault(actor); + BrokerActor actor = new BrokerActor(startupMessageLogger); CurrentActor.set(actor); - + CurrentActor.setDefault(actor); + GenericActor.setDefaultMessageLogger(_rootMessageLogger); try { - initialiseStatistics(); - - if(_configuration.getHTTPManagementEnabled()) - { - _httpManagementPort = _configuration.getHTTPManagementPort(); - } - if (_configuration.getHTTPSManagementEnabled()) - { - _httpsManagementPort = _configuration.getHTTPSManagementPort(); - } - - _broker = new BrokerAdapter(this); - - _configuration.initialise(); logStartupMessages(CurrentActor.get()); - // Management needs to be registered so that JMXManagement.childAdded can create optional management objects - createAndStartManagementPlugins(_configuration, _broker); + _taskExecutor = new TaskExecutor(); + _taskExecutor.start(); - _securityManager = new SecurityManager(_configuration.getConfig()); + RecovererProvider provider = new DefaultRecovererProvider((StatisticsGatherer)this, _virtualHostRegistry, _logRecorder, _rootMessageLogger, _taskExecutor); + ConfiguredObjectRecoverer<? extends ConfiguredObject> brokerRecoverer = provider.getRecoverer(Broker.class.getSimpleName()); + _broker = (Broker) brokerRecoverer.create(provider, _store.getRootEntry()); - _groupManagerList = createGroupManagers(_configuration); + _virtualHostRegistry.setDefaultVirtualHostName((String)_broker.getAttribute(Broker.DEFAULT_VIRTUAL_HOST)); - _authenticationManagerRegistry = createAuthenticationManagerRegistry(_configuration, new GroupPrincipalAccessor(_groupManagerList)); + initialiseStatisticsReporting(); - if(!_authManagerChangeListeners.isEmpty()) - { - for(IAuthenticationManagerRegistry.RegistryChangeListener listener : _authManagerChangeListeners) - { + // starting the broker + _broker.setDesiredState(State.INITIALISING, State.ACTIVE); - _authenticationManagerRegistry.addRegistryChangeListener(listener); - for(AuthenticationManager authMgr : _authenticationManagerRegistry.getAvailableAuthenticationManagers().values()) - { - listener.authenticationManagerRegistered(authMgr); - } - } - _authManagerChangeListeners.clear(); - } + CurrentActor.get().message(BrokerMessages.READY()); } finally { CurrentActor.remove(); } - CurrentActor.set(new BrokerActor(_rootMessageLogger)); - try - { - initialiseVirtualHosts(); - initialiseStatisticsReporting(); - } - finally - { - // Startup complete, so pop the current actor - CurrentActor.remove(); - } + CurrentActor.setDefault(new BrokerActor(_rootMessageLogger)); } - private void createAndStartManagementPlugins(ServerConfiguration configuration, Broker broker) throws Exception + private void initialiseStatisticsReporting() { - QpidServiceLoader<ManagementFactory> factories = new QpidServiceLoader<ManagementFactory>(); - for (ManagementFactory managementFactory: factories.instancesOf(ManagementFactory.class)) - { - ManagementPlugin managementPlugin = managementFactory.createInstance(configuration, broker); - if(managementPlugin != null) - { - try - { - managementPlugin.start(); - } - catch(Exception e) - { - _logger.error("Management plugin " + managementPlugin.getClass().getSimpleName() + " failed to start normally, stopping it now", e); - managementPlugin.stop(); - throw e; - } - - _managmentInstanceList.add(managementPlugin); - } - } - - if (_logger.isDebugEnabled()) - { - _logger.debug("Configured " + _managmentInstanceList.size() + " management instance(s)"); - } - } - - private void closeAllManagementPlugins() - { - for (ManagementPlugin managementPlugin : _managmentInstanceList) - { - try - { - managementPlugin.stop(); - } - catch (Exception e) - { - _logger.error("Exception thrown whilst stopping management plugin " + managementPlugin.getClass().getSimpleName(), e); - } - } - } - - private List<GroupManager> createGroupManagers(ServerConfiguration configuration) throws ConfigurationException - { - List<GroupManager> groupManagerList = new ArrayList<GroupManager>(); - Configuration securityConfig = configuration.getConfig().subset("security"); - - for(GroupManagerFactory factory : _groupManagerServiceLoader.instancesOf(GroupManagerFactory.class)) - { - GroupManager groupManager = factory.createInstance(securityConfig); - if (groupManager != null) - { - groupManagerList.add(groupManager); - for(GroupManagerChangeListener listener : _groupManagerChangeListeners) - { - listener.groupManagerRegistered(groupManager); - } - } - } - - if (_logger.isDebugEnabled()) - { - _logger.debug("Configured " + groupManagerList.size() + " group manager(s)"); - } - return groupManagerList; - } - - protected IAuthenticationManagerRegistry createAuthenticationManagerRegistry(ServerConfiguration configuration, GroupPrincipalAccessor groupPrincipalAccessor) - throws ConfigurationException - { - return new AuthenticationManagerRegistry(configuration, groupPrincipalAccessor); - } - - protected void initialiseVirtualHosts() throws Exception - { - for (String name : _configuration.getVirtualHosts()) - { - createVirtualHost(_configuration.getVirtualHostConfig(name)); - } - getVirtualHostRegistry().setDefaultVirtualHostName(_configuration.getDefaultVirtualHost()); - } - - public void initialiseStatisticsReporting() - { - long report = _configuration.getStatisticsReportingPeriod() * 1000; // convert to ms - final boolean broker = _configuration.isStatisticsGenerationBrokerEnabled(); - final boolean virtualhost = _configuration.isStatisticsGenerationVirtualhostsEnabled(); - final boolean reset = _configuration.isStatisticsReportResetEnabled(); + long report = ((Number)_broker.getAttribute(Broker.STATISTICS_REPORTING_PERIOD)).intValue() * 1000; // convert to ms + final boolean reset = (Boolean)_broker.getAttribute(Broker.STATISTICS_REPORTING_RESET_ENABLED); /* add a timer task to report statistics if generation is enabled for broker or virtualhosts */ - if (report > 0L && (broker || virtualhost)) + if (report > 0L) { _reportingTimer = new Timer("Statistics-Reporting", true); - - - - _reportingTimer.scheduleAtFixedRate(new StatisticsReportingTask(broker, virtualhost, reset), - report / 2, - report); + StatisticsReportingTask task = new StatisticsReportingTask(reset, _rootMessageLogger); + _reportingTimer.scheduleAtFixedRate(task, report / 2, report); } } @@ -378,76 +152,62 @@ public class ApplicationRegistry implements IApplicationRegistry private final int DELIVERED = 0; private final int RECEIVED = 1; - private boolean _broker; - private boolean _virtualhost; - private boolean _reset; - + private final boolean _reset; + private final RootMessageLogger _logger; - public StatisticsReportingTask(boolean broker, boolean virtualhost, boolean reset) + public StatisticsReportingTask(boolean reset, RootMessageLogger logger) { - _broker = broker; - _virtualhost = virtualhost; _reset = reset; + _logger = logger; } public void run() { - CurrentActor.set(new AbstractActor(ApplicationRegistry.getInstance().getRootMessageLogger()) { + CurrentActor.set(new AbstractActor(_logger) + { public String getLogMessage() { return "[" + Thread.currentThread().getName() + "] "; } }); - - if (_broker) + try { CurrentActor.get().message(BrokerMessages.STATS_DATA(DELIVERED, _dataDelivered.getPeak() / 1024.0, _dataDelivered.getTotal())); CurrentActor.get().message(BrokerMessages.STATS_MSGS(DELIVERED, _messagesDelivered.getPeak(), _messagesDelivered.getTotal())); CurrentActor.get().message(BrokerMessages.STATS_DATA(RECEIVED, _dataReceived.getPeak() / 1024.0, _dataReceived.getTotal())); CurrentActor.get().message(BrokerMessages.STATS_MSGS(RECEIVED, _messagesReceived.getPeak(), _messagesReceived.getTotal())); - } + Collection<VirtualHost> hosts = _virtualHostRegistry.getVirtualHosts(); - if (_virtualhost) - { - for (VirtualHost vhost : getVirtualHostRegistry().getVirtualHosts()) + if (hosts.size() > 1) { - String name = vhost.getName(); - StatisticsCounter dataDelivered = vhost.getDataDeliveryStatistics(); - StatisticsCounter messagesDelivered = vhost.getMessageDeliveryStatistics(); - StatisticsCounter dataReceived = vhost.getDataReceiptStatistics(); - StatisticsCounter messagesReceived = vhost.getMessageReceiptStatistics(); - - CurrentActor.get().message(VirtualHostMessages.STATS_DATA(name, DELIVERED, dataDelivered.getPeak() / 1024.0, dataDelivered.getTotal())); - CurrentActor.get().message(VirtualHostMessages.STATS_MSGS(name, DELIVERED, messagesDelivered.getPeak(), messagesDelivered.getTotal())); - CurrentActor.get().message(VirtualHostMessages.STATS_DATA(name, RECEIVED, dataReceived.getPeak() / 1024.0, dataReceived.getTotal())); - CurrentActor.get().message(VirtualHostMessages.STATS_MSGS(name, RECEIVED, messagesReceived.getPeak(), messagesReceived.getTotal())); + for (VirtualHost vhost : hosts) + { + String name = vhost.getName(); + StatisticsCounter dataDelivered = vhost.getDataDeliveryStatistics(); + StatisticsCounter messagesDelivered = vhost.getMessageDeliveryStatistics(); + StatisticsCounter dataReceived = vhost.getDataReceiptStatistics(); + StatisticsCounter messagesReceived = vhost.getMessageReceiptStatistics(); + + CurrentActor.get().message(VirtualHostMessages.STATS_DATA(name, DELIVERED, dataDelivered.getPeak() / 1024.0, dataDelivered.getTotal())); + CurrentActor.get().message(VirtualHostMessages.STATS_MSGS(name, DELIVERED, messagesDelivered.getPeak(), messagesDelivered.getTotal())); + CurrentActor.get().message(VirtualHostMessages.STATS_DATA(name, RECEIVED, dataReceived.getPeak() / 1024.0, dataReceived.getTotal())); + CurrentActor.get().message(VirtualHostMessages.STATS_MSGS(name, RECEIVED, messagesReceived.getPeak(), messagesReceived.getTotal())); + } } - } - if (_reset) + if (_reset) + { + resetStatistics(); + } + } + catch(Exception e) { - resetStatistics(); + ApplicationRegistry._logger.warn("Unexpected exception occured while reporting the statistics", e); + } + finally + { + CurrentActor.remove(); } - - CurrentActor.remove(); - } - } - - /** - * Get the ApplicationRegistry - * @return the IApplicationRegistry instance - * @throws IllegalStateException if no registry instance has been initialised. - */ - public static IApplicationRegistry getInstance() throws IllegalStateException - { - IApplicationRegistry iApplicationRegistry = _instance.get(); - if (iApplicationRegistry == null) - { - throw new IllegalStateException("No ApplicationRegistry has been initialised"); - } - else - { - return iApplicationRegistry; } } @@ -478,7 +238,7 @@ public class ApplicationRegistry implements IApplicationRegistry } //Set the Actor for Broker Shutdown - CurrentActor.set(new BrokerActor(getRootMessageLogger())); + CurrentActor.set(new BrokerActor(_rootMessageLogger)); try { //Stop Statistics Reporting @@ -487,128 +247,34 @@ public class ApplicationRegistry implements IApplicationRegistry _reportingTimer.cancel(); } - //Stop incoming connections - unbind(); + if (_broker != null) + { + _broker.setDesiredState(_broker.getActualState(), State.STOPPED); + } //Shutdown virtualhosts close(_virtualHostRegistry); - close(_authenticationManagerRegistry); + if (_taskExecutor != null) + { + _taskExecutor.stop(); + } CurrentActor.get().message(BrokerMessages.STOPPED()); _logRecorder.closeLogRecorder(); - closeAllManagementPlugins(); } finally { - CurrentActor.remove(); - } - } - - private void unbind() - { - List<QpidAcceptor> removedAcceptors = new ArrayList<QpidAcceptor>(); - synchronized (_acceptors) - { - for (InetSocketAddress bindAddress : _acceptors.keySet()) - { - QpidAcceptor acceptor = _acceptors.get(bindAddress); - - removedAcceptors.add(acceptor); - try - { - acceptor.getNetworkTransport().close(); - } - catch (Throwable e) - { - _logger.error("Unable to close network driver due to:" + e.getMessage()); - } - - CurrentActor.get().message(BrokerMessages.SHUTTING_DOWN(acceptor.toString(), bindAddress.getPort())); - } - } - synchronized (_portBindingListeners) - { - for(QpidAcceptor acceptor : removedAcceptors) + if (_taskExecutor != null) { - for(PortBindingListener listener : _portBindingListeners) - { - listener.unbound(acceptor); - } - } - } - } - - public ServerConfiguration getConfiguration() - { - return _configuration; - } - - public void addAcceptor(InetSocketAddress bindAddress, QpidAcceptor acceptor) - { - synchronized (_acceptors) - { - _acceptors.put(bindAddress, acceptor); - } - synchronized (_portBindingListeners) - { - for(PortBindingListener listener : _portBindingListeners) - { - listener.bound(acceptor, bindAddress); + _taskExecutor.stopImmediately(); } + CurrentActor.remove(); } - } - - public VirtualHostRegistry getVirtualHostRegistry() - { - return _virtualHostRegistry; - } - - public SecurityManager getSecurityManager() - { - return _securityManager; - } - - @Override - public SubjectCreator getSubjectCreator(SocketAddress localAddress) - { - return _authenticationManagerRegistry.getSubjectCreator(localAddress); - } - - @Override - public IAuthenticationManagerRegistry getAuthenticationManagerRegistry() - { - return _authenticationManagerRegistry; - } - - @Override - public List<GroupManager> getGroupManagers() - { - return _groupManagerList; - } - - public RootMessageLogger getRootMessageLogger() - { - return _rootMessageLogger; - } - - public RootMessageLogger getCompositeStartupMessageLogger() - { - return _startupMessageLogger; - } - - public UUID getBrokerId() - { - return getBroker().getId(); - } - - public VirtualHost createVirtualHost(final VirtualHostConfiguration vhostConfig) throws Exception - { - VirtualHostImpl virtualHost = new VirtualHostImpl(this, vhostConfig); - _virtualHostRegistry.registerVirtualHost(virtualHost); - return virtualHost; + _store = null; + _broker = null; } public void registerMessageDelivered(long messageSize) @@ -677,66 +343,4 @@ public class ApplicationRegistry implements IApplicationRegistry logActor.message(BrokerMessages.MAX_MEMORY(Runtime.getRuntime().maxMemory())); } - public Broker getBroker() - { - return _broker; - } - - @Override - public void addPortBindingListener(PortBindingListener listener) - { - synchronized (_portBindingListeners) - { - _portBindingListeners.add(listener); - } - } - - - @Override - public boolean useHTTPManagement() - { - return _httpManagementPort != -1; - } - - @Override - public int getHTTPManagementPort() - { - return _httpManagementPort; - } - - @Override - public boolean useHTTPSManagement() - { - return _httpsManagementPort != -1; - } - - @Override - public int getHTTPSManagementPort() - { - return _httpsManagementPort; - } - - public LogRecorder getLogRecorder() - { - return _logRecorder; - } - - @Override - public void addAuthenticationManagerRegistryChangeListener(IAuthenticationManagerRegistry.RegistryChangeListener registryChangeListener) - { - if(_authenticationManagerRegistry == null) - { - _authManagerChangeListeners.add(registryChangeListener); - } - else - { - _authenticationManagerRegistry.addRegistryChangeListener(registryChangeListener); - } - } - - @Override - public void addGroupManagerChangeListener(GroupManagerChangeListener groupManagerChangeListener) - { - _groupManagerChangeListeners.add(groupManagerChangeListener); - } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/IApplicationRegistry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/IApplicationRegistry.java index 8cdb576f76..0f9ddbfb59 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/IApplicationRegistry.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/registry/IApplicationRegistry.java @@ -20,25 +20,7 @@ */ package org.apache.qpid.server.registry; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.configuration.VirtualHostConfiguration; -import org.apache.qpid.server.logging.RootMessageLogger; -import org.apache.qpid.server.model.Broker; -import org.apache.qpid.server.security.SecurityManager; -import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.auth.manager.AuthenticationManager; -import org.apache.qpid.server.security.auth.manager.IAuthenticationManagerRegistry; -import org.apache.qpid.server.security.group.GroupManager; import org.apache.qpid.server.stats.StatisticsGatherer; -import org.apache.qpid.server.transport.QpidAcceptor; -import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.server.virtualhost.VirtualHostRegistry; - -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.util.List; -import java.util.Map; -import java.util.UUID; public interface IApplicationRegistry extends StatisticsGatherer { @@ -54,71 +36,4 @@ public interface IApplicationRegistry extends StatisticsGatherer */ void close(); - /** - * Get the low level configuration. For use cases where the configured object approach is not required - * you can get the complete configuration information. - * @return a Commons Configuration instance - */ - ServerConfiguration getConfiguration(); - - /** - * Get the SubjectCreator for the given socket address. - * - * @param address The (listening) socket address for which the AuthenticationManager is required - */ - SubjectCreator getSubjectCreator(SocketAddress localAddress); - - IAuthenticationManagerRegistry getAuthenticationManagerRegistry(); - - List<GroupManager> getGroupManagers(); - - VirtualHostRegistry getVirtualHostRegistry(); - - SecurityManager getSecurityManager(); - - RootMessageLogger getRootMessageLogger(); - - /** - * Register any acceptors for this registry - * @param bindAddress The address that the acceptor has been bound with - * @param acceptor The acceptor in use - */ - void addAcceptor(InetSocketAddress bindAddress, QpidAcceptor acceptor); - - public UUID getBrokerId(); - - Broker getBroker(); - - VirtualHost createVirtualHost(VirtualHostConfiguration vhostConfig) throws Exception; - - void initialiseStatisticsReporting(); - - Map<InetSocketAddress, QpidAcceptor> getAcceptors(); - - void addPortBindingListener(PortBindingListener listener); - - boolean useHTTPManagement(); - - int getHTTPManagementPort(); - - boolean useHTTPSManagement(); - - int getHTTPSManagementPort(); - - void addAuthenticationManagerRegistryChangeListener(IAuthenticationManagerRegistry.RegistryChangeListener registryChangeListener); - - public interface PortBindingListener - { - public void bound(QpidAcceptor acceptor, InetSocketAddress bindAddress); - public void unbound(QpidAcceptor acceptor); - - } - - void addGroupManagerChangeListener(GroupManagerChangeListener groupManagerChangeListener); - - public static interface GroupManagerChangeListener - { - void groupManagerRegistered(GroupManager groupManager); - void groupManagerUnregistered(GroupManager groupManager); - } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/SecurityManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/SecurityManager.java index 6099d11c46..1a1cce171b 100755 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/SecurityManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/SecurityManager.java @@ -18,8 +18,6 @@ */ package org.apache.qpid.server.security; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; import org.apache.log4j.Logger; import org.apache.qpid.framing.AMQShortString; @@ -122,21 +120,21 @@ public class SecurityManager /* * Used by the VirtualHost to allow deferring to the broker level security plugins if required. */ - public SecurityManager(SecurityManager parent, Configuration config) throws ConfigurationException + public SecurityManager(SecurityManager parent, String aclFile) { - this(config); + this(aclFile); // our global plugins are the parent's host plugins _globalPlugins = parent._hostPlugins; } - public SecurityManager(Configuration config) throws ConfigurationException + public SecurityManager(String aclFile) { - Configuration securityConfig = config.subset("security"); - + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put("aclFile", aclFile); for (AccessControlFactory provider : (new QpidServiceLoader<AccessControlFactory>()).instancesOf(AccessControlFactory.class)) { - AccessControl accessControl = provider.createInstance(securityConfig); + AccessControl accessControl = provider.createInstance(attributes); if(accessControl != null) { addHostPlugin(accessControl); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AbstractPrincipalDatabaseAuthManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AbstractPrincipalDatabaseAuthManagerFactory.java new file mode 100644 index 0000000000..ff21d63c87 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AbstractPrincipalDatabaseAuthManagerFactory.java @@ -0,0 +1,71 @@ +/* + * 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.security.auth.manager; + +import java.io.IOException; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.security.auth.database.PrincipalDatabase; + +/** + * Factory for {@link PrincipalDatabaseAuthenticationManager} objects configured + * with either the Plain or Base64MD5 digest {@link PrincipalDatabase} + * implementation. + */ +public abstract class AbstractPrincipalDatabaseAuthManagerFactory implements AuthenticationManagerFactory +{ + public static final String ATTRIBUTE_PATH = "path"; + + private static final Logger LOGGER = Logger.getLogger(AbstractPrincipalDatabaseAuthManagerFactory.class); + + @Override + public AuthenticationManager createInstance(Map<String, Object> attributes) + { + if (attributes == null || !getType().equals(attributes.get(ATTRIBUTE_TYPE))) + { + return null; + } + + String passwordFile = (String) attributes.get(ATTRIBUTE_PATH); + if (passwordFile == null) + { + LOGGER.warn("Password file path must not be null"); + return null; + } + + PrincipalDatabase principalDatabase = createPrincipalDatabase(); + try + { + principalDatabase.setPasswordFile(passwordFile); + } + catch (IOException e) + { + throw new RuntimeException(e.getMessage(), e); + } + + return new PrincipalDatabaseAuthenticationManager(principalDatabase); + } + + abstract String getType(); + + abstract PrincipalDatabase createPrincipalDatabase(); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerFactory.java index 1dabb3d203..1b1995500c 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AnonymousAuthenticationManagerFactory.java @@ -19,20 +19,22 @@ */ package org.apache.qpid.server.security.auth.manager; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.plugin.AuthenticationManagerFactory; public class AnonymousAuthenticationManagerFactory implements AuthenticationManagerFactory { + public static final String PROVIDER_TYPE = AnonymousAuthenticationManager.class.getSimpleName(); + @Override - public AuthenticationManager createInstance(Configuration configuration) + public AuthenticationManager createInstance(Map<String, Object> attributes) { - if (configuration.subset("anonymous-auth-manager").isEmpty()) + if (attributes != null && PROVIDER_TYPE.equals(attributes.get(ATTRIBUTE_TYPE))) { - return null; + return new AnonymousAuthenticationManager(); } - - return new AnonymousAuthenticationManager(); + return null; } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistry.java deleted file mode 100644 index 0f311e9aca..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistry.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * 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.security.auth.manager; - -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.qpid.common.Closeable; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.plugin.AuthenticationManagerFactory; -import org.apache.qpid.server.plugin.QpidServiceLoader; -import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.group.GroupPrincipalAccessor; - -/** - * A concrete implementation of {@link IAuthenticationManagerRegistry} that registers all {@link AuthenticationManager} - * instances defined in the configuration, building an optional mapping between port number and AuthenticationManager. - * - * <p>The default AuthenticationManager is either the one nominated as default within the configuration with - * {@link ServerConfiguration#getDefaultAuthenticationManager()}, or if there is only one, it is implicitly - * the default.</p> - * - * <p>It is important to {@link #close()} the registry after use and this allows the AuthenticationManagers - * to reverse any security registrations they have performed.</p> - */ -public class AuthenticationManagerRegistry implements Closeable, IAuthenticationManagerRegistry -{ - private final Map<String,AuthenticationManager> _classToAuthManagerMap = new HashMap<String,AuthenticationManager>(); - private final SubjectCreator _defaultSubjectCreator; - private final Map<Integer, SubjectCreator> _portToSubjectCreatorMap; - private final List<RegistryChangeListener> _listeners = - Collections.synchronizedList(new ArrayList<RegistryChangeListener>()); - private final QpidServiceLoader<AuthenticationManagerFactory> _authManagerFactoryServiceLoader; - - public AuthenticationManagerRegistry(ServerConfiguration serverConfiguration, GroupPrincipalAccessor groupPrincipalAccessor) - throws ConfigurationException - { - this(serverConfiguration, groupPrincipalAccessor, new QpidServiceLoader<AuthenticationManagerFactory>()); - } - - // Exists as separate constructor for unit testing purposes - AuthenticationManagerRegistry(ServerConfiguration serverConfiguration, GroupPrincipalAccessor groupPrincipalAccessor, QpidServiceLoader<AuthenticationManagerFactory> authManagerFactoryServiceLoader) - throws ConfigurationException - { - _authManagerFactoryServiceLoader = authManagerFactoryServiceLoader; - - boolean willClose = true; - try - { - createAuthManagers(serverConfiguration.getConfig()); - - if(_classToAuthManagerMap.isEmpty()) - { - throw new ConfigurationException("No authentication managers configured within the configuration file."); - } - - _defaultSubjectCreator = createDefaultSubectCreator(serverConfiguration, groupPrincipalAccessor); - - _portToSubjectCreatorMap = createPortToSubjectCreatorMap(serverConfiguration, groupPrincipalAccessor); - willClose = false; - } - finally - { - // if anyConfigurationExceptionthing went wrong whilst configuring the registry, try to close all the AuthentcationManagers instantiated so far. - // This is done to allow the AuthenticationManager to undo any security registrations that they have performed. - if (willClose) - { - close(); - } - } - } - - @Override - public SubjectCreator getSubjectCreator(SocketAddress address) - { - SubjectCreator subjectCreator = - address instanceof InetSocketAddress - ? _portToSubjectCreatorMap.get(((InetSocketAddress)address).getPort()) - : null; - - return subjectCreator == null ? _defaultSubjectCreator : subjectCreator; - } - - @Override - public void close() - { - for (AuthenticationManager authManager : _classToAuthManagerMap.values()) - { - authManager.close(); - } - } - - private void createAuthManagers(Configuration config) - { - Configuration securityConfiguration = config.subset("security"); - - for(AuthenticationManagerFactory factory : _authManagerFactoryServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)) - { - AuthenticationManager plugin = factory.createInstance(securityConfiguration); - if(plugin != null) - { - validateAndInitialiseAuthenticationManager(plugin); - } - } - } - - private void validateAndInitialiseAuthenticationManager(AuthenticationManager authenticationManager) - { - // TODO Should be a user-defined name rather than the classname. - final String authManagerName = authenticationManager.getClass().getSimpleName(); - if (_classToAuthManagerMap.containsKey(authManagerName)) - { - throw new RuntimeException("Cannot configure more than one authentication manager with name " - + authManagerName + "."); - } - - authenticationManager.initialise(); - - _classToAuthManagerMap.put(authManagerName, authenticationManager); - } - - private SubjectCreator createDefaultSubectCreator( - ServerConfiguration serverConfiguration, GroupPrincipalAccessor groupAccessor) - throws ConfigurationException - { - final AuthenticationManager defaultAuthenticationManager; - if(_classToAuthManagerMap.size() == 1) - { - defaultAuthenticationManager = _classToAuthManagerMap.values().iterator().next(); - } - else if(serverConfiguration.getDefaultAuthenticationManager() != null) - { - defaultAuthenticationManager = _classToAuthManagerMap.get(serverConfiguration.getDefaultAuthenticationManager()); - if(defaultAuthenticationManager == null) - { - throw new ConfigurationException("No authentication managers configured of type " - + serverConfiguration.getDefaultAuthenticationManager() - + " which is specified as the default. Available managers are: " - + _classToAuthManagerMap.keySet()); - } - } - else - { - throw new ConfigurationException("If more than one authentication manager is configured a default MUST be specified."); - } - return new SubjectCreator(defaultAuthenticationManager, groupAccessor); - } - - private Map<Integer, SubjectCreator> createPortToSubjectCreatorMap( - ServerConfiguration serverConfiguration, GroupPrincipalAccessor groupPrincipalAccessor) - throws ConfigurationException - { - Map<Integer,SubjectCreator> portToSubjectCreatorMap = new HashMap<Integer, SubjectCreator>(); - - for(Map.Entry<Integer,String> portMapping : serverConfiguration.getPortAuthenticationMappings().entrySet()) - { - - AuthenticationManager authenticationManager = _classToAuthManagerMap.get(portMapping.getValue()); - if(authenticationManager == null) - { - throw new ConfigurationException("Unknown authentication manager class " + portMapping.getValue() + - " configured for port " + portMapping.getKey()); - } - - SubjectCreator subjectCreator = new SubjectCreator(authenticationManager, groupPrincipalAccessor); - portToSubjectCreatorMap.put(portMapping.getKey(), subjectCreator); - } - - return portToSubjectCreatorMap; - } - - @Override - public Map<String, AuthenticationManager> getAvailableAuthenticationManagers() - { - return Collections.unmodifiableMap(new HashMap<String, AuthenticationManager>(_classToAuthManagerMap)); - } - - @Override - public void addRegistryChangeListener(RegistryChangeListener listener) - { - _listeners.add(listener); - } - -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactory.java new file mode 100644 index 0000000000..c61567ef77 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactory.java @@ -0,0 +1,42 @@ +/* + * + * 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.security.auth.manager; + +import org.apache.qpid.server.security.auth.database.Base64MD5PasswordFilePrincipalDatabase; +import org.apache.qpid.server.security.auth.database.PrincipalDatabase; + +public class Base64MD5PasswordFileAuthenticationManagerFactory extends AbstractPrincipalDatabaseAuthManagerFactory +{ + public static final String PROVIDER_TYPE = "Base64MD5PasswordFileAuthenticationProvider"; + + @Override + String getType() + { + return PROVIDER_TYPE; + } + + @Override + PrincipalDatabase createPrincipalDatabase() + { + return new Base64MD5PasswordFilePrincipalDatabase(); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerFactory.java index 686f084c93..3c3628e9db 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/ExternalAuthenticationManagerFactory.java @@ -19,20 +19,22 @@ */ package org.apache.qpid.server.security.auth.manager; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.plugin.AuthenticationManagerFactory; public class ExternalAuthenticationManagerFactory implements AuthenticationManagerFactory { + public static final String PROVIDER_TYPE = ExternalAuthenticationManager.class.getSimpleName(); + @Override - public AuthenticationManager createInstance(Configuration configuration) + public AuthenticationManager createInstance(Map<String, Object> attributes) { - if (configuration.subset("external-auth-manager").isEmpty()) + if (attributes != null && PROVIDER_TYPE.equals(attributes.get(ATTRIBUTE_TYPE))) { - return null; + return new ExternalAuthenticationManager(); } - - return new ExternalAuthenticationManager(); + return null; } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/IAuthenticationManagerRegistry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/IAuthenticationManagerRegistry.java deleted file mode 100644 index 6ed8f95512..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/IAuthenticationManagerRegistry.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.security.auth.manager; - -import java.net.SocketAddress; - -import java.util.Map; -import org.apache.qpid.common.Closeable; -import org.apache.qpid.server.security.SubjectCreator; - -/** - * Registry for {@link AuthenticationManager} instances, also exposing them wrapped in {@link SubjectCreator}'s - * as a convenience. - * - * <p>It is important to {@link #close()} the registry after use and this allows the AuthenticationManagers - * to reverse any security registrations they have performed.</p> - */ -public interface IAuthenticationManagerRegistry extends Closeable -{ - /** - * Returns the {@link SubjectCreator} associated with a particular {@link SocketAddress}. - * If no subject creator is associated with this address, a default will be - * returned. Null is never returned. - */ - public SubjectCreator getSubjectCreator(SocketAddress address); - - Map<String, AuthenticationManager> getAvailableAuthenticationManagers(); - - public static interface RegistryChangeListener - { - void authenticationManagerRegistered(AuthenticationManager authenticationManager); - void authenticationManagerUnregistered(AuthenticationManager authenticationManager); - } - - public void addRegistryChangeListener(RegistryChangeListener listener); -}
\ No newline at end of file diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/KerberosAuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/KerberosAuthenticationManagerFactory.java index 470a51b6a6..7af6727280 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/KerberosAuthenticationManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/KerberosAuthenticationManagerFactory.java @@ -19,19 +19,21 @@ */ package org.apache.qpid.server.security.auth.manager; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.plugin.AuthenticationManagerFactory; public class KerberosAuthenticationManagerFactory implements AuthenticationManagerFactory { + public static final String PROVIDER_TYPE = KerberosAuthenticationManager.class.getSimpleName(); + @Override - public AuthenticationManager createInstance(Configuration configuration) + public AuthenticationManager createInstance(Map<String, Object> attributes) { - if (configuration.subset("kerberos-auth-manager").isEmpty()) + if (attributes != null && PROVIDER_TYPE.equals(attributes.get(ATTRIBUTE_TYPE))) { - return null; + return new KerberosAuthenticationManager(); } - - return new KerberosAuthenticationManager(); + return null; } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactory.java new file mode 100644 index 0000000000..43b92735f1 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactory.java @@ -0,0 +1,42 @@ +/* + * + * 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.security.auth.manager; + +import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase; +import org.apache.qpid.server.security.auth.database.PrincipalDatabase; + +public class PlainPasswordFileAuthenticationManagerFactory extends AbstractPrincipalDatabaseAuthManagerFactory +{ + public static final String PROVIDER_TYPE = "PlainPasswordFileAuthenticationProvider"; + + @Override + String getType() + { + return PROVIDER_TYPE; + } + + @Override + PrincipalDatabase createPrincipalDatabase() + { + return new PlainPasswordFilePrincipalDatabase(); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthManagerFactory.java deleted file mode 100644 index d616bbb7ae..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthManagerFactory.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.security.auth.manager; - -import java.io.IOException; - -import org.apache.commons.configuration.Configuration; -import org.apache.log4j.Logger; -import org.apache.qpid.server.plugin.AuthenticationManagerFactory; -import org.apache.qpid.server.security.auth.database.Base64MD5PasswordFilePrincipalDatabase; -import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase; -import org.apache.qpid.server.security.auth.database.PrincipalDatabase; - -/** - * Factory for {@link PrincipalDatabaseAuthenticationManager} objects configured with either the - * Plain or Base64MD5 digest {@link PrincipalDatabase} implementation. - */ -public class PrincipalDatabaseAuthManagerFactory implements AuthenticationManagerFactory -{ - private static final Logger LOGGER = Logger.getLogger(PrincipalDatabaseAuthManagerFactory.class); - - @Override - public AuthenticationManager createInstance(Configuration configuration) - { - if(configuration.subset("pd-auth-manager").isEmpty()) - { - return null; - } - - String clazz = configuration.getString("pd-auth-manager.principal-database.class"); - String passwordArgumentName = configuration.getString("pd-auth-manager.principal-database.attributes.attribute.name"); - String passwordFile = configuration.getString("pd-auth-manager.principal-database.attributes.attribute.value"); - - PrincipalDatabase principalDatabase = createKnownImplementation(clazz); - if (principalDatabase == null) - { - LOGGER.warn("Config for pd-auth-manager found but principal-database class specified in config " + clazz + - " not recognised."); - return null; - } - - if (!"passwordFile".equals(passwordArgumentName) || passwordFile == null) - { - LOGGER.warn("Config for pd-auth-manager found but config incomplete - expected attributes not found."); - return null; - } - - try - { - principalDatabase.setPasswordFile(passwordFile); - } - catch (IOException e) - { - throw new RuntimeException(e.getMessage(), e); - } - - return new PrincipalDatabaseAuthenticationManager(principalDatabase); - } - - private PrincipalDatabase createKnownImplementation(String clazz) - { - if (PlainPasswordFilePrincipalDatabase.class.getName().equals(clazz)) - { - return new PlainPasswordFilePrincipalDatabase(); - } - else if (Base64MD5PasswordFilePrincipalDatabase.class.getName().equals(clazz)) - { - return new Base64MD5PasswordFilePrincipalDatabase(); - } - else - { - return null; - } - } -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java index 8f84732f36..f4c834810d 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java @@ -50,8 +50,6 @@ import java.util.TreeMap; */ public class PrincipalDatabaseAuthenticationManager implements AuthenticationManager { - public static final String PD_CLASS = "pd.class"; - public static final String PD_PASSWORD_FILE = "pd.passwordFile"; private static final Logger _logger = Logger.getLogger(PrincipalDatabaseAuthenticationManager.class); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactory.java index 8a1d23ffb1..05a692fb0e 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactory.java @@ -19,32 +19,51 @@ */ package org.apache.qpid.server.security.auth.manager; -import org.apache.commons.configuration.Configuration; +import java.util.Map; + import org.apache.qpid.server.plugin.AuthenticationManagerFactory; public class SimpleLDAPAuthenticationManagerFactory implements AuthenticationManagerFactory { - private static final String DEFAULT_LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; + public static final String PROVIDER_TYPE = SimpleLDAPAuthenticationManager.class.getSimpleName(); + + public static final String ATTRIBUTE_LDAP_CONTEXT_FACTORY = "ldapContextFactory"; + public static final String ATTRIBUTE_SEARCH_FILTER = "searchFilter"; + public static final String ATTRIBUTE_SEARCH_CONTEXT = "searchContext"; + public static final String ATTRIBUTE_PROVIDER_AUTH_URL = "providerAuthUrl"; + public static final String ATTRIBUTE_PROVIDER_SEARCH_URL = "providerSearchUrl"; + public static final String ATTRIBUTE_PROVIDER_URL = "providerUrl"; + @Override - public AuthenticationManager createInstance(Configuration configuration) + public AuthenticationManager createInstance(Map<String, Object> attributes) { - - final Configuration subset = configuration.subset("simple-ldap-auth-manager"); - if(subset.isEmpty()) + if (attributes == null || !PROVIDER_TYPE.equals(attributes.get(ATTRIBUTE_TYPE))) { return null; } + String providerUrl = (String) attributes.get(ATTRIBUTE_PROVIDER_URL); + String providerSearchUrl = (String) attributes.get(ATTRIBUTE_PROVIDER_SEARCH_URL); + if (providerSearchUrl == null) + { + providerSearchUrl = providerUrl; + } + String providerAuthUrl = (String) attributes.get(ATTRIBUTE_PROVIDER_AUTH_URL); + if (providerAuthUrl == null) + { + providerAuthUrl = providerUrl; + } + String searchContext = (String) attributes.get(ATTRIBUTE_SEARCH_CONTEXT); + String searchFilter = (String) attributes.get(ATTRIBUTE_SEARCH_FILTER); + String ldapContextFactory = (String) attributes.get(ATTRIBUTE_LDAP_CONTEXT_FACTORY); + if (ldapContextFactory == null) + { + ldapContextFactory = DEFAULT_LDAP_CONTEXT_FACTORY; + } - String providerUrl = configuration.getString("simple-ldap-auth-manager.provider-url"); - String providerSearchUrl = configuration.getString("simple-ldap-auth-manager.provider-search-url", providerUrl); - String providerAuthUrl = configuration.getString("simple-ldap-auth-manager.provider-auth-url", providerUrl); - String searchContext = configuration.getString("simple-ldap-auth-manager.search-context"); - String searchFilter = configuration.getString("simple-ldap-auth-manager.search-filter"); - String ldapContextFactory = configuration.getString("simple-ldap-auth-manager.ldap-context-factory", DEFAULT_LDAP_CONTEXT_FACTORY); - - return new SimpleLDAPAuthenticationManager(providerSearchUrl, providerAuthUrl, searchContext, searchFilter, ldapContextFactory); + return new SimpleLDAPAuthenticationManager(providerSearchUrl, providerAuthUrl, searchContext, searchFilter, + ldapContextFactory); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java index 9f5d87bf62..abb8677e90 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java @@ -22,7 +22,7 @@ package org.apache.qpid.server.security.auth.rmi; import java.net.SocketAddress; -import org.apache.qpid.server.registry.IApplicationRegistry; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.security.SubjectCreator; import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus; @@ -42,13 +42,13 @@ public class RMIPasswordAuthenticator implements JMXAuthenticator static final String CREDENTIALS_REQUIRED = "User details are required. " + "Please ensure you are using an up to date management console to connect."; - private final IApplicationRegistry _appRegistry; - private final SocketAddress _socketAddress; + private final Broker _broker; + private final SocketAddress _address; - public RMIPasswordAuthenticator(IApplicationRegistry appRegistry, SocketAddress socketAddress) + public RMIPasswordAuthenticator(Broker broker, SocketAddress address) { - _appRegistry = appRegistry; - _socketAddress = socketAddress; + _broker = broker; + _address = address; } public Subject authenticate(Object credentials) throws SecurityException @@ -94,10 +94,10 @@ public class RMIPasswordAuthenticator implements JMXAuthenticator throw new SecurityException(SHOULD_BE_NON_NULL); } - SubjectCreator subjectCreator = _appRegistry.getSubjectCreator(_socketAddress); + SubjectCreator subjectCreator = _broker.getSubjectCreator(_address); if (subjectCreator == null) { - throw new SecurityException("Can't get subject creator for " + _socketAddress); + throw new SecurityException("Can't get subject creator for " + _address); } final SubjectAuthenticationResult result = subjectCreator.authenticate(username, password); @@ -121,7 +121,7 @@ public class RMIPasswordAuthenticator implements JMXAuthenticator SecurityManager.setThreadSubject(authenticatedSubject); try { - if (!_appRegistry.getSecurityManager().accessManagement()) + if (!_broker.getSecurityManager().accessManagement()) { throw new SecurityException(USER_NOT_AUTHORISED_FOR_MANAGEMENT); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManager.java index 5cc1085a0d..8295f28f9e 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManager.java @@ -25,7 +25,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; -import org.apache.commons.configuration.ConfigurationException; +import org.apache.qpid.server.configuration.IllegalConfigurationException; import org.apache.qpid.server.security.auth.UsernamePrincipal; /** @@ -49,7 +49,7 @@ public class FileGroupManager implements GroupManager private final FileGroupDatabase _groupDatabase; - public FileGroupManager(String groupFile) throws ConfigurationException + public FileGroupManager(String groupFile) { _groupDatabase = new FileGroupDatabase(); try @@ -58,7 +58,7 @@ public class FileGroupManager implements GroupManager } catch (IOException e) { - throw new ConfigurationException("Unable to set group file " + groupFile, e); + throw new IllegalConfigurationException("Unable to set group file " + groupFile, e); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManagerFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManagerFactory.java index a55cdbf602..5c4730a9c8 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManagerFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/FileGroupManagerFactory.java @@ -18,48 +18,34 @@ */ package org.apache.qpid.server.security.group; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; +import static org.apache.qpid.server.util.MapValueConverter.getStringAttribute; + +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.model.GroupProvider; import org.apache.qpid.server.plugin.GroupManagerFactory; public class FileGroupManagerFactory implements GroupManagerFactory { - private static final String GROUP_FILE_MARKER = "groupFile"; - private static final String FILE_ATTRIBUTE_VALUE = "file-group-manager.attributes.attribute.value"; - private static final String FILE_ATTRIBUTE_NAME = "file-group-manager.attributes.attribute.name"; + static final String FILE_GROUP_MANAGER_TYPE = "file-group-manager"; + static final String FILE = "file"; @Override - public GroupManager createInstance(Configuration configuration) + public GroupManager createInstance(Map<String, Object> attributes) { - if(configuration.subset("file-group-manager").isEmpty()) + if(!FILE_GROUP_MANAGER_TYPE.equals(getStringAttribute(GroupProvider.TYPE, attributes, null))) { return null; } - String groupFileArgumentName = configuration.getString(FILE_ATTRIBUTE_NAME); - String groupFile = configuration.getString(FILE_ATTRIBUTE_VALUE); - - if (!GROUP_FILE_MARKER.equals(groupFileArgumentName)) - { - throw new RuntimeException("Config for file-group-manager found but " + FILE_ATTRIBUTE_NAME - + " has no value or " + groupFileArgumentName - + " does not equal " + GROUP_FILE_MARKER); - } - - if (groupFile == null) - { - throw new RuntimeException("Config for file-group-manager found but " + FILE_ATTRIBUTE_VALUE + " has no value." - + " Filename expected."); - } - - try - { - return new FileGroupManager(groupFile); - } - catch (ConfigurationException e) + String groupFile = getStringAttribute(FILE, attributes, null); + if (StringUtils.isBlank(groupFile)) { - throw new RuntimeException(e); + throw new IllegalConfigurationException("Path to file containing groups is not specified!"); } + return new FileGroupManager(groupFile); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/GroupPrincipalAccessor.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/GroupPrincipalAccessor.java index 9ab8ee586c..d549b76aab 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/GroupPrincipalAccessor.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/group/GroupPrincipalAccessor.java @@ -19,27 +19,30 @@ package org.apache.qpid.server.security.group; import java.security.Principal; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.Set; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.adapter.GroupProviderAdapter; + public class GroupPrincipalAccessor { - private final List<GroupManager> _groupManagerList; + private final Collection<GroupProvider> _groupProviders; - public GroupPrincipalAccessor(List<GroupManager> groupManagerList) + public GroupPrincipalAccessor(Collection<GroupProvider> groupProviders) { - _groupManagerList = groupManagerList; + _groupProviders = groupProviders; } public Set<Principal> getGroupPrincipals(String username) { Set<Principal> principals = new HashSet<Principal>(); - for (GroupManager groupManager : _groupManagerList) + for (GroupProvider groupProvider : _groupProviders) { - Set<Principal> groups = groupManager.getGroupPrincipalsForUser(username); + Set<Principal> groups = groupProvider.getGroupPrincipalsForUser(username); if (groups != null) { principals.addAll(groups); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/state/AMQStateManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/state/AMQStateManager.java index f8b8d14abf..ff41536a23 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/state/AMQStateManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/state/AMQStateManager.java @@ -31,11 +31,10 @@ import org.apache.qpid.framing.MethodDispatcher; import org.apache.qpid.protocol.AMQConstant; import org.apache.qpid.protocol.AMQMethodEvent; import org.apache.qpid.protocol.AMQMethodListener; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.AMQProtocolSession; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.auth.manager.AuthenticationManager; import org.apache.qpid.server.virtualhost.VirtualHostRegistry; import java.util.concurrent.CopyOnWriteArraySet; @@ -48,32 +47,29 @@ public class AMQStateManager implements AMQMethodListener { private static final Logger _logger = Logger.getLogger(AMQStateManager.class); - private final VirtualHostRegistry _virtualHostRegistry; + private final Broker _broker; private final AMQProtocolSession _protocolSession; /** The current state */ private AMQState _currentState; private CopyOnWriteArraySet<StateListener> _stateListeners = new CopyOnWriteArraySet<StateListener>(); - public AMQStateManager(VirtualHostRegistry virtualHostRegistry, AMQProtocolSession protocolSession) + public AMQStateManager(Broker broker, AMQProtocolSession protocolSession) { - - _virtualHostRegistry = virtualHostRegistry; + _broker = broker; _protocolSession = protocolSession; _currentState = AMQState.CONNECTION_NOT_STARTED; } /** - * Get the ApplicationRegistry associated with this AMQStateManager - * - * returns the application registry associated with the VirtualHostRegistry of the AMQStateManager + * Get the Broker instance * - * @return the ApplicationRegistry + * @return the Broker */ - public IApplicationRegistry getApplicationRegistry() + public Broker getBroker() { - return _virtualHostRegistry.getApplicationRegistry(); + return _broker; } public AMQState getCurrentState() @@ -149,7 +145,7 @@ public class AMQStateManager implements AMQMethodListener public VirtualHostRegistry getVirtualHostRegistry() { - return _virtualHostRegistry; + return _broker.getVirtualHostRegistry(); } public AMQProtocolSession getProtocolSession() @@ -161,6 +157,6 @@ public class AMQStateManager implements AMQMethodListener public SubjectCreator getSubjectCreator() { - return getApplicationRegistry().getSubjectCreator(getProtocolSession().getLocalAddress()); + return _broker.getSubjectCreator(getProtocolSession().getLocalAddress()); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStore.java index 262d7d0213..3f1d1b9530 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStore.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStore.java @@ -30,6 +30,7 @@ import java.util.concurrent.atomic.AtomicLong; /** A simple message store that stores the messages in a thread-safe structure in memory. */ public class MemoryMessageStore extends NullMessageStore { + public static final String TYPE = "Memory"; private final AtomicLong _messageId = new AtomicLong(1); private final AtomicBoolean _closed = new AtomicBoolean(false); @@ -138,6 +139,6 @@ public class MemoryMessageStore extends NullMessageStore @Override public String getStoreType() { - return "Memory"; + return TYPE; } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStoreFactory.java new file mode 100644 index 0000000000..20b6b7a8a6 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MemoryMessageStoreFactory.java @@ -0,0 +1,39 @@ +/* + * + * 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; + + +public class MemoryMessageStoreFactory implements MessageStoreFactory +{ + + @Override + public String getType() + { + return MemoryMessageStore.TYPE; + } + + @Override + public MessageStore createMessageStore() + { + return new MemoryMessageStore(); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MessageStoreCreator.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MessageStoreCreator.java new file mode 100644 index 0000000000..0d5a4850f6 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MessageStoreCreator.java @@ -0,0 +1,66 @@ +/* + * + * 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; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.plugin.QpidServiceLoader; + +public class MessageStoreCreator +{ + private Map<String, MessageStoreFactory> _factories = new HashMap<String, MessageStoreFactory>(); + + public MessageStoreCreator() + { + QpidServiceLoader<MessageStoreFactory> qpidServiceLoader = new QpidServiceLoader<MessageStoreFactory>(); + Iterable<MessageStoreFactory> factories = qpidServiceLoader.atLeastOneInstanceOf(MessageStoreFactory.class); + for (MessageStoreFactory messageStoreFactory : factories) + { + String type = messageStoreFactory.getType(); + MessageStoreFactory factory = _factories.put(type.toLowerCase(), messageStoreFactory); + if (factory != null) + { + throw new IllegalStateException("MessageStoreFactory with type name '" + type + + "' is already registered using class '" + factory.getClass().getName() + "', can not register class '" + + messageStoreFactory.getClass().getName() + "'"); + } + } + } + + public MessageStore createMessageStore(String storeType) + { + MessageStoreFactory factory = _factories.get(storeType.toLowerCase()); + if (factory == null) + { + throw new IllegalConfigurationException("Unknown store type: " + storeType); + } + return factory.createMessageStore(); + } + + public Collection<MessageStoreFactory> getFactories() + { + return Collections.unmodifiableCollection(_factories.values()); + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/management/plugin/ManagementPlugin.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MessageStoreFactory.java index 86de4aa2f0..a1afd02f12 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/management/plugin/ManagementPlugin.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/MessageStoreFactory.java @@ -1,4 +1,5 @@ /* + * * 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 @@ -15,11 +16,13 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * */ -package org.apache.qpid.server.management.plugin; +package org.apache.qpid.server.store; -public interface ManagementPlugin +public interface MessageStoreFactory { - void start() throws Exception; - void stop() throws Exception; -}
\ No newline at end of file + String getType(); + + MessageStore createMessageStore(); +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStore.java index 2f6ad96fb1..e9946d1860 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStore.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStore.java @@ -231,7 +231,7 @@ public class DerbyMessageStore implements MessageStore private static final String DERBY_SINGLE_DB_SHUTDOWN_CODE = "08006"; - private static final String DERBY_STORE_TYPE = "DERBY"; + public static final String TYPE = "DERBY"; private final StateManager _stateManager; @@ -2333,7 +2333,7 @@ public class DerbyMessageStore implements MessageStore @Override public String getStoreType() { - return DERBY_STORE_TYPE; + return TYPE; } }
\ No newline at end of file diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStoreFactory.java new file mode 100644 index 0000000000..046b503d8a --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/store/derby/DerbyMessageStoreFactory.java @@ -0,0 +1,41 @@ +/* + * + * 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.derby; + +import org.apache.qpid.server.store.MessageStore; +import org.apache.qpid.server.store.MessageStoreFactory; + +public class DerbyMessageStoreFactory implements MessageStoreFactory +{ + + @Override + public String getType() + { + return DerbyMessageStore.TYPE; + } + + @Override + public MessageStore createMessageStore() + { + return new DerbyMessageStore(); + } + +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/QpidAcceptor.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/QpidAcceptor.java deleted file mode 100644 index 7c4188bfcd..0000000000 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/QpidAcceptor.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * - * 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.transport; - -import org.apache.qpid.server.protocol.AmqpProtocolVersion; -import org.apache.qpid.transport.network.NetworkTransport; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -public class QpidAcceptor -{ - public enum Transport - { - TCP("TCP"), - SSL("TCP/SSL"); - - private final String _asString; - - Transport(String asString) - { - _asString = asString; - } - - public String toString() - { - return _asString; - } - } - - private NetworkTransport _networkTransport; - private Transport _transport; - private Set<AmqpProtocolVersion> _supported; - - - public QpidAcceptor(NetworkTransport transport, Transport protocol, Set<AmqpProtocolVersion> supported) - { - _networkTransport = transport; - _transport = protocol; - _supported = Collections.unmodifiableSet(new HashSet<AmqpProtocolVersion>(supported)); - } - - public NetworkTransport getNetworkTransport() - { - return _networkTransport; - } - - public Transport getTransport() - { - return _transport; - } - - public Set<AmqpProtocolVersion> getSupported() - { - return _supported; - } - - public String toString() - { - return _transport.toString(); - } -} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerConnectionDelegate.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerConnectionDelegate.java index f48121f9f0..f3153fde62 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerConnectionDelegate.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerConnectionDelegate.java @@ -28,13 +28,13 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.StringTokenizer; -import java.util.UUID; import javax.security.sasl.SaslException; import javax.security.sasl.SaslServer; import org.apache.qpid.common.ServerPropertyNames; import org.apache.qpid.properties.ConnectionStartProperties; +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.AMQConnectionModel; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.security.SubjectCreator; import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus; @@ -54,35 +54,36 @@ public class ServerConnectionDelegate extends ServerDelegate { private static final Logger LOGGER = LoggerFactory.getLogger(ServerConnectionDelegate.class); + private final Broker _broker; private final String _localFQDN; - private final IApplicationRegistry _appRegistry; private int _maxNoOfChannels; private Map<String,Object> _clientProperties; private final SubjectCreator _subjectCreator; - public ServerConnectionDelegate(IApplicationRegistry appRegistry, String localFQDN, SubjectCreator subjectCreator) + public ServerConnectionDelegate(Broker broker, String localFQDN, SubjectCreator subjectCreator) { - this(createConnectionProperties(appRegistry), Collections.singletonList((Object)"en_US"), appRegistry, localFQDN, subjectCreator); + this(createConnectionProperties(broker), Collections.singletonList((Object)"en_US"), broker, localFQDN, subjectCreator); } private ServerConnectionDelegate(Map<String, Object> properties, List<Object> locales, - IApplicationRegistry appRegistry, + Broker broker, String localFQDN, SubjectCreator subjectCreator) { super(properties, parseToList(subjectCreator.getMechanisms()), locales); - _appRegistry = appRegistry; + _broker = broker; _localFQDN = localFQDN; - _maxNoOfChannels = appRegistry.getConfiguration().getMaxChannelCount(); + _maxNoOfChannels = (Integer)broker.getAttribute(Broker.SESSION_COUNT_LIMIT); _subjectCreator = subjectCreator; } - private static List<String> getFeatures(IApplicationRegistry appRegistry) + private static List<String> getFeatures(Broker broker) { + String brokerDisabledFeatures = System.getProperty(BrokerProperties.PROPERTY_DISABLED_FEATURES); final List<String> features = new ArrayList<String>(); - if (!appRegistry.getConfiguration().getDisabledFeatures().contains(ServerPropertyNames.FEATURE_QPID_JMS_SELECTOR)) + if (brokerDisabledFeatures == null || !brokerDisabledFeatures.contains(ServerPropertyNames.FEATURE_QPID_JMS_SELECTOR)) { features.add(ServerPropertyNames.FEATURE_QPID_JMS_SELECTOR); } @@ -90,12 +91,12 @@ public class ServerConnectionDelegate extends ServerDelegate return Collections.unmodifiableList(features); } - private static Map<String, Object> createConnectionProperties(final IApplicationRegistry applicationRegistry) + private static Map<String, Object> createConnectionProperties(final Broker broker) { final Map<String,Object> map = new HashMap<String,Object>(2); // Federation tag is used by the client to identify the broker instance - map.put(ServerPropertyNames.FEDERATION_TAG, applicationRegistry.getBrokerId().toString()); - final List<String> features = getFeatures(applicationRegistry); + map.put(ServerPropertyNames.FEDERATION_TAG, broker.getId().toString()); + final List<String> features = getFeatures(broker); if (features != null && features.size() > 0) { map.put(ServerPropertyNames.QPID_FEATURES, features); @@ -179,7 +180,7 @@ public class ServerConnectionDelegate extends ServerDelegate { vhostName = ""; } - vhost = _appRegistry.getVirtualHostRegistry().getVirtualHost(vhostName); + vhost = _broker.getVirtualHostRegistry().getVirtualHost(vhostName); SecurityManager.setThreadSubject(sconn.getAuthorizedSubject()); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerSessionDelegate.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerSessionDelegate.java index 2c9edb4ffe..d83bd1c4dc 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerSessionDelegate.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/transport/ServerSessionDelegate.java @@ -45,7 +45,6 @@ import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.AMQQueueFactory; import org.apache.qpid.server.queue.BaseQueue; import org.apache.qpid.server.queue.QueueRegistry; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.store.DurableConfigurationStore; import org.apache.qpid.server.store.MessageStore; @@ -1266,18 +1265,12 @@ public class ServerSessionDelegate extends SessionDelegate } } queueRegistry.registerQueue(queue); - boolean autoRegister = ApplicationRegistry.getInstance().getConfiguration().getQueueAutoRegister(); - if (autoRegister) - { - - ExchangeRegistry exchangeRegistry = getExchangeRegistry(session); + ExchangeRegistry exchangeRegistry = getExchangeRegistry(session); - Exchange defaultExchange = exchangeRegistry.getDefaultExchange(); + Exchange defaultExchange = exchangeRegistry.getDefaultExchange(); - virtualHost.getBindingFactory().addBinding(queueName, queue, defaultExchange, null); - - } + virtualHost.getBindingFactory().addBinding(queueName, queue, defaultExchange, null); if (method.hasAutoDelete() && method.getAutoDelete() 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 new file mode 100644 index 0000000000..4732900c60 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/util/MapValueConverter.java @@ -0,0 +1,335 @@ +/* + * + * 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.util; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class MapValueConverter +{ + + public static String getStringAttribute(String name, Map<String,Object> attributes, String defaultVal) + { + final Object value = attributes.get(name); + return toString(value, defaultVal); + } + + public static String toString(final Object value) + { + return toString(value, null); + } + + public static String toString(final Object value, String defaultVal) + { + if (value == null) + { + return defaultVal; + } + else if (value instanceof String) + { + return (String)value; + } + return String.valueOf(value); + } + + public static String getStringAttribute(String name, Map<String, Object> attributes) + { + assertMandatoryAttribute(name, attributes); + return getStringAttribute(name, attributes, null); + } + + private static void assertMandatoryAttribute(String name, Map<String, Object> attributes) + { + if (!attributes.containsKey(name)) + { + throw new IllegalArgumentException("Value for attribute " + name + " is not found"); + } + } + + public static Map<String,Object> getMapAttribute(String name, Map<String,Object> attributes, Map<String,Object> defaultVal) + { + final Object value = attributes.get(name); + if(value == null) + { + return defaultVal; + } + else if(value instanceof Map) + { + @SuppressWarnings("unchecked") + Map<String,Object> retVal = (Map<String,Object>) value; + return retVal; + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Map"); + } + } + + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static <E extends Enum> E getEnumAttribute(Class<E> clazz, String name, Map<String,Object> attributes, E defaultVal) + { + Object obj = attributes.get(name); + if(obj == null) + { + return defaultVal; + } + else if(clazz.isInstance(obj)) + { + return (E) obj; + } + else if(obj instanceof String) + { + return (E) Enum.valueOf(clazz, (String)obj); + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type " + clazz.getSimpleName()); + } + } + + public static <E extends Enum<?>> E getEnumAttribute(Class<E> clazz, String name, Map<String,Object> attributes) + { + assertMandatoryAttribute(name, attributes); + return getEnumAttribute(clazz, name, attributes, null); + } + + public static Boolean getBooleanAttribute(String name, Map<String,Object> attributes, Boolean defaultValue) + { + Object obj = attributes.get(name); + return toBoolean(name, obj, defaultValue); + } + + public static Boolean toBoolean(String name, Object obj) + { + return toBoolean(name, obj, null); + } + + public static Boolean toBoolean(String name, Object obj, Boolean defaultValue) + { + if(obj == null) + { + return defaultValue; + } + else if(obj instanceof Boolean) + { + return (Boolean) obj; + } + else if(obj instanceof String) + { + return Boolean.parseBoolean((String) obj); + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Boolean"); + } + } + + + public static boolean getBooleanAttribute(String name, Map<String, Object> attributes) + { + assertMandatoryAttribute(name, attributes); + return getBooleanAttribute(name, attributes, null); + } + + public static Integer getIntegerAttribute(String name, Map<String,Object> attributes, Integer defaultValue) + { + Object obj = attributes.get(name); + return toInteger(name, obj, defaultValue); + } + + public static Integer toInteger(String name, Object obj) + { + return toInteger(name, obj, null); + } + + public static Integer toInteger(String name, Object obj, Integer defaultValue) + { + if(obj == null) + { + return defaultValue; + } + else if(obj instanceof Number) + { + return ((Number) obj).intValue(); + } + else if(obj instanceof String) + { + return Integer.valueOf((String) obj); + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Integer"); + } + } + + public static Integer getIntegerAttribute(String name, Map<String,Object> attributes) + { + assertMandatoryAttribute(name, attributes); + return getIntegerAttribute(name, attributes, null); + } + + public static Long getLongAttribute(String name, Map<String,Object> attributes, Long defaultValue) + { + Object obj = attributes.get(name); + return toLong(name, obj, defaultValue); + } + + public static Long toLong(String name, Object obj) + { + return toLong(name, obj, null); + } + + public static Long toLong(String name, Object obj, Long defaultValue) + { + if(obj == null) + { + return defaultValue; + } + else if(obj instanceof Number) + { + return ((Number) obj).longValue(); + } + else if(obj instanceof String) + { + return Long.valueOf((String) obj); + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Long"); + } + } + + public static <T> Set<T> getSetAttribute(String name, Map<String,Object> attributes) + { + assertMandatoryAttribute(name, attributes); + return getSetAttribute(name, attributes, Collections.<T>emptySet()); + } + + @SuppressWarnings("unchecked") + public static <T> Set<T> getSetAttribute(String name, Map<String,Object> attributes, Set<T> defaultValue) + { + Object obj = attributes.get(name); + if(obj == null) + { + return defaultValue; + } + else if(obj instanceof Set) + { + return (Set<T>) obj; + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + " is not of required type Set"); + } + } + + @SuppressWarnings("unchecked") + public static <T extends Enum<T>> Set<T> getEnumSetAttribute(String name, Map<String, Object> attributes, Class<T> clazz) + { + Object obj = attributes.get(name); + Object[] items = null; + if (obj == null) + { + return null; + } + else if (obj instanceof Collection) + { + Collection<?> data = (Collection<?>) obj; + items = data.toArray(new Object[data.size()]); + } + else if (obj instanceof String[]) + { + items = (String[]) obj; + } + else if (obj instanceof Object[]) + { + items = (Object[]) obj; + } + else + { + throw new IllegalArgumentException("Value for attribute " + name + "[" + obj + + "] cannot be converted into set of enum of " + clazz); + } + Set<T> set = new HashSet<T>(); + for (int i = 0; i < items.length; i++) + { + T item = null; + Object value = items[i]; + if (value instanceof String) + { + item = (T) Enum.valueOf(clazz, (String) value); + } + else if (clazz.isInstance(value)) + { + item = (T) value; + } + else + { + throw new IllegalArgumentException("Cannot convert " + value + " from [" + obj + "] into enum of " + clazz + + " for attribute " + name); + } + set.add(item); + } + return set; + } + + public static Map<String, Object> convert(Map<String, Object> configurationAttributes, Map<String, Class<?>> attributeTypes) + { + Map<String, Object> attributes = new HashMap<String, Object>(); + for (Map.Entry<String, Class<?>> attributeEntry : attributeTypes.entrySet()) + { + String attributeName = attributeEntry.getKey(); + if (configurationAttributes.containsKey(attributeName)) + { + Class<?> classObject = attributeEntry.getValue(); + Object rawValue = configurationAttributes.get(attributeName); + Object value = null; + if (classObject == Long.class || classObject == long.class) + { + value = toLong(attributeName, rawValue); + } + else if (classObject == Integer.class || classObject == int.class) + { + value = toInteger(attributeName, rawValue); + } + else if (classObject == Boolean.class || classObject == boolean.class) + { + value = toBoolean(attributeName, rawValue); + } + else if (classObject == String.class) + { + value = toString(rawValue); + } + else + { + throw new IllegalArgumentException("Cannot convert '" + rawValue + "' into " + classObject); + } + attributes.put(attributeName, value); + } + } + return attributes; + } +} diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHost.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHost.java index ff39e7ae0d..d24f79c56c 100755 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHost.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHost.java @@ -58,8 +58,6 @@ public interface VirtualHost extends DurableConfigurationStore.Source, Closeable void close(); - UUID getBrokerId(); - UUID getId(); void scheduleHouseKeepingTask(long period, HouseKeepingTask task); @@ -74,7 +72,7 @@ public interface VirtualHost extends DurableConfigurationStore.Source, Closeable int getHouseKeepingActiveCount(); - IApplicationRegistry getApplicationRegistry(); + VirtualHostRegistry getVirtualHostRegistry(); BindingFactory getBindingFactory(); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostImpl.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostImpl.java index caff2b11d2..dd3610373f 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostImpl.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostImpl.java @@ -55,13 +55,14 @@ import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.AMQQueueFactory; import org.apache.qpid.server.queue.DefaultQueueRegistry; import org.apache.qpid.server.queue.QueueRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.stats.StatisticsCounter; +import org.apache.qpid.server.stats.StatisticsGatherer; import org.apache.qpid.server.store.Event; import org.apache.qpid.server.store.EventListener; import org.apache.qpid.server.store.HAMessageStore; import org.apache.qpid.server.store.MessageStore; +import org.apache.qpid.server.store.MessageStoreCreator; import org.apache.qpid.server.store.OperationalLoggingListener; import org.apache.qpid.server.txn.DtxRegistry; @@ -79,7 +80,9 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr private final ScheduledThreadPoolExecutor _houseKeepingTasks; - private final IApplicationRegistry _appRegistry; + private final VirtualHostRegistry _virtualHostRegistry; + + private final StatisticsGatherer _brokerStatisticsGatherer; private final SecurityManager _securityManager; @@ -106,7 +109,7 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr private final Map<String, LinkRegistry> _linkRegistry = new HashMap<String, LinkRegistry>(); private boolean _blocked; - public VirtualHostImpl(IApplicationRegistry appRegistry, VirtualHostConfiguration hostConfig) throws Exception + public VirtualHostImpl(VirtualHostRegistry virtualHostRegistry, StatisticsGatherer brokerStatisticsGatherer, SecurityManager parentSecurityManager, VirtualHostConfiguration hostConfig) throws Exception { if (hostConfig == null) { @@ -118,7 +121,8 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr throw new IllegalArgumentException("Illegal name (" + hostConfig.getName() + ") for virtualhost."); } - _appRegistry = appRegistry; + _virtualHostRegistry = virtualHostRegistry; + _brokerStatisticsGatherer = brokerStatisticsGatherer; _vhostConfig = hostConfig; _name = _vhostConfig.getName(); _dtxRegistry = new DtxRegistry(); @@ -127,7 +131,7 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr CurrentActor.get().message(VirtualHostMessages.CREATED(_name)); - _securityManager = new SecurityManager(_appRegistry.getSecurityManager(), _vhostConfig.getConfig()); + _securityManager = new SecurityManager(parentSecurityManager, _vhostConfig.getConfig().getString("security.acl")); _connectionRegistry = new ConnectionRegistry(); _connectionRegistry.addRegistryChangeListener(this); @@ -142,7 +146,7 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr _bindingFactory = new BindingFactory(this); - _messageStore = initialiseMessageStore(hostConfig.getMessageStoreClass()); + _messageStore = initialiseMessageStore(hostConfig); configureMessageStore(hostConfig); @@ -266,19 +270,34 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr if (!(o instanceof MessageStore)) { - throw new ClassCastException("Message store factory class must implement " + MessageStore.class + + throw new ClassCastException("Message store class must implement " + MessageStore.class + ". Class " + clazz + " does not."); } final MessageStore messageStore = (MessageStore) o; - final MessageStoreLogSubject storeLogSubject = new MessageStoreLogSubject(this, clazz.getSimpleName()); + return messageStore; + } + + private MessageStore initialiseMessageStore(VirtualHostConfiguration hostConfig) throws Exception + { + String storeType = hostConfig.getConfig().getString("store.type"); + MessageStore messageStore = null; + if (storeType == null) + { + messageStore = initialiseMessageStore(hostConfig.getMessageStoreClass()); + } + else + { + messageStore = new MessageStoreCreator().createMessageStore(storeType); + } + + final MessageStoreLogSubject storeLogSubject = new MessageStoreLogSubject(this, messageStore.getClass().getSimpleName()); OperationalLoggingListener.listen(messageStore, storeLogSubject); messageStore.addEventListener(new BeforeActivationListener(), Event.BEFORE_ACTIVATE); messageStore.addEventListener(new AfterActivationListener(), Event.AFTER_ACTIVATE); messageStore.addEventListener(new BeforeCloseListener(), Event.BEFORE_CLOSE); messageStore.addEventListener(new BeforePassivationListener(), Event.BEFORE_PASSIVATE); - return messageStore; } @@ -461,14 +480,9 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr CurrentActor.get().message(VirtualHostMessages.CLOSED()); } - public UUID getBrokerId() - { - return _appRegistry.getBrokerId(); - } - - public IApplicationRegistry getApplicationRegistry() + public VirtualHostRegistry getVirtualHostRegistry() { - return _appRegistry; + return _virtualHostRegistry; } public BindingFactory getBindingFactory() @@ -480,14 +494,14 @@ public class VirtualHostImpl implements VirtualHost, IConnectionRegistry.Registr { _messagesDelivered.registerEvent(1L); _dataDelivered.registerEvent(messageSize); - _appRegistry.registerMessageDelivered(messageSize); + _brokerStatisticsGatherer.registerMessageDelivered(messageSize); } public void registerMessageReceived(long messageSize, long timestamp) { _messagesReceived.registerEvent(1L, timestamp); _dataReceived.registerEvent(messageSize, timestamp); - _appRegistry.registerMessageReceived(messageSize, timestamp); + _brokerStatisticsGatherer.registerMessageReceived(messageSize, timestamp); } public StatisticsCounter getMessageReceiptStatistics() diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostRegistry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostRegistry.java index 9f26c00841..483e11942b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostRegistry.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/VirtualHostRegistry.java @@ -21,12 +21,9 @@ package org.apache.qpid.server.virtualhost; import org.apache.qpid.common.Closeable; -import org.apache.qpid.server.exchange.ExchangeRegistry; -import org.apache.qpid.server.registry.ApplicationRegistry; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -34,44 +31,26 @@ import java.util.concurrent.ConcurrentHashMap; public class VirtualHostRegistry implements Closeable { private final Map<String, VirtualHost> _registry = new ConcurrentHashMap<String, VirtualHost>(); - - private String _defaultVirtualHostName; - private ApplicationRegistry _applicationRegistry; - private final Collection<RegistryChangeListener> _listeners = - Collections.synchronizedCollection(new ArrayList<RegistryChangeListener>()); - public VirtualHostRegistry(ApplicationRegistry applicationRegistry) + + public VirtualHostRegistry() { - _applicationRegistry = applicationRegistry; + super(); } - public synchronized void registerVirtualHost(VirtualHost host) throws Exception + public synchronized void registerVirtualHost(VirtualHost host) { if(_registry.containsKey(host.getName())) { - throw new Exception("Virtual Host with name " + host.getName() + " already registered."); + throw new IllegalArgumentException("Virtual Host with name " + host.getName() + " already registered."); } _registry.put(host.getName(),host); - synchronized (_listeners) - { - for(RegistryChangeListener listener : _listeners) - { - listener.virtualHostRegistered(host); - } - } } - + public synchronized void unregisterVirtualHost(VirtualHost host) { _registry.remove(host.getName()); - synchronized (_listeners) - { - for(RegistryChangeListener listener : _listeners) - { - listener.virtualHostUnregistered(host); - } - } } public VirtualHost getVirtualHost(String name) @@ -105,30 +84,12 @@ public class VirtualHostRegistry implements Closeable return new ArrayList<VirtualHost>(_registry.values()); } - public ApplicationRegistry getApplicationRegistry() - { - return _applicationRegistry; - } - public void close() { for (VirtualHost virtualHost : getVirtualHosts()) { virtualHost.close(); } - - } - - public static interface RegistryChangeListener - { - void virtualHostRegistered(VirtualHost virtualHost); - void virtualHostUnregistered(VirtualHost virtualHost); - - } - - public void addRegistryChangeListener(RegistryChangeListener listener) - { - _listeners.add(listener); } } diff --git a/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.configuration.ConfigurationStoreFactory b/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.configuration.ConfigurationStoreFactory new file mode 100644 index 0000000000..5f75a8c4c9 --- /dev/null +++ b/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.configuration.ConfigurationStoreFactory @@ -0,0 +1,19 @@ +# +# 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. +# +org.apache.qpid.server.configuration.store.factory.JsonConfigurationStoreFactory diff --git a/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.plugin.AuthenticationManagerFactory b/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.plugin.AuthenticationManagerFactory index ee41fe966d..8ff67030ef 100644 --- a/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.plugin.AuthenticationManagerFactory +++ b/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.plugin.AuthenticationManagerFactory @@ -17,7 +17,8 @@ # under the License. # org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManagerFactory +org.apache.qpid.server.security.auth.manager.Base64MD5PasswordFileAuthenticationManagerFactory org.apache.qpid.server.security.auth.manager.ExternalAuthenticationManagerFactory org.apache.qpid.server.security.auth.manager.KerberosAuthenticationManagerFactory -org.apache.qpid.server.security.auth.manager.PrincipalDatabaseAuthManagerFactory +org.apache.qpid.server.security.auth.manager.PlainPasswordFileAuthenticationManagerFactory org.apache.qpid.server.security.auth.manager.SimpleLDAPAuthenticationManagerFactory diff --git a/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.store.MessageStoreFactory b/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.store.MessageStoreFactory new file mode 100644 index 0000000000..1357f816b7 --- /dev/null +++ b/qpid/java/broker/src/main/resources/META-INF/services/org.apache.qpid.server.store.MessageStoreFactory @@ -0,0 +1,20 @@ +# +# 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. +# +org.apache.qpid.server.store.derby.DerbyMessageStoreFactory +org.apache.qpid.server.store.MemoryMessageStoreFactory
\ No newline at end of file diff --git a/qpid/java/broker/src/main/resources/initial-store.json b/qpid/java/broker/src/main/resources/initial-store.json new file mode 100644 index 0000000000..a80ad95bd4 --- /dev/null +++ b/qpid/java/broker/src/main/resources/initial-store.json @@ -0,0 +1,58 @@ +/* + * + * 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. + * + */ +{ + "name": "QpidBroker", + "defaultAuthenticationProvider" : "defaultAuthenticationProvider", + "defaultVirtualHost" : "default", + "authenticationproviders" : [ { + "name" : "defaultAuthenticationProvider", + "authenticationProviderType" : "PlainPasswordFileAuthenticationProvider", + "path" : "${QPID_HOME}/etc/passwd" + } ], + "ports" : [ { + "name" : "5672-AMQP", + "port" : 5672 + }, { + "name" : "8080-HTTP", + "port" : 8080, + "protocols" : [ "HTTP" ] + }, { + "name" : "8999-RMI", + "port" : 8999, + "protocols" : [ "RMI" ] + }, { + "name" : "9099-JMX_RMI", + "port" : 9099, + "protocols" : [ "JMX_RMI" ] + }], + "virtualhosts" : [ { + "name" : "default", + "storeType" : "DERBY", + "storePath" : "${QPID_WORK}/store" + } ], + "plugins" : [ { + "pluginType" : "MANAGEMENT-HTTP", + "name" : "httpManagement" + }, { + "pluginType" : "MANAGEMENT-JMX", + "name" : "jmxManagement" + } ] +}
\ No newline at end of file diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/AMQChannelTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/AMQChannelTest.java index fc6cbcb248..e10bdbbb35 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/AMQChannelTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/AMQChannelTest.java @@ -20,23 +20,69 @@ */ package org.apache.qpid.server; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.qpid.AMQException; +import org.apache.qpid.framing.AMQShortString; +import org.apache.qpid.framing.BasicContentHeaderProperties; +import org.apache.qpid.framing.ContentHeaderBody; +import org.apache.qpid.framing.abstraction.MessagePublishInfo; +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.exchange.Exchange; +import org.apache.qpid.server.message.MessageContentSource; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.protocol.AMQProtocolSession; import org.apache.qpid.server.protocol.InternalTestProtocolSession; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; -public class AMQChannelTest extends InternalBrokerBaseCase +public class AMQChannelTest extends QpidTestCase { private VirtualHost _virtualHost; private AMQProtocolSession _protocolSession; + private Map<Integer,String> _replies; + private Broker _broker; @Override public void setUp() throws Exception { super.setUp(); - _virtualHost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHosts().iterator().next(); - _protocolSession = new InternalTestProtocolSession(_virtualHost); + BrokerTestHelper.setUp(); + _virtualHost = BrokerTestHelper.createVirtualHost(getTestName()); + _broker = BrokerTestHelper.createBrokerMock(); + _protocolSession = new InternalTestProtocolSession(_virtualHost, _broker) + { + @Override + public void writeReturn(MessagePublishInfo messagePublishInfo, + ContentHeaderBody header, + MessageContentSource msgContent, + int channelId, + int replyCode, + AMQShortString replyText) throws AMQException + { + _replies.put(replyCode, replyText.asString()); + } + }; + _replies = new HashMap<Integer, String>(); + } + + @Override + public void tearDown() throws Exception + { + try + { + _virtualHost.close(); + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } } public void testCompareTo() throws Exception @@ -44,9 +90,54 @@ public class AMQChannelTest extends InternalBrokerBaseCase AMQChannel channel1 = new AMQChannel(_protocolSession, 1, _virtualHost.getMessageStore()); // create a channel with the same channelId but on a different session - AMQChannel channel2 = new AMQChannel(new InternalTestProtocolSession(_virtualHost), 1, _virtualHost.getMessageStore()); + AMQChannel channel2 = new AMQChannel(new InternalTestProtocolSession(_virtualHost, _broker), 1, _virtualHost.getMessageStore()); assertFalse("Unexpected compare result", channel1.compareTo(channel2) == 0); assertEquals("Unexpected compare result", 0, channel1.compareTo(channel1)); } + public void testPublishContentHeaderWhenMessageAuthorizationFails() throws Exception + { + setTestSystemProperty(BrokerProperties.PROPERTY_MSG_AUTH, "true"); + AMQChannel channel = new AMQChannel(_protocolSession, 1, _virtualHost.getMessageStore()); + channel.setLocalTransactional(); + + MessagePublishInfo info = mock(MessagePublishInfo.class); + Exchange e = mock(Exchange.class); + ContentHeaderBody contentHeaderBody= mock(ContentHeaderBody.class); + BasicContentHeaderProperties properties = mock(BasicContentHeaderProperties.class); + + when(contentHeaderBody.getProperties()).thenReturn(properties); + when(info.getExchange()).thenReturn(new AMQShortString("test")); + when(properties.getUserId()).thenReturn(new AMQShortString(_protocolSession.getAuthorizedPrincipal().getName() + "_incorrect")); + + channel.setPublishFrame(info, e); + channel.publishContentHeader(contentHeaderBody); + channel.commit(); + + assertEquals("Unexpected number of replies", 1, _replies.size()); + assertEquals("Message authorization passed", "Access Refused", _replies.get(403)); + } + + public void testPublishContentHeaderWhenMessageAuthorizationPasses() throws Exception + { + setTestSystemProperty(BrokerProperties.PROPERTY_MSG_AUTH, "true"); + AMQChannel channel = new AMQChannel(_protocolSession, 1, _virtualHost.getMessageStore()); + channel.setLocalTransactional(); + + MessagePublishInfo info = mock(MessagePublishInfo.class); + Exchange e = mock(Exchange.class); + ContentHeaderBody contentHeaderBody= mock(ContentHeaderBody.class); + BasicContentHeaderProperties properties = mock(BasicContentHeaderProperties.class); + + when(contentHeaderBody.getProperties()).thenReturn(properties); + when(info.getExchange()).thenReturn(new AMQShortString("test")); + when(properties.getUserId()).thenReturn(new AMQShortString(_protocolSession.getAuthorizedPrincipal().getName())); + + channel.setPublishFrame(info, e); + channel.publishContentHeader(contentHeaderBody); + channel.commit(); + + assertEquals("Unexpected number of replies", 0, _replies.size()); + } + } 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 43824e713f..9b7e3794d0 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 @@ -22,91 +22,36 @@ package org.apache.qpid.server; import org.apache.qpid.test.utils.QpidTestCase; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - - public class BrokerOptionsTest extends QpidTestCase { private BrokerOptions _options; - - private static final int TEST_PORT1 = 6789; - private static final int TEST_PORT2 = 6790; - protected void setUp() { _options = new BrokerOptions(); } - - public void testDefaultPort() - { - assertEquals(Collections.<Integer>emptySet(), _options.getPorts()); - } - - public void testOverriddenPort() - { - _options.addPort(TEST_PORT1); - assertEquals(Collections.singleton(TEST_PORT1), _options.getPorts()); - } - - public void testManyOverriddenPorts() - { - _options.addPort(TEST_PORT1); - _options.addPort(TEST_PORT2); - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getPorts()); - } - - public void testDuplicateOverriddenPortsAreSilentlyIgnored() - { - _options.addPort(TEST_PORT1); - _options.addPort(TEST_PORT2); - _options.addPort(TEST_PORT1); // duplicate - should be silently ignored - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getPorts()); - } - - public void testDefaultSSLPort() - { - assertEquals(Collections.<Integer>emptySet(), _options.getSSLPorts()); - } - public void testOverriddenSSLPort() + public void testDefaultConfigurationStoreType() { - _options.addSSLPort(TEST_PORT1); - assertEquals(Collections.singleton(TEST_PORT1), _options.getSSLPorts()); + assertEquals("json", _options.getConfigurationStoreType()); } - public void testManyOverriddenSSLPorts() + public void testOverriddenConfigurationStoreType() { - _options.addSSLPort(TEST_PORT1); - _options.addSSLPort(TEST_PORT2); - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getSSLPorts()); + _options.setConfigurationStoreType("dby"); + assertEquals("dby", _options.getConfigurationStoreType()); } - public void testDuplicateOverriddenSSLPortsAreSilentlyIgnored() + public void testDefaultConfigurationStoreLocation() { - _options.addSSLPort(TEST_PORT1); - _options.addSSLPort(TEST_PORT2); - _options.addSSLPort(TEST_PORT1); // duplicate - should be silently ignored - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getSSLPorts()); + assertNull(_options.getConfigurationStoreLocation()); } - public void testDefaultConfigFile() - { - assertNull(_options.getConfigFile()); - } - - public void testOverriddenConfigFile() + public void testOverriddenConfigurationStoreLocation() { final String testConfigFile = "etc/mytestconfig.xml"; - _options.setConfigFile(testConfigFile); - assertEquals(testConfigFile, _options.getConfigFile()); + _options.setConfigurationStoreLocation(testConfigFile); + assertEquals(testConfigFile, _options.getConfigurationStoreLocation()); } public void testDefaultLogConfigFile() @@ -121,72 +66,6 @@ public class BrokerOptionsTest extends QpidTestCase assertEquals(testLogConfigFile, _options.getLogConfigFile()); } - public void testDefaultJmxPortRegistryServer() - { - assertNull(_options.getJmxPortRegistryServer()); - } - - public void testJmxPortRegistryServer() - { - _options.setJmxPortRegistryServer(TEST_PORT1); - assertEquals(Integer.valueOf(TEST_PORT1), _options.getJmxPortRegistryServer()); - } - - public void testDefaultJmxPortConnectorServer() - { - assertNull(_options.getJmxPortConnectorServer()); - } - - public void testJmxPortConnectorServer() - { - _options.setJmxPortConnectorServer(TEST_PORT1); - assertEquals(Integer.valueOf(TEST_PORT1), _options.getJmxPortConnectorServer()); - } - - public void testQpidHomeExposesSysProperty() - { - assertEquals(System.getProperty("QPID_HOME"), _options.getQpidHome()); - } - - public void testDefaultExcludesPortFor0_10() - { - assertEquals(Collections.EMPTY_SET, _options.getExcludedPorts(ProtocolExclusion.v0_10)); - } - - public void testOverriddenExcludesPortFor0_10() - { - _options.addExcludedPort(ProtocolExclusion.v0_10, TEST_PORT1); - assertEquals(Collections.singleton(TEST_PORT1), _options.getExcludedPorts(ProtocolExclusion.v0_10)); - } - - public void testManyOverriddenExcludedPortFor0_10() - { - _options.addExcludedPort(ProtocolExclusion.v0_10, TEST_PORT1); - _options.addExcludedPort(ProtocolExclusion.v0_10, TEST_PORT2); - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getExcludedPorts(ProtocolExclusion.v0_10)); - } - - public void testDuplicatedOverriddenExcludedPortFor0_10AreSilentlyIgnored() - { - _options.addExcludedPort(ProtocolExclusion.v0_10, TEST_PORT1); - _options.addExcludedPort(ProtocolExclusion.v0_10, TEST_PORT2); - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getExcludedPorts(ProtocolExclusion.v0_10)); - } - - public void testDefaultBind() - { - assertNull(_options.getBind()); - } - - public void testOverriddenBind() - { - final String bind = "192.168.0.1"; - _options.setBind(bind); - assertEquals(bind, _options.getBind()); - } - public void testDefaultLogWatchFrequency() { assertEquals(0L, _options.getLogWatchFrequency()); @@ -199,31 +78,4 @@ public class BrokerOptionsTest extends QpidTestCase _options.setLogWatchFrequency(myFreq); assertEquals(myFreq, _options.getLogWatchFrequency()); } - - public void testDefaultIncludesPortFor0_10() - { - assertEquals(Collections.EMPTY_SET, _options.getIncludedPorts(ProtocolInclusion.v0_10)); - } - - public void testOverriddenIncludesPortFor0_10() - { - _options.addIncludedPort(ProtocolInclusion.v0_10, TEST_PORT1); - assertEquals(Collections.singleton(TEST_PORT1), _options.getIncludedPorts(ProtocolInclusion.v0_10)); - } - - public void testManyOverriddenIncludedPortFor0_10() - { - _options.addIncludedPort(ProtocolInclusion.v0_10, TEST_PORT1); - _options.addIncludedPort(ProtocolInclusion.v0_10, TEST_PORT2); - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getIncludedPorts(ProtocolInclusion.v0_10)); - } - - public void testDuplicatedOverriddenIncludedPortFor0_10AreSilentlyIgnored() - { - _options.addIncludedPort(ProtocolInclusion.v0_10, TEST_PORT1); - _options.addIncludedPort(ProtocolInclusion.v0_10, TEST_PORT2); - final Set<Integer> expectedPorts = new HashSet<Integer>(Arrays.asList(new Integer[] {TEST_PORT1, TEST_PORT2})); - assertEquals(expectedPorts, _options.getIncludedPorts(ProtocolInclusion.v0_10)); - } } 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 ffd607574e..b0533080a5 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 @@ -23,8 +23,6 @@ package org.apache.qpid.server; import org.apache.commons.cli.CommandLine; import org.apache.qpid.test.utils.QpidTestCase; -import java.util.EnumSet; - /** * Test to verify the command line parsing within the Main class, by * providing it a series of command line arguments and verifying the @@ -36,105 +34,24 @@ public class MainTest extends QpidTestCase { BrokerOptions options = startDummyMain(""); - assertTrue(options.getPorts().isEmpty()); - assertTrue(options.getSSLPorts().isEmpty()); - assertEquals(null, options.getJmxPortRegistryServer()); - assertEquals(null, options.getConfigFile()); + assertEquals("json", options.getConfigurationStoreType()); + assertEquals(null, options.getConfigurationStoreLocation()); assertEquals(null, options.getLogConfigFile()); - assertEquals(null, options.getBind()); - - for(ProtocolExclusion pe : EnumSet.allOf(ProtocolExclusion.class)) - { - assertEquals(0, options.getExcludedPorts(pe).size()); - } - - for(ProtocolInclusion pe : EnumSet.allOf(ProtocolInclusion.class)) - { - assertEquals(0, options.getIncludedPorts(pe).size()); - } - } - - public void testPortOverriddenSingle() - { - BrokerOptions options = startDummyMain("-p 1234"); - - assertTrue(options.getPorts().contains(1234)); - assertEquals(1, options.getPorts().size()); - assertTrue(options.getSSLPorts().isEmpty()); - } - - public void testPortOverriddenMultiple() - { - BrokerOptions options = startDummyMain("-p 1234 -p 4321"); - - assertTrue(options.getPorts().contains(1234)); - assertTrue(options.getPorts().contains(4321)); - assertEquals(2, options.getPorts().size()); - assertTrue(options.getSSLPorts().isEmpty()); + assertEquals(0, options.getLogWatchFrequency()); } - public void testSSLPortOverriddenSingle() + public void testConfigurationStoreLocation() { - BrokerOptions options = startDummyMain("-s 5678"); + BrokerOptions options = startDummyMain("-sp abcd/config.xml"); - assertTrue(options.getSSLPorts().contains(5678)); - assertEquals(1, options.getSSLPorts().size()); - assertTrue(options.getPorts().isEmpty()); + assertEquals("abcd/config.xml", options.getConfigurationStoreLocation()); } - public void testSSLPortOverriddenMultiple() + public void testConfigurationStoreType() { - BrokerOptions options = startDummyMain("-s 5678 -s 8765"); + BrokerOptions options = startDummyMain("-st dby"); - assertTrue(options.getSSLPorts().contains(5678)); - assertTrue(options.getSSLPorts().contains(8765)); - assertEquals(2, options.getSSLPorts().size()); - assertTrue(options.getPorts().isEmpty()); - } - - public void testNonSSLandSSLPortsOverridden() - { - BrokerOptions options = startDummyMain("-p 5678 -s 8765"); - - assertTrue(options.getPorts().contains(5678)); - assertTrue(options.getSSLPorts().contains(8765)); - assertEquals(1, options.getPorts().size()); - assertEquals(1, options.getSSLPorts().size()); - } - - public void testJmxPortRegistryServerOverridden() - { - BrokerOptions options = startDummyMain("--jmxregistryport 3456"); - - assertEquals(Integer.valueOf(3456), options.getJmxPortRegistryServer()); - - options = startDummyMain("-m 3457"); - assertEquals(Integer.valueOf(3457), options.getJmxPortRegistryServer()); - } - - public void testJmxPortConnectorServerOverridden() - { - BrokerOptions options = startDummyMain("--jmxconnectorport 3456"); - - assertEquals(Integer.valueOf(3456), options.getJmxPortConnectorServer()); - } - - public void testExclude0_10() - { - BrokerOptions options = startDummyMain("-p 3456 --exclude-0-10 3456"); - - assertTrue(options.getPorts().contains(3456)); - assertEquals(1, options.getPorts().size()); - assertTrue(options.getExcludedPorts(ProtocolExclusion.v0_10).contains(3456)); - assertEquals(1, options.getExcludedPorts(ProtocolExclusion.v0_10).size()); - assertEquals(0, options.getExcludedPorts(ProtocolExclusion.v0_9_1).size()); - } - - public void testConfig() - { - BrokerOptions options = startDummyMain("-c abcd/config.xml"); - - assertEquals("abcd/config.xml", options.getConfigFile()); + assertEquals("dby", options.getConfigurationStoreType()); } public void testLogConfig() @@ -167,20 +84,6 @@ public class MainTest extends QpidTestCase assertTrue("Parsed command line didnt pick up help option", main.getCommandLine().hasOption("h")); } - public void testInclude010() - { - BrokerOptions options = startDummyMain("-p 5678 --include-0-10 5678"); - - assertTrue(options.getPorts().contains(5678)); - assertEquals(1, options.getPorts().size()); - assertTrue(options.getIncludedPorts(ProtocolInclusion.v0_10).contains(5678)); - assertEquals(1, options.getIncludedPorts(ProtocolInclusion.v0_10).size()); - assertEquals(0, options.getIncludedPorts(ProtocolInclusion.v0_9_1).size()); - assertEquals(0, options.getIncludedPorts(ProtocolInclusion.v0_9).size()); - assertEquals(0, options.getIncludedPorts(ProtocolInclusion.v0_8).size()); - assertEquals(0, options.getIncludedPorts(ProtocolInclusion.v1_0).size()); - } - 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/ack/AcknowledgeTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/ack/AcknowledgeTest.java index b3223f16c4..4d6d60906d 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/ack/AcknowledgeTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/ack/AcknowledgeTest.java @@ -22,14 +22,72 @@ package org.apache.qpid.server.ack; import org.apache.qpid.AMQException; +import org.apache.qpid.exchange.ExchangeDefaults; import org.apache.qpid.framing.AMQShortString; +import org.apache.qpid.server.AMQChannel; +import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.protocol.InternalTestProtocolSession; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.queue.SimpleAMQQueue; +import org.apache.qpid.server.store.MessageStore; +import org.apache.qpid.server.store.TestableMemoryMessageStore; +import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.List; -public class AcknowledgeTest extends InternalBrokerBaseCase +public class AcknowledgeTest extends QpidTestCase { + private AMQChannel _channel; + private SimpleAMQQueue _queue; + private MessageStore _messageStore; + private String _queueName; + + @Override + public void setUp() throws Exception + { + super.setUp(); + BrokerTestHelper.setUp(); + _channel = BrokerTestHelper.createChannel(); + VirtualHost virtualHost = _channel.getVirtualHost(); + _queueName = getTestName(); + _queue = BrokerTestHelper.createQueue(_queueName, virtualHost); + _messageStore = virtualHost.getMessageStore(); + Exchange defaultExchange = virtualHost.getExchangeRegistry().getDefaultExchange(); + virtualHost.getBindingFactory().addBinding(_queueName, _queue, defaultExchange, null); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_channel != null) + { + _channel.getVirtualHost().close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + } + + private AMQChannel getChannel() + { + return _channel; + } + + private InternalTestProtocolSession getSession() + { + return (InternalTestProtocolSession)_channel.getProtocolSession(); + } + + private SimpleAMQQueue getQueue() + { + return _queue; + } public void testTransactionalSingleAck() throws AMQException { @@ -70,7 +128,7 @@ public class AcknowledgeTest extends InternalBrokerBaseCase checkStoreContents(0); //Send required messsages to the queue - publishMessages(getSession(), getChannel(), sendMessageCount); + BrokerTestHelper.publishMessages(getChannel(), sendMessageCount, _queueName, ExchangeDefaults.DEFAULT_EXCHANGE_NAME.asString()); if (getChannel().isTransactional()) { @@ -84,7 +142,7 @@ public class AcknowledgeTest extends InternalBrokerBaseCase assertEquals("Channel should have no unacked msgs ", 0, getChannel().getUnacknowledgedMessageMap().size()); //Subscribe to the queue - AMQShortString subscriber = subscribe(getSession(), getChannel(), getQueue()); + AMQShortString subscriber = _channel.subscribeToQueue(null, _queue, true, null, false, true); getQueue().deliverAsync(); @@ -117,4 +175,9 @@ public class AcknowledgeTest extends InternalBrokerBaseCase checkStoreContents(remainingUnackedMessages); } + private void checkStoreContents(int messageCount) + { + assertEquals("Message header count incorrect in the MetaDataMap", messageCount, ((TestableMemoryMessageStore) _messageStore).getMessageCount()); + } + } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java new file mode 100644 index 0000000000..d9bdd444aa --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java @@ -0,0 +1,107 @@ +/* + * + * 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; + +import java.io.File; +import java.util.Set; +import java.util.UUID; + +import org.apache.qpid.server.BrokerOptions; +import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.util.FileUtils; + +public class BrokerConfigurationStoreCreatorTest extends QpidTestCase +{ + private File _userStoreLocation; + private BrokerConfigurationStoreCreator _storeCreator; + private BrokerOptions _options; + + public void setUp() throws Exception + { + super.setUp(); + + // check whether QPID_HOME JVM system property is set + if (QPID_HOME == null) + { + // set the properties in order to resolve the defaults store settings + setTestSystemProperty("QPID_HOME", TMP_FOLDER); + } + _storeCreator = new BrokerConfigurationStoreCreator(); + _userStoreLocation = new File(TMP_FOLDER, "_store_" + System.currentTimeMillis() + "_" + getTestName()); + _options = new BrokerOptions(); + } + + public void tearDown() throws Exception + { + try + { + super.tearDown(); + } + finally + { + if (_userStoreLocation != null) + { + FileUtils.delete(_userStoreLocation, true); + } + } + } + + public void testCreateJsonStore() + { + ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", _options); + assertNotNull("Store was not created", store); + assertTrue("File should exists", _userStoreLocation.exists()); + assertTrue("File size should be greater than 0", _userStoreLocation.length() > 0); + JsonConfigurationEntryStore jsonStore = new JsonConfigurationEntryStore(); + jsonStore.open(_userStoreLocation.getAbsolutePath()); + Set<UUID> childrenIds = jsonStore.getRootEntry().getChildrenIds(); + assertFalse("Unexpected children: " + childrenIds, childrenIds.isEmpty()); + } + + public void testCreateDerbyStore() + { + //TODO: Implement DERBY store + try + { + _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "derby", _options); + fail("Store is not yet supported"); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + + public void testCreateXmlStore() throws Exception + { + try + { + _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "xml", _options); + fail("Store is not yet supported"); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerPropertiesTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerPropertiesTest.java new file mode 100644 index 0000000000..5e9e19ffaf --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerPropertiesTest.java @@ -0,0 +1,51 @@ +/* + * + * 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; + +import java.util.Locale; + +import org.apache.qpid.test.utils.QpidTestCase; + +public class BrokerPropertiesTest extends QpidTestCase +{ + public void testGetLocaleDefault() + { + Locale locale = BrokerProperties.getLocale(); + assertEquals("Unexpected locale", Locale.US, locale); + } + + public void testGetLocaleSetWithJVMProperty() + { + setTestSystemProperty(BrokerProperties.PROPERTY_LOCALE, "en_GB"); + Locale locale = BrokerProperties.getLocale(); + assertEquals("Unexpected locale", Locale.UK, locale); + } + + public void testGetLocaleSetWithJVMPropertyInUnexpectedFormat() + { + setTestSystemProperty(BrokerProperties.PROPERTY_LOCALE, "penguins_ANTARCTIC_Moubray_Bay"); + Locale locale = BrokerProperties.getLocale(); + assertEquals("Unexpected locale language", "penguins", locale.getLanguage()); + assertEquals("Unexpected locale country", "ANTARCTIC", locale.getCountry()); + assertEquals("Unexpected locale country", "Moubray_Bay", locale.getVariant()); + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/QueueConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/QueueConfigurationTest.java index 3c5b85cd90..0bb65479ce 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/QueueConfigurationTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/QueueConfigurationTest.java @@ -20,25 +20,31 @@ */ package org.apache.qpid.server.configuration; +import static org.mockito.Mockito.when; + import junit.framework.TestCase; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.util.TestApplicationRegistry; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.util.BrokerTestHelper; public class QueueConfigurationTest extends TestCase { - private VirtualHostConfiguration _emptyConf; private PropertiesConfiguration _env; private VirtualHostConfiguration _fullHostConf; + private Broker _broker; + @Override public void setUp() throws Exception { + super.setUp(); + BrokerTestHelper.setUp(); + _broker = BrokerTestHelper.createBrokerMock(); _env = new PropertiesConfiguration(); - _emptyConf = new VirtualHostConfiguration("test", _env); + _emptyConf = new VirtualHostConfiguration("test", _env, _broker); PropertiesConfiguration fullEnv = new PropertiesConfiguration(); fullEnv.setProperty("queues.maximumMessageAge", 1); @@ -49,35 +55,41 @@ public class QueueConfigurationTest extends TestCase fullEnv.setProperty("queues.deadLetterQueues", true); fullEnv.setProperty("queues.maximumDeliveryCount", 5); - _fullHostConf = new VirtualHostConfiguration("test", fullEnv); + _fullHostConf = new VirtualHostConfiguration("test", fullEnv, _broker); } + @Override + public void tearDown() throws Exception + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + public void testMaxDeliveryCount() throws Exception { - try - { - ApplicationRegistry registry = new TestApplicationRegistry(new ServerConfiguration(_env)); - ApplicationRegistry.initialise(registry); - - // Check default value - QueueConfiguration qConf = new QueueConfiguration("test", _emptyConf); - assertEquals("Unexpected default server configuration for max delivery count ", 0, qConf.getMaxDeliveryCount()); - - // Check explicit value - VirtualHostConfiguration vhostConfig = overrideConfiguration("maximumDeliveryCount", 7); - qConf = new QueueConfiguration("test", vhostConfig); - assertEquals("Unexpected host configuration for max delivery count", 7, qConf.getMaxDeliveryCount()); - - // Check inherited value - qConf = new QueueConfiguration("test", _fullHostConf); - assertEquals("Unexpected queue configuration for max delivery count", 5, qConf.getMaxDeliveryCount()); - - } - finally - { - ApplicationRegistry.remove(); - } + // broker MAXIMUM_DELIVERY_ATTEMPTS attribute is not set + when(_broker.getAttribute(Broker.MAXIMUM_DELIVERY_ATTEMPTS)).thenReturn(null); + + // Check default value + QueueConfiguration qConf = new QueueConfiguration("test", _emptyConf); + assertEquals("Unexpected default server configuration for max delivery count ", 0, qConf.getMaxDeliveryCount()); + + // set broker MAXIMUM_DELIVERY_ATTEMPTS attribute to 2 + when(_broker.getAttribute(Broker.MAXIMUM_DELIVERY_ATTEMPTS)).thenReturn(2); + + // Check that queue inherits the MAXIMUM_DELIVERY_ATTEMPTS value from broker + qConf = new QueueConfiguration("test", _emptyConf); + assertEquals("Unexpected default server configuration for max delivery count ", 2, qConf.getMaxDeliveryCount()); + + // Check explicit value + VirtualHostConfiguration vhostConfig = overrideConfiguration("maximumDeliveryCount", 7); + qConf = new QueueConfiguration("test", vhostConfig); + assertEquals("Unexpected host configuration for max delivery count", 7, qConf.getMaxDeliveryCount()); + + // Check inherited value + qConf = new QueueConfiguration("test", _fullHostConf); + assertEquals("Unexpected queue configuration for max delivery count", 5, qConf.getMaxDeliveryCount()); } /** @@ -87,28 +99,28 @@ public class QueueConfigurationTest extends TestCase */ public void testIsDeadLetterQueueEnabled() throws Exception { - try - { - ApplicationRegistry registry = new TestApplicationRegistry(new ServerConfiguration(_env)); - ApplicationRegistry.initialise(registry); - - // Check default value - QueueConfiguration qConf = new QueueConfiguration("test", _emptyConf); - assertFalse("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); - - // Check explicit value - VirtualHostConfiguration vhostConfig = overrideConfiguration("deadLetterQueues", true); - qConf = new QueueConfiguration("test", vhostConfig); - assertTrue("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); - - // Check inherited value - qConf = new QueueConfiguration("test", _fullHostConf); - assertTrue("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); - } - finally - { - ApplicationRegistry.remove(); - } + // enable dead letter queues broker wide + when(_broker.getAttribute(Broker.DEAD_LETTER_QUEUE_ENABLED)).thenReturn(true); + + // Check that queue inherits the broker setting + QueueConfiguration qConf = new QueueConfiguration("test", _emptyConf); + assertTrue("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); + + // broker DEAD_LETTER_QUEUE_ENABLED is not set + when(_broker.getAttribute(Broker.DEAD_LETTER_QUEUE_ENABLED)).thenReturn(null); + + // Check that queue dead letter queue is not enabled + qConf = new QueueConfiguration("test", _emptyConf); + assertFalse("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); + + // Check explicit value + VirtualHostConfiguration vhostConfig = overrideConfiguration("deadLetterQueues", true); + qConf = new QueueConfiguration("test", vhostConfig); + assertTrue("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); + + // Check inherited value + qConf = new QueueConfiguration("test", _fullHostConf); + assertTrue("Unexpected queue configuration for dead letter enabled attribute", qConf.isDeadLetterQueueEnabled()); } public void testGetMaximumMessageAge() throws ConfigurationException @@ -178,27 +190,28 @@ public class QueueConfigurationTest extends TestCase public void testGetMinimumAlertRepeatGap() throws Exception { - try - { - ApplicationRegistry registry = new TestApplicationRegistry(new ServerConfiguration(_env)); - ApplicationRegistry.initialise(registry); - // Check default value - QueueConfiguration qConf = new QueueConfiguration("test", _emptyConf); - assertEquals(ServerConfiguration.DEFAULT_MINIMUM_ALERT_REPEAT_GAP, qConf.getMinimumAlertRepeatGap()); - - // Check explicit value - VirtualHostConfiguration vhostConfig = overrideConfiguration("minimumAlertRepeatGap", 2); - qConf = new QueueConfiguration("test", vhostConfig); - assertEquals(2, qConf.getMinimumAlertRepeatGap()); - - // Check inherited value - qConf = new QueueConfiguration("test", _fullHostConf); - assertEquals(1, qConf.getMinimumAlertRepeatGap()); - } - finally - { - ApplicationRegistry.remove(); - } + // set broker attribute ALERT_REPEAT_GAP to 10 + when(_broker.getAttribute(Broker.ALERT_REPEAT_GAP)).thenReturn(10); + + // check that broker level setting is available on queue configuration + QueueConfiguration qConf = new QueueConfiguration("test", _emptyConf); + assertEquals(10, qConf.getMinimumAlertRepeatGap()); + + // remove configuration for ALERT_REPEAT_GAP on broker level + when(_broker.getAttribute(Broker.ALERT_REPEAT_GAP)).thenReturn(null); + + // Check default value + qConf = new QueueConfiguration("test", _emptyConf); + assertEquals(0, qConf.getMinimumAlertRepeatGap()); + + // Check explicit value + VirtualHostConfiguration vhostConfig = overrideConfiguration("minimumAlertRepeatGap", 2); + qConf = new QueueConfiguration("test", vhostConfig); + assertEquals(2, qConf.getMinimumAlertRepeatGap()); + + // Check inherited value + qConf = new QueueConfiguration("test", _fullHostConf); + assertEquals(1, qConf.getMinimumAlertRepeatGap()); } public void testSortQueueConfiguration() throws ConfigurationException @@ -235,6 +248,6 @@ public class QueueConfigurationTest extends TestCase config.addConfiguration(_fullHostConf.getConfig()); config.addConfiguration(queueConfig); - return new VirtualHostConfiguration("test", config); + return new VirtualHostConfiguration("test", config, _broker); } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/ServerConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/ServerConfigurationTest.java deleted file mode 100644 index c77732e356..0000000000 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/ServerConfigurationTest.java +++ /dev/null @@ -1,1791 +0,0 @@ -/* - * - * 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; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.XMLConfiguration; - -import org.apache.qpid.framing.AMQShortString; -import org.apache.qpid.server.exchange.Exchange; -import org.apache.qpid.server.protocol.AmqpProtocolVersion; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.util.TestApplicationRegistry; -import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.server.virtualhost.VirtualHostRegistry; -import org.apache.qpid.test.utils.QpidTestCase; - -import static org.apache.qpid.transport.ConnectionSettings.WILDCARD_ADDRESS; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Writer; -import java.util.Locale; - -import javax.net.ssl.KeyManagerFactory; - -public class ServerConfigurationTest extends QpidTestCase -{ - private XMLConfiguration _config = new XMLConfiguration(); - private ServerConfiguration _serverConfig = null; - - @Override - protected void setUp() throws Exception - { - super.setUp(); - _serverConfig = new ServerConfiguration(_config); - ApplicationRegistry.initialise(new TestApplicationRegistry(_serverConfig)); - } - - @Override - protected void tearDown() throws Exception - { - super.tearDown(); - ApplicationRegistry.remove(); - } - - public void testSetJMXPortRegistryServer() throws ConfigurationException - { - _serverConfig.initialise(); - _serverConfig.setJMXPortRegistryServer(23); - assertEquals(23, _serverConfig.getJMXPortRegistryServer()); - } - - public void testGetJMXPortRegistryServer() throws ConfigurationException - { - _config.setProperty(ServerConfiguration.MGMT_JMXPORT_REGISTRYSERVER, 42); - _serverConfig.initialise(); - assertEquals(42, _serverConfig.getJMXPortRegistryServer()); - } - - public void testDefaultJMXPortRegistryServer() throws ConfigurationException - { - _serverConfig.initialise(); - assertEquals(8999, _serverConfig.getJMXPortRegistryServer()); - } - - public void testSetJMXPortConnectorServer() throws ConfigurationException - { - ServerConfiguration serverConfig = new ServerConfiguration(_config); - serverConfig.setJMXPortConnectorServer(67); - assertEquals(67, serverConfig.getJMXConnectorServerPort()); - } - - public void testGetJMXPortConnectorServer() throws ConfigurationException - { - _config.setProperty(ServerConfiguration.MGMT_JMXPORT_CONNECTORSERVER, 67); - ServerConfiguration serverConfig = new ServerConfiguration(_config); - assertEquals(67, serverConfig.getJMXConnectorServerPort()); - } - - public void testDefaultJMXPortConnectorServer() throws ConfigurationException - { - ServerConfiguration serverConfig = new ServerConfiguration(_config); - assertEquals(ServerConfiguration.DEFAULT_JMXPORT_REGISTRYSERVER + ServerConfiguration.JMXPORT_CONNECTORSERVER_OFFSET, - serverConfig.getJMXConnectorServerPort()); - } - - public void testGetPlatformMbeanserver() throws ConfigurationException - { - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getPlatformMbeanserver()); - - // Check value we set - _config.setProperty("management.platform-mbeanserver", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getPlatformMbeanserver()); - } - - public void testGetFrameSize() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(65536, _serverConfig.getFrameSize()); - - // Check value we set - _config.setProperty("advanced.framesize", "23"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(23, _serverConfig.getFrameSize()); - } - - public void testGetStatusEnabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(ServerConfiguration.DEFAULT_STATUS_UPDATES.equalsIgnoreCase("on"), - _serverConfig.getStatusUpdatesEnabled()); - - // Check disabling we set - _config.setProperty(ServerConfiguration.STATUS_UPDATES, "off"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getStatusUpdatesEnabled()); - - // Check invalid values don't cause error but result in disabled - _config.setProperty(ServerConfiguration.STATUS_UPDATES, "Yes Please"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getStatusUpdatesEnabled()); - - } - public void testGetSynchedClocks() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getSynchedClocks()); - - // Check value we set - _config.setProperty("advanced.synced-clocks", true); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getSynchedClocks()); - } - - public void testGetLocale() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - - // The Default is what ever the VMs default is - Locale defaultLocale = Locale.getDefault(); - - assertEquals(defaultLocale, _serverConfig.getLocale()); - - - //Test Language only - Locale update = new Locale("es"); - _config.setProperty(ServerConfiguration.ADVANCED_LOCALE, "es"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(update, _serverConfig.getLocale()); - - //Test Language and Country - update = new Locale("es","ES"); - _config.setProperty(ServerConfiguration.ADVANCED_LOCALE, "es_ES"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(update, _serverConfig.getLocale()); - - //Test Language and Country and Variant - update = new Locale("es","ES", "Traditional_WIN"); - _config.setProperty(ServerConfiguration.ADVANCED_LOCALE, "es_ES_Traditional_WIN"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(update, _serverConfig.getLocale()); - } - - - public void testGetMsgAuth() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getMsgAuth()); - - // Check value we set - _config.setProperty("security.msg-auth", true); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getMsgAuth()); - } - - public void testGetManagementKeyStorePath() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(null, _serverConfig.getManagementKeyStorePath()); - - // Check value we set - _config.setProperty("management.ssl.keyStorePath", "a"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("a", _serverConfig.getManagementKeyStorePath()); - } - - public void testGetManagementSSLEnabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getManagementSSLEnabled()); - - // Check value we set - _config.setProperty("management.ssl.enabled", true); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getManagementSSLEnabled()); - } - - public void testGetManagementKeystorePassword() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(null, _serverConfig.getManagementKeyStorePassword()); - - // Check value we set - _config.setProperty("management.ssl.keyStorePassword", "a"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("a", _serverConfig.getManagementKeyStorePassword()); - } - - public void testGetQueueAutoRegister() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getQueueAutoRegister()); - - // Check value we set - _config.setProperty("queue.auto_register", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getQueueAutoRegister()); - } - - public void testGetJMXManagementEnabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getJMXManagementEnabled()); - - // Check value we set - _config.setProperty("management.enabled", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getJMXManagementEnabled()); - } - - public void testGetHTTPManagementEnabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getHTTPManagementEnabled()); - - // Check value we set - _config.setProperty("management.http.enabled", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getHTTPManagementEnabled()); - } - - public void testGetHTTPManagementSessionTimeout() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(60 * 15, _serverConfig.getHTTPManagementSessionTimeout()); - - // Check value we set - _config.setProperty("management.http.session-timeout", 60); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(60, _serverConfig.getHTTPManagementSessionTimeout()); - } - - public void testGetHTTPManagementSaslAuthEnabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getHTTPManagementSaslAuthEnabled()); - - // Check value we set - _config.setProperty("management.http.sasl-auth", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getHTTPManagementSaslAuthEnabled()); - } - - public void testGetHTTPSManagementSaslAuthEnabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getHTTPSManagementSaslAuthEnabled()); - - // Check value we set - _config.setProperty("management.https.sasl-auth", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getHTTPSManagementSaslAuthEnabled()); - } - - public void testGetManagementRightsInferAllAccess() throws Exception - { - _serverConfig.initialise(); - - //check default - assertTrue("default should be true", _serverConfig.getManagementRightsInferAllAccess()); - - //update it - _config.setProperty("management.managementRightsInferAllAccess", "false"); - assertFalse("New value should be false", _serverConfig.getManagementRightsInferAllAccess()); - } - - public void testGetHeartBeatDelay() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(5, _serverConfig.getHeartBeatDelay()); - - // Check value we set - _config.setProperty("heartbeat.delay", 23); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(23, _serverConfig.getHeartBeatDelay()); - } - - public void testGetHeartBeatTimeout() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(2.0, _serverConfig.getHeartBeatTimeout()); - - // Check value we set - _config.setProperty("heartbeat.timeoutFactor", 2.3); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(2.3, _serverConfig.getHeartBeatTimeout()); - } - - public void testGetMaximumMessageAge() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(0, _serverConfig.getMaximumMessageAge()); - - // Check value we set - _config.setProperty("maximumMessageAge", 10L); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(10, _serverConfig.getMaximumMessageAge()); - } - - public void testGetMaximumMessageCount() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(0, _serverConfig.getMaximumMessageCount()); - - // Check value we set - _config.setProperty("maximumMessageCount", 10L); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(10, _serverConfig.getMaximumMessageCount()); - } - - public void testGetMaximumQueueDepth() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(0, _serverConfig.getMaximumQueueDepth()); - - // Check value we set - _config.setProperty("maximumQueueDepth", 10L); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(10, _serverConfig.getMaximumQueueDepth()); - } - - public void testGetMaximumMessageSize() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(0, _serverConfig.getMaximumMessageSize()); - - // Check value we set - _config.setProperty("maximumMessageSize", 10L); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(10, _serverConfig.getMaximumMessageSize()); - } - - public void testGetMinimumAlertRepeatGap() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(30000l, _serverConfig.getMinimumAlertRepeatGap()); - - // Check value we set - _config.setProperty("minimumAlertRepeatGap", 10L); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(10, _serverConfig.getMinimumAlertRepeatGap()); - } - - public void testGetProcessors() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(4, _serverConfig.getConnectorProcessors()); - - // Check value we set - _config.setProperty("connector.processors", 10); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(10, _serverConfig.getConnectorProcessors()); - } - - public void testGetPorts() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertNotNull(_serverConfig.getPorts()); - assertEquals(1, _serverConfig.getPorts().size()); - assertEquals(5672, _serverConfig.getPorts().get(0)); - - - // Check value we set - _config.setProperty("connector.port", "10"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertNotNull(_serverConfig.getPorts()); - assertEquals(1, _serverConfig.getPorts().size()); - assertEquals("10", _serverConfig.getPorts().get(0)); - } - - public void testGetBind() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(WILDCARD_ADDRESS, _serverConfig.getBind()); - - // Check value we set - _config.setProperty("connector.bind", "a"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("a", _serverConfig.getBind()); - } - - public void testGetReceiveBufferSize() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(ServerConfiguration.DEFAULT_BUFFER_SIZE, _serverConfig.getReceiveBufferSize()); - - // Check value we set - _config.setProperty("connector.socketReceiveBuffer", "23"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(23, _serverConfig.getReceiveBufferSize()); - } - - public void testGetWriteBufferSize() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(ServerConfiguration.DEFAULT_BUFFER_SIZE, _serverConfig.getWriteBufferSize()); - - // Check value we set - _config.setProperty("connector.socketWriteBuffer", "23"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(23, _serverConfig.getWriteBufferSize()); - } - - public void testGetTcpNoDelay() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getTcpNoDelay()); - - // Check value we set - _config.setProperty("connector.tcpNoDelay", false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getTcpNoDelay()); - } - - public void testGetEnableSSL() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getEnableSSL()); - - // Check value we set - _config.setProperty("connector.ssl.enabled", true); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getEnableSSL()); - } - - public void testGetSSLOnly() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(false, _serverConfig.getSSLOnly()); - - // Check value we set - _config.setProperty("connector.ssl.sslOnly", true); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getSSLOnly()); - } - - public void testGetSSLPorts() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertNotNull(_serverConfig.getSSLPorts()); - assertEquals(1, _serverConfig.getSSLPorts().size()); - assertEquals(ServerConfiguration.DEFAULT_SSL_PORT, _serverConfig.getSSLPorts().get(0)); - - - // Check value we set - _config.setProperty("connector.ssl.port", "10"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertNotNull(_serverConfig.getSSLPorts()); - assertEquals(1, _serverConfig.getSSLPorts().size()); - assertEquals("10", _serverConfig.getSSLPorts().get(0)); - } - - public void testGetConnectorKeystorePath() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertNull(_serverConfig.getConnectorKeyStorePath()); - - // Check value we set - _config.setProperty("connector.ssl.keyStorePath", "a"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("a", _serverConfig.getConnectorKeyStorePath()); - - // Ensure we continue to support the old name keystorePath - _config.clearProperty("connector.ssl.keyStorePath"); - _config.setProperty("connector.ssl.keystorePath", "b"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("b", _serverConfig.getConnectorKeyStorePath()); - } - - public void testGetConnectorKeystorePassword() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertNull(_serverConfig.getConnectorKeyStorePassword()); - - // Check value we set - _config.setProperty("connector.ssl.keyStorePassword", "a"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("a", _serverConfig.getConnectorKeyStorePassword()); - - // Ensure we continue to support the old name keystorePassword - _config.clearProperty("connector.ssl.keyStorePassword"); - _config.setProperty("connector.ssl.keystorePassword", "b"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("b", _serverConfig.getConnectorKeyStorePassword()); - } - - public void testConnectorGetKeyManagerAlgorithm() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(KeyManagerFactory.getDefaultAlgorithm(), _serverConfig.getConnectorKeyManagerFactoryAlgorithm()); - - // Check value we set - _config.setProperty("connector.ssl.keyManagerFactoryAlgorithm", "a"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("a", _serverConfig.getConnectorKeyManagerFactoryAlgorithm()); - - // Ensure we continue to support the old name certType - _config.clearProperty("connector.ssl.keyManagerFactoryAlgorithm"); - _config.setProperty("connector.ssl.certType", "b"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("b", _serverConfig.getConnectorKeyManagerFactoryAlgorithm()); - } - - public void testGetHousekeepingCheckPeriod() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(30000, _serverConfig.getHousekeepingCheckPeriod()); - - // Check value we set - _config.setProperty("housekeeping.checkPeriod", 23L); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - _serverConfig.setHousekeepingCheckPeriod(42L); - assertEquals(42, _serverConfig.getHousekeepingCheckPeriod()); - } - - public void testSingleConfiguration() throws IOException, ConfigurationException - { - File fileA = File.createTempFile(getClass().getName(), null); - fileA.deleteOnExit(); - FileWriter out = new FileWriter(fileA); - out.write("<broker><connector><port>2342</port><ssl><port>4235</port></ssl></connector></broker>"); - out.close(); - ServerConfiguration conf = new ServerConfiguration(fileA); - conf.initialise(); - assertEquals("4235", conf.getSSLPorts().get(0)); - } - - public void testCombinedConfiguration() throws IOException, ConfigurationException - { - File mainFile = File.createTempFile(getClass().getName(), null); - File fileA = File.createTempFile(getClass().getName(), null); - File fileB = File.createTempFile(getClass().getName(), null); - - mainFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - - FileWriter out = new FileWriter(mainFile); - out.write("<configuration><system/>"); - out.write("<xml fileName=\"" + fileA.getAbsolutePath() + "\"/>"); - out.write("<xml fileName=\"" + fileB.getAbsolutePath() + "\"/>"); - out.write("</configuration>"); - out.close(); - - out = new FileWriter(fileA); - out.write("<broker><connector><port>2342</port><ssl><port>4235</port></ssl></connector></broker>"); - out.close(); - - out = new FileWriter(fileB); - out.write("<broker><connector><ssl><port>2345</port></ssl></connector></broker>"); - out.close(); - - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - assertEquals("4235", config.getSSLPorts().get(0)); // From first file, not - // overriden by second - assertNotNull(config.getPorts()); - assertEquals(1, config.getPorts().size()); - assertEquals("2342", config.getPorts().get(0)); // From the first file, not - // present in the second - } - - public void testVariableInterpolation() throws Exception - { - File mainFile = File.createTempFile(getClass().getName(), null); - - mainFile.deleteOnExit(); - - FileWriter out = new FileWriter(mainFile); - out.write("<broker>\n"); - out.write("\t<work>foo</work>\n"); - out.write("\t<management><ssl><keyStorePath>${work}</keyStorePath></ssl></management>\n"); - out.write("</broker>\n"); - out.close(); - - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - assertEquals("Did not get correct interpolated value", - "foo", config.getManagementKeyStorePath()); - } - - private void writeConfigFile(File mainFile, boolean allow) throws IOException { - writeConfigFile(mainFile, allow, true, null, "test"); - } - - private void writeConfigFile(File mainFile, boolean allow, boolean includeVhosts, File vhostsFile, String name) throws IOException { - FileWriter out = new FileWriter(mainFile); - out.write("<broker>\n"); - out.write("\t<management><enabled>false</enabled></management>\n"); - out.write("\t<security>\n"); - out.write("\t\t<pd-auth-manager>\n"); - out.write("\t\t\t<principal-database>\n"); - out.write("\t\t\t\t<class>org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase</class>\n"); - out.write("\t\t\t\t<attributes>\n"); - out.write("\t\t\t\t\t<attribute>\n"); - out.write("\t\t\t\t\t\t<name>passwordFile</name>\n"); - out.write("\t\t\t\t\t\t<value>/dev/null</value>\n"); - out.write("\t\t\t\t\t</attribute>\n"); - out.write("\t\t\t\t</attributes>\n"); - out.write("\t\t\t</principal-database>\n"); - out.write("\t\t</pd-auth-manager>\n"); - out.write("\t\t<firewall>\n"); - out.write("\t\t\t<rule access=\""+ ((allow) ? "allow" : "deny") +"\" network=\"127.0.0.1\"/>"); - out.write("\t\t</firewall>\n"); - out.write("\t</security>\n"); - if (includeVhosts) - { - out.write("\t<virtualhosts>\n"); - out.write("\t\t<default>test</default>\n"); - out.write("\t\t<virtualhost>\n"); - out.write(String.format("\t\t\t<name>%s</name>\n", name)); - out.write(String.format("\t\t<%s> \n", name)); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<type>topic</type>\n"); - out.write(String.format("\t\t\t\t\t<name>%s.topic</name>\n", name)); - out.write("\t\t\t\t\t<durable>true</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write(String.format("\t\t</%s> \n", name)); - out.write("\t\t</virtualhost>\n"); - out.write("\t</virtualhosts>\n"); - } - if (vhostsFile != null) - { - out.write("\t<virtualhosts>"+vhostsFile.getAbsolutePath()+"</virtualhosts>\n"); - } - out.write("</broker>\n"); - out.close(); - } - - private void writeTestFishConfigFile(File mainFile) throws IOException { - FileWriter out = new FileWriter(mainFile); - out.write("<broker>\n"); - out.write("\t<management><enabled>false</enabled></management>\n"); - out.write("\t<security>\n"); - out.write("\t\t<pd-auth-manager>\n"); - out.write("\t\t\t<principal-database>\n"); - out.write("\t\t\t\t<class>org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase</class>\n"); - out.write("\t\t\t\t<attributes>\n"); - out.write("\t\t\t\t\t<attribute>\n"); - out.write("\t\t\t\t\t\t<name>passwordFile</name>\n"); - out.write("\t\t\t\t\t\t<value>/dev/null</value>\n"); - out.write("\t\t\t\t\t</attribute>\n"); - out.write("\t\t\t\t</attributes>\n"); - out.write("\t\t\t</principal-database>\n"); - out.write("\t\t</pd-auth-manager>\n"); - out.write("\t\t<firewall>\n"); - out.write("\t\t\t<rule access=\"allow\" network=\"127.0.0.1\"/>"); - out.write("\t\t</firewall>\n"); - out.write("\t</security>\n"); - out.write("\t<virtualhosts>\n"); - out.write("\t\t<virtualhost>\n"); - out.write("\t\t\t<name>test</name>\n"); - out.write("\t\t<test> \n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<type>topic</type>\n"); - out.write("\t\t\t\t\t<name>test.topic</name>\n"); - out.write("\t\t\t\t\t<durable>true</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</test> \n"); - out.write("\t\t</virtualhost>\n"); - out.write("\t\t<virtualhost>\n"); - out.write("\t\t\t<name>fish</name>\n"); - out.write("\t\t<fish> \n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<type>topic</type>\n"); - out.write("\t\t\t\t\t<name>fish.topic</name>\n"); - out.write("\t\t\t\t\t<durable>false</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</fish> \n"); - out.write("\t\t</virtualhost>\n"); - out.write("\t</virtualhosts>\n"); - out.write("</broker>\n"); - out.close(); - } - - private void writeVirtualHostsFile(File vhostsFile, String name) throws IOException { - FileWriter out = new FileWriter(vhostsFile); - out.write("<virtualhosts>\n"); - out.write(String.format("\t\t<default>%s</default>\n", name)); - out.write("\t<virtualhost>\n"); - out.write(String.format("\t\t<name>%s</name>\n", name)); - out.write(String.format("\t\t<%s>\n", name)); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<type>topic</type>\n"); - out.write("\t\t\t\t\t<name>test.topic</name>\n"); - out.write("\t\t\t\t\t<durable>true</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write(String.format("\t\t</%s>\n", name)); - out.write("\t</virtualhost>\n"); - out.write("</virtualhosts>\n"); - out.close(); - } - - private void writeMultiVirtualHostsFile(File vhostsFile) throws IOException { - FileWriter out = new FileWriter(vhostsFile); - out.write("<virtualhosts>\n"); - out.write("\t<virtualhost>\n"); - out.write("\t\t<name>topic</name>\n"); - out.write("\t\t<topic>\n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<type>topic</type>\n"); - out.write("\t\t\t\t\t<name>test.topic</name>\n"); - out.write("\t\t\t\t\t<durable>true</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</topic>\n"); - out.write("\t</virtualhost>\n"); - out.write("\t<virtualhost>\n"); - out.write("\t\t<name>fanout</name>\n"); - out.write("\t\t<fanout>\n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<type>fanout</type>\n"); - out.write("\t\t\t\t\t<name>test.fanout</name>\n"); - out.write("\t\t\t\t\t<durable>true</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</fanout>\n"); - out.write("\t</virtualhost>\n"); - out.write("</virtualhosts>\n"); - out.close(); - } - - private void writeMultipleVhostsConfigFile(File mainFile, File[] vhostsFileArray) throws IOException { - FileWriter out = new FileWriter(mainFile); - out.write("<broker>\n"); - out.write("\t<management><enabled>false</enabled></management>\n"); - out.write("\t<security>\n"); - out.write("\t\t<pd-auth-manager>\n"); - out.write("\t\t\t<principal-database>\n"); - out.write("\t\t\t\t<class>org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase</class>\n"); - out.write("\t\t\t\t<attributes>\n"); - out.write("\t\t\t\t\t<attribute>\n"); - out.write("\t\t\t\t\t\t<name>passwordFile</name>\n"); - out.write("\t\t\t\t\t\t<value>/dev/null</value>\n"); - out.write("\t\t\t\t\t</attribute>\n"); - out.write("\t\t\t\t</attributes>\n"); - out.write("\t\t\t</principal-database>\n"); - out.write("\t\t</pd-auth-manager>\n"); - out.write("\t\t<firewall>\n"); - out.write("\t\t\t<rule access=\"allow\" network=\"127.0.0.1\"/>"); - out.write("\t\t</firewall>\n"); - out.write("\t</security>\n"); - for (File vhostsFile : vhostsFileArray) - { - out.write("\t<virtualhosts>"+vhostsFile.getAbsolutePath()+"</virtualhosts>\n"); - } - out.write("</broker>\n"); - out.close(); - } - - private void writeCombinedConfigFile(File mainFile, File fileA, File fileB) throws Exception - { - FileWriter out = new FileWriter(mainFile); - out.write("<configuration><system/>"); - out.write("<xml fileName=\"" + fileA.getAbsolutePath() + "\"/>"); - out.write("<xml fileName=\"" + fileB.getAbsolutePath() + "\"/>"); - out.write("</configuration>"); - out.close(); - } - - /** - * Test that configuration loads correctly when virtual hosts are specified in the main - * configuration file only. - * <p> - * Test for QPID-2361 - */ - public void testInternalVirtualhostConfigFile() throws Exception - { - // Write out config - File mainFile = File.createTempFile(getClass().getName(), "config"); - mainFile.deleteOnExit(); - writeConfigFile(mainFile, false, true, null, "test"); - - // Load config - ApplicationRegistry.remove(); - ApplicationRegistry reg = new ApplicationRegistry(new ServerConfiguration(mainFile)); - ApplicationRegistry.initialise(reg); - - // Test config - VirtualHostRegistry virtualHostRegistry = reg.getVirtualHostRegistry(); - String defaultVirtualHost = reg.getConfiguration().getDefaultVirtualHost(); - VirtualHost virtualHost = virtualHostRegistry.getVirtualHost("test"); - Exchange exchange = virtualHost.getExchangeRegistry().getExchange(new AMQShortString("test.topic")); - - assertEquals("Incorrect default host", "test", defaultVirtualHost); - assertEquals("Incorrect virtualhost count", 1, virtualHostRegistry.getVirtualHosts().size()); - assertEquals("Incorrect virtualhost name", "test", virtualHost.getName()); - assertEquals("Incorrect exchange type", "topic", exchange.getType().getName().toString()); - } - - /** - * Test that configuration loads correctly when virtual hosts are specified in an external - * configuration file only. - * <p> - * Test for QPID-2361 - */ - public void testExternalVirtualhostXMLFile() throws Exception - { - // Write out config - File mainFile = File.createTempFile(getClass().getName(), "config"); - mainFile.deleteOnExit(); - File vhostsFile = File.createTempFile(getClass().getName(), "vhosts"); - vhostsFile.deleteOnExit(); - writeConfigFile(mainFile, false, false, vhostsFile, null); - writeVirtualHostsFile(vhostsFile, "test"); - - // Load config - ApplicationRegistry.remove(); - ApplicationRegistry reg = new ApplicationRegistry(new ServerConfiguration(mainFile)); - ApplicationRegistry.initialise(reg); - - // Test config - VirtualHostRegistry virtualHostRegistry = reg.getVirtualHostRegistry(); - String defaultVirtualHost = reg.getConfiguration().getDefaultVirtualHost(); - VirtualHost virtualHost = virtualHostRegistry.getVirtualHost("test"); - Exchange exchange = virtualHost.getExchangeRegistry().getExchange(new AMQShortString("test.topic")); - - assertEquals("Incorrect default host", "test", defaultVirtualHost); - assertEquals("Incorrect virtualhost count", 1, virtualHostRegistry.getVirtualHosts().size()); - assertEquals("Incorrect virtualhost name", "test", virtualHost.getName()); - assertEquals("Incorrect exchange type", "topic", exchange.getType().getName().toString()); - } - - /** - * Test that configuration loads correctly when virtual hosts are specified in an external - * configuration file only, with two vhosts that have different properties. - * <p> - * Test for QPID-2361 - */ - public void testExternalMultiVirtualhostXMLFile() throws Exception - { - // Write out vhosts - File vhostsFile = File.createTempFile(getClass().getName(), "vhosts-multi"); - vhostsFile.deleteOnExit(); - writeMultiVirtualHostsFile(vhostsFile); - - // Write out config - File mainFile = File.createTempFile(getClass().getName(), "config"); - mainFile.deleteOnExit(); - writeConfigFile(mainFile, false, false, vhostsFile, null); - - // Load config - ApplicationRegistry.remove(); - ApplicationRegistry reg = new ApplicationRegistry(new ServerConfiguration(mainFile)); - ApplicationRegistry.initialise(reg); - - // Test config - VirtualHostRegistry virtualHostRegistry = reg.getVirtualHostRegistry(); - - assertEquals("Incorrect virtualhost count", 2, virtualHostRegistry.getVirtualHosts().size()); - - // test topic host - VirtualHost topicVirtualHost = virtualHostRegistry.getVirtualHost("topic"); - Exchange topicExchange = topicVirtualHost.getExchangeRegistry().getExchange(new AMQShortString("test.topic")); - - assertEquals("Incorrect topic virtualhost name", "topic", topicVirtualHost.getName()); - assertEquals("Incorrect topic exchange type", "topic", topicExchange.getType().getName().toString()); - - // Test fanout host - VirtualHost fanoutVirtualHost = virtualHostRegistry.getVirtualHost("fanout"); - Exchange fanoutExchange = fanoutVirtualHost.getExchangeRegistry().getExchange(new AMQShortString("test.fanout")); - - assertEquals("Incorrect fanout virtualhost name", "fanout", fanoutVirtualHost.getName()); - assertEquals("Incorrect fanout exchange type", "fanout", fanoutExchange.getType().getName().toString()); - } - - /** - * Test that configuration does not load when virtual hosts are specified in both the main - * configuration file and an external file. Should throw a {@link ConfigurationException}. - * <p> - * Test for QPID-2361 - */ - public void testInternalAndExternalVirtualhostXMLFile() throws Exception - { - // Write out vhosts - File vhostsFile = File.createTempFile(getClass().getName(), "vhosts"); - vhostsFile.deleteOnExit(); - writeVirtualHostsFile(vhostsFile, "test"); - - // Write out config - File mainFile = File.createTempFile(getClass().getName(), "config"); - mainFile.deleteOnExit(); - writeConfigFile(mainFile, false, true, vhostsFile, "test"); - - // Load config - try - { - ApplicationRegistry.remove(); - ApplicationRegistry reg = new ApplicationRegistry(new ServerConfiguration(mainFile)); - ApplicationRegistry.initialise(reg); - fail("Different virtualhost XML configurations not allowed"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", "Only one of external or embedded virtualhosts configuration allowed.", ce.getMessage()); - } - } - - /** - * Test that configuration does not load when virtual hosts are specified in multiple external - * files. Should throw a {@link ConfigurationException}. - * <p> - * Test for QPID-2361 - */ - public void testMultipleInternalVirtualhostXMLFile() throws Exception - { - // Write out vhosts - File vhostsFileOne = File.createTempFile(getClass().getName(), "vhosts-one"); - vhostsFileOne.deleteOnExit(); - writeVirtualHostsFile(vhostsFileOne, "one"); - File vhostsFileTwo = File.createTempFile(getClass().getName(), "vhosts-two"); - vhostsFileTwo.deleteOnExit(); - writeVirtualHostsFile(vhostsFileTwo, "two"); - - // Write out config - File mainFile = File.createTempFile(getClass().getName(), "config"); - mainFile.deleteOnExit(); - writeMultipleVhostsConfigFile(mainFile, new File[] { vhostsFileOne, vhostsFileTwo }); - - // Load config - try - { - ApplicationRegistry.remove(); - ApplicationRegistry reg = new ApplicationRegistry(new ServerConfiguration(mainFile)); - ApplicationRegistry.initialise(reg); - fail("Multiple virtualhost XML configurations not allowed"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", - "Only one external virtualhosts configuration file allowed, multiple filenames found.", - ce.getMessage()); - } - } - - /** - * Test that configuration loads correctly when virtual hosts are specified in an external - * configuration file in the first of two configurations and embedded in the second. This - * will throe a {@link ConfigurationException} since the configurations have different - * types. - * <p> - * Test for QPID-2361 - */ - public void testCombinedDifferentVirtualhostConfig() throws Exception - { - // Write out vhosts config - File vhostsFile = File.createTempFile(getClass().getName(), "vhosts"); - vhostsFile.deleteOnExit(); - writeVirtualHostsFile(vhostsFile, "external"); - - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File fileA = File.createTempFile(getClass().getName(), "a"); - File fileB = File.createTempFile(getClass().getName(), "b"); - mainFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - writeCombinedConfigFile(mainFile, fileA, fileB); - writeConfigFile(fileA, false, false, vhostsFile, null); - writeConfigFile(fileB, false); - - // Load config - try - { - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - fail("Different virtualhost XML configurations not allowed"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", "Only one of external or embedded virtualhosts configuration allowed.", ce.getMessage()); - } - } - - /** - * Test that configuration loads correctly when virtual hosts are specified two overriding configurations - * each with an embedded virtualhost section. The first configuration section should be used. - * <p> - * Test for QPID-2361 - */ - public void testCombinedConfigEmbeddedVirtualhost() throws Exception - { - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File fileA = File.createTempFile(getClass().getName(), "a"); - File fileB = File.createTempFile(getClass().getName(), "b"); - mainFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - writeCombinedConfigFile(mainFile, fileA, fileB); - writeConfigFile(fileA, false, true, null, "a"); - writeConfigFile(fileB, false, true, null, "b"); - - // Load config - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - - // Test config - VirtualHostConfiguration virtualHost = config.getVirtualHostConfig("a"); - - assertEquals("Incorrect virtualhost count", 1, config.getVirtualHosts().length); - assertEquals("Incorrect virtualhost name", "a", virtualHost.getName()); - } - - /** - * Test that configuration loads correctly when virtual hosts are specified two overriding configurations - * each with an external virtualhost XML file. The first configuration file should be used. - * <p> - * Test for QPID-2361 - */ - public void testCombinedConfigExternalVirtualhost() throws Exception - { - // Write out vhosts config - File vhostsOne = File.createTempFile(getClass().getName(), "vhosts-one"); - vhostsOne.deleteOnExit(); - writeVirtualHostsFile(vhostsOne, "one"); - File vhostsTwo = File.createTempFile(getClass().getName(), "vhosts-two"); - vhostsTwo.deleteOnExit(); - writeVirtualHostsFile(vhostsTwo, "two"); - - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File fileA = File.createTempFile(getClass().getName(), "a"); - File fileB = File.createTempFile(getClass().getName(), "b"); - mainFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - writeCombinedConfigFile(mainFile, fileA, fileB); - writeConfigFile(fileA, false, false, vhostsOne, null); - writeConfigFile(fileB, false, false, vhostsTwo, null); - - // Load config - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - - // Test config - VirtualHostConfiguration virtualHost = config.getVirtualHostConfig("one"); - - assertEquals("Incorrect virtualhost count", 1, config.getVirtualHosts().length); - assertEquals("Incorrect virtualhost name", "one", virtualHost.getName()); - } - - /** - * Test that configuration loads correctly when an overriding virtualhost configuration resets - * a property of an embedded virtualhost section. The overriding configuration property value - * should be used. - * <p> - * Test for QPID-2361 - */ - public void testCombinedConfigEmbeddedVirtualhostOverride() throws Exception - { - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File fileA = File.createTempFile(getClass().getName(), "override"); - File fileB = File.createTempFile(getClass().getName(), "config"); - mainFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - writeCombinedConfigFile(mainFile, fileA, fileB); - writeTestFishConfigFile(fileB); - - // Write out overriding virtualhosts section - FileWriter out = new FileWriter(fileA); - out.write("<broker>\n"); - out.write("<virtualhosts>\n"); - out.write("\t<virtualhost>\n"); - out.write("\t\t<test>\n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<durable>false</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</test>\n"); - out.write("\t\t<fish>\n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<durable>true</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</fish>\n"); - out.write("\t</virtualhost>\n"); - out.write("</virtualhosts>\n"); - out.write("</broker>\n"); - out.close(); - - // Load config - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - - // Test config - VirtualHostConfiguration testHost = config.getVirtualHostConfig("test"); - ExchangeConfiguration testExchange = testHost.getExchangeConfiguration("test.topic"); - VirtualHostConfiguration fishHost = config.getVirtualHostConfig("fish"); - ExchangeConfiguration fishExchange = fishHost.getExchangeConfiguration("fish.topic"); - - assertEquals("Incorrect virtualhost count", 2, config.getVirtualHosts().length); - assertEquals("Incorrect virtualhost name", "test", testHost.getName()); - assertFalse("Incorrect exchange durable property", testExchange.getDurable()); - assertEquals("Incorrect virtualhost name", "fish", fishHost.getName()); - assertTrue("Incorrect exchange durable property", fishExchange.getDurable()); - } - - /** - * Test that configuration loads correctly when the virtualhost configuration is a set of overriding - * configuration files that resets a property of a virtualhost. The opmost overriding configuration - * property value should be used. - * <p> - * Test for QPID-2361 - */ - public void testCombinedVirtualhostOverride() throws Exception - { - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File vhostsFile = File.createTempFile(getClass().getName(), "vhosts"); - File fileA = File.createTempFile(getClass().getName(), "vhosts-override"); - File fileB = File.createTempFile(getClass().getName(), "vhosts-base"); - mainFile.deleteOnExit(); - vhostsFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - writeConfigFile(mainFile, true, false, vhostsFile, null); - writeCombinedConfigFile(vhostsFile, fileA, fileB); - - // Write out overriding virtualhosts sections - FileWriter out = new FileWriter(fileA); - out.write("<virtualhosts>\n"); - out.write("\t<virtualhost>\n"); - out.write("\t\t<test>\n"); - out.write("\t\t\t<exchanges>\n"); - out.write("\t\t\t\t<exchange>\n"); - out.write("\t\t\t\t\t<durable>false</durable>\n"); - out.write("\t\t\t\t</exchange>\n"); - out.write("\t\tt</exchanges>\n"); - out.write("\t\t</test>\n"); - out.write("\t</virtualhost>\n"); - out.write("</virtualhosts>\n"); - out.close(); - writeVirtualHostsFile(fileB, "test"); - - // Load config - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - - // Test config - VirtualHostConfiguration testHost = config.getVirtualHostConfig("test"); - ExchangeConfiguration testExchange = testHost.getExchangeConfiguration("test.topic"); - - assertEquals("Incorrect virtualhost count", 1, config.getVirtualHosts().length); - assertEquals("Incorrect virtualhost name", "test", testHost.getName()); - assertFalse("Incorrect exchange durable property", testExchange.getDurable()); - } - - /** - * Test that configuration loads correctly when the virtualhost configuration is a set of overriding - * configuration files that define multiple virtualhosts, one per file. Only the virtualhosts defined in - * the topmost file should be used. - * <p> - * Test for QPID-2361 - */ - public void testCombinedMultipleVirtualhosts() throws Exception - { - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File vhostsFile = File.createTempFile(getClass().getName(), "vhosts"); - File fileA = File.createTempFile(getClass().getName(), "vhosts-one"); - File fileB = File.createTempFile(getClass().getName(), "vhosts-two"); - mainFile.deleteOnExit(); - vhostsFile.deleteOnExit(); - fileA.deleteOnExit(); - fileB.deleteOnExit(); - writeConfigFile(mainFile, true, false, vhostsFile, null); - writeCombinedConfigFile(vhostsFile, fileA, fileB); - - // Write both virtualhosts definitions - writeVirtualHostsFile(fileA, "test-one"); - writeVirtualHostsFile(fileB, "test-two"); - - // Load config - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - - // Test config - VirtualHostConfiguration oneHost = config.getVirtualHostConfig("test-one"); - - assertEquals("Incorrect virtualhost count", 1, config.getVirtualHosts().length); - assertEquals("Incorrect virtualhost name", "test-one", oneHost.getName()); - } - - /** - * Test that a non-existent virtualhost file throws a {@link ConfigurationException}. - * <p> - * Test for QPID-2624 - */ - public void testNonExistantVirtualhosts() throws Exception - { - // Write out combined config file - File mainFile = File.createTempFile(getClass().getName(), "main"); - File vhostsFile = new File("doesnotexist"); - mainFile.deleteOnExit(); - writeConfigFile(mainFile, true, false, vhostsFile, null); - - // Load config - try - { - ServerConfiguration config = new ServerConfiguration(mainFile.getAbsoluteFile()); - config.initialise(); - } - catch (ConfigurationException ce) - { - assertEquals("Virtualhosts file does not exist", ce.getMessage()); - } - catch (Exception e) - { - fail("Should throw a ConfigurationException"); - } - } - - /** - * Tests that element disabledFeatures allows features that would - * otherwise be advertised by the broker to be turned off. - */ - public void testDisabledFeatures() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - _serverConfig = new ServerConfiguration(_config); - assertEquals("Unexpected size", 0, _serverConfig.getDisabledFeatures().size()); - - // Check value we set - _config.addProperty("disabledFeatures", "qpid.feature1"); - _config.addProperty("disabledFeatures", "qpid.feature2"); - _serverConfig = new ServerConfiguration(_config); - - assertEquals("Unexpected size",2, _serverConfig.getDisabledFeatures().size()); - assertTrue("Unexpected contents", _serverConfig.getDisabledFeatures().contains("qpid.feature1")); - } - - /** - * Tests that the old element security.jmx.access (that used to be used - * to define JMX access rights) is rejected. - */ - public void testManagementAccessRejected() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - - // Check value we set - _config.setProperty("security.jmx.access(0)", "jmxremote.access"); - _serverConfig = new ServerConfiguration(_config); - - try - { - _serverConfig.initialise(); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", - "Validation error : security/jmx/access is no longer a supported element within the configuration xml.", - ce.getMessage()); - } - } - - /** - * Tests that the old element security.jmx.principal-database (that used to define the - * principal database used for JMX authentication) is rejected. - */ - public void testManagementPrincipalDatabaseRejected() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - - // Check value we set - _config.setProperty("security.jmx.principal-database(0)", "mydb"); - _serverConfig = new ServerConfiguration(_config); - - try - { - _serverConfig.initialise(); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", - "Validation error : security/jmx/principal-database is no longer a supported element within the configuration xml.", - ce.getMessage()); - } - } - - /** - * Tests that the old element security.principal-databases. ... (that used to define - * principal databases) is rejected. - */ - public void testPrincipalDatabasesRejected() throws ConfigurationException - { - _serverConfig.initialise(); - - // Check value we set - _config.setProperty("security.principal-databases.principal-database.class", "myclass"); - _serverConfig = new ServerConfiguration(_config); - - try - { - _serverConfig.initialise(); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", - "Validation error : security/principal-databases is no longer supported within the configuration xml.", - ce.getMessage()); - } - } - - /** - * Tests that the old element housekeeping.expiredMessageCheckPeriod. ... (that was - * replaced by housekeeping.checkPeriod) is rejected. - */ - public void testExpiredMessageCheckPeriodRejected() throws ConfigurationException - { - _serverConfig.initialise(); - - // Check value we set - _config.setProperty("housekeeping.expiredMessageCheckPeriod", 23L); - _serverConfig = new ServerConfiguration(_config); - - try - { - _serverConfig.initialise(); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - assertEquals("Incorrect error message", - "Validation error : housekeeping/expiredMessageCheckPeriod must be replaced by housekeeping/checkPeriod.", - ce.getMessage()); - } - } - - public void testMaxDeliveryCountDefault() throws Exception - { - final ServerConfiguration serverConfig = new ServerConfiguration(_config); - assertEquals(0, serverConfig.getMaxDeliveryCount()); - } - - public void testMaxDeliveryCount() throws Exception - { - _config.setProperty("maximumDeliveryCount", 5); - final ServerConfiguration serverConfig = new ServerConfiguration(_config); - assertEquals(5, serverConfig.getMaxDeliveryCount()); - } - - /** - * Test XML configuration file correctly enables dead letter queues - */ - public void testDeadLetterQueueConfigurationFile() throws Exception - { - // Write config - File xml = File.createTempFile(getClass().getName(), "xml"); - xml.deleteOnExit(); - FileWriter config = new FileWriter(xml); - config.write("<broker>\n"); - writeSecurity(config); - config.write("<deadLetterQueues>true</deadLetterQueues>\n"); - config.write("<virtualhosts>\n"); - config.write("<virtualhost>\n"); - config.write("<name>test</name>\n"); - config.write("<test>\n"); - config.write("<queues>\n"); - config.write("<deadLetterQueues>false</deadLetterQueues>\n"); - config.write("<queue>\n"); - config.write("<name>biggles</name>\n"); - config.write("<biggles>\n"); - config.write("<deadLetterQueues>true</deadLetterQueues>\n"); - config.write("</biggles>\n"); - config.write("</queue>\n"); - config.write("<queue>\n"); - config.write("<name>beetle</name>\n"); - config.write("<beetle />\n"); - config.write("</queue>\n"); - config.write("</queues>\n"); - config.write("</test>\n"); - config.write("</virtualhost>\n"); - config.write("<virtualhost>\n"); - config.write("<name>extra</name>\n"); - config.write("<extra>\n"); - config.write("<queues>\n"); - config.write("<queue>\n"); - config.write("<name>r2d2</name>\n"); - config.write("<r2d2>\n"); - config.write("<deadLetterQueues>false</deadLetterQueues>\n"); - config.write("</r2d2>\n"); - config.write("</queue>\n"); - config.write("<queue>\n"); - config.write("<name>c3p0</name>\n"); - config.write("<c3p0 />\n"); - config.write("</queue>\n"); - config.write("</queues>\n"); - config.write("</extra>\n"); - config.write("</virtualhost>\n"); - config.write("</virtualhosts>\n"); - config.write("</broker>\n"); - config.close(); - - // Load config - ApplicationRegistry.remove(); - ApplicationRegistry registry = new ApplicationRegistry(new ServerConfiguration(xml)); - ApplicationRegistry.initialise(registry); - ServerConfiguration serverConfiguration = ApplicationRegistry.getInstance().getConfiguration(); - - VirtualHostConfiguration test = serverConfiguration.getVirtualHostConfig("test"); - assertNotNull("Host 'test' is not found", test); - VirtualHostConfiguration extra = serverConfiguration.getVirtualHostConfig("extra"); - assertNotNull("Host 'extra' is not found", test); - - QueueConfiguration biggles = test.getQueueConfiguration("biggles"); - QueueConfiguration beetle = test.getQueueConfiguration("beetle"); - QueueConfiguration r2d2 = extra.getQueueConfiguration("r2d2"); - QueueConfiguration c3p0 = extra.getQueueConfiguration("c3p0"); - - // Validate config - assertTrue("Broker DLQ should be configured as enabled", serverConfiguration.isDeadLetterQueueEnabled()); - assertFalse("Test vhost DLQ should be configured as disabled", test.isDeadLetterQueueEnabled()); - assertTrue("Extra vhost DLQ should be enabled, using broker default", extra.isDeadLetterQueueEnabled()); - assertTrue("Biggles queue DLQ should be configured as enabled", biggles.isDeadLetterQueueEnabled()); - assertFalse("Beetle queue DLQ should be disabled, using test vhost default", beetle.isDeadLetterQueueEnabled()); - assertFalse("R2D2 queue DLQ should be configured as disabled", r2d2.isDeadLetterQueueEnabled()); - assertTrue("C3P0 queue DLQ should be enabled, using broker default", c3p0.isDeadLetterQueueEnabled()); - } - - public void testIsAmqp010enabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.isAmqp010enabled()); - - // Check value we set - _config.setProperty(ServerConfiguration.CONNECTOR_AMQP010ENABLED, false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.isAmqp010enabled()); - } - - public void testIsAmqp091enabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.isAmqp091enabled()); - - // Check value we set - _config.setProperty(ServerConfiguration.CONNECTOR_AMQP091ENABLED, false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.isAmqp091enabled()); - } - - public void testIsAmqp09enabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.isAmqp09enabled()); - - // Check value we set - _config.setProperty(ServerConfiguration.CONNECTOR_AMQP09ENABLED, false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.isAmqp09enabled()); - } - - public void testIsAmqp08enabled() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.isAmqp08enabled()); - - // Check value we set - _config.setProperty(ServerConfiguration.CONNECTOR_AMQP08ENABLED, false); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(false, _serverConfig.isAmqp08enabled()); - } - - public void testPortInclude08() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getPortInclude08().isEmpty()); - - // Check values we set - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_08, "1"); - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_08, "2"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(2, _serverConfig.getPortInclude08().size()); - assertTrue(_serverConfig.getPortInclude08().contains("1")); - assertTrue(_serverConfig.getPortInclude08().contains("2")); - } - - public void testPortInclude09() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getPortInclude09().isEmpty()); - - // Check values we set - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_09, "3"); - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_09, "4"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(2, _serverConfig.getPortInclude09().size()); - assertTrue(_serverConfig.getPortInclude09().contains("3")); - assertTrue(_serverConfig.getPortInclude09().contains("4")); - } - - public void testPortInclude091() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getPortInclude091().isEmpty()); - - // Check values we set - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_091, "5"); - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_091, "6"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(2, _serverConfig.getPortInclude091().size()); - assertTrue(_serverConfig.getPortInclude091().contains("5")); - assertTrue(_serverConfig.getPortInclude091().contains("6")); - } - - public void testPortInclude010() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getPortInclude010().isEmpty()); - - // Check values we set - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_010, "7"); - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_010, "8"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(2, _serverConfig.getPortInclude010().size()); - assertTrue(_serverConfig.getPortInclude010().contains("7")); - assertTrue(_serverConfig.getPortInclude010().contains("8")); - } - - public void testPortInclude10() throws ConfigurationException - { - // Check default - _serverConfig.initialise(); - assertEquals(true, _serverConfig.getPortInclude10().isEmpty()); - - // Check values we set - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_10, "9"); - _config.addProperty(ServerConfiguration.CONNECTOR_INCLUDE_10, "10"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(2, _serverConfig.getPortInclude10().size()); - assertTrue(_serverConfig.getPortInclude10().contains("9")); - assertTrue(_serverConfig.getPortInclude10().contains("10")); - } - - public void testGetDefaultSupportedProtocolReply() throws Exception - { - // Check default - _serverConfig.initialise(); - assertNull("unexpected default value", _serverConfig.getDefaultSupportedProtocolReply()); - - // Check values we set - _config.addProperty(ServerConfiguration.CONNECTOR_AMQP_SUPPORTED_REPLY, "v0_10"); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(AmqpProtocolVersion.v0_10, _serverConfig.getDefaultSupportedProtocolReply()); - } - - public void testDefaultAuthenticationManager() throws Exception - { - // Check default - _serverConfig.initialise(); - assertNull("unexpected default value", _serverConfig.getDefaultAuthenticationManager()); - - // Check values we set - String testAuthManager = "myauthmanager"; - _config.addProperty("security.default-auth-manager", testAuthManager); - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals(testAuthManager, _serverConfig.getDefaultAuthenticationManager()); - } - - public void testPortAuthenticationMappingsDefault() throws Exception - { - _serverConfig.initialise(); - assertEquals("unexpected default number of port/authmanager mappings", 0, _serverConfig.getPortAuthenticationMappings().size()); - } - - public void testPortAuthenticationMappingsWithSingleMapping() throws Exception - { - String testAuthManager = "myauthmanager"; - _config.addProperty("security.port-mappings.port-mapping.port", 1234); - _config.addProperty("security.port-mappings.port-mapping.auth-manager", testAuthManager); - - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - assertEquals("unexpected number of port/authmanager mappings", 1, _serverConfig.getPortAuthenticationMappings().size()); - assertEquals("unexpected mapping for port", testAuthManager, _serverConfig.getPortAuthenticationMappings().get(1234)); - } - - public void testPortAuthenticationMappingsWithManyMapping() throws Exception - { - String testAuthManager1 = "myauthmanager1"; - String testAuthManager2 = "myauthmanager2"; - _config.addProperty("security.port-mappings.port-mapping(-1).port", 1234); - _config.addProperty("security.port-mappings.port-mapping.auth-manager", testAuthManager1); - - _config.addProperty("security.port-mappings.port-mapping(-1).port", 2345); - _config.addProperty("security.port-mappings.port-mapping.auth-manager", testAuthManager2); - - _serverConfig = new ServerConfiguration(_config); - _serverConfig.initialise(); - - assertEquals("unexpected number of port/authmanager mappings", 2, _serverConfig.getPortAuthenticationMappings().size()); - assertEquals("unexpected mapping for port", testAuthManager1, _serverConfig.getPortAuthenticationMappings().get(1234)); - assertEquals("unexpected mapping for port", testAuthManager2, _serverConfig.getPortAuthenticationMappings().get(2345)); - } - - public void testPortAuthenticationMappingWithMissingAuthManager() throws Exception - { - _config.addProperty("security.port-mappings.port-mapping(-1).port", 1234); - // no auth manager defined for port - _serverConfig = new ServerConfiguration(_config); - try - { - _serverConfig.initialise(); - fail("Exception not thrown"); - } - catch(ConfigurationException ce) - { - // PASS - assertEquals("Incorrect error message", - "Validation error: Each port-mapping must have exactly one port and exactly one auth-manager.", - ce.getMessage()); - } - } - - /** - * Convenience method to output required security preamble for broker config - */ - private void writeSecurity(Writer out) throws Exception - { - out.write("\t<management><enabled>false</enabled></management>\n"); - out.write("\t<security>\n"); - out.write("\t\t<pd-auth-manager>\n"); - out.write("\t\t\t<principal-database>\n"); - out.write("\t\t\t\t<class>org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase</class>\n"); - out.write("\t\t\t\t<attributes>\n"); - out.write("\t\t\t\t\t<attribute>\n"); - out.write("\t\t\t\t\t\t<name>passwordFile</name>\n"); - out.write("\t\t\t\t\t\t<value>/dev/null</value>\n"); - out.write("\t\t\t\t\t</attribute>\n"); - out.write("\t\t\t\t</attributes>\n"); - out.write("\t\t\t</principal-database>\n"); - out.write("\t\t</pd-auth-manager>\n"); - out.write("\t</security>\n"); - } -} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/VirtualHostConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/VirtualHostConfigurationTest.java index 25e066de76..570bd004c5 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/VirtualHostConfigurationTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/VirtualHostConfigurationTest.java @@ -19,24 +19,69 @@ */ package org.apache.qpid.server.configuration; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; import org.apache.qpid.framing.AMQShortString; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.queue.AMQPriorityQueue; import org.apache.qpid.server.queue.AMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.TestableMemoryMessageStore; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; +import org.apache.qpid.test.utils.QpidTestCase; -public class VirtualHostConfigurationTest extends InternalBrokerBaseCase +public class VirtualHostConfigurationTest extends QpidTestCase { + private VirtualHostRegistry _virtualHostRegistry; + private XMLConfiguration _configXml; + private Broker _broker; @Override - public void createBroker() + public void setUp() throws Exception { - // Prevent auto broker startup + super.setUp(); + BrokerTestHelper.setUp(); + _configXml = new XMLConfiguration(); + _configXml.addProperty("virtualhosts.virtualhost(-1).name", getName()); + _configXml.addProperty("virtualhosts.virtualhost(-1)."+getName()+".store.class", TestableMemoryMessageStore.class.getName()); + _virtualHostRegistry = new VirtualHostRegistry(); + _broker = mock(Broker.class); + when(_broker.getAttribute(Broker.HOUSEKEEPING_CHECK_PERIOD)).thenReturn(30000l); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_virtualHostRegistry != null) + { + _virtualHostRegistry.close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + } + + private XMLConfiguration getConfigXml() + { + return _configXml; + } + + private VirtualHost createVirtualHost(String hostName) throws Exception + { + Configuration config = getConfigXml().subset("virtualhosts.virtualhost." + XmlConfigurationUtilities.escapeTagName(hostName)); + VirtualHostConfiguration virtualHostConfiguration = new VirtualHostConfiguration(hostName, config, _broker); + return BrokerTestHelper.createVirtualHost(virtualHostConfiguration, _virtualHostRegistry); } public void testQueuePriority() throws Exception @@ -65,11 +110,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase getConfigXml().addProperty("virtualhosts.virtualhost.testQueuePriority.queues.queue.ntest.priority", "false"); - // Start the broker now. - super.createBroker(); - - VirtualHost vhost = - ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); + VirtualHost vhost = createVirtualHost(getName()); // Check that atest was a priority queue with 5 priorities AMQQueue atest = vhost.getQueueRegistry().getQueue(new AMQShortString("atest")); @@ -102,11 +143,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase getConfigXml().addProperty("virtualhosts.virtualhost.testQueueAlerts.queues(-1).queue(-1).name(-1)", "btest"); - // Start the broker now. - super.createBroker(); - - VirtualHost vhost = - ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); + VirtualHost vhost = createVirtualHost(getName()); // Check specifically configured values AMQQueue aTest = vhost.getQueueRegistry().getQueue(new AMQShortString("atest")); @@ -129,11 +166,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase getConfigXml().addProperty("virtualhosts.virtualhost." + getName() + ".queues.queue.biggles.maximumDeliveryCount", 4); getConfigXml().addProperty("virtualhosts.virtualhost." + getName() + ".queues(-1).queue(-1).name", "beetle"); - // Start the broker now. - super.createBroker(); - - // Get vhosts - VirtualHost test = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); + VirtualHost test = createVirtualHost(getName()); // Enabled specifically assertEquals("Test vhost MDC was configured as enabled", 5 ,test.getConfiguration().getMaxDeliveryCount()); @@ -163,12 +196,8 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase getConfigXml().addProperty("virtualhosts.virtualhost." + getName() + "Extra.queues(-1).queue(-1).name", "c3p0"); getConfigXml().addProperty("virtualhosts.virtualhost." + getName() + "Extra.store.class", TestableMemoryMessageStore.class.getName()); - // Start the broker now. - super.createBroker(); - - // Get vhosts - VirtualHost test = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); - VirtualHost extra = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName() + "Extra"); + VirtualHost test = createVirtualHost(getName()); + VirtualHost extra = createVirtualHost(getName() + "Extra"); // Enabled specifically assertTrue("Test vhost DLQ was configured as enabled", test.getConfiguration().isDeadLetterQueueEnabled()); @@ -215,37 +244,13 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase getConfigXml().addProperty("virtualhosts.virtualhost.testHouseKeepingThreadCount.housekeeping.poolSize", initialPoolSize); - // Start the broker now. - super.createBroker(); - - VirtualHost vhost = - ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); + VirtualHost vhost = createVirtualHost(getName()); assertEquals("HouseKeeping PoolSize not set correctly.", initialPoolSize, vhost.getHouseKeepingPoolSize()); } /** - * Test default house keeping tasks - * - * @throws Exception - */ - public void testDefaultHouseKeepingTasks() throws Exception - { - // Start the broker now. - super.createBroker(); - - VirtualHost vhost = - ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); - - assertEquals("Default houseKeeping task count incorrect.", 1, - vhost.getHouseKeepingTaskCount()); - - // Currently the task is: - // ExpiredMessageTask from VirtualHostC - } - - /** * Test that we can dynamically change the thread pool size * * @throws Exception @@ -257,11 +262,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase getConfigXml().addProperty("virtualhosts.virtualhost.testDynamicHouseKeepingPoolSizeChange.housekeeping.poolSize", initialPoolSize); - // Start the broker now. - super.createBroker(); - - VirtualHost vhost = - ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); + VirtualHost vhost = createVirtualHost(getName()); assertEquals("HouseKeeping PoolSize not set correctly.", initialPoolSize, vhost.getHouseKeepingPoolSize()); @@ -285,7 +286,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase try { - super.createBroker(); + createVirtualHost(getName()); fail("Exception not thrown"); } catch(ConfigurationException ce) @@ -308,7 +309,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase try { - super.createBroker(); + createVirtualHost(getName()); fail("Exception not thrown"); } catch (ConfigurationException ce) @@ -332,11 +333,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase // Add a single property which is inside the <dot.in.a.name> queue tag - the maximum delivery count getConfigXml().addProperty("virtualhosts.virtualhost." + getName() + ".queues.queue.dot..in..a..name.maximumDeliveryCount", 5); - // Start the broker now. - super.createBroker(); - - // Get vhosts - VirtualHost test = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName()); + VirtualHost test = createVirtualHost(getName()); // Check, that the property stored within the <dot.in.a.name> tag has been properly loaded assertEquals("queue with dots in its name has been properly loaded", 5, test.getConfiguration().getQueueConfiguration("dot.in.a.name").getMaxDeliveryCount()); @@ -355,11 +352,7 @@ public class VirtualHostConfigurationTest extends InternalBrokerBaseCase // Add a single property which is inside the <dot.in.a.name> virtual host tag - the message store getConfigXml().addProperty("virtualhosts.virtualhost.dot..in..a..name.store.class", TestableMemoryMessageStore.class.getName()); - // Start the broker now. - super.createBroker(); - - // Get vhosts - VirtualHost test = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost("dot.in.a.name"); + VirtualHost test = createVirtualHost("dot.in.a.name"); // Check, that the property stored within the <dot.in.a.name> tag has been properly loaded assertEquals("virtual host with dots in the name has been properly loaded", TestableMemoryMessageStore.class.getName(), test.getMessageStore().getClass().getName()); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/plugins/AbstractConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/plugins/AbstractConfigurationTest.java index 164e54ac3e..674abbfeb7 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/plugins/AbstractConfigurationTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/plugins/AbstractConfigurationTest.java @@ -24,7 +24,7 @@ import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.List; @@ -32,7 +32,7 @@ import java.util.List; * Test that verifies that given a Configuration a ConfigurationPlugin can * process and validate that data. */ -public class AbstractConfigurationTest extends InternalBrokerBaseCase +public class AbstractConfigurationTest extends QpidTestCase { private static final double DOUBLE = 3.14; private static final long POSITIVE_LONG = 1000; diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/BrokerRecovererTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/BrokerRecovererTest.java new file mode 100644 index 0000000000..c1ebe26f52 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/BrokerRecovererTest.java @@ -0,0 +1,405 @@ +/* + * + * 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.startup; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; + +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 junit.framework.TestCase; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.Plugin; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.model.adapter.AuthenticationProviderFactory; +import org.apache.qpid.server.model.adapter.PortFactory; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; + +public class BrokerRecovererTest extends TestCase +{ + private BrokerRecoverer _brokerRecoverer; + private ConfigurationEntry _brokerEntry = mock(ConfigurationEntry.class); + + private UUID _brokerId = UUID.randomUUID(); + private Map<String, Collection<ConfigurationEntry>> _brokerEntryChildren = new HashMap<String, Collection<ConfigurationEntry>>(); + private ConfigurationEntry _authenticationProviderEntry1; + private AuthenticationProvider _authenticationProvider1; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + + _brokerRecoverer = new BrokerRecoverer(mock(AuthenticationProviderFactory.class), mock(PortFactory.class), mock(StatisticsGatherer.class), + mock(VirtualHostRegistry.class), mock(LogRecorder.class), mock(RootMessageLogger.class), mock(TaskExecutor.class)); + when(_brokerEntry.getId()).thenReturn(_brokerId); + when(_brokerEntry.getChildren()).thenReturn(_brokerEntryChildren); + + //Add a base AuthenticationProvider for all tests + _authenticationProvider1 = mock(AuthenticationProvider.class); + when(_authenticationProvider1.getName()).thenReturn("authenticationProvider1"); + _authenticationProviderEntry1 = mock(ConfigurationEntry.class); + _brokerEntryChildren.put(AuthenticationProvider.class.getSimpleName(), Arrays.asList(_authenticationProviderEntry1)); + } + + public void testCreateBrokerAttributes() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Broker.DEFAULT_VIRTUAL_HOST, "test"); + attributes.put(Broker.DEFAULT_AUTHENTICATION_PROVIDER, "authenticationProvider1"); + attributes.put(Broker.ALERT_THRESHOLD_MESSAGE_AGE, 9l); + attributes.put(Broker.ALERT_THRESHOLD_MESSAGE_COUNT, 8l); + attributes.put(Broker.ALERT_THRESHOLD_QUEUE_DEPTH, 7l); + attributes.put(Broker.ALERT_THRESHOLD_MESSAGE_SIZE, 6l); + attributes.put(Broker.ALERT_REPEAT_GAP, 5l); + attributes.put(Broker.FLOW_CONTROL_SIZE_BYTES, 5l); + attributes.put(Broker.FLOW_CONTROL_RESUME_SIZE_BYTES, 3l); + attributes.put(Broker.MAXIMUM_DELIVERY_ATTEMPTS, 2); + attributes.put(Broker.DEAD_LETTER_QUEUE_ENABLED, true); + attributes.put(Broker.HOUSEKEEPING_CHECK_PERIOD, 1l); + attributes.put(Broker.ACL_FILE, "/path/to/acl"); + attributes.put(Broker.SESSION_COUNT_LIMIT, 1000); + attributes.put(Broker.HEART_BEAT_DELAY, 2000); + attributes.put(Broker.STATISTICS_REPORTING_PERIOD, 4000); + attributes.put(Broker.STATISTICS_REPORTING_RESET_ENABLED, true); + + Map<String, Object> entryAttributes = new HashMap<String, Object>(); + for (Map.Entry<String, Object> attribute : attributes.entrySet()) + { + String value = convertToString(attribute.getValue()); + entryAttributes.put(attribute.getKey(), value); + } + + when(_brokerEntry.getAttributes()).thenReturn(entryAttributes); + + final ConfigurationEntry virtualHostEntry = mock(ConfigurationEntry.class); + String typeName = VirtualHost.class.getSimpleName(); + when(virtualHostEntry.getType()).thenReturn(typeName); + _brokerEntryChildren.put(typeName, Arrays.asList(virtualHostEntry)); + final VirtualHost virtualHost = mock(VirtualHost.class); + when(virtualHost.getName()).thenReturn("test"); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[] { virtualHostEntry, _authenticationProviderEntry1 }, + new ConfiguredObject[] { virtualHost, _authenticationProvider1 }); + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + + for (Map.Entry<String, Object> attribute : attributes.entrySet()) + { + Object attributeValue = broker.getAttribute(attribute.getKey()); + assertEquals("Unexpected value of attribute '" + attribute.getKey() + "'", attribute.getValue(), attributeValue); + } + } + + public void testCreateBrokerWithVirtualHost() + { + final ConfigurationEntry virtualHostEntry = mock(ConfigurationEntry.class); + + String typeName = VirtualHost.class.getSimpleName(); + when(virtualHostEntry.getType()).thenReturn(typeName); + _brokerEntryChildren.put(typeName, Arrays.asList(virtualHostEntry)); + + final VirtualHost virtualHost = mock(VirtualHost.class); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{virtualHostEntry, _authenticationProviderEntry1}, + new ConfiguredObject[]{virtualHost, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(1, broker.getVirtualHosts().size()); + assertEquals(virtualHost, broker.getVirtualHosts().iterator().next()); + } + + public void testCreateBrokerWithPorts() + { + ConfigurationEntry portEntry = mock(ConfigurationEntry.class); + Port port = mock(Port.class); + _brokerEntryChildren.put(Port.class.getSimpleName(), Arrays.asList(portEntry)); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{portEntry, _authenticationProviderEntry1}, + new ConfiguredObject[]{port, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(Collections.singletonList(port), broker.getPorts()); + } + + public void testCreateBrokerWithoutAuthenticationProviderThrowsException() + { + assertNotNull("expected to remove the base entry", _brokerEntryChildren.remove(AuthenticationProvider.class.getSimpleName())); + assertTrue("should be empty", _brokerEntryChildren.isEmpty()); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[0], new ConfiguredObject[0]); + + try + { + _brokerRecoverer.create(recovererProvider, _brokerEntry); + fail("should have thrown an exception due to missing authentication provider configuration"); + } + catch(IllegalConfigurationException e) + { + //expected + } + } + + public void testCreateBrokerWithOneAuthenticationProvider() + { + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{_authenticationProviderEntry1}, + new ConfiguredObject[]{_authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(Collections.singletonList(_authenticationProvider1), broker.getAuthenticationProviders()); + } + + public void testCreateBrokerWithMultipleAuthenticationProvidersAndNoDefaultThrowsException() + { + AuthenticationProvider authenticationProvider2 = mock(AuthenticationProvider.class); + when(authenticationProvider2.getName()).thenReturn("authenticationProvider2"); + ConfigurationEntry authenticationProviderEntry2 = mock(ConfigurationEntry.class); + _brokerEntryChildren.put(AuthenticationProvider.class.getSimpleName(), Arrays.asList(_authenticationProviderEntry1, authenticationProviderEntry2)); + + Map<String,Object> emptyBrokerAttributes = new HashMap<String,Object>(); + when(_brokerEntry.getAttributes()).thenReturn(emptyBrokerAttributes); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{authenticationProviderEntry2, _authenticationProviderEntry1}, + new ConfiguredObject[]{authenticationProvider2, _authenticationProvider1}); + try + { + _brokerRecoverer.create(recovererProvider, _brokerEntry); + fail("should have thrown an exception due to missing authentication provider default"); + } + catch(IllegalConfigurationException e) + { + //expected + } + } + + public void testCreateBrokerWithMultipleAuthenticationProvidersAndPorts() + { + //Create a second authentication provider + AuthenticationProvider authenticationProvider2 = mock(AuthenticationProvider.class); + when(authenticationProvider2.getName()).thenReturn("authenticationProvider2"); + ConfigurationEntry authenticationProviderEntry2 = mock(ConfigurationEntry.class); + _brokerEntryChildren.put(AuthenticationProvider.class.getSimpleName(), Arrays.asList(_authenticationProviderEntry1, authenticationProviderEntry2)); + + //Set the default authentication provider + Map<String,Object> brokerAtttributes = new HashMap<String,Object>(); + when(_brokerEntry.getAttributes()).thenReturn(brokerAtttributes); + brokerAtttributes.put(Broker.DEFAULT_AUTHENTICATION_PROVIDER, "authenticationProvider2"); + + //Add a couple ports, one with a defined authentication provider and + //one without (which should then use the default) + ConfigurationEntry portEntry1 = mock(ConfigurationEntry.class); + Port port1 = mock(Port.class); + when(port1.getName()).thenReturn("port1"); + when(port1.getPort()).thenReturn(5671); + when(port1.getAttribute(Port.AUTHENTICATION_MANAGER)).thenReturn("authenticationProvider1"); + ConfigurationEntry portEntry2 = mock(ConfigurationEntry.class); + Port port2 = mock(Port.class); + when(port2.getName()).thenReturn("port2"); + when(port2.getPort()).thenReturn(5672); + _brokerEntryChildren.put(Port.class.getSimpleName(), Arrays.asList(portEntry1, portEntry2)); + + RecovererProvider recovererProvider = createRecoveryProvider( + new ConfigurationEntry[]{portEntry1, portEntry2, authenticationProviderEntry2, _authenticationProviderEntry1}, + new ConfiguredObject[]{port1, port2, authenticationProvider2, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals("Unexpected number of authentication providers", 2,broker.getAuthenticationProviders().size()); + + Collection<Port> ports = broker.getPorts(); + assertEquals("Unexpected number of ports", 2, ports.size()); + assertTrue(ports.contains(port1)); + assertTrue(ports.contains(port2)); + + verify(port1).setAuthenticationProvider(any(AuthenticationProvider.class)); + verify(port1).setAuthenticationProvider(_authenticationProvider1); + + verify(port2).setAuthenticationProvider(any(AuthenticationProvider.class)); + verify(port2).setAuthenticationProvider(authenticationProvider2); + } + + public void testCreateBrokerAssignsGroupAccessorToAuthenticationProviders() + { + //Create a second authentication provider + AuthenticationProvider authenticationProvider2 = mock(AuthenticationProvider.class); + when(authenticationProvider2.getName()).thenReturn("authenticationProvider2"); + ConfigurationEntry authenticationProviderEntry2 = mock(ConfigurationEntry.class); + _brokerEntryChildren.put(AuthenticationProvider.class.getSimpleName(), Arrays.asList(_authenticationProviderEntry1, authenticationProviderEntry2)); + + //Set the default authentication provider + Map<String,Object> brokerAtttributes = new HashMap<String,Object>(); + when(_brokerEntry.getAttributes()).thenReturn(brokerAtttributes); + brokerAtttributes.put(Broker.DEFAULT_AUTHENTICATION_PROVIDER, "authenticationProvider2"); + + //Create a group provider + ConfigurationEntry groupProviderEntry = mock(ConfigurationEntry.class); + GroupProvider groupProvider = mock(GroupProvider.class); + _brokerEntryChildren.put(GroupProvider.class.getSimpleName(), Arrays.asList(groupProviderEntry)); + + RecovererProvider recovererProvider = createRecoveryProvider( + new ConfigurationEntry[]{groupProviderEntry, authenticationProviderEntry2, _authenticationProviderEntry1}, + new ConfiguredObject[]{groupProvider, authenticationProvider2, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals("Unexpected number of authentication providers", 2, broker.getAuthenticationProviders().size()); + + //verify that a GroupAcessor was added to the AuthenticationProviders + verify(_authenticationProvider1).setGroupAccessor(any(GroupPrincipalAccessor.class)); + verify(authenticationProvider2).setGroupAccessor(any(GroupPrincipalAccessor.class)); + } + + public void testCreateBrokerWithGroupProvider() + { + ConfigurationEntry groupProviderEntry = mock(ConfigurationEntry.class); + GroupProvider groupProvider = mock(GroupProvider.class); + _brokerEntryChildren.put(GroupProvider.class.getSimpleName(), Arrays.asList(groupProviderEntry)); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{groupProviderEntry, _authenticationProviderEntry1}, + new ConfiguredObject[]{groupProvider, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(Collections.singletonList(groupProvider), broker.getGroupProviders()); + } + + public void testCreateBrokerWithPlugins() + { + ConfigurationEntry pluginEntry = mock(ConfigurationEntry.class); + Plugin plugin = mock(Plugin.class); + _brokerEntryChildren.put(Plugin.class.getSimpleName(), Arrays.asList(pluginEntry)); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{pluginEntry, _authenticationProviderEntry1}, + new ConfiguredObject[]{plugin, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(Collections.singleton(plugin), new HashSet<ConfiguredObject>(broker.getChildren(Plugin.class))); + } + + public void testCreateBrokerWithKeyStores() + { + ConfigurationEntry pluginEntry = mock(ConfigurationEntry.class); + KeyStore keyStore = mock(KeyStore.class); + _brokerEntryChildren.put(KeyStore.class.getSimpleName(), Arrays.asList(pluginEntry)); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{pluginEntry, _authenticationProviderEntry1}, + new ConfiguredObject[]{keyStore, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(Collections.singleton(keyStore), new HashSet<ConfiguredObject>(broker.getChildren(KeyStore.class))); + } + + public void testCreateBrokerWithTrustStores() + { + ConfigurationEntry pluginEntry = mock(ConfigurationEntry.class); + TrustStore trustStore = mock(TrustStore.class); + _brokerEntryChildren.put(TrustStore.class.getSimpleName(), Arrays.asList(pluginEntry)); + + RecovererProvider recovererProvider = createRecoveryProvider(new ConfigurationEntry[]{pluginEntry, _authenticationProviderEntry1}, + new ConfiguredObject[]{trustStore, _authenticationProvider1}); + + Broker broker = _brokerRecoverer.create(recovererProvider, _brokerEntry); + + assertNotNull(broker); + assertEquals(_brokerId, broker.getId()); + assertEquals(Collections.singleton(trustStore), new HashSet<ConfiguredObject>(broker.getChildren(TrustStore.class))); + } + + private String convertToString(Object attributeValue) + { + return String.valueOf(attributeValue); + } + + private RecovererProvider createRecoveryProvider(final ConfigurationEntry[] entries, final ConfiguredObject[] objectsToRecoverer) + { + RecovererProvider recovererProvider = new RecovererProvider() + { + @Override + public ConfiguredObjectRecoverer<? extends ConfiguredObject> getRecoverer(String type) + { + @SuppressWarnings({ "unchecked", "rawtypes" }) + final ConfiguredObjectRecoverer<? extends ConfiguredObject> recovever = new ConfiguredObjectRecoverer() + { + @Override + public ConfiguredObject create(RecovererProvider recovererProvider, ConfigurationEntry entry, ConfiguredObject... parents) + { + for (int i = 0; i < entries.length; i++) + { + ConfigurationEntry e = entries[i]; + if (entry == e) + { + return objectsToRecoverer[i]; + } + } + return null; + } + }; + + return recovever; + } + }; + return recovererProvider; + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/DefaultRecovererProviderTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/DefaultRecovererProviderTest.java new file mode 100644 index 0000000000..c95f67beb9 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/DefaultRecovererProviderTest.java @@ -0,0 +1,62 @@ +/* + * + * 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.startup; + +import static org.mockito.Mockito.mock; +import junit.framework.TestCase; + +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.Plugin; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; + +public class DefaultRecovererProviderTest extends TestCase +{ + public void testGetRecoverer() + { + String[] supportedTypes = {Broker.class.getSimpleName(), + VirtualHost.class.getSimpleName(), AuthenticationProvider.class.getSimpleName(), + GroupProvider.class.getSimpleName(), Plugin.class.getSimpleName(), Port.class.getSimpleName()}; + + // mocking the required object + StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); + VirtualHostRegistry virtualHostRegistry = mock(VirtualHostRegistry.class); + LogRecorder logRecorder = mock(LogRecorder.class); + RootMessageLogger rootMessageLogger = mock(RootMessageLogger.class); + TaskExecutor taskExecutor = mock(TaskExecutor.class); + + DefaultRecovererProvider provider = new DefaultRecovererProvider(statisticsGatherer, virtualHostRegistry, logRecorder, rootMessageLogger, taskExecutor); + for (String configuredObjectType : supportedTypes) + { + ConfiguredObjectRecoverer<?> recovever = provider.getRecoverer(configuredObjectType); + assertNotNull("Null recoverer for type: " + configuredObjectType, recovever); + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/GroupProviderRecovererTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/GroupProviderRecovererTest.java new file mode 100644 index 0000000000..6713574e4b --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/GroupProviderRecovererTest.java @@ -0,0 +1,97 @@ +/* + * + * 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.startup; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.plugin.GroupManagerFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; +import org.apache.qpid.server.security.group.GroupManager; + +import junit.framework.TestCase; + +public class GroupProviderRecovererTest extends TestCase +{ + + private UUID _id; + private Map<String, Object> _attributes; + + private GroupManagerFactory _factory; + private QpidServiceLoader<GroupManagerFactory> _groupManagerServiceLoader; + private Broker _broker; + private ConfigurationEntry _configurationEntry; + + @SuppressWarnings("unchecked") + protected void setUp() throws Exception + { + super.setUp(); + _id = UUID.randomUUID(); + _attributes = new HashMap<String, Object>(); + + _factory = mock(GroupManagerFactory.class); + + _groupManagerServiceLoader = mock(QpidServiceLoader.class); + when(_groupManagerServiceLoader.instancesOf(GroupManagerFactory.class)).thenReturn(Collections.singletonList(_factory )); + + _broker = mock(Broker.class); + + _configurationEntry = mock(ConfigurationEntry.class); + when(_configurationEntry.getId()).thenReturn(_id); + when(_configurationEntry.getAttributes()).thenReturn(_attributes); + } + + public void testCreate() + { + GroupManager groupManager = mock(GroupManager.class); + String name = groupManager.getClass().getSimpleName(); + when(_factory.createInstance(_attributes)).thenReturn(groupManager); + GroupProviderRecoverer groupProviderRecoverer = new GroupProviderRecoverer(_groupManagerServiceLoader); + GroupProvider groupProvider = groupProviderRecoverer.create(null, _configurationEntry, _broker); + assertNotNull("Null group provider", groupProvider); + assertEquals("Unexpected name", name, groupProvider.getName()); + assertEquals("Unexpected ID", _id, groupProvider.getId()); + } + + public void testCreateThrowsExceptionWhenNoGroupManagerIsCreated() + { + when(_factory.createInstance(_attributes)).thenReturn(null); + + GroupProviderRecoverer groupProviderRecoverer = new GroupProviderRecoverer(_groupManagerServiceLoader); + try + { + groupProviderRecoverer.create(null, _configurationEntry, _broker); + fail("Configuration exception should be thrown when group manager is not created"); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/KeyStoreRecovererTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/KeyStoreRecovererTest.java new file mode 100644 index 0000000000..0d7dc1bb06 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/KeyStoreRecovererTest.java @@ -0,0 +1,111 @@ +/* + * + * 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.startup; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.TrustStore; + +import junit.framework.TestCase; + +public class KeyStoreRecovererTest extends TestCase +{ + + public void testCreateWithAllAttributesProvided() + { + Map<String, Object> attributes = getKeyStoreAttributes(); + + UUID id = UUID.randomUUID(); + Broker broker = mock(Broker.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + when(entry.getAttributes()).thenReturn(attributes); + when(entry.getId()).thenReturn(id); + + KeyStoreRecoverer recovever = new KeyStoreRecoverer(); + + KeyStore KeyStore = recovever.create(null, entry, broker); + assertNotNull("Key store configured object is not created", KeyStore); + assertEquals(id, KeyStore.getId()); + assertEquals("my-secret-password", KeyStore.getPassword()); + + assertNull("Password was unexpectedly returned from configured object", KeyStore.getAttribute(TrustStore.PASSWORD)); + + // password attribute should not be exposed by a key store configured object + // so, we should set password value to null in the map being used to create the key store configured object + attributes.put(KeyStore.PASSWORD, null); + for (Map.Entry<String, Object> attribute : attributes.entrySet()) + { + Object attributeValue = KeyStore.getAttribute(attribute.getKey()); + assertEquals("Unexpected value of attribute '" + attribute.getKey() + "'", attribute.getValue(), attributeValue); + } + } + + private Map<String, Object> getKeyStoreAttributes() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(KeyStore.NAME, getName()); + attributes.put(KeyStore.PATH, "/path/to/KeyStore"); + attributes.put(KeyStore.PASSWORD, "my-secret-password"); + attributes.put(KeyStore.TYPE, "NON-JKS"); + attributes.put(KeyStore.KEY_MANAGER_FACTORY_ALGORITHM, "NON-STANDARD"); + attributes.put(KeyStore.CERTIFICATE_ALIAS, "my-cert-alias"); + attributes.put(KeyStore.DESCRIPTION, "description"); + return attributes; + } + + public void testCreateWithMissedRequiredAttributes() + { + Map<String, Object> attributes = getKeyStoreAttributes(); + + UUID id = UUID.randomUUID(); + Broker broker = mock(Broker.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + when(entry.getId()).thenReturn(id); + + KeyStoreRecoverer recovever = new KeyStoreRecoverer(); + + String[] mandatoryProperties = {KeyStore.NAME, KeyStore.PATH, KeyStore.PASSWORD}; + for (int i = 0; i < mandatoryProperties.length; i++) + { + Map<String, Object> properties = new HashMap<String, Object>(attributes); + properties.remove(mandatoryProperties[i]); + when(entry.getAttributes()).thenReturn(properties); + try + { + recovever.create(null, entry, broker); + fail("Cannot create key store without a " + mandatoryProperties[i]); + } + catch(IllegalArgumentException e) + { + // pass + } + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/PluginRecovererTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/PluginRecovererTest.java new file mode 100644 index 0000000000..42fd742407 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/PluginRecovererTest.java @@ -0,0 +1,117 @@ +/* + * + * 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.startup; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import junit.framework.TestCase; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.Plugin; +import org.apache.qpid.server.plugin.PluginFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; + +public class PluginRecovererTest extends TestCase +{ + private UUID _id; + private Map<String, Object> _attributes; + + private PluginFactory _factory; + private QpidServiceLoader<PluginFactory> _pluginFactoryServiceLoader; + private Broker _broker; + private ConfigurationEntry _configurationEntry; + + @SuppressWarnings("unchecked") + protected void setUp() throws Exception + { + super.setUp(); + _id = UUID.randomUUID(); + _attributes = new HashMap<String, Object>(); + + _factory = mock(PluginFactory.class); + + _pluginFactoryServiceLoader = mock(QpidServiceLoader.class); + when(_pluginFactoryServiceLoader.instancesOf(PluginFactory.class)).thenReturn(Collections.singletonList(_factory )); + + _broker = mock(Broker.class); + + _configurationEntry = mock(ConfigurationEntry.class); + when(_configurationEntry.getId()).thenReturn(_id); + when(_configurationEntry.getAttributes()).thenReturn(_attributes); + } + + public void testCreate() + { + Plugin pluginFromFactory = mock(Plugin.class); + when(pluginFromFactory.getId()).thenReturn(_id); + when(_factory.createInstance(_id, _attributes, _broker)).thenReturn(pluginFromFactory); + + PluginRecoverer pluginRecoverer = new PluginRecoverer(_pluginFactoryServiceLoader); + ConfiguredObject pluginFromRecoverer = pluginRecoverer.create(null, _configurationEntry, _broker); + assertNotNull("Null group provider", pluginFromRecoverer); + assertSame("Unexpected plugin", pluginFromFactory, pluginFromRecoverer); + assertEquals("Unexpected ID", _id, pluginFromRecoverer.getId()); + } + + public void testCreateThrowsExceptionForUnexpectedId() + { + Plugin pluginFromFactory = mock(Plugin.class); + when(pluginFromFactory.getId()).thenReturn(UUID.randomUUID()); + when(_factory.createInstance(_id, _attributes, _broker)).thenReturn(pluginFromFactory); + + PluginRecoverer pluginRecoverer = new PluginRecoverer(_pluginFactoryServiceLoader); + try + { + pluginRecoverer.create(null, _configurationEntry, _broker); + fail("An exception should be thrown for incorrect id"); + } + catch(IllegalStateException e) + { + //pass + } + } + + public void testCreateThrowsExceptionWhenNoPluginIsCreated() + { + when(_factory.createInstance(_id, _attributes, _broker)).thenReturn(null); + + PluginRecoverer pluginRecoverer = new PluginRecoverer(_pluginFactoryServiceLoader); + try + { + pluginRecoverer.create(null, _configurationEntry, _broker); + fail("Configuration exception should be thrown when plugin is not created"); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/TrustStoreRecovererTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/TrustStoreRecovererTest.java new file mode 100644 index 0000000000..3b64f45c80 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/TrustStoreRecovererTest.java @@ -0,0 +1,108 @@ +/* + * + * 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.startup; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.test.utils.QpidTestCase; + +public class TrustStoreRecovererTest extends QpidTestCase +{ + public void testCreateWithAllAttributesProvided() + { + Map<String, Object> attributes = getTrustStoreAttributes(); + + UUID id = UUID.randomUUID(); + Broker broker = mock(Broker.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + when(entry.getAttributes()).thenReturn(attributes); + when(entry.getId()).thenReturn(id); + + TrustStoreRecoverer recovever = new TrustStoreRecoverer(); + + TrustStore trustStore = recovever.create(null, entry, broker); + assertNotNull("Trust store configured object is not created", trustStore); + assertEquals(id, trustStore.getId()); + assertEquals("my-secret-password", trustStore.getPassword()); + + assertNull("Password was unexpectedly returned from configured object", trustStore.getAttribute(TrustStore.PASSWORD)); + + // password attribute should not be exposed by a trust store configured object + // so, we should set password value to null in the map being used to create the trust store configured object + attributes.put(TrustStore.PASSWORD, null); + for (Map.Entry<String, Object> attribute : attributes.entrySet()) + { + Object attributeValue = trustStore.getAttribute(attribute.getKey()); + assertEquals("Unexpected value of attribute '" + attribute.getKey() + "'", attribute.getValue(), attributeValue); + } + } + + private Map<String, Object> getTrustStoreAttributes() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(TrustStore.NAME, getName()); + attributes.put(TrustStore.PATH, "/path/to/truststore"); + attributes.put(TrustStore.PASSWORD, "my-secret-password"); + attributes.put(TrustStore.TYPE, "NON-JKS"); + attributes.put(TrustStore.KEY_MANAGER_FACTORY_ALGORITHM, "NON-STANDARD"); + attributes.put(TrustStore.DESCRIPTION, "Description"); + return attributes; + } + + public void testCreateWithMissedRequiredAttributes() + { + Map<String, Object> attributes = getTrustStoreAttributes(); + + UUID id = UUID.randomUUID(); + Broker broker = mock(Broker.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + when(entry.getAttributes()).thenReturn(attributes); + when(entry.getId()).thenReturn(id); + + TrustStoreRecoverer recovever = new TrustStoreRecoverer(); + + String[] mandatoryProperties = {TrustStore.NAME, TrustStore.PATH, TrustStore.PASSWORD}; + for (int i = 0; i < mandatoryProperties.length; i++) + { + Map<String, Object> properties = new HashMap<String, Object>(attributes); + properties.remove(mandatoryProperties[i]); + when(entry.getAttributes()).thenReturn(properties); + try + { + recovever.create(null, entry, broker); + fail("Cannot create key store without a " + mandatoryProperties[i]); + } + catch(IllegalArgumentException e) + { + // pass + } + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/VirtualHostRecovererTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/VirtualHostRecovererTest.java new file mode 100644 index 0000000000..57d219f85f --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/startup/VirtualHostRecovererTest.java @@ -0,0 +1,124 @@ +/* + * + * 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.startup; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.security.SecurityManager; +import org.apache.qpid.server.stats.StatisticsGatherer; + +public class VirtualHostRecovererTest extends TestCase +{ + public void testCreate() + { + StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); + SecurityManager securityManager = mock(SecurityManager.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + Broker parent = mock(Broker.class); + when(parent.getSecurityManager()).thenReturn(securityManager); + + VirtualHostRecoverer recoverer = new VirtualHostRecoverer(statisticsGatherer); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(VirtualHost.NAME, getName()); + attributes.put(VirtualHost.CONFIG_PATH, "/path/to/virtualhost.xml"); + when(entry.getAttributes()).thenReturn(attributes); + + VirtualHost host = recoverer.create(null, entry, parent); + + assertNotNull("Null is returned", host); + assertEquals("Unexpected name", getName(), host.getName()); + } + + public void testCreateVirtualHostFromStoreConfigAtrributes() + { + StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); + SecurityManager securityManager = mock(SecurityManager.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + Broker parent = mock(Broker.class); + when(parent.getSecurityManager()).thenReturn(securityManager); + + VirtualHostRecoverer recoverer = new VirtualHostRecoverer(statisticsGatherer); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(VirtualHost.NAME, getName()); + attributes.put(VirtualHost.STORE_PATH, "/path/to/virtualhost/store"); + attributes.put(VirtualHost.STORE_TYPE, "DERBY"); + when(entry.getAttributes()).thenReturn(attributes); + + VirtualHost host = recoverer.create(null, entry, parent); + + assertNotNull("Null is returned", host); + assertEquals("Unexpected name", getName(), host.getName()); + } + + public void testCreateWithoutMandatoryAttributesResultsInException() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(VirtualHost.NAME, getName()); + attributes.put(VirtualHost.CONFIG_PATH, "/path/to/virtualhost.xml"); + String[] mandatoryAttributes = {VirtualHost.NAME, VirtualHost.CONFIG_PATH}; + + checkMandatoryAttributesAreValidated(mandatoryAttributes, attributes); + + attributes = new HashMap<String, Object>(); + attributes.put(VirtualHost.NAME, getName()); + attributes.put(VirtualHost.STORE_PATH, "/path/to/store"); + attributes.put(VirtualHost.STORE_TYPE, "DERBY"); + mandatoryAttributes = new String[]{VirtualHost.NAME, VirtualHost.STORE_PATH, VirtualHost.STORE_TYPE}; + + checkMandatoryAttributesAreValidated(mandatoryAttributes, attributes); + } + + public void checkMandatoryAttributesAreValidated(String[] mandatoryAttributes, Map<String, Object> attributes) + { + StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); + SecurityManager securityManager = mock(SecurityManager.class); + ConfigurationEntry entry = mock(ConfigurationEntry.class); + Broker parent = mock(Broker.class); + when(parent.getSecurityManager()).thenReturn(securityManager); + VirtualHostRecoverer recoverer = new VirtualHostRecoverer(statisticsGatherer); + + for (String name : mandatoryAttributes) + { + Map<String, Object> copy = new HashMap<String, Object>(attributes); + copy.remove(name); + when(entry.getAttributes()).thenReturn(copy); + try + { + recoverer.create(null, entry, parent); + fail("Cannot create a virtual host without a manadatory attribute " + name); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/ConfigurationEntryStoreTestCase.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/ConfigurationEntryStoreTestCase.java new file mode 100644 index 0000000000..b357c0b8e9 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/ConfigurationEntryStoreTestCase.java @@ -0,0 +1,392 @@ +/* + * + * 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 java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.server.model.KeyStore; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.Transport; +import org.apache.qpid.server.model.TrustStore; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager; +import org.apache.qpid.server.security.auth.manager.ExternalAuthenticationManager; +import org.apache.qpid.test.utils.QpidTestCase; + +public abstract class ConfigurationEntryStoreTestCase extends QpidTestCase +{ + private ConfigurationEntryStore _store; + + private UUID _brokerId; + private UUID _virtualHostId; + private UUID _authenticationProviderId; + + private Map<String, Object> _brokerAttributes; + private Map<String, Object> _virtualHostAttributes; + private Map<String, Object> _authenticationProviderAttributes; + + public void setUp() throws Exception + { + super.setUp(); + + _brokerId = UUID.randomUUID(); + _brokerAttributes = new HashMap<String, Object>(); + _brokerAttributes.put(Broker.DEFAULT_VIRTUAL_HOST, "test"); + _brokerAttributes.put(Broker.DEFAULT_AUTHENTICATION_PROVIDER, "authenticationProvider1"); + _brokerAttributes.put(Broker.ALERT_THRESHOLD_MESSAGE_AGE, 9); + _brokerAttributes.put(Broker.ALERT_THRESHOLD_MESSAGE_COUNT, 8); + _brokerAttributes.put(Broker.ALERT_THRESHOLD_QUEUE_DEPTH, 7); + _brokerAttributes.put(Broker.ALERT_THRESHOLD_MESSAGE_SIZE, 6); + _brokerAttributes.put(Broker.ALERT_REPEAT_GAP, 5); + _brokerAttributes.put(Broker.FLOW_CONTROL_SIZE_BYTES, 5); + _brokerAttributes.put(Broker.FLOW_CONTROL_RESUME_SIZE_BYTES, 3); + _brokerAttributes.put(Broker.MAXIMUM_DELIVERY_ATTEMPTS, 2); + _brokerAttributes.put(Broker.DEAD_LETTER_QUEUE_ENABLED, true); + _brokerAttributes.put(Broker.HOUSEKEEPING_CHECK_PERIOD, 1); + _brokerAttributes.put(Broker.ACL_FILE, "/path/to/acl"); + _brokerAttributes.put(Broker.SESSION_COUNT_LIMIT, 1000); + _brokerAttributes.put(Broker.HEART_BEAT_DELAY, 2000); + _brokerAttributes.put(Broker.STATISTICS_REPORTING_PERIOD, 4000); + _brokerAttributes.put(Broker.STATISTICS_REPORTING_RESET_ENABLED, true); + + _virtualHostId = UUID.randomUUID(); + _virtualHostAttributes = new HashMap<String, Object>(); + _virtualHostAttributes.put(VirtualHost.NAME, "test"); + _virtualHostAttributes.put(VirtualHost.CONFIG_PATH, "/path/to/phantom/test"); + + _authenticationProviderId = UUID.randomUUID(); + _authenticationProviderAttributes = new HashMap<String, Object>(); + _authenticationProviderAttributes.put(AuthenticationProvider.NAME, "authenticationProvider1"); + _authenticationProviderAttributes.put(AuthenticationManagerFactory.ATTRIBUTE_TYPE, AnonymousAuthenticationManager.class.getSimpleName()); + + _store = createStore(_brokerId, _brokerAttributes); + addConfiguration(_virtualHostId, VirtualHost.class.getSimpleName(), _virtualHostAttributes); + addConfiguration(_authenticationProviderId, AuthenticationProvider.class.getSimpleName(), _authenticationProviderAttributes); + } + + // ??? perhaps it should not be abstract + protected abstract ConfigurationEntryStore createStore(UUID brokerId, Map<String, Object> brokerAttributes) throws Exception; + + protected abstract void addConfiguration(UUID id, String type, Map<String, Object> attributes); + + protected ConfigurationEntryStore getStore() + { + return _store; + } + + public void testGetRootEntry() + { + ConfigurationEntry brokerConfigEntry = _store.getRootEntry(); + assertNotNull("Root entry does not exist", brokerConfigEntry); + assertEquals("Unexpected id", _brokerId, brokerConfigEntry.getId()); + assertEquals("Unexpected type ", Broker.class.getSimpleName(), brokerConfigEntry.getType()); + Map<String, Object> attributes = brokerConfigEntry.getAttributes(); + assertNotNull("Attributes cannot be null", attributes); + assertEquals("Unexpected attributes", _brokerAttributes, attributes); + } + + public void testGetEntry() + { + ConfigurationEntry authenticationProviderConfigEntry = _store.getEntry(_authenticationProviderId); + assertNotNull("Provider with id " + _authenticationProviderId + " should exist", authenticationProviderConfigEntry); + assertEquals("Unexpected id", _authenticationProviderId, authenticationProviderConfigEntry.getId()); + assertEquals("Unexpected type ", AuthenticationProvider.class.getSimpleName(), authenticationProviderConfigEntry.getType()); + Map<String, Object> attributes = authenticationProviderConfigEntry.getAttributes(); + assertNotNull("Attributes cannot be null", attributes); + assertEquals("Unexpected attributes", _authenticationProviderAttributes, attributes); + } + + public void testRemove() + { + Map<String, Object> virtualHostAttributes = new HashMap<String, Object>(); + virtualHostAttributes.put(VirtualHost.NAME, getName()); + virtualHostAttributes.put(VirtualHost.CONFIG_PATH, "/path/to/phantom/virtualhost/config"); + UUID virtualHostId = UUID.randomUUID(); + addConfiguration(virtualHostId, VirtualHost.class.getSimpleName(), virtualHostAttributes); + + assertNotNull("Virtual host with id " + virtualHostId + " should exist", _store.getEntry(virtualHostId)); + + _store.remove(virtualHostId); + assertNull("Authentication provider configuration should be removed", _store.getEntry(virtualHostId)); + } + + public void testRemoveMultipleEntries() + { + Map<String, Object> virtualHost1Attributes = new HashMap<String, Object>(); + virtualHost1Attributes.put(VirtualHost.NAME, "test1"); + virtualHost1Attributes.put(VirtualHost.CONFIG_PATH, "/path/to/phantom/virtualhost/config1"); + UUID virtualHost1Id = UUID.randomUUID(); + addConfiguration(virtualHost1Id, VirtualHost.class.getSimpleName(), virtualHost1Attributes); + + Map<String, Object> virtualHost2Attributes = new HashMap<String, Object>(); + virtualHost2Attributes.put(VirtualHost.NAME, "test1"); + virtualHost2Attributes.put(VirtualHost.CONFIG_PATH, "/path/to/phantom/virtualhost/config2"); + UUID virtualHost2Id = UUID.randomUUID(); + addConfiguration(virtualHost2Id, VirtualHost.class.getSimpleName(), virtualHost2Attributes); + + assertNotNull("Virtual host with id " + virtualHost1Id + " should exist", _store.getEntry(virtualHost1Id)); + assertNotNull("Virtual host with id " + virtualHost2Id + " should exist", _store.getEntry(virtualHost2Id)); + + UUID[] deletedIds = _store.remove(virtualHost1Id, virtualHost2Id); + assertNotNull("Unexpected deleted ids", deletedIds); + assertEquals("Unexpected id of first deleted virtual host", virtualHost1Id , deletedIds[0]); + assertEquals("Unexpected id of second deleted virtual host", virtualHost2Id , deletedIds[1]); + assertNull("First virtual host configuration should be removed", _store.getEntry(virtualHost1Id)); + assertNull("Second virtual host configuration should be removed", _store.getEntry(virtualHost2Id)); + } + + public void testSaveBroker() + { + ConfigurationEntry brokerConfigEntry = _store.getRootEntry(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Broker.DEFAULT_VIRTUAL_HOST, "test"); + attributes.put(Broker.DEFAULT_AUTHENTICATION_PROVIDER, "authenticationProvider1"); + attributes.put(Broker.ALERT_THRESHOLD_MESSAGE_AGE, 19); + attributes.put(Broker.ALERT_THRESHOLD_MESSAGE_COUNT, 18); + attributes.put(Broker.ALERT_THRESHOLD_QUEUE_DEPTH, 17); + attributes.put(Broker.ALERT_THRESHOLD_MESSAGE_SIZE, 16); + attributes.put(Broker.ALERT_REPEAT_GAP, 15); + attributes.put(Broker.FLOW_CONTROL_SIZE_BYTES, 15); + attributes.put(Broker.FLOW_CONTROL_RESUME_SIZE_BYTES, 13); + attributes.put(Broker.MAXIMUM_DELIVERY_ATTEMPTS, 12); + attributes.put(Broker.DEAD_LETTER_QUEUE_ENABLED, false); + attributes.put(Broker.HOUSEKEEPING_CHECK_PERIOD, 11); + attributes.put(Broker.ACL_FILE, "/path/to/acl1"); + attributes.put(Broker.SESSION_COUNT_LIMIT, 11000); + attributes.put(Broker.HEART_BEAT_DELAY, 12000); + attributes.put(Broker.STATISTICS_REPORTING_PERIOD, 14000); + attributes.put(Broker.STATISTICS_REPORTING_RESET_ENABLED, false); + ConfigurationEntry updatedBrokerEntry = new ConfigurationEntry(_brokerId, Broker.class.getSimpleName(), attributes, + brokerConfigEntry.getChildrenIds(), _store); + + _store.save(updatedBrokerEntry); + + ConfigurationEntry newBrokerConfigEntry = _store.getRootEntry(); + assertNotNull("Root entry does not exist", newBrokerConfigEntry); + assertEquals("Unexpected id", _brokerId, newBrokerConfigEntry.getId()); + assertEquals("Unexpected type ", Broker.class.getSimpleName(), newBrokerConfigEntry.getType()); + Map<String, Object> newBrokerattributes = newBrokerConfigEntry.getAttributes(); + assertNotNull("Attributes cannot be null", newBrokerattributes); + assertEquals("Unexpected attributes", attributes, newBrokerattributes); + } + + public void testSaveNewVirtualHost() + { + Map<String, Object> virtualHostAttributes = new HashMap<String, Object>(); + virtualHostAttributes.put(VirtualHost.NAME, "test1"); + virtualHostAttributes.put(VirtualHost.CONFIG_PATH, "/path/to/phantom/virtualhost/config1"); + UUID virtualHostId = UUID.randomUUID(); + ConfigurationEntry hostEntry = new ConfigurationEntry(virtualHostId, VirtualHost.class.getSimpleName(), virtualHostAttributes, + Collections.<UUID> emptySet(), _store); + + _store.save(hostEntry); + + ConfigurationEntry configurationEntry = _store.getEntry(virtualHostId); + assertEquals("Unexpected virtual host configuration", hostEntry, configurationEntry); + assertEquals("Unexpected type", VirtualHost.class.getSimpleName(), configurationEntry.getType()); + assertEquals("Unexpected virtual host attributes", hostEntry.getAttributes(), configurationEntry.getAttributes()); + assertTrue("Unexpected virtual host children found", hostEntry.getChildrenIds().isEmpty()); + } + + public void testSaveExistingVirtualHost() + { + ConfigurationEntry hostEntry = _store.getEntry(_virtualHostId); + assertNotNull("Host configuration is not found", hostEntry); + + Map<String, Object> virtualHostAttributes = new HashMap<String, Object>(); + virtualHostAttributes.put(VirtualHost.NAME, "test"); + virtualHostAttributes.put(VirtualHost.CONFIG_PATH, "/path/to/new/phantom/test/configuration"); + + ConfigurationEntry updatedEntry = new ConfigurationEntry(_virtualHostId, VirtualHost.class.getSimpleName(), virtualHostAttributes, + hostEntry.getChildrenIds(), _store); + _store.save(updatedEntry); + + ConfigurationEntry newHostEntry = _store.getEntry(_virtualHostId); + assertEquals("Unexpected virtual host configuration", updatedEntry, newHostEntry); + assertEquals("Unexpected type", VirtualHost.class.getSimpleName(), newHostEntry.getType()); + assertEquals("Unexpected virtual host attributes", updatedEntry.getAttributes(), newHostEntry.getAttributes()); + assertEquals("Unexpected virtual host children found", updatedEntry.getChildrenIds(), newHostEntry.getChildrenIds()); + } + + public void testSaveNewAuthenticationProvider() + { + UUID authenticationProviderId = UUID.randomUUID(); + Map<String, Object> authenticationProviderAttributes = new HashMap<String, Object>(); + authenticationProviderAttributes.put(AuthenticationProvider.NAME, "authenticationProvider1"); + authenticationProviderAttributes.put(AuthenticationManagerFactory.ATTRIBUTE_TYPE, ExternalAuthenticationManager.class.getSimpleName()); + ConfigurationEntry providerEntry = new ConfigurationEntry(authenticationProviderId, AuthenticationProvider.class.getSimpleName(), + authenticationProviderAttributes, Collections.<UUID> emptySet(), _store); + + _store.save(providerEntry); + + ConfigurationEntry storeEntry = _store.getEntry(authenticationProviderId); + assertEquals("Unexpected provider configuration", providerEntry, storeEntry); + assertEquals("Unexpected type", AuthenticationProvider.class.getSimpleName(), storeEntry.getType()); + assertEquals("Unexpected provider attributes", providerEntry.getAttributes(), storeEntry.getAttributes()); + assertTrue("Unexpected provider children found", storeEntry.getChildrenIds().isEmpty()); + } + + public void testSaveExistingAuthenticationProvider() + { + ConfigurationEntry providerEntry = _store.getEntry(_authenticationProviderId); + assertNotNull("provider configuration is not found", providerEntry); + + Map<String, Object> authenticationProviderAttributes = new HashMap<String, Object>(); + authenticationProviderAttributes.put(AuthenticationProvider.NAME, "authenticationProvider1"); + authenticationProviderAttributes.put(AuthenticationManagerFactory.ATTRIBUTE_TYPE, ExternalAuthenticationManager.class.getSimpleName()); + ConfigurationEntry updatedEntry = new ConfigurationEntry(_authenticationProviderId, AuthenticationProvider.class.getSimpleName(), + authenticationProviderAttributes, Collections.<UUID> emptySet(), _store); + _store.save(updatedEntry); + + ConfigurationEntry storeEntry = _store.getEntry(_authenticationProviderId); + assertEquals("Unexpected provider configuration", updatedEntry, storeEntry); + assertEquals("Unexpected type", AuthenticationProvider.class.getSimpleName(), storeEntry.getType()); + assertEquals("Unexpected provider attributes", updatedEntry.getAttributes(), storeEntry.getAttributes()); + assertTrue("Unexpected provider children found", storeEntry.getChildrenIds().isEmpty()); + } + + public void testSaveTrustStore() + { + UUID trustStoreId = UUID.randomUUID(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(TrustStore.NAME, getName()); + attributes.put(TrustStore.PATH, "/path/to/truststore"); + attributes.put(TrustStore.PASSWORD, "my-secret-password"); + attributes.put(TrustStore.TYPE, "NON-JKS"); + attributes.put(TrustStore.KEY_MANAGER_FACTORY_ALGORITHM, "NON-STANDARD"); + attributes.put(TrustStore.DESCRIPTION, "Description"); + + ConfigurationEntry trustStoreEntry = new ConfigurationEntry(trustStoreId, TrustStore.class.getSimpleName(), attributes, + Collections.<UUID> emptySet(), _store); + + _store.save(trustStoreEntry); + + ConfigurationEntry storeEntry = _store.getEntry(trustStoreId); + assertEquals("Unexpected trust store configuration", trustStoreEntry, storeEntry); + assertEquals("Unexpected type", TrustStore.class.getSimpleName(), storeEntry.getType()); + assertEquals("Unexpected provider attributes", trustStoreEntry.getAttributes(), storeEntry.getAttributes()); + assertTrue("Unexpected provider children found", storeEntry.getChildrenIds().isEmpty()); + } + + public void testSaveKeyStore() + { + UUID keyStoreId = UUID.randomUUID(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(KeyStore.NAME, getName()); + attributes.put(KeyStore.PATH, "/path/to/truststore"); + attributes.put(KeyStore.PASSWORD, "my-secret-password"); + attributes.put(KeyStore.TYPE, "NON-JKS"); + attributes.put(KeyStore.KEY_MANAGER_FACTORY_ALGORITHM, "NON-STANDARD"); + attributes.put(KeyStore.DESCRIPTION, "Description"); + attributes.put(KeyStore.CERTIFICATE_ALIAS, "Alias"); + + ConfigurationEntry keyStoreEntry = new ConfigurationEntry(keyStoreId, KeyStore.class.getSimpleName(), attributes, Collections.<UUID> emptySet(), + _store); + + _store.save(keyStoreEntry); + + ConfigurationEntry storeEntry = _store.getEntry(keyStoreId); + assertEquals("Unexpected key store configuration", keyStoreEntry, storeEntry); + assertEquals("Unexpected type", KeyStore.class.getSimpleName(), storeEntry.getType()); + assertEquals("Unexpected provider attributes", keyStoreEntry.getAttributes(), storeEntry.getAttributes()); + assertTrue("Unexpected provider children found", storeEntry.getChildrenIds().isEmpty()); + } + + public void testSaveGroupProvider() + { + UUID groupProviderId = UUID.randomUUID(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(GroupProvider.NAME, getName()); + + ConfigurationEntry groupProviderEntry = new ConfigurationEntry(groupProviderId, GroupProvider.class.getSimpleName(), attributes, + Collections.<UUID> emptySet(), _store); + + _store.save(groupProviderEntry); + + ConfigurationEntry storeEntry = _store.getEntry(groupProviderId); + assertEquals("Unexpected group provider configuration", groupProviderEntry, storeEntry); + assertEquals("Unexpected type", GroupProvider.class.getSimpleName(), storeEntry.getType()); + assertEquals("Unexpected group provider attributes", groupProviderEntry.getAttributes(), storeEntry.getAttributes()); + assertTrue("Unexpected provider children found", storeEntry.getChildrenIds().isEmpty()); + } + + public void testSavePort() + { + UUID portId = UUID.randomUUID(); + Map<String, Object> attributes = new HashMap<String, Object>(); + Set<String> tcpTransportSet = Collections.singleton(Transport.TCP.name()); + attributes.put(Port.PORT, 9999); + attributes.put(Port.TRANSPORTS, tcpTransportSet); + attributes.put(Port.TCP_NO_DELAY, true); + attributes.put(Port.RECEIVE_BUFFER_SIZE, 1); + attributes.put(Port.SEND_BUFFER_SIZE, 2); + attributes.put(Port.NEED_CLIENT_AUTH, true); + attributes.put(Port.WANT_CLIENT_AUTH, true); + + ConfigurationEntry portEntry = new ConfigurationEntry(portId, Port.class.getSimpleName(), attributes, Collections.<UUID> emptySet(), _store); + + _store.save(portEntry); + + ConfigurationEntry storeEntry = _store.getEntry(portId); + assertEquals("Unexpected port configuration", portEntry, storeEntry); + assertEquals("Unexpected type", Port.class.getSimpleName(), storeEntry.getType()); + assertEquals("Unexpected port attributes", portEntry.getAttributes(), storeEntry.getAttributes()); + assertTrue("Unexpected port children found", storeEntry.getChildrenIds().isEmpty()); + } + + public void testMultipleSave() + { + UUID virtualHostId = UUID.randomUUID(); + Map<String, Object> virtualHostAttributes = new HashMap<String, Object>(); + virtualHostAttributes.put(VirtualHost.NAME, "test1"); + virtualHostAttributes.put(VirtualHost.CONFIG_PATH, "/path/to/phantom/virtualhost/config1"); + ConfigurationEntry hostEntry = new ConfigurationEntry(virtualHostId, VirtualHost.class.getSimpleName(), virtualHostAttributes, + Collections.<UUID> emptySet(), _store); + + UUID keyStoreId = UUID.randomUUID(); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(KeyStore.NAME, getName()); + attributes.put(KeyStore.PATH, "/path/to/truststore"); + attributes.put(KeyStore.PASSWORD, "my-secret-password"); + attributes.put(KeyStore.TYPE, "NON-JKS"); + attributes.put(KeyStore.KEY_MANAGER_FACTORY_ALGORITHM, "NON-STANDARD"); + attributes.put(KeyStore.DESCRIPTION, "Description"); + attributes.put(KeyStore.CERTIFICATE_ALIAS, "Alias"); + + ConfigurationEntry keyStoreEntry = new ConfigurationEntry(keyStoreId, KeyStore.class.getSimpleName(), attributes, Collections.<UUID> emptySet(), + _store); + + _store.save(hostEntry, keyStoreEntry); + + assertNotNull("Virtual host is not found", _store.getEntry(virtualHostId)); + assertNotNull("Key store is not found", _store.getEntry(keyStoreId)); + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java new file mode 100644 index 0000000000..a18d5501f0 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java @@ -0,0 +1,77 @@ +package org.apache.qpid.server.configuration.store; + +import java.io.File; +import java.io.StringWriter; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.test.utils.TestFileUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; + +public class JsonConfigurationEntryStoreTest extends ConfigurationEntryStoreTestCase +{ + private File _storeFile; + private ObjectMapper _objectMapper; + + public void tearDown() throws Exception + { + _storeFile.delete(); + super.tearDown(); + } + + @Override + protected ConfigurationEntryStore createStore(UUID brokerId, Map<String, Object> brokerAttributes) throws Exception + { + _objectMapper = new ObjectMapper(); + _objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + + Map<String, Object> brokerObjectMap = new HashMap<String, Object>(); + brokerObjectMap.put(Broker.ID, brokerId); + brokerObjectMap.put("@type", Broker.class.getSimpleName()); + brokerObjectMap.putAll(brokerAttributes); + + StringWriter sw = new StringWriter(); + _objectMapper.writeValue(sw, brokerObjectMap); + + String brokerJson = sw.toString(); + + _storeFile = TestFileUtils.createTempFile(this, ".json", brokerJson); + + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(_storeFile.getAbsolutePath()); + return store; + } + + @Override + protected void addConfiguration(UUID id, String type, Map<String, Object> attributes) + { + ConfigurationEntryStore store = getStore(); + store.save(new ConfigurationEntry(id, type, attributes, Collections.<UUID> emptySet(), store)); + } + + public void testAttributeIsResolvedFromSystemProperties() + { + String aclLocation = "path/to/acl/" + getTestName(); + setTestSystemProperty("my.test.property", aclLocation); + + ConfigurationEntryStore store = getStore(); + ConfigurationEntry brokerConfigEntry = store.getRootEntry(); + Map<String, Object> attributes = new HashMap<String, Object>(brokerConfigEntry.getAttributes()); + attributes.put(Broker.ACL_FILE, "${my.test.property}"); + ConfigurationEntry updatedBrokerEntry = new ConfigurationEntry(brokerConfigEntry.getId(), Broker.class.getSimpleName(), + attributes, brokerConfigEntry.getChildrenIds(), store); + store.save(updatedBrokerEntry); + + JsonConfigurationEntryStore store2 = new JsonConfigurationEntryStore(); + store2.open(_storeFile.getAbsolutePath()); + + assertEquals("Unresolved ACL value", aclLocation, store2.getRootEntry().getAttributes().get(Broker.ACL_FILE)); + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/StoreConfigurationChangeListenerTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/StoreConfigurationChangeListenerTest.java new file mode 100644 index 0000000000..a77a0e9fcc --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/StoreConfigurationChangeListenerTest.java @@ -0,0 +1,83 @@ +package org.apache.qpid.server.configuration.store; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.when; + +import java.util.UUID; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.Queue; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; + +public class StoreConfigurationChangeListenerTest extends QpidTestCase +{ + private ConfigurationEntryStore _store; + private StoreConfigurationChangeListener _listener; + + protected void setUp() throws Exception + { + super.setUp(); + _store = mock(ConfigurationEntryStore.class); + _listener = new StoreConfigurationChangeListener(_store); + } + + public void testStateChanged() + { + notifyBrokerStarted(); + UUID id = UUID.randomUUID(); + ConfiguredObject object = mock(VirtualHost.class); + when(object.getId()).thenReturn(id); + _listener.stateChanged(object, State.ACTIVE, State.DELETED); + verify(_store).remove(id); + } + + public void testChildAdded() + { + notifyBrokerStarted(); + Broker broker = mock(Broker.class); + VirtualHost child = mock(VirtualHost.class); + _listener.childAdded(broker, child); + verify(_store).save(any(ConfigurationEntry.class), any(ConfigurationEntry.class)); + } + + public void testChildRemoved() + { + notifyBrokerStarted(); + Broker broker = mock(Broker.class); + VirtualHost child = mock(VirtualHost.class); + _listener.childRemoved(broker, child); + verify(_store).save(any(ConfigurationEntry.class)); + } + + public void testAttributeSet() + { + notifyBrokerStarted(); + Broker broker = mock(Broker.class); + _listener.attributeSet(broker, Broker.FLOW_CONTROL_SIZE_BYTES, null, 1); + verify(_store).save(any(ConfigurationEntry.class)); + } + + public void testChildAddedForVirtualHost() + { + notifyBrokerStarted(); + + VirtualHost object = mock(VirtualHost.class); + Queue queue = mock(Queue.class); + _listener.childAdded(object, queue); + verifyNoMoreInteractions(_store); + } + + private void notifyBrokerStarted() + { + Broker broker = mock(Broker.class); + _listener.stateChanged(broker, State.INITIALISING, State.ACTIVE); + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/updater/TaskExecutorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/updater/TaskExecutorTest.java new file mode 100644 index 0000000000..cd6302d55b --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/updater/TaskExecutorTest.java @@ -0,0 +1,296 @@ +/* + * + * 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.updater; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import javax.security.auth.Subject; + +import junit.framework.TestCase; + +import org.apache.qpid.server.logging.LogActor; +import org.apache.qpid.server.logging.NullRootMessageLogger; +import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.logging.actors.TestLogActor; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.security.SecurityManager; + +public class TaskExecutorTest extends TestCase +{ + private TaskExecutor _executor; + + protected void setUp() throws Exception + { + super.setUp(); + _executor = new TaskExecutor(); + } + + protected void tearDown() throws Exception + { + try + { + _executor.stopImmediately(); + } + finally + { + super.tearDown(); + } + } + + public void testGetState() + { + assertEquals("Unxpected initial state", State.INITIALISING, _executor.getState()); + } + + public void testStart() + { + _executor.start(); + assertEquals("Unxpected started state", State.ACTIVE, _executor.getState()); + } + + public void testStopImmediately() throws Exception + { + _executor.start(); + final CountDownLatch submitLatch = new CountDownLatch(2); + final CountDownLatch waitForCallLatch = new CountDownLatch(1); + final BlockingQueue<Exception> submitExceptions = new LinkedBlockingQueue<Exception>(); + + Runnable runnable = new Runnable() + { + @Override + public void run() + { + try + { + Future<?> f = _executor.submit(new NeverEndingCallable(waitForCallLatch)); + submitLatch.countDown(); + f.get(); + } + catch (Exception e) + { + if (e instanceof ExecutionException) + { + e = (Exception) e.getCause(); + } + submitExceptions.add(e); + } + } + }; + new Thread(runnable).start(); + new Thread(runnable).start(); + assertTrue("Tasks have not been submitted", submitLatch.await(1000, TimeUnit.MILLISECONDS)); + assertTrue("The first task has not been triggered", waitForCallLatch.await(1000, TimeUnit.MILLISECONDS)); + + _executor.stopImmediately(); + assertEquals("Unxpected stopped state", State.STOPPED, _executor.getState()); + + Exception e = submitExceptions.poll(1000l, TimeUnit.MILLISECONDS); + assertNotNull("The task execution was not interrupted or cancelled", e); + Exception e2 = submitExceptions.poll(1000l, TimeUnit.MILLISECONDS); + assertNotNull("The task execution was not interrupted or cancelled", e2); + + assertTrue("One of the exceptions should be CancellationException:", e2 instanceof CancellationException + || e instanceof CancellationException); + assertTrue("One of the exceptions should be InterruptedException:", e2 instanceof InterruptedException + || e instanceof InterruptedException); + } + + public void testStop() + { + _executor.start(); + _executor.stop(); + assertEquals("Unxpected stopped state", State.STOPPED, _executor.getState()); + } + + public void testSubmitAndWait() throws Exception + { + _executor.start(); + Object result = _executor.submitAndWait(new Callable<String>() + { + @Override + public String call() throws Exception + { + return "DONE"; + } + }); + assertEquals("Unexpected task execution result", "DONE", result); + } + + public void testSubmitAndWaitInNotAuthorizedContext() + { + _executor.start(); + Object subject = _executor.submitAndWait(new SubjectRetriever()); + assertNull("Subject must be null", subject); + } + + public void testSubmitAndWaitInAuthorizedContext() + { + _executor.start(); + Subject subject = new Subject(); + Object result = Subject.doAs(subject, new PrivilegedAction<Object>() + { + @Override + public Object run() + { + return _executor.submitAndWait(new SubjectRetriever()); + } + }); + assertEquals("Unexpected subject", subject, result); + } + + public void testSubmitAndWaitInAuthorizedContextWithNullSubject() + { + _executor.start(); + Object result = Subject.doAs(null, new PrivilegedAction<Object>() + { + @Override + public Object run() + { + return _executor.submitAndWait(new SubjectRetriever()); + } + }); + assertEquals("Unexpected subject", null, result); + } + + public void testSubmitAndWaitReThrowsOriginalRuntimeException() + { + final RuntimeException exception = new RuntimeException(); + _executor.start(); + try + { + _executor.submitAndWait(new Callable<Void>() + { + + @Override + public Void call() throws Exception + { + throw exception; + } + }); + fail("Exception is expected"); + } + catch (Exception e) + { + assertEquals("Unexpected exception", exception, e); + } + } + + public void testSubmitAndWaitPassesOriginalCheckedException() + { + final Exception exception = new Exception(); + _executor.start(); + try + { + _executor.submitAndWait(new Callable<Void>() + { + + @Override + public Void call() throws Exception + { + throw exception; + } + }); + fail("Exception is expected"); + } + catch (Exception e) + { + assertEquals("Unexpected exception", exception, e.getCause()); + } + } + + public void testSubmitAndWaitCurrentActorAndSecurityManagerSubjectAreRespected() throws Exception + { + _executor.start(); + LogActor actor = new TestLogActor(new NullRootMessageLogger()); + Subject subject = new Subject(); + Subject currentSecurityManagerSubject = SecurityManager.getThreadSubject(); + final AtomicReference<LogActor> taskLogActor = new AtomicReference<LogActor>(); + final AtomicReference<Subject> taskSubject = new AtomicReference<Subject>(); + try + { + CurrentActor.set(actor); + SecurityManager.setThreadSubject(subject); + _executor.submitAndWait(new Callable<Void>() + { + @Override + public Void call() throws Exception + { + taskLogActor.set(CurrentActor.get()); + taskSubject.set(SecurityManager.getThreadSubject()); + return null; + } + }); + } + finally + { + SecurityManager.setThreadSubject(currentSecurityManagerSubject); + CurrentActor.remove(); + } + assertEquals("Unexpected task log actor", actor, taskLogActor.get()); + assertEquals("Unexpected security manager subject", subject, taskSubject.get()); + } + + private class SubjectRetriever implements Callable<Subject> + { + @Override + public Subject call() throws Exception + { + return Subject.getSubject(AccessController.getContext()); + } + } + + private class NeverEndingCallable implements Callable<Void> + { + private CountDownLatch _waitLatch; + + public NeverEndingCallable(CountDownLatch waitLatch) + { + super(); + _waitLatch = waitLatch; + } + + @Override + public Void call() throws Exception + { + if (_waitLatch != null) + { + _waitLatch.countDown(); + } + + // wait forever + synchronized (this) + { + this.wait(); + } + return null; + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/AbstractHeadersExchangeTestBase.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/AbstractHeadersExchangeTestBase.java index 656490c9d8..0bb698a46c 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/AbstractHeadersExchangeTestBase.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/AbstractHeadersExchangeTestBase.java @@ -53,25 +53,56 @@ import org.apache.qpid.server.queue.IncomingMessage; import org.apache.qpid.server.queue.MockStoredMessage; import org.apache.qpid.server.queue.QueueEntry; import org.apache.qpid.server.queue.SimpleAMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.StoredMessage; import org.apache.qpid.server.subscription.Subscription; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; -public class AbstractHeadersExchangeTestBase extends InternalBrokerBaseCase +public class AbstractHeadersExchangeTestBase extends QpidTestCase { private static final Logger _log = Logger.getLogger(AbstractHeadersExchangeTestBase.class); private final HeadersExchange exchange = new HeadersExchange(); - protected final Set<TestQueue> queues = new HashSet<TestQueue>(); - + private final Set<TestQueue> queues = new HashSet<TestQueue>(); + private VirtualHost _virtualHost; private int count; + @Override + public void setUp() throws Exception + { + super.setUp(); + BrokerTestHelper.setUp(); + _virtualHost = BrokerTestHelper.createVirtualHost(getClass().getName()); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_virtualHost != null) + { + _virtualHost.close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + } + public void testDoNothing() { // this is here only to make junit under Eclipse happy } + public VirtualHost getVirtualHost() + { + return _virtualHost; + } + protected TestQueue bindDefault(String... bindings) throws AMQException { String queueName = "Queue" + (++count); @@ -92,7 +123,7 @@ public class AbstractHeadersExchangeTestBase extends InternalBrokerBaseCase private TestQueue bind(String key, String queueName, Map<String,Object> args) throws AMQException { - TestQueue queue = new TestQueue(new AMQShortString(queueName)); + TestQueue queue = new TestQueue(new AMQShortString(queueName), _virtualHost); queues.add(queue); exchange.onBind(new Binding(null, key, queue, exchange, args)); return queue; @@ -273,10 +304,10 @@ public class AbstractHeadersExchangeTestBase extends InternalBrokerBaseCase return getNameShortString().toString(); } - public TestQueue(AMQShortString name) throws AMQException + public TestQueue(AMQShortString name, VirtualHost host) throws AMQException { - super(UUIDGenerator.generateRandomUUID(), name, false, new AMQShortString("test"), true, false,ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost("test"), Collections.EMPTY_MAP); - ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost("test").getQueueRegistry().registerQueue(this); + super(UUIDGenerator.generateRandomUUID(), name, false, new AMQShortString("test"), true, false, host, Collections.EMPTY_MAP); + host.getQueueRegistry().registerQueue(this); } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/HeadersExchangeTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/HeadersExchangeTest.java index 326d36df05..bd6a02d69b 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/HeadersExchangeTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/HeadersExchangeTest.java @@ -23,8 +23,7 @@ package org.apache.qpid.server.exchange; import org.apache.qpid.AMQException; import org.apache.qpid.server.protocol.AMQProtocolSession; import org.apache.qpid.server.protocol.InternalTestProtocolSession; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.server.util.BrokerTestHelper; public class HeadersExchangeTest extends AbstractHeadersExchangeTestBase { @@ -34,10 +33,15 @@ public class HeadersExchangeTest extends AbstractHeadersExchangeTestBase public void setUp() throws Exception { super.setUp(); - // Just use the first vhost. - VirtualHost - virtualHost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHosts().iterator().next(); - _protocolSession = new InternalTestProtocolSession(virtualHost); + BrokerTestHelper.setUp(); + _protocolSession = new InternalTestProtocolSession(getVirtualHost(), BrokerTestHelper.createBrokerMock()); + } + + @Override + public void tearDown() throws Exception + { + BrokerTestHelper.tearDown(); + super.tearDown(); } public void testSimple() throws AMQException diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/TopicExchangeTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/TopicExchangeTest.java index d06649a77d..f1bf632235 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/TopicExchangeTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/exchange/TopicExchangeTest.java @@ -31,36 +31,49 @@ import org.apache.qpid.server.binding.Binding; import org.apache.qpid.server.message.AMQMessage; import org.apache.qpid.server.message.MessageMetaData; import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.protocol.InternalTestProtocolSession; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.AMQQueueFactory; import org.apache.qpid.server.queue.BaseQueue; import org.apache.qpid.server.queue.IncomingMessage; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.MemoryMessageStore; import org.apache.qpid.server.store.MessageStore; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; -public class TopicExchangeTest extends InternalBrokerBaseCase +public class TopicExchangeTest extends QpidTestCase { private TopicExchange _exchange; - private VirtualHost _vhost; private MessageStore _store; - private InternalTestProtocolSession _protocolSession; - @Override public void setUp() throws Exception { super.setUp(); + BrokerTestHelper.setUp(); _exchange = new TopicExchange(); - _vhost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHosts().iterator().next(); + _vhost = BrokerTestHelper.createVirtualHost(getName()); _store = new MemoryMessageStore(); - _protocolSession = new InternalTestProtocolSession(_vhost); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_vhost != null) + { + _vhost.close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } } public void testNoRoute() throws AMQException diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/UnitTestMessageLogger.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/UnitTestMessageLogger.java index fabbe8640e..be31f3d039 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/UnitTestMessageLogger.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/UnitTestMessageLogger.java @@ -20,7 +20,6 @@ */ package org.apache.qpid.server.logging; -import org.apache.qpid.server.configuration.ServerConfiguration; import java.util.LinkedList; import java.util.List; @@ -34,9 +33,9 @@ public class UnitTestMessageLogger extends AbstractRootMessageLogger } - public UnitTestMessageLogger(ServerConfiguration config) + public UnitTestMessageLogger(boolean statusUpdatesEnabled) { - super(config); + super(statusUpdatesEnabled); } public void rawMessage(String message, String logHierarchy) diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPChannelActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPChannelActorTest.java index f739d3fcb9..e2472dbf01 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPChannelActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPChannelActorTest.java @@ -20,9 +20,8 @@ */ package org.apache.qpid.server.logging.actors; -import org.apache.commons.configuration.ConfigurationException; - -import org.apache.qpid.AMQException; +import org.apache.qpid.server.AMQChannel; +import org.apache.qpid.server.util.BrokerTestHelper; import java.util.List; @@ -38,24 +37,17 @@ import java.util.List; public class AMQPChannelActorTest extends BaseConnectionActorTestCase { - @Override - public void configure() + public void setUp() { - // Prevent defaulting Logging to ON + // do nothing } - - @Override - public void createBroker() throws Exception + private void setUpNow() throws Exception { - //prevent auto-broker startup - } + super.setUp(); + AMQChannel channel = BrokerTestHelper.createChannel(1, getSession()); - private void startBrokerNow() throws Exception - { - super.createBroker(); - - _amqpActor = new AMQPChannelActor(getChannel(), _rootLogger); + setAmqpActor(new AMQPChannelActor(channel, getRootLogger())); } @@ -68,13 +60,11 @@ public class AMQPChannelActorTest extends BaseConnectionActorTestCase */ public void testChannel() throws Exception { - getConfigXml().setProperty("status-updates", "ON"); - - startBrokerNow(); + setUpNow(); - final String message = sendTestLogMessage(_amqpActor); + final String message = sendTestLogMessage(getAmqpActor()); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 1, logs.size()); @@ -95,128 +85,22 @@ public class AMQPChannelActorTest extends BaseConnectionActorTestCase // Verify that the logged message contains the 'ch:1' marker assertTrue("Message was not logged as part of channel 1" + logs.get(0), logs.get(0).toString().contains("/ch:1")); - - } - - /** - * Test that if logging is configured to be off in the configuration that - * no logging is presented - * @throws ConfigurationException - * @throws AMQException - */ - public void testChannelLoggingOFF() throws Exception, AMQException - { - getConfigXml().setProperty("status-updates", "OFF"); - - // Start the broker now. - startBrokerNow(); - - sendTestLogMessage(_amqpActor); - - List<Object> logs = _rawLogger.getLogMessages(); - - assertEquals("Message log size not as expected.", 0, logs.size()); - - } - - /** - * Test that if logging is configured to be off in the configuration that - * no logging is presented - * @throws ConfigurationException - * @throws AMQException - */ - public void testChannelLoggingOfF() throws Exception, AMQException - { - getConfigXml().setProperty("status-updates", "OfF"); - - startBrokerNow(); - - sendTestLogMessage(_amqpActor); - - List<Object> logs = _rawLogger.getLogMessages(); - - assertEquals("Message log size not as expected.", 0, logs.size()); - - } - - /** - * Test that if logging is configured to be off in the configuration that - * no logging is presented - * @throws ConfigurationException - * @throws AMQException - */ - public void testChannelLoggingOff() throws Exception, AMQException - { - getConfigXml().setProperty("status-updates", "Off"); - - startBrokerNow(); - - sendTestLogMessage(_amqpActor); - - List<Object> logs = _rawLogger.getLogMessages(); - - assertEquals("Message log size not as expected.", 0, logs.size()); - } /** - * Test that if logging is configured to be off in the configuration that + * Test that if logging is configured to be off via system property that * no logging is presented - * @throws ConfigurationException - * @throws AMQException */ - public void testChannelLoggingofF() throws Exception, AMQException + public void testChannelLoggingOFF() throws Exception { - getConfigXml().setProperty("status-updates", "ofF"); + setStatusUpdatesEnabled(false); - startBrokerNow(); + setUpNow(); - sendTestLogMessage(_amqpActor); + sendTestLogMessage(getAmqpActor()); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 0, logs.size()); - } - - /** - * Test that if logging is configured to be off in the configuration that - * no logging is presented - * @throws ConfigurationException - * @throws AMQException - */ - public void testChannelLoggingoff() throws Exception, AMQException - { - getConfigXml().setProperty("status-updates", "off"); - - startBrokerNow(); - - sendTestLogMessage(_amqpActor); - - List<Object> logs = _rawLogger.getLogMessages(); - - assertEquals("Message log size not as expected.", 0, logs.size()); - - } - - /** - * Test that if logging is configured to be off in the configuration that - * no logging is presented - * @throws ConfigurationException - * @throws AMQException - */ - public void testChannelLoggingoFf() throws Exception, AMQException - { - getConfigXml().setProperty("status-updates", "oFf"); - - startBrokerNow(); - - sendTestLogMessage(_amqpActor); - - List<Object> logs = _rawLogger.getLogMessages(); - - assertEquals("Message log size not as expected.", 0, logs.size()); - - } - } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPConnectionActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPConnectionActorTest.java index 4eda9e9da1..d1cf256563 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPConnectionActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/AMQPConnectionActorTest.java @@ -38,16 +38,9 @@ import java.util.List; public class AMQPConnectionActorTest extends BaseConnectionActorTestCase { @Override - public void configure() + public void setUp() { - // Prevent defaulting Logging to ON - } - - - @Override - public void createBroker() - { - //Prevent auto-broker startup + //Prevent logger creation } /** @@ -60,13 +53,11 @@ public class AMQPConnectionActorTest extends BaseConnectionActorTestCase */ public void testConnection() throws Exception { - getConfigXml().setProperty("status-updates", "ON"); - - super.createBroker(); + super.setUp(); final String message = sendLogMessage(); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 1, logs.size()); @@ -90,14 +81,13 @@ public class AMQPConnectionActorTest extends BaseConnectionActorTestCase public void testConnectionLoggingOff() throws Exception, AMQException { - getConfigXml().setProperty("status-updates", "OFF"); + setStatusUpdatesEnabled(false); - // Start the broker now. - super.createBroker(); + super.setUp(); sendLogMessage(); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 0, logs.size()); @@ -107,7 +97,7 @@ public class AMQPConnectionActorTest extends BaseConnectionActorTestCase { final String message = "test logging"; - _amqpActor.message(new LogSubject() + getAmqpActor().message(new LogSubject() { public String toLogString() { diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseActorTestCase.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseActorTestCase.java index ec2cdd5585..30c3a51604 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseActorTestCase.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseActorTestCase.java @@ -20,39 +20,39 @@ */ package org.apache.qpid.server.logging.actors; -import org.apache.qpid.server.configuration.ServerConfiguration; import org.apache.qpid.server.logging.LogActor; import org.apache.qpid.server.logging.LogMessage; import org.apache.qpid.server.logging.LogSubject; import org.apache.qpid.server.logging.RootMessageLogger; import org.apache.qpid.server.logging.UnitTestMessageLogger; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.test.utils.QpidTestCase; -public class BaseActorTestCase extends InternalBrokerBaseCase +public class BaseActorTestCase extends QpidTestCase { - protected LogActor _amqpActor; - protected UnitTestMessageLogger _rawLogger; - protected RootMessageLogger _rootLogger; + private boolean _statusUpdatesEnabled = true; + private LogActor _amqpActor; + private UnitTestMessageLogger _rawLogger; + private RootMessageLogger _rootLogger; @Override - public void configure() + public void setUp() throws Exception { - getConfiguration().getConfig().setProperty(ServerConfiguration.STATUS_UPDATES, "on"); - } - - @Override - public void createBroker() throws Exception - { - super.createBroker(); - - _rawLogger = new UnitTestMessageLogger(getConfiguration()); + super.setUp(); + CurrentActor.removeAll(); + CurrentActor.setDefault(null); + _rawLogger = new UnitTestMessageLogger(_statusUpdatesEnabled); _rootLogger = _rawLogger; } + @Override public void tearDown() throws Exception { - _rawLogger.clearLogMessages(); - + if(_rawLogger != null) + { + _rawLogger.clearLogMessages(); + } + CurrentActor.removeAll(); + CurrentActor.setDefault(null); super.tearDown(); } @@ -87,4 +87,34 @@ public class BaseActorTestCase extends InternalBrokerBaseCase }); } + public boolean isStatusUpdatesEnabled() + { + return _statusUpdatesEnabled; + } + + public void setStatusUpdatesEnabled(boolean statusUpdatesEnabled) + { + _statusUpdatesEnabled = statusUpdatesEnabled; + } + + public LogActor getAmqpActor() + { + return _amqpActor; + } + + public void setAmqpActor(LogActor amqpActor) + { + _amqpActor = amqpActor; + } + + public UnitTestMessageLogger getRawLogger() + { + return _rawLogger; + } + + public RootMessageLogger getRootLogger() + { + return _rootLogger; + } + } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseConnectionActorTestCase.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseConnectionActorTestCase.java index 956d296dce..09dd48e4d3 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseConnectionActorTestCase.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/BaseConnectionActorTestCase.java @@ -20,14 +20,43 @@ */ package org.apache.qpid.server.logging.actors; +import org.apache.qpid.server.protocol.AMQProtocolSession; +import org.apache.qpid.server.util.BrokerTestHelper; + public class BaseConnectionActorTestCase extends BaseActorTestCase { + private AMQProtocolSession _session; @Override - public void createBroker() throws Exception + public void setUp() throws Exception { - super.createBroker(); + super.setUp(); + BrokerTestHelper.setUp(); + _session = BrokerTestHelper.createSession(); + + setAmqpActor(new AMQPConnectionActor(_session, getRootLogger())); + } - _amqpActor = new AMQPConnectionActor(getSession(), _rootLogger); + @Override + public void tearDown() throws Exception + { + try + { + if (_session != null) + { + _session.getVirtualHost().close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } } + + public AMQProtocolSession getSession() + { + return _session; + } + } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java index f73765f5aa..8ea5510ce6 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java @@ -70,12 +70,7 @@ public class CurrentActorTest extends BaseConnectionActorTestCase */ public void testLIFO() throws AMQException, ConfigurationException { - // This test only needs the local objects created, _session etc. - // So stopping the broker and making them useless will not affect the - // test, but the extra actors the test broker adds will so by stopping - // we remove the session actor and so all is good. - stopBroker(); - + assertTrue("Unexpected actor: " + CurrentActor.get(), CurrentActor.get() instanceof TestLogActor); AMQPConnectionActor connectionActor = new AMQPConnectionActor(getSession(), new NullRootMessageLogger()); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/HttpManagementActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/HttpManagementActorTest.java index b40405e1f4..905de4b639 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/HttpManagementActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/HttpManagementActorTest.java @@ -34,10 +34,10 @@ public class HttpManagementActorTest extends BaseActorTestCase private static final String SUFFIX = "(" + IP + ":" + PORT + ")] "; @Override - public void createBroker() throws Exception + public void setUp() throws Exception { - super.createBroker(); - _amqpActor = new HttpManagementActor(_rootLogger, IP, PORT); + super.setUp(); + setAmqpActor(new HttpManagementActor(getRootLogger(), IP, PORT)); } public void testSubjectPrincipalNameAppearance() @@ -48,13 +48,13 @@ public class HttpManagementActorTest extends BaseActorTestCase { public String run() { - return sendTestLogMessage(_amqpActor); + return sendTestLogMessage(getAmqpActor()); } }); assertNotNull("Test log message is not created!", message); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 1, logs.size()); String logMessage = logs.get(0).toString(); @@ -74,7 +74,7 @@ public class HttpManagementActorTest extends BaseActorTestCase private void assertLogMessageWithoutPrincipal() { - String message = _amqpActor.getLogMessage(); + String message = getAmqpActor().getLogMessage(); assertEquals("Unexpected log message", "[mng:" + AbstractManagementActor.UNKNOWN_PRINCIPAL + SUFFIX, message); } @@ -85,7 +85,7 @@ public class HttpManagementActorTest extends BaseActorTestCase { public String run() { - return _amqpActor.getLogMessage(); + return getAmqpActor().getLogMessage(); } }); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/ManagementActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/ManagementActorTest.java index 20cc321aab..a0bfa592db 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/ManagementActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/ManagementActorTest.java @@ -35,10 +35,10 @@ public class ManagementActorTest extends BaseActorTestCase private String _threadName; @Override - public void createBroker() throws Exception + public void setUp() throws Exception { - super.createBroker(); - _amqpActor = new ManagementActor(_rootLogger); + super.setUp(); + setAmqpActor(new ManagementActor(getRootLogger())); // Set the thread name to be the same as a RMI JMX Connection would use _threadName = Thread.currentThread().getName(); @@ -57,14 +57,14 @@ public class ManagementActorTest extends BaseActorTestCase * * The test sends a message then verifies that it entered the logs. * - * The log message should be fully repalaced (no '{n}' values) and should + * The log message should be fully replaced (no '{n}' values) and should * not contain any channel identification. */ public void testConnection() { - final String message = sendTestLogMessage(_amqpActor); + final String message = sendTestLogMessage(getAmqpActor()); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 1, logs.size()); @@ -101,14 +101,14 @@ public class ManagementActorTest extends BaseActorTestCase { public String run() { - return sendTestLogMessage(_amqpActor); + return sendTestLogMessage(getAmqpActor()); } }); // Verify that the log message was created assertNotNull("Test log message is not created!", message); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); // Verify that at least one log message was added to log assertEquals("Message log size not as expected.", 1, logs.size()); @@ -130,8 +130,8 @@ public class ManagementActorTest extends BaseActorTestCase public void testGetLogMessageWithoutSubjectButWithActorPrincipal() { String principalName = "my_principal"; - _amqpActor = new ManagementActor(_rootLogger, principalName); - String message = _amqpActor.getLogMessage(); + setAmqpActor(new ManagementActor(getRootLogger(), principalName)); + String message = getAmqpActor().getLogMessage(); assertEquals("Unexpected log message", "[mng:" + principalName + "(" + IP + ")] ", message); } @@ -149,7 +149,7 @@ public class ManagementActorTest extends BaseActorTestCase assertLogMessageInRMIThreadWithPrincipal("RMI TCP Connection(1)-" + IP, "my_principal"); Thread.currentThread().setName("RMI TCP Connection(2)-" + IP ); - String message = _amqpActor.getLogMessage(); + String message = getAmqpActor().getLogMessage(); assertEquals("Unexpected log message", "[mng:N/A(" + IP + ")] ", message); assertLogMessageWithoutPrincipal("TEST"); @@ -158,14 +158,14 @@ public class ManagementActorTest extends BaseActorTestCase private void assertLogMessageInRMIThreadWithoutPrincipal(String threadName) { Thread.currentThread().setName(threadName ); - String message = _amqpActor.getLogMessage(); + String message = getAmqpActor().getLogMessage(); assertEquals("Unexpected log message", "[mng:N/A(" + IP + ")] ", message); } private void assertLogMessageWithoutPrincipal(String threadName) { Thread.currentThread().setName(threadName ); - String message = _amqpActor.getLogMessage(); + String message = getAmqpActor().getLogMessage(); assertEquals("Unexpected log message", "[" + threadName +"] ", message); } @@ -177,7 +177,7 @@ public class ManagementActorTest extends BaseActorTestCase { public String run() { - return _amqpActor.getLogMessage(); + return getAmqpActor().getLogMessage(); } }); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/QueueActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/QueueActorTest.java index 409f7c84b7..2dc44c58ce 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/QueueActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/QueueActorTest.java @@ -22,14 +22,16 @@ package org.apache.qpid.server.logging.actors; import java.util.List; +import org.apache.qpid.server.util.BrokerTestHelper; + public class QueueActorTest extends BaseConnectionActorTestCase { @Override - public void createBroker() throws Exception + public void setUp() throws Exception { - super.createBroker(); - _amqpActor = new QueueActor(getQueue(), _rootLogger); + super.setUp(); + setAmqpActor(new QueueActor(BrokerTestHelper.createQueue(getName(), getSession().getVirtualHost()), getRootLogger())); } /** @@ -42,9 +44,9 @@ public class QueueActorTest extends BaseConnectionActorTestCase */ public void testQueueActor() { - final String message = sendTestLogMessage(_amqpActor); + final String message = sendTestLogMessage(getAmqpActor()); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 1, logs.size()); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/SubscriptionActorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/SubscriptionActorTest.java index 8eaa165853..58fca488c4 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/SubscriptionActorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/SubscriptionActorTest.java @@ -21,6 +21,7 @@ package org.apache.qpid.server.logging.actors; import org.apache.qpid.server.subscription.MockSubscription; +import org.apache.qpid.server.util.BrokerTestHelper; import java.util.List; @@ -37,15 +38,15 @@ public class SubscriptionActorTest extends BaseConnectionActorTestCase { @Override - public void createBroker() throws Exception + public void setUp() throws Exception { - super.createBroker(); + super.setUp(); MockSubscription mockSubscription = new MockSubscription(); - mockSubscription.setQueue(getQueue(), false); + mockSubscription.setQueue(BrokerTestHelper.createQueue(getName(), getSession().getVirtualHost()), false); - _amqpActor = new SubscriptionActor(_rootLogger, mockSubscription); + setAmqpActor(new SubscriptionActor(getRootLogger(), mockSubscription)); } /** @@ -58,9 +59,9 @@ public class SubscriptionActorTest extends BaseConnectionActorTestCase */ public void testSubscription() { - final String message = sendTestLogMessage(_amqpActor); + final String message = sendTestLogMessage(getAmqpActor()); - List<Object> logs = _rawLogger.getLogMessages(); + List<Object> logs = getRawLogger().getLogMessages(); assertEquals("Message log size not as expected.", 1, logs.size()); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/AbstractTestMessages.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/AbstractTestMessages.java index 24e7225d82..229d75c69f 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/AbstractTestMessages.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/AbstractTestMessages.java @@ -29,11 +29,12 @@ import org.apache.qpid.server.logging.LogSubject; import org.apache.qpid.server.logging.UnitTestMessageLogger; import org.apache.qpid.server.logging.actors.TestLogActor; import org.apache.qpid.server.logging.subjects.TestBlankSubject; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.List; -public abstract class AbstractTestMessages extends InternalBrokerBaseCase +public abstract class AbstractTestMessages extends QpidTestCase { protected Configuration _config = new PropertiesConfiguration(); protected LogMessage _logMessage = null; @@ -49,6 +50,14 @@ public abstract class AbstractTestMessages extends InternalBrokerBaseCase _logger = new UnitTestMessageLogger(); _actor = new TestLogActor(_logger); + BrokerTestHelper.setUp(); + } + + @Override + public void tearDown() throws Exception + { + BrokerTestHelper.tearDown(); + super.tearDown(); } protected List<Object> performLog() diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/ExchangeMessagesTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/ExchangeMessagesTest.java index 4364376000..1cb4da55c3 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/ExchangeMessagesTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/messages/ExchangeMessagesTest.java @@ -21,7 +21,7 @@ package org.apache.qpid.server.logging.messages; import org.apache.qpid.server.exchange.Exchange; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import java.util.List; @@ -30,12 +30,9 @@ import java.util.List; */ public class ExchangeMessagesTest extends AbstractTestMessages { - public void testExchangeCreated_Transient() + public void testExchangeCreated_Transient() throws Exception { - // Get the Default Exchange on the Test Vhost for testing - Exchange exchange = ApplicationRegistry.getInstance(). - getVirtualHostRegistry().getVirtualHost("test"). - getExchangeRegistry().getDefaultExchange(); + Exchange exchange = BrokerTestHelper.createExchange("test"); String type = exchange.getTypeShortString().toString(); String name = exchange.getNameShortString().toString(); @@ -48,12 +45,9 @@ public class ExchangeMessagesTest extends AbstractTestMessages validateLogMessage(log, "EXH-1001", expected); } - public void testExchangeCreated_Persistent() + public void testExchangeCreated_Persistent() throws Exception { - // Get the Default Exchange on the Test Vhost for testing - Exchange exchange = ApplicationRegistry.getInstance(). - getVirtualHostRegistry().getVirtualHost("test"). - getExchangeRegistry().getDefaultExchange(); + Exchange exchange = BrokerTestHelper.createExchange("test"); String type = exchange.getTypeShortString().toString(); String name = exchange.getNameShortString().toString(); @@ -76,12 +70,9 @@ public class ExchangeMessagesTest extends AbstractTestMessages validateLogMessage(log, "EXH-1002", expected); } - public void testExchangeDiscardedMessage() + public void testExchangeDiscardedMessage() throws Exception { - // Get the Default Exchange on the Test Vhost for testing - final Exchange exchange = ApplicationRegistry.getInstance(). - getVirtualHostRegistry().getVirtualHost("test"). - getExchangeRegistry().getDefaultExchange(); + Exchange exchange = BrokerTestHelper.createExchange("test"); final String name = exchange.getNameShortString().toString(); final String routingKey = "routingKey"; diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/AbstractTestLogSubject.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/AbstractTestLogSubject.java index c2558d2d1b..193e8a490d 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/AbstractTestLogSubject.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/AbstractTestLogSubject.java @@ -20,21 +20,19 @@ */ package org.apache.qpid.server.logging.subjects; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.qpid.framing.AMQShortString; -import org.apache.qpid.server.configuration.ServerConfiguration; import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.logging.LogActor; import org.apache.qpid.server.logging.LogMessage; import org.apache.qpid.server.logging.LogSubject; import org.apache.qpid.server.logging.UnitTestMessageLogger; +import org.apache.qpid.server.logging.actors.CurrentActor; import org.apache.qpid.server.logging.actors.TestLogActor; import org.apache.qpid.server.queue.AMQQueue; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.List; @@ -49,29 +47,39 @@ import java.util.List; * The resulting log file is then validated. * */ -public abstract class AbstractTestLogSubject extends InternalBrokerBaseCase +public abstract class AbstractTestLogSubject extends QpidTestCase { - protected Configuration _config = new PropertiesConfiguration(); protected LogSubject _subject = null; @Override public void setUp() throws Exception { super.setUp(); - - _config.setProperty(ServerConfiguration.STATUS_UPDATES, "ON"); + BrokerTestHelper.setUp(); } + @Override + public void tearDown() throws Exception + { + BrokerTestHelper.tearDown(); + try + { + CurrentActor.removeAll(); + } + finally + { + super.tearDown(); + } + } - protected List<Object> performLog() throws ConfigurationException + protected List<Object> performLog(boolean statusUpdatesEnabled) { if (_subject == null) { throw new NullPointerException("LogSubject has not been set"); } - ServerConfiguration serverConfig = new ServerConfiguration(_config); - UnitTestMessageLogger logger = new UnitTestMessageLogger(serverConfig); + UnitTestMessageLogger logger = new UnitTestMessageLogger(statusUpdatesEnabled); LogActor actor = new TestLogActor(logger); @@ -247,11 +255,10 @@ public abstract class AbstractTestLogSubject extends InternalBrokerBaseCase /** * Test that when Logging occurs a single log statement is provided * - * @throws ConfigurationException */ - public void testEnabled() throws ConfigurationException + public void testEnabled() { - List<Object> logs = performLog(); + List<Object> logs = performLog(true); assertEquals("Log has incorrect message count", 1, logs.size()); @@ -267,15 +274,11 @@ public abstract class AbstractTestLogSubject extends InternalBrokerBaseCase protected abstract void validateLogStatement(String message); /** - * Ensure that when status-updates are off this does not perform logging - * - * @throws ConfigurationException + * Ensure that when status updates are off this does not perform logging */ - public void testDisabled() throws ConfigurationException + public void testDisabled() { - _config.setProperty(ServerConfiguration.STATUS_UPDATES, "OFF"); - - List<Object> logs = performLog(); + List<Object> logs = performLog(false); assertEquals("Log has incorrect message count", 0, logs.size()); } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/BindingLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/BindingLogSubjectTest.java index e80c4c4679..dd8d28e836 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/BindingLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/BindingLogSubjectTest.java @@ -24,7 +24,7 @@ import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.MockAMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; /** @@ -38,13 +38,12 @@ public class BindingLogSubjectTest extends AbstractTestLogSubject private Exchange _exchange; private VirtualHost _testVhost; + @Override public void setUp() throws Exception { super.setUp(); - _testVhost = ApplicationRegistry.getInstance().getVirtualHostRegistry(). - getVirtualHost("test"); - // Configure items for subjectCreation + _testVhost = BrokerTestHelper.createVirtualHost("test"); _routingKey = new AMQShortString("RoutingKey"); _exchange = _testVhost.getExchangeRegistry().getDefaultExchange(); _queue = new MockAMQQueue("BindingLogSubjectTest"); @@ -53,6 +52,16 @@ public class BindingLogSubjectTest extends AbstractTestLogSubject _subject = new BindingLogSubject(String.valueOf(_routingKey), _exchange, _queue); } + @Override + public void tearDown() throws Exception + { + if (_testVhost != null) + { + _testVhost.close(); + } + super.tearDown(); + } + /** * Validate that the logged Subject message is as expected: * MESSAGE [Blank][vh(/test)/ex(direct/<<default>>)/qu(BindingLogSubjectTest)/rk(RoutingKey)] <Log Message> diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ChannelLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ChannelLogSubjectTest.java index 6bc5effa05..d75e033739 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ChannelLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ChannelLogSubjectTest.java @@ -34,6 +34,7 @@ public class ChannelLogSubjectTest extends ConnectionLogSubjectTest { super.setUp(); + AMQChannel channel = new AMQChannel(getSession(), _channelID, getSession().getVirtualHost().getMessageStore()); _subject = new ChannelLogSubject(channel); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ConnectionLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ConnectionLogSubjectTest.java index c246fff2a8..7dc4c443ba 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ConnectionLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ConnectionLogSubjectTest.java @@ -20,17 +20,34 @@ */ package org.apache.qpid.server.logging.subjects; +import org.apache.qpid.server.protocol.InternalTestProtocolSession; +import org.apache.qpid.server.util.BrokerTestHelper; + /** * Validate ConnectionLogSubjects are logged as expected */ public class ConnectionLogSubjectTest extends AbstractTestLogSubject { + private InternalTestProtocolSession _session; + + @Override public void setUp() throws Exception { super.setUp(); - _subject = new ConnectionLogSubject(getSession()); + _session = BrokerTestHelper.createSession("test"); + _subject = new ConnectionLogSubject(_session); + } + + @Override + public void tearDown() throws Exception + { + if (_session != null) + { + _session.getVirtualHost().close(); + } + super.tearDown(); } /** @@ -40,7 +57,12 @@ public class ConnectionLogSubjectTest extends AbstractTestLogSubject */ protected void validateLogStatement(String message) { - verifyConnection(getSession().getSessionID(), "InternalTestProtocolSession", "127.0.0.1:1", "test", message); + verifyConnection(_session.getSessionID(), "InternalTestProtocolSession", "127.0.0.1:1", "test", message); + } + + public InternalTestProtocolSession getSession() + { + return _session; } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ExchangeLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ExchangeLogSubjectTest.java index cc06b05bf6..8d1b89bf3c 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ExchangeLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/ExchangeLogSubjectTest.java @@ -21,7 +21,7 @@ package org.apache.qpid.server.logging.subjects; import org.apache.qpid.server.exchange.Exchange; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; @@ -33,17 +33,27 @@ public class ExchangeLogSubjectTest extends AbstractTestLogSubject private Exchange _exchange; private VirtualHost _testVhost; + @Override public void setUp() throws Exception { super.setUp(); - _testVhost = ApplicationRegistry.getInstance().getVirtualHostRegistry(). - getVirtualHost("test"); + _testVhost = BrokerTestHelper.createVirtualHost("test"); _exchange = _testVhost.getExchangeRegistry().getDefaultExchange(); _subject = new ExchangeLogSubject(_exchange,_testVhost); } + @Override + public void tearDown() throws Exception + { + if (_testVhost != null) + { + _testVhost.close(); + } + super.tearDown(); + } + /** * Validate that the logged Subject message is as expected: * MESSAGE [Blank][vh(/test)/ex(direct/<<default>>)] <Log Message> diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/MessageStoreLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/MessageStoreLogSubjectTest.java index c62b24c3b9..65fd249d03 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/MessageStoreLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/MessageStoreLogSubjectTest.java @@ -20,7 +20,7 @@ */ package org.apache.qpid.server.logging.subjects; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; /** @@ -30,16 +30,26 @@ public class MessageStoreLogSubjectTest extends AbstractTestLogSubject { private VirtualHost _testVhost; + @Override public void setUp() throws Exception { super.setUp(); - _testVhost = ApplicationRegistry.getInstance().getVirtualHostRegistry(). - getVirtualHost("test"); + _testVhost = BrokerTestHelper.createVirtualHost("test"); _subject = new MessageStoreLogSubject(_testVhost, _testVhost.getMessageStore().getClass().getSimpleName()); } + @Override + public void tearDown() throws Exception + { + if (_testVhost != null) + { + _testVhost.close(); + } + super.tearDown(); + } + /** * Validate that the logged Subject message is as expected: * MESSAGE [Blank][vh(/test)/ms(MemoryMessageStore)] <Log Message> diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/QueueLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/QueueLogSubjectTest.java index 1f432be57a..e2765f338b 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/QueueLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/QueueLogSubjectTest.java @@ -22,7 +22,7 @@ package org.apache.qpid.server.logging.subjects; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.MockAMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; /** @@ -39,8 +39,7 @@ public class QueueLogSubjectTest extends AbstractTestLogSubject { super.setUp(); - _testVhost = ApplicationRegistry.getInstance().getVirtualHostRegistry(). - getVirtualHost("test"); + _testVhost = BrokerTestHelper.createVirtualHost("test"); _queue = new MockAMQQueue("QueueLogSubjectTest"); ((MockAMQQueue) _queue).setVirtualHost(_testVhost); @@ -48,6 +47,16 @@ public class QueueLogSubjectTest extends AbstractTestLogSubject _subject = new QueueLogSubject(_queue); } + @Override + public void tearDown() throws Exception + { + if (_testVhost != null) + { + _testVhost.close(); + } + super.tearDown(); + } + /** * Validate that the logged Subject message is as expected: * MESSAGE [Blank][vh(/test)/qu(QueueLogSubjectTest)] <Log Message> diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/SubscriptionLogSubjectTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/SubscriptionLogSubjectTest.java index 0c356e1838..153d01f355 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/SubscriptionLogSubjectTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/subjects/SubscriptionLogSubjectTest.java @@ -23,12 +23,13 @@ package org.apache.qpid.server.logging.subjects; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.server.AMQChannel; import org.apache.qpid.server.flow.LimitlessCreditManager; +import org.apache.qpid.server.protocol.InternalTestProtocolSession; import org.apache.qpid.server.queue.AMQQueue; import org.apache.qpid.server.queue.MockAMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.subscription.Subscription; import org.apache.qpid.server.subscription.SubscriptionFactory; import org.apache.qpid.server.subscription.SubscriptionFactoryImpl; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; /** @@ -42,23 +43,24 @@ public class SubscriptionLogSubjectTest extends AbstractTestLogSubject private int _channelID = 1; private Subscription _subscription; + @Override public void setUp() throws Exception { super.setUp(); - _testVhost = ApplicationRegistry.getInstance().getVirtualHostRegistry(). - getVirtualHost("test"); + InternalTestProtocolSession session = BrokerTestHelper.createSession(); + _testVhost = session.getVirtualHost(); _queue = new MockAMQQueue("SubscriptionLogSubjectTest"); ((MockAMQQueue) _queue).setVirtualHost(_testVhost); - AMQChannel channel = new AMQChannel(getSession(), _channelID, getSession().getVirtualHost().getMessageStore()); + AMQChannel channel = new AMQChannel(session, _channelID, _testVhost.getMessageStore()); - getSession().addChannel(channel); + session.addChannel(channel); SubscriptionFactory factory = new SubscriptionFactoryImpl(); - _subscription = factory.createSubscription(_channelID, getSession(), new AMQShortString("cTag"), + _subscription = factory.createSubscription(_channelID, session, new AMQShortString("cTag"), false, null, false, new LimitlessCreditManager()); @@ -67,6 +69,16 @@ public class SubscriptionLogSubjectTest extends AbstractTestLogSubject _subject = new SubscriptionLogSubject(_subscription); } + @Override + public void tearDown() throws Exception + { + if (_testVhost != null) + { + _testVhost.close(); + } + super.tearDown(); + } + /** * Validate that the logged Subject message is as expected: * MESSAGE [Blank][sub:0(vh(/test)/qu(SubscriptionLogSubjectTest))] <Log Message> diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java new file mode 100644 index 0000000000..6ed9740c6d --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java @@ -0,0 +1,216 @@ +/* + * + * 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.model; + +import static org.mockito.Mockito.mock; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer; +import org.apache.qpid.server.configuration.RecovererProvider; +import org.apache.qpid.server.configuration.startup.DefaultRecovererProvider; +import org.apache.qpid.server.logging.LogRecorder; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.configuration.updater.TaskExecutor; +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.security.auth.manager.PlainPasswordFileAuthenticationManagerFactory; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.test.utils.TestFileUtils; + +import java.io.File; +import java.security.Provider; +import java.security.Security; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * QPID-1390 : Test to validate that the AuthenticationManger can successfully unregister any new SASL providers when + * the broker is stopped. + */ +public class BrokerShutdownTest extends QpidTestCase +{ + private Provider[] _defaultProviders; + private Broker _broker; + private TaskExecutor _taskExecutor; + + @Override + public void setUp() throws Exception + { + // Get default providers + _defaultProviders = Security.getProviders(); + + super.setUp(); + + _taskExecutor = new TaskExecutor(); + _taskExecutor.start(); + + // Startup the new broker and register the new providers + _broker = startBroker(); + } + + @Override + public void tearDown() throws Exception + { + try + { + super.tearDown(); + } + finally + { + if (_taskExecutor != null) + { + _taskExecutor.stopImmediately(); + } + } + + } + + private Broker startBroker() throws Exception + { + // test store with only broker and authentication provider entries + ConfigurationEntryStore store = new ConfigurationEntryStore() + { + private UUID _brokerId = UUID.randomUUID(); + private UUID _authenticationProviderId = UUID.randomUUID(); + + @Override + public ConfigurationEntry getRootEntry() + { + return new ConfigurationEntry(_brokerId, Broker.class.getSimpleName(), Collections.<String, Object> emptyMap(), + Collections.singleton(_authenticationProviderId), this); + } + + @Override + public ConfigurationEntry getEntry(UUID id) + { + if (_authenticationProviderId.equals(id)) + { + File file = TestFileUtils.createTempFile(BrokerShutdownTest.this, ".db.users"); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(AuthenticationManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + attributes.put(PlainPasswordFileAuthenticationManagerFactory.ATTRIBUTE_PATH, file.getAbsolutePath()); + return new ConfigurationEntry(_authenticationProviderId, AuthenticationProvider.class.getSimpleName(), attributes, + Collections.<UUID> emptySet(), this); + } + return null; + } + + @Override + public void save(ConfigurationEntry... entries) + { + } + + @Override + public UUID[] remove(UUID... entryIds) + { + return null; + } + + @Override + public void open(String storeLocation) + { + } + + }; + + // mocking the required object + StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); + VirtualHostRegistry virtualHostRegistry = mock(VirtualHostRegistry.class); + LogRecorder logRecorder = mock(LogRecorder.class); + RootMessageLogger rootMessageLogger = mock(RootMessageLogger.class); + + // recover the broker from the store + RecovererProvider provider = new DefaultRecovererProvider(statisticsGatherer, virtualHostRegistry, logRecorder, rootMessageLogger, _taskExecutor); + ConfiguredObjectRecoverer<? extends ConfiguredObject> brokerRecoverer = provider.getRecoverer(Broker.class.getSimpleName()); + + Broker broker = (Broker) brokerRecoverer.create(provider, store.getRootEntry()); + + // start broker + broker.setDesiredState(State.INITIALISING, State.ACTIVE); + return broker; + } + + private void stopBroker() + { + _broker.setDesiredState(State.ACTIVE, State.STOPPED); + } + + /** + * QPID-1399 : Ensure that the Authentication manager unregisters any SASL providers created during + * broker start-up. + * + */ + public void testAuthenticationMangerCleansUp() throws Exception + { + + // Get the providers after initialisation + Provider[] providersAfterInitialisation = Security.getProviders(); + + // Find the additions + List<Provider> additions = new LinkedList<Provider>(); + for (Provider afterInit : providersAfterInitialisation) + { + boolean found = false; + for (Provider defaultProvider : _defaultProviders) + { + if (defaultProvider == afterInit) + { + found = true; + break; + } + } + + // Record added registies + if (!found) + { + additions.add(afterInit); + } + } + + assertFalse("No new SASL mechanisms added by initialisation.", additions.isEmpty()); + + // Close the registry which will perform the close the + // AuthenticationManager + stopBroker(); + + // Validate that the SASL plugins have been removed. + Provider[] providersAfterClose = Security.getProviders(); + + assertTrue("No providers unregistered", providersAfterInitialisation.length > providersAfterClose.length); + + // Ensure that the additions are not still present after close(). + for (Provider afterClose : providersAfterClose) + { + assertFalse("Added provider not unregistered", additions.contains(afterClose)); + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java new file mode 100644 index 0000000000..585fecae83 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java @@ -0,0 +1,85 @@ +/* + * + * 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.model.adapter; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import junit.framework.TestCase; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.model.AuthenticationProvider; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.PasswordCredentialManagingAuthenticationProvider; +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.plugin.QpidServiceLoader; +import org.apache.qpid.server.security.auth.manager.AuthenticationManager; +import org.apache.qpid.server.security.auth.manager.PrincipalDatabaseAuthenticationManager; + +public class AuthenticationProviderFactoryTest extends TestCase +{ + + public void testCreatePasswordCredentialManagingAuthenticationProvider() + { + AuthenticationProvider provider = testForFactory(mock(PrincipalDatabaseAuthenticationManager.class)); + assertTrue("The created provider should match the factory's AuthenticationManager type", + provider instanceof PasswordCredentialManagingAuthenticationProvider); + } + + public void testCreateNonPasswordCredentialManagingAuthenticationProvider() + { + AuthenticationProvider provider = testForFactory(mock(AuthenticationManager.class)); + assertFalse("The created provider should match the factory's AuthenticationManager type", + provider instanceof PasswordCredentialManagingAuthenticationProvider); + } + + @SuppressWarnings("unchecked") + private AuthenticationProvider testForFactory(AuthenticationManager authenticationManager) + { + UUID id = UUID.randomUUID(); + Map<String, Object> attributes = new HashMap<String, Object>(); + + QpidServiceLoader<AuthenticationManagerFactory> authManagerFactoryServiceLoader = mock(QpidServiceLoader.class); + AuthenticationManagerFactory authenticationManagerFactory = mock(AuthenticationManagerFactory.class); + ConfigurationEntry configurationEntry = mock(ConfigurationEntry.class); + + when(configurationEntry.getId()).thenReturn(id); + Broker broker = mock(Broker.class); + + when(authManagerFactoryServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn( + Collections.singleton(authenticationManagerFactory)); + when(authenticationManagerFactory.createInstance(attributes)).thenReturn(authenticationManager); + + AuthenticationProviderFactory providerFactory = new AuthenticationProviderFactory(authManagerFactoryServiceLoader); + AuthenticationProvider provider = providerFactory.create(id, broker, attributes, null); + + assertNotNull("Provider is not created", provider); + assertEquals("Unexpected ID", id, provider.getId()); + + return provider; + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/PortFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/PortFactoryTest.java new file mode 100644 index 0000000000..14c5c265c9 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/PortFactoryTest.java @@ -0,0 +1,212 @@ +/* + * + * 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.model.adapter; + +import static org.mockito.Mockito.mock; + +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.Map; +import java.util.Set; +import java.util.UUID; + +import org.apache.qpid.server.configuration.BrokerProperties; +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.Transport; +import org.apache.qpid.test.utils.QpidTestCase; + +public class PortFactoryTest extends QpidTestCase +{ + private UUID _portId = UUID.randomUUID(); + private int _portNumber = 123; + private Set<String> _tcpStringSet = Collections.singleton(Transport.SSL.name()); + private Set<Transport> _tcpTransportSet = Collections.singleton(Transport.SSL); + + private Map<String, Object> _attributes = new HashMap<String, Object>(); + + private Broker _broker = mock(Broker.class); + private PortFactory _portFactory; + + @Override + protected void setUp() throws Exception + { + setTestSystemProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_EXCLUDES, null); + setTestSystemProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_INCLUDES, null); + _portFactory = new PortFactory(); + + _attributes.put(Port.PORT, _portNumber); + _attributes.put(Port.TRANSPORTS, _tcpStringSet); + + _attributes.put(Port.TCP_NO_DELAY, "true"); + _attributes.put(Port.RECEIVE_BUFFER_SIZE, "1"); + _attributes.put(Port.SEND_BUFFER_SIZE, "2"); + _attributes.put(Port.NEED_CLIENT_AUTH, "true"); + _attributes.put(Port.WANT_CLIENT_AUTH, "true"); + _attributes.put(Port.BINDING_ADDRESS, "127.0.0.1"); + } + + public void testDefaultProtocols() + { + Collection<Protocol> protocols = _portFactory.getDefaultProtocols(); + EnumSet<Protocol> expected = EnumSet.of(Protocol.AMQP_0_8, Protocol.AMQP_0_9, Protocol.AMQP_0_9_1, Protocol.AMQP_0_10, + Protocol.AMQP_1_0); + assertEquals("Unexpected protocols", new HashSet<Protocol>(expected), new HashSet<Protocol>(protocols)); + } + + public void testDefaultProtocolsWhenProtocolExcludeSystemPropertyIsSet() + { + setTestSystemProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_EXCLUDES, Protocol.AMQP_1_0.name() + "," + + Protocol.AMQP_0_10.name()); + _portFactory = new PortFactory(); + Collection<Protocol> protocols = _portFactory.getDefaultProtocols(); + EnumSet<Protocol> expected = EnumSet.of(Protocol.AMQP_0_8, Protocol.AMQP_0_9, Protocol.AMQP_0_9_1); + assertEquals("Unexpected protocols", new HashSet<Protocol>(expected), new HashSet<Protocol>(protocols)); + } + + public void testDefaultProtocolsWhenProtocolIncludeSystemPropertyIsSet() + { + setTestSystemProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_EXCLUDES, Protocol.AMQP_1_0.name() + "," + + Protocol.AMQP_0_10.name() + "," + Protocol.AMQP_0_9_1.name()); + setTestSystemProperty(BrokerProperties.PROPERTY_BROKER_DEFAULT_AMQP_PROTOCOL_INCLUDES, Protocol.AMQP_0_10.name() + "," + + Protocol.AMQP_0_9_1.name()); + _portFactory = new PortFactory(); + Collection<Protocol> protocols = _portFactory.getDefaultProtocols(); + EnumSet<Protocol> expected = EnumSet.of(Protocol.AMQP_0_8, Protocol.AMQP_0_9, Protocol.AMQP_0_9_1, Protocol.AMQP_0_10); + assertEquals("Unexpected protocols", new HashSet<Protocol>(expected), new HashSet<Protocol>(protocols)); + } + + public void testCreatePortWithMinimumAttributes() + { + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(Port.PORT, 1); + Port port = _portFactory.createPort(_portId, _broker, attributes); + + assertNotNull(port); + assertTrue(port instanceof AmqpPortAdapter); + assertEquals("Unexpected port", 1, port.getPort()); + assertEquals("Unexpected transports", Collections.singleton(PortFactory.DEFAULT_TRANSPORT), port.getTransports()); + assertEquals("Unexpected protocols", _portFactory.getDefaultProtocols(), port.getProtocols()); + assertEquals("Unexpected send buffer size", PortFactory.DEFAULT_AMQP_SEND_BUFFER_SIZE, + port.getAttribute(Port.SEND_BUFFER_SIZE)); + assertEquals("Unexpected receive buffer size", PortFactory.DEFAULT_AMQP_RECEIVE_BUFFER_SIZE, + port.getAttribute(Port.RECEIVE_BUFFER_SIZE)); + assertEquals("Unexpected need client auth", PortFactory.DEFAULT_AMQP_NEED_CLIENT_AUTH, + port.getAttribute(Port.NEED_CLIENT_AUTH)); + assertEquals("Unexpected want client auth", PortFactory.DEFAULT_AMQP_WANT_CLIENT_AUTH, + port.getAttribute(Port.WANT_CLIENT_AUTH)); + assertEquals("Unexpected tcp no delay", PortFactory.DEFAULT_AMQP_TCP_NO_DELAY, port.getAttribute(Port.TCP_NO_DELAY)); + assertEquals("Unexpected binding", PortFactory.DEFAULT_AMQP_BINDING, port.getAttribute(Port.BINDING_ADDRESS)); + } + + public void testCreateAmqpPort() + { + Set<Protocol> amqp010ProtocolSet = Collections.singleton(Protocol.AMQP_0_10); + Set<String> amqp010StringSet = Collections.singleton(Protocol.AMQP_0_10.name()); + _attributes.put(Port.PROTOCOLS, amqp010StringSet); + + Port port = _portFactory.createPort(_portId, _broker, _attributes); + + assertNotNull(port); + assertTrue(port instanceof AmqpPortAdapter); + assertEquals(_portId, port.getId()); + assertEquals(_portNumber, port.getPort()); + assertEquals(_tcpTransportSet, port.getTransports()); + assertEquals(amqp010ProtocolSet, port.getProtocols()); + assertEquals("Unexpected send buffer size", 2, port.getAttribute(Port.SEND_BUFFER_SIZE)); + assertEquals("Unexpected receive buffer size", 1, port.getAttribute(Port.RECEIVE_BUFFER_SIZE)); + assertEquals("Unexpected need client auth", true, port.getAttribute(Port.NEED_CLIENT_AUTH)); + assertEquals("Unexpected want client auth", true, port.getAttribute(Port.WANT_CLIENT_AUTH)); + assertEquals("Unexpected tcp no delay", true, port.getAttribute(Port.TCP_NO_DELAY)); + assertEquals("Unexpected binding", "127.0.0.1", port.getAttribute(Port.BINDING_ADDRESS)); + } + + public void testCreateNonAmqpPort() + { + Set<Protocol> nonAmqpProtocolSet = Collections.singleton(Protocol.JMX_RMI); + Set<String> nonAmqpStringSet = Collections.singleton(Protocol.JMX_RMI.name()); + _attributes = new HashMap<String, Object>(); + _attributes.put(Port.PROTOCOLS, nonAmqpStringSet); + _attributes.put(Port.PORT, _portNumber); + _attributes.put(Port.TRANSPORTS, _tcpStringSet); + + Port port = _portFactory.createPort(_portId, _broker, _attributes); + + assertNotNull(port); + assertFalse("Port should be a PortAdapter, not its AMQP-specific subclass", port instanceof AmqpPortAdapter); + assertEquals(_portId, port.getId()); + assertEquals(_portNumber, port.getPort()); + assertEquals(_tcpTransportSet, port.getTransports()); + assertEquals(nonAmqpProtocolSet, port.getProtocols()); + assertNull("Unexpected send buffer size", port.getAttribute(Port.SEND_BUFFER_SIZE)); + assertNull("Unexpected receive buffer size", port.getAttribute(Port.RECEIVE_BUFFER_SIZE)); + assertNull("Unexpected need client auth", port.getAttribute(Port.NEED_CLIENT_AUTH)); + assertNull("Unexpected want client auth", port.getAttribute(Port.WANT_CLIENT_AUTH)); + assertNull("Unexpected tcp no delay", port.getAttribute(Port.TCP_NO_DELAY)); + assertNull("Unexpected binding", port.getAttribute(Port.BINDING_ADDRESS)); + } + + public void testCreateNonAmqpPortWithPartiallySetAttributes() + { + Set<Protocol> nonAmqpProtocolSet = Collections.singleton(Protocol.JMX_RMI); + Set<String> nonAmqpStringSet = Collections.singleton(Protocol.JMX_RMI.name()); + _attributes = new HashMap<String, Object>(); + _attributes.put(Port.PROTOCOLS, nonAmqpStringSet); + _attributes.put(Port.PORT, _portNumber); + + Port port = _portFactory.createPort(_portId, _broker, _attributes); + + assertNotNull(port); + assertFalse("Port should be a PortAdapter, not its AMQP-specific subclass", port instanceof AmqpPortAdapter); + assertEquals(_portId, port.getId()); + assertEquals(_portNumber, port.getPort()); + assertEquals(Collections.singleton(PortFactory.DEFAULT_TRANSPORT), port.getTransports()); + assertEquals(nonAmqpProtocolSet, port.getProtocols()); + assertNull("Unexpected send buffer size", port.getAttribute(Port.SEND_BUFFER_SIZE)); + assertNull("Unexpected receive buffer size", port.getAttribute(Port.RECEIVE_BUFFER_SIZE)); + assertNull("Unexpected need client auth", port.getAttribute(Port.NEED_CLIENT_AUTH)); + assertNull("Unexpected want client auth", port.getAttribute(Port.WANT_CLIENT_AUTH)); + assertNull("Unexpected tcp no delay", port.getAttribute(Port.TCP_NO_DELAY)); + assertNull("Unexpected binding", port.getAttribute(Port.BINDING_ADDRESS)); + } + + public void testCreateMixedAmqpAndNonAmqpThrowsException() + { + Set<String> mixedProtocolSet = new HashSet<String>(Arrays.asList(Protocol.AMQP_0_10.name(), Protocol.JMX_RMI.name())); + _attributes.put(Port.PROTOCOLS, mixedProtocolSet); + + try + { + _portFactory.createPort(_portId, _broker, _attributes); + fail("Exception not thrown"); + } + catch (IllegalConfigurationException e) + { + // pass + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/configuration/ConfigurationEntryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/configuration/ConfigurationEntryTest.java new file mode 100644 index 0000000000..dd48d7b56d --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/configuration/ConfigurationEntryTest.java @@ -0,0 +1,129 @@ +/* + * + * 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.model.configuration; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +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.Set; +import java.util.UUID; + +import junit.framework.TestCase; + +import org.apache.qpid.server.configuration.ConfigurationEntry; +import org.apache.qpid.server.configuration.ConfigurationEntryStore; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.VirtualHost; + +public class ConfigurationEntryTest extends TestCase +{ + public void testGetChildren() + { + ConfigurationEntryStore store = mock(ConfigurationEntryStore.class); + + ConfigurationEntry virtualHostEntry1 = new ConfigurationEntry(UUID.randomUUID(), VirtualHost.class.getSimpleName(), + Collections.<String, Object> emptyMap(), Collections.<UUID> emptySet(), store); + ConfigurationEntry virtualHostEntry2 = new ConfigurationEntry(UUID.randomUUID(), VirtualHost.class.getSimpleName(), + Collections.<String, Object> emptyMap(), Collections.<UUID> emptySet(), store); + ConfigurationEntry portEntry = new ConfigurationEntry(UUID.randomUUID(), Port.class.getSimpleName(), + Collections.<String, Object> emptyMap(), Collections.<UUID> emptySet(), store); + + when(store.getEntry(virtualHostEntry1.getId())).thenReturn(virtualHostEntry1); + when(store.getEntry(virtualHostEntry2.getId())).thenReturn(virtualHostEntry2); + when(store.getEntry(portEntry.getId())).thenReturn(portEntry); + + Set<UUID> childrenIds = new HashSet<UUID>(); + childrenIds.add(virtualHostEntry1.getId()); + childrenIds.add(virtualHostEntry2.getId()); + childrenIds.add(portEntry.getId()); + ConfigurationEntry parentEntry = new ConfigurationEntry(UUID.randomUUID(), Broker.class.getSimpleName(), + Collections.<String, Object> emptyMap(), childrenIds, store); + + Map<String, Collection<ConfigurationEntry>> children = parentEntry.getChildren(); + assertNotNull("Null is returned for children", children); + assertEquals("Unexpected size", 2, children.size()); + Collection<ConfigurationEntry> virtualHosts = children.get(VirtualHost.class.getSimpleName()); + Collection<ConfigurationEntry> ports = children.get(Port.class.getSimpleName()); + assertEquals("Unexpected virtual hosts", + new HashSet<ConfigurationEntry>(Arrays.asList(virtualHostEntry1, virtualHostEntry2)), + new HashSet<ConfigurationEntry>(virtualHosts)); + assertEquals("Unexpected ports", new HashSet<ConfigurationEntry>(Arrays.asList(portEntry)), + new HashSet<ConfigurationEntry>(ports)); + } + + public void testHashCode() + { + ConfigurationEntryStore store = mock(ConfigurationEntryStore.class); + + UUID id = UUID.randomUUID(); + ConfigurationEntry entry1 = new ConfigurationEntry(id, VirtualHost.class.getSimpleName(), + Collections.<String, Object> emptyMap(), Collections.singleton(UUID.randomUUID()), store); + ConfigurationEntry entry2 = new ConfigurationEntry(id, VirtualHost.class.getSimpleName(), + Collections.<String, Object> emptyMap(), Collections.singleton(UUID.randomUUID()), store); + ConfigurationEntry entryWithDifferentId = new ConfigurationEntry(UUID.randomUUID(), + VirtualHost.class.getSimpleName(), Collections.<String, Object> emptyMap(), Collections.singleton(UUID.randomUUID()), store); + + assertTrue(entry1.hashCode() == entry2.hashCode()); + assertFalse(entry1.hashCode() == entryWithDifferentId.hashCode()); + } + + public void testEqualsObject() + { + ConfigurationEntryStore store = mock(ConfigurationEntryStore.class); + + UUID id = UUID.randomUUID(); + Map<String, Object> attributes1 = new HashMap<String, Object>(); + attributes1.put(VirtualHost.NAME, "name1"); + Set<UUID> childrenIds = Collections.singleton(UUID.randomUUID()); + ConfigurationEntry entry1 = new ConfigurationEntry(id, VirtualHost.class.getSimpleName(), attributes1, + childrenIds, store); + + Map<String, Object> attributes2 = new HashMap<String, Object>(); + attributes2.put(VirtualHost.NAME, "name2"); + + ConfigurationEntry entry2 = new ConfigurationEntry(id, VirtualHost.class.getSimpleName(), attributes1, + childrenIds, store); + ConfigurationEntry entryWithDifferentId = new ConfigurationEntry(UUID.randomUUID(), + VirtualHost.class.getSimpleName(), attributes1, childrenIds, store); + + assertTrue(entry1.equals(entry2)); + assertFalse("Entries should be diferrent because of diferrent IDs", entry1.equals(entryWithDifferentId)); + + ConfigurationEntry entryWithDifferentChildId = new ConfigurationEntry(id, + VirtualHost.class.getSimpleName(), attributes1, Collections.singleton(UUID.randomUUID()), store); + assertFalse("Entries should be diferrent because of diferrent children", entry1.equals(entryWithDifferentChildId)); + + ConfigurationEntry entryWithDifferentName = new ConfigurationEntry(id, + VirtualHost.class.getSimpleName(), attributes2, childrenIds, store); + assertFalse("Entries should be diferrent because of diferrent attributes", entry1.equals(entryWithDifferentName)); + + ConfigurationEntry entryWithDifferentType = new ConfigurationEntry(id, + Broker.class.getSimpleName(), attributes1, childrenIds, store); + assertFalse("Entries should be diferrent because of diferrent types", entry1.equals(entryWithDifferentType)); + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/InternalTestProtocolSession.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/InternalTestProtocolSession.java index 0016e31236..3216f8886a 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/InternalTestProtocolSession.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/InternalTestProtocolSession.java @@ -28,9 +28,9 @@ import org.apache.qpid.protocol.AMQConstant; import org.apache.qpid.server.AMQChannel; import org.apache.qpid.server.message.AMQMessage; import org.apache.qpid.server.message.MessageContentSource; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.output.ProtocolOutputConverter; import org.apache.qpid.server.queue.QueueEntry; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.auth.AuthenticatedPrincipal; import org.apache.qpid.server.security.auth.UsernamePrincipal; import org.apache.qpid.server.subscription.ClientDeliveryMethod; @@ -58,9 +58,9 @@ public class InternalTestProtocolSession extends AMQProtocolEngine implements Pr private AtomicInteger _deliveryCount = new AtomicInteger(0); private static final AtomicLong ID_GENERATOR = new AtomicLong(0); - public InternalTestProtocolSession(VirtualHost virtualHost) throws AMQException + public InternalTestProtocolSession(VirtualHost virtualHost, Broker broker) throws AMQException { - super(ApplicationRegistry.getInstance().getVirtualHostRegistry(), new TestNetworkConnection(), ID_GENERATOR.getAndIncrement()); + super(broker, new TestNetworkConnection(), ID_GENERATOR.getAndIncrement()); _channelDelivers = new HashMap<Integer, Map<AMQShortString, LinkedList<DeliveryPair>>>(); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MaxChannelsTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MaxChannelsTest.java index e8ee2c4d0b..99dd42e179 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MaxChannelsTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MaxChannelsTest.java @@ -23,20 +23,24 @@ package org.apache.qpid.server.protocol; import org.apache.qpid.AMQException; import org.apache.qpid.protocol.AMQConstant; import org.apache.qpid.server.AMQChannel; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.util.InternalBrokerBaseCase; -import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.test.utils.QpidTestCase; /** Test class to test MBean operations for AMQMinaProtocolSession. */ -public class MaxChannelsTest extends InternalBrokerBaseCase +public class MaxChannelsTest extends QpidTestCase { - private AMQProtocolEngine _session; + private AMQProtocolEngine _session; - public void testChannels() throws Exception + @Override + public void setUp() throws Exception { - VirtualHost vhost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost("test"); - _session = new InternalTestProtocolSession(vhost); + super.setUp(); + BrokerTestHelper.setUp(); + _session = BrokerTestHelper.createSession(); + } + public void testChannels() throws Exception + { // check the channel count is correct int channelCount = _session.getChannels().size(); assertEquals("Initial channel count wrong", 0, channelCount); @@ -45,13 +49,15 @@ public class MaxChannelsTest extends InternalBrokerBaseCase _session.setMaximumNumberOfChannels(maxChannels); assertEquals("Number of channels not correctly set.", new Long(maxChannels), _session.getMaximumNumberOfChannels()); + for (long currentChannel = 0L; currentChannel < maxChannels; currentChannel++) + { + _session.addChannel(new AMQChannel(_session, (int) currentChannel, null)); + } try { - for (long currentChannel = 0L; currentChannel < maxChannels; currentChannel++) - { - _session.addChannel(new AMQChannel(_session, (int) currentChannel, null)); - } + _session.addChannel(new AMQChannel(_session, (int) maxChannels, null)); + fail("Cannot create more channels then maximum"); } catch (AMQException e) { @@ -63,14 +69,14 @@ public class MaxChannelsTest extends InternalBrokerBaseCase @Override public void tearDown() throws Exception { - try { - _session.closeSession(); - } catch (AMQException e) { - // Yikes - fail(e.getMessage()); - } + try + { + _session.getVirtualHost().close(); + _session.closeSession(); + } finally { + BrokerTestHelper.tearDown(); super.tearDown(); } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactoryTest.java index e951dd494b..02b8c74feb 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactoryTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/protocol/MultiVersionProtocolEngineFactoryTest.java @@ -20,61 +20,52 @@ */ package org.apache.qpid.server.protocol; -import java.util.UUID; -import java.util.concurrent.ScheduledFuture; -import org.apache.commons.configuration.XMLConfiguration; +import static org.mockito.Mockito.when; + +import java.nio.ByteBuffer; +import java.util.EnumSet; +import java.util.Set; import org.apache.qpid.protocol.ServerProtocolEngine; -import org.apache.qpid.server.binding.BindingFactory; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.configuration.VirtualHostConfiguration; -import org.apache.qpid.server.connection.IConnectionRegistry; -import org.apache.qpid.server.exchange.ExchangeFactory; -import org.apache.qpid.server.exchange.ExchangeRegistry; -import org.apache.qpid.server.protocol.v1_0.LinkRegistry; -import org.apache.qpid.server.queue.QueueRegistry; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; -import org.apache.qpid.server.security.*; -import org.apache.qpid.server.stats.StatisticsCounter; -import org.apache.qpid.server.store.MessageStore; -import org.apache.qpid.server.txn.DtxRegistry; -import org.apache.qpid.server.util.TestApplicationRegistry; -import org.apache.qpid.server.virtualhost.HouseKeepingTask; -import org.apache.qpid.server.virtualhost.State; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.server.virtualhost.VirtualHostImpl; import org.apache.qpid.server.virtualhost.VirtualHostRegistry; import org.apache.qpid.test.utils.QpidTestCase; import org.apache.qpid.transport.TestNetworkConnection; -import java.nio.ByteBuffer; -import java.util.EnumSet; -import java.util.Set; - public class MultiVersionProtocolEngineFactoryTest extends QpidTestCase { + private VirtualHost _virtualHost; + private Broker _broker; + @Override protected void setUp() throws Exception { super.setUp(); + BrokerTestHelper.setUp(); + _broker = BrokerTestHelper.createBrokerMock(); + VirtualHostRegistry virtualHostRegistry = _broker.getVirtualHostRegistry(); + when(_broker.getAttribute(Broker.DEFAULT_VIRTUAL_HOST)).thenReturn("default"); - //the factory needs a registry instance - ApplicationRegistry.initialise(new TestApplicationRegistry(new ServerConfiguration(new XMLConfiguration()))); // AMQP 1-0 connection needs default vhost to be present - VirtualHostRegistry virtualHostRegistry = ApplicationRegistry.getInstance().getVirtualHostRegistry(); - VirtualHostImpl vhostImpl = new VirtualHostImpl(ApplicationRegistry.getInstance(), new VirtualHostConfiguration("default",new XMLConfiguration())); - virtualHostRegistry.registerVirtualHost(vhostImpl); - virtualHostRegistry.setDefaultVirtualHostName("default"); - + _virtualHost = BrokerTestHelper.createVirtualHost("default", virtualHostRegistry); } - protected void tearDown() + @Override + protected void tearDown() throws Exception { - //the factory opens a registry instance - ApplicationRegistry.remove(); + try + { + _virtualHost.close(); + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } } - + private static final byte[] AMQP_0_8_HEADER = new byte[] { (byte) 'A', (byte) 'M', @@ -133,6 +124,7 @@ public class MultiVersionProtocolEngineFactoryTest extends QpidTestCase (byte) 0 }; + private byte[] getAmqpHeader(final AmqpProtocolVersion version) { switch(version) @@ -162,7 +154,7 @@ public class MultiVersionProtocolEngineFactoryTest extends QpidTestCase Set<AmqpProtocolVersion> versions = EnumSet.allOf(AmqpProtocolVersion.class); MultiVersionProtocolEngineFactory factory = - new MultiVersionProtocolEngineFactory(versions, null); + new MultiVersionProtocolEngineFactory(_broker, versions, null); //create a dummy to retrieve the 'current' ID number long previousId = factory.newProtocolEngine().getConnectionId(); @@ -200,7 +192,7 @@ public class MultiVersionProtocolEngineFactoryTest extends QpidTestCase try { - new MultiVersionProtocolEngineFactory(versions, AmqpProtocolVersion.v0_9); + new MultiVersionProtocolEngineFactory(_broker, versions, AmqpProtocolVersion.v0_9); fail("should not have been allowed to create the factory"); } catch(IllegalArgumentException iae) diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQPriorityQueueTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQPriorityQueueTest.java index c3d58f3bdc..81ad57c040 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQPriorityQueueTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQPriorityQueueTest.java @@ -36,8 +36,9 @@ public class AMQPriorityQueueTest extends SimpleAMQQueueTest @Override public void setUp() throws Exception { - _arguments = new FieldTable(); - _arguments.put(new AMQShortString(AMQQueueFactory.X_QPID_PRIORITIES), 3); + FieldTable arguments = new FieldTable(); + arguments.put(new AMQShortString(AMQQueueFactory.X_QPID_PRIORITIES), 3); + setArguments(arguments); super.setUp(); } @@ -45,25 +46,26 @@ public class AMQPriorityQueueTest extends SimpleAMQQueueTest { // Enqueue messages in order - _queue.enqueue(createMessage(1L, (byte) 10)); - _queue.enqueue(createMessage(2L, (byte) 4)); - _queue.enqueue(createMessage(3L, (byte) 0)); + SimpleAMQQueue queue = getQueue(); + queue.enqueue(createMessage(1L, (byte) 10)); + queue.enqueue(createMessage(2L, (byte) 4)); + queue.enqueue(createMessage(3L, (byte) 0)); // Enqueue messages in reverse order - _queue.enqueue(createMessage(4L, (byte) 0)); - _queue.enqueue(createMessage(5L, (byte) 4)); - _queue.enqueue(createMessage(6L, (byte) 10)); + queue.enqueue(createMessage(4L, (byte) 0)); + queue.enqueue(createMessage(5L, (byte) 4)); + queue.enqueue(createMessage(6L, (byte) 10)); // Enqueue messages out of order - _queue.enqueue(createMessage(7L, (byte) 4)); - _queue.enqueue(createMessage(8L, (byte) 10)); - _queue.enqueue(createMessage(9L, (byte) 0)); + queue.enqueue(createMessage(7L, (byte) 4)); + queue.enqueue(createMessage(8L, (byte) 10)); + queue.enqueue(createMessage(9L, (byte) 0)); // Register subscriber - _queue.registerSubscription(_subscription, false); + queue.registerSubscription(getSubscription(), false); Thread.sleep(150); - ArrayList<QueueEntry> msgs = _subscription.getMessages(); + ArrayList<QueueEntry> msgs = getSubscription().getMessages(); try { assertEquals(1L, msgs.get(0).getMessage().getMessageNumber()); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQQueueFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQQueueFactoryTest.java index 186be4dff7..0f82345271 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQQueueFactoryTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AMQQueueFactoryTest.java @@ -20,23 +20,23 @@ */ package org.apache.qpid.server.queue; +import static org.mockito.Mockito.when; + import org.apache.commons.configuration.XMLConfiguration; import org.apache.qpid.AMQException; import org.apache.qpid.exchange.ExchangeDefaults; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; -import org.apache.qpid.server.configuration.ServerConfiguration; +import org.apache.qpid.server.configuration.BrokerProperties; +import org.apache.qpid.server.configuration.VirtualHostConfiguration; import org.apache.qpid.server.exchange.DefaultExchangeFactory; import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.exchange.ExchangeRegistry; -import org.apache.qpid.server.logging.SystemOutMessageLogger; -import org.apache.qpid.server.logging.actors.CurrentActor; -import org.apache.qpid.server.logging.actors.TestLogActor; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.TestableMemoryMessageStore; -import org.apache.qpid.server.util.TestApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; import org.apache.qpid.test.utils.QpidTestCase; @@ -50,19 +50,19 @@ public class AMQQueueFactoryTest extends QpidTestCase { super.setUp(); - CurrentActor.set(new TestLogActor(new SystemOutMessageLogger())); - + BrokerTestHelper.setUp(); XMLConfiguration configXml = new XMLConfiguration(); - configXml.addProperty("virtualhosts.virtualhost(-1).name", getName()); - configXml.addProperty("virtualhosts.virtualhost(-1)."+getName()+".store.class", TestableMemoryMessageStore.class.getName()); + configXml.addProperty("store.class", TestableMemoryMessageStore.class.getName()); - ServerConfiguration configuration = new ServerConfiguration(configXml); - ApplicationRegistry registry = new TestApplicationRegistry(configuration); - ApplicationRegistry.initialise(registry); - registry.getVirtualHostRegistry().setDefaultVirtualHostName(getName()); + Broker broker = BrokerTestHelper.createBrokerMock(); + if (getName().equals("testDeadLetterQueueDoesNotInheritDLQorMDCSettings")) + { + when(broker.getAttribute(Broker.MAXIMUM_DELIVERY_ATTEMPTS)).thenReturn(5); + when(broker.getAttribute(Broker.DEAD_LETTER_QUEUE_ENABLED)).thenReturn(true); + } - _virtualHost = registry.getVirtualHostRegistry().getVirtualHost(getName()); + _virtualHost = BrokerTestHelper.createVirtualHost(new VirtualHostConfiguration(getName(), configXml, broker)); _queueRegistry = _virtualHost.getQueueRegistry(); @@ -73,11 +73,12 @@ public class AMQQueueFactoryTest extends QpidTestCase { try { - super.tearDown(); + _virtualHost.close(); } finally { - ApplicationRegistry.remove(); + BrokerTestHelper.tearDown(); + super.tearDown(); } } @@ -172,11 +173,8 @@ public class AMQQueueFactoryTest extends QpidTestCase * are not applied to the DLQ itself. * @throws AMQException */ - public void testDeadLetterQueueDoesNotInheritDLQorMDCSettings() throws AMQException + public void testDeadLetterQueueDoesNotInheritDLQorMDCSettings() throws Exception { - ApplicationRegistry.getInstance().getConfiguration().getConfig().addProperty("deadLetterQueues","true"); - ApplicationRegistry.getInstance().getConfiguration().getConfig().addProperty("maximumDeliveryCount","5"); - String queueName = "testDeadLetterQueueEnabled"; AMQShortString dlExchangeName = new AMQShortString(queueName + DefaultExchangeFactory.DEFAULT_DLE_NAME_SUFFIX); AMQShortString dlQueueName = new AMQShortString(queueName + AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX); @@ -336,11 +334,8 @@ public class AMQQueueFactoryTest extends QpidTestCase try { // change DLQ name to make its length bigger than exchange name - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterExchangeSuffix", "_DLE"); - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterQueueSuffix", "_DLQUEUE"); - + setTestSystemProperty(BrokerProperties.PROPERTY_DEAD_LETTER_EXCHANGE_SUFFIX, "_DLE"); + setTestSystemProperty(BrokerProperties.PROPERTY_DEAD_LETTER_QUEUE_SUFFIX, "_DLQUEUE"); FieldTable fieldTable = new FieldTable(); fieldTable.setBoolean(AMQQueueFactory.X_QPID_DLQ_ENABLED, true); AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), queueName, false, "owner", @@ -353,13 +348,6 @@ public class AMQQueueFactoryTest extends QpidTestCase assertTrue("Unexpected exception message!", e.getMessage().contains("DLQ queue name") && e.getMessage().contains("length exceeds limit of 255")); } - finally - { - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterExchangeSuffix", DefaultExchangeFactory.DEFAULT_DLE_NAME_SUFFIX); - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterQueueSuffix", AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX); - } } /** @@ -372,11 +360,8 @@ public class AMQQueueFactoryTest extends QpidTestCase try { // change DLQ name to make its length bigger than exchange name - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterExchangeSuffix", "_DLEXCHANGE"); - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterQueueSuffix", "_DLQ"); - + setTestSystemProperty(BrokerProperties.PROPERTY_DEAD_LETTER_EXCHANGE_SUFFIX, "_DLEXCHANGE"); + setTestSystemProperty(BrokerProperties.PROPERTY_DEAD_LETTER_QUEUE_SUFFIX, "_DLQ"); FieldTable fieldTable = new FieldTable(); fieldTable.setBoolean(AMQQueueFactory.X_QPID_DLQ_ENABLED, true); AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), queueName, false, "owner", @@ -389,13 +374,6 @@ public class AMQQueueFactoryTest extends QpidTestCase assertTrue("Unexpected exception message!", e.getMessage().contains("DL exchange name") && e.getMessage().contains("length exceeds limit of 255")); } - finally - { - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterExchangeSuffix", DefaultExchangeFactory.DEFAULT_DLE_NAME_SUFFIX); - ApplicationRegistry.getInstance().getConfiguration().getConfig() - .addProperty("deadLetterQueueSuffix", AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX); - } } private String generateStringWithLength(char ch, int length) diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AckTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AckTest.java index 190d5c777b..cbbf183232 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AckTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/AckTest.java @@ -32,18 +32,16 @@ import org.apache.qpid.server.flow.LimitlessCreditManager; import org.apache.qpid.server.flow.Pre0_10CreditManager; import org.apache.qpid.server.message.AMQMessage; import org.apache.qpid.server.message.MessageMetaData; -import org.apache.qpid.server.model.UUIDGenerator; import org.apache.qpid.server.protocol.AMQProtocolSession; -import org.apache.qpid.server.protocol.InternalTestProtocolSession; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.StoredMessage; import org.apache.qpid.server.store.TestableMemoryMessageStore; import org.apache.qpid.server.subscription.Subscription; import org.apache.qpid.server.subscription.SubscriptionFactoryImpl; import org.apache.qpid.server.txn.AutoCommitTransaction; import org.apache.qpid.server.txn.ServerTransaction; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.ArrayList; import java.util.Set; @@ -51,7 +49,7 @@ import java.util.Set; /** * Tests that acknowledgements are handled correctly. */ -public class AckTest extends InternalBrokerBaseCase +public class AckTest extends QpidTestCase { private Subscription _subscription; @@ -70,15 +68,19 @@ public class AckTest extends InternalBrokerBaseCase public void setUp() throws Exception { super.setUp(); - _virtualHost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost("test"); - _messageStore = new TestableMemoryMessageStore(); - _protocolSession = new InternalTestProtocolSession(_virtualHost); - _channel = new AMQChannel(_protocolSession,5, _messageStore /*dont need exchange registry*/); - - _protocolSession.addChannel(_channel); + BrokerTestHelper.setUp(); + _channel = BrokerTestHelper.createChannel(5); + _protocolSession = _channel.getProtocolSession(); + _virtualHost = _protocolSession.getVirtualHost(); + _queue = BrokerTestHelper.createQueue(getTestName(), _virtualHost); + _messageStore = (TestableMemoryMessageStore)_virtualHost.getMessageStore(); + } - _queue = AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), "myQ", false, "guest", true, false, - _virtualHost, null); + @Override + protected void tearDown() throws Exception + { + BrokerTestHelper.tearDown(); + super.tearDown(); } private void publishMessages(int count) throws AMQException diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueTest.java index ea07c14f8a..ece42f7de3 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueTest.java @@ -28,8 +28,6 @@ import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Matchers.contains; import static org.mockito.Matchers.eq; -import org.apache.commons.configuration.PropertiesConfiguration; - import org.apache.qpid.AMQException; import org.apache.qpid.AMQInternalException; import org.apache.qpid.AMQSecurityException; @@ -39,7 +37,6 @@ import org.apache.qpid.framing.BasicContentHeaderProperties; import org.apache.qpid.framing.ContentHeaderBody; import org.apache.qpid.framing.FieldTable; import org.apache.qpid.framing.abstraction.MessagePublishInfo; -import org.apache.qpid.server.configuration.VirtualHostConfiguration; import org.apache.qpid.server.exchange.DirectExchange; import org.apache.qpid.server.message.AMQMessage; import org.apache.qpid.server.message.MessageMetaData; @@ -47,16 +44,15 @@ import org.apache.qpid.server.message.ServerMessage; import org.apache.qpid.server.model.UUIDGenerator; import org.apache.qpid.server.queue.BaseQueue.PostEnqueueAction; import org.apache.qpid.server.queue.SimpleAMQQueue.QueueEntryFilter; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.store.StoredMessage; import org.apache.qpid.server.store.TestableMemoryMessageStore; import org.apache.qpid.server.subscription.MockSubscription; import org.apache.qpid.server.subscription.Subscription; import org.apache.qpid.server.txn.AutoCommitTransaction; import org.apache.qpid.server.txn.ServerTransaction; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.server.virtualhost.VirtualHostImpl; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.ArrayList; import java.util.Collections; @@ -64,17 +60,17 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -public class SimpleAMQQueueTest extends InternalBrokerBaseCase +public class SimpleAMQQueueTest extends QpidTestCase { - protected SimpleAMQQueue _queue; - protected VirtualHost _virtualHost; - protected AMQShortString _qname = new AMQShortString("qname"); - protected AMQShortString _owner = new AMQShortString("owner"); - protected AMQShortString _routingKey = new AMQShortString("routing key"); - protected DirectExchange _exchange; - protected MockSubscription _subscription = new MockSubscription(); - protected FieldTable _arguments = null; + private SimpleAMQQueue _queue; + private VirtualHost _virtualHost; + private AMQShortString _qname = new AMQShortString("qname"); + private AMQShortString _owner = new AMQShortString("owner"); + private AMQShortString _routingKey = new AMQShortString("routing key"); + private DirectExchange _exchange; + private MockSubscription _subscription = new MockSubscription(); + private FieldTable _arguments = null; private MessagePublishInfo info = new MessagePublishInfo() { @@ -108,25 +104,29 @@ public class SimpleAMQQueueTest extends InternalBrokerBaseCase public void setUp() throws Exception { super.setUp(); - //Create Application Registry for test - ApplicationRegistry applicationRegistry = (ApplicationRegistry)ApplicationRegistry.getInstance(); + BrokerTestHelper.setUp(); - PropertiesConfiguration env = new PropertiesConfiguration(); - final VirtualHostConfiguration vhostConfig = new VirtualHostConfiguration(getClass().getName(), env); - vhostConfig.setMessageStoreClass(TestableMemoryMessageStore.class.getName()); - _virtualHost = new VirtualHostImpl(ApplicationRegistry.getInstance(), vhostConfig); - applicationRegistry.getVirtualHostRegistry().registerVirtualHost(_virtualHost); + _virtualHost = BrokerTestHelper.createVirtualHost(getClass().getName()); - _queue = (SimpleAMQQueue) AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), _qname.asString(), false, _owner.asString(), false, false, _virtualHost, FieldTable.convertToMap(_arguments)); + _queue = (SimpleAMQQueue) AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), _qname.asString(), false, _owner.asString(), + false, false, _virtualHost, FieldTable.convertToMap(_arguments)); - _exchange = (DirectExchange)_virtualHost.getExchangeRegistry().getExchange(ExchangeDefaults.DIRECT_EXCHANGE_NAME); + _exchange = (DirectExchange) _virtualHost.getExchangeRegistry().getExchange(ExchangeDefaults.DIRECT_EXCHANGE_NAME); } @Override public void tearDown() throws Exception { - _queue.stop(); - super.tearDown(); + try + { + _queue.stop(); + _virtualHost.close(); + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } } public void testCreateQueue() throws AMQException @@ -1269,6 +1269,26 @@ public class SimpleAMQQueueTest extends InternalBrokerBaseCase } } + public SimpleAMQQueue getQueue() + { + return _queue; + } + + public MockSubscription getSubscription() + { + return _subscription; + } + + public FieldTable getArguments() + { + return _arguments; + } + + public void setArguments(FieldTable arguments) + { + _arguments = arguments; + } + public class TestMessage extends AMQMessage { private final long _tag; diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueThreadPoolTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueThreadPoolTest.java index 6b82cd361a..4abb7233dc 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueThreadPoolTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/SimpleAMQQueueThreadPoolTest.java @@ -20,20 +20,33 @@ */ package org.apache.qpid.server.queue; -import org.apache.qpid.AMQException; import org.apache.qpid.pool.ReferenceCountingExecutorService; import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; -public class SimpleAMQQueueThreadPoolTest extends InternalBrokerBaseCase +public class SimpleAMQQueueThreadPoolTest extends QpidTestCase { - public void test() throws AMQException + @Override + public void setUp() throws Exception + { + super.setUp(); + BrokerTestHelper.setUp(); + } + + @Override + public void tearDown() throws Exception + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + + public void test() throws Exception { int initialCount = ReferenceCountingExecutorService.getInstance().getReferenceCount(); - VirtualHost test = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost("test"); + VirtualHost test = BrokerTestHelper.createVirtualHost("test"); try { @@ -50,7 +63,7 @@ public class SimpleAMQQueueThreadPoolTest extends InternalBrokerBaseCase } finally { - ApplicationRegistry.remove(); + test.close(); } } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java deleted file mode 100644 index 9af950d385..0000000000 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/registry/ApplicationRegistryShutdownTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * - * 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.registry; - -import org.apache.qpid.server.util.InternalBrokerBaseCase; - -import java.security.Provider; -import java.security.Security; -import java.util.LinkedList; -import java.util.List; - -/** - * QPID-1390 : Test to validate that the AuthenticationManger can successfully unregister any new SASL providers when - * The ApplicationRegistry is closed. - * - * This should be expanded as QPID-1399 is implemented. - */ -public class ApplicationRegistryShutdownTest extends InternalBrokerBaseCase -{ - - private Provider[] _defaultProviders; - @Override - public void setUp() throws Exception - { - // Get default providers - _defaultProviders = Security.getProviders(); - - //Startup the new broker and register the new providers - super.setUp(); - } - - - /** - * QPID-1399 : Ensure that the Authentication manager unregisters any SASL providers created during - * ApplicationRegistry initialisation. - * - */ - public void testAuthenticationMangerCleansUp() throws Exception - { - - // Get the providers after initialisation - Provider[] providersAfterInitialisation = Security.getProviders(); - - // Find the additions - List additions = new LinkedList(); - for (Provider afterInit : providersAfterInitialisation) - { - boolean found = false; - for (Provider defaultProvider : _defaultProviders) - { - if (defaultProvider == afterInit) - { - found=true; - break; - } - } - - // Record added registies - if (!found) - { - additions.add(afterInit); - } - } - - assertFalse("No new SASL mechanisms added by initialisation.", additions.isEmpty()); - - //Close the registry which will perform the close the AuthenticationManager - stopBroker(); - - //Validate that the SASL plugFins have been removed. - Provider[] providersAfterClose = Security.getProviders(); - - assertTrue("No providers unregistered", providersAfterInitialisation.length > providersAfterClose.length); - - //Ensure that the additions are not still present after close(). - for (Provider afterClose : providersAfterClose) - { - assertFalse("Added provider not unregistered", additions.contains(afterClose)); - } - } - - - - - -} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java deleted file mode 100644 index aeab1e4343..0000000000 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/AuthenticationManagerRegistryTest.java +++ /dev/null @@ -1,355 +0,0 @@ -/* - * 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.security.auth.manager; - -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.security.Principal; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import javax.security.sasl.SaslException; -import javax.security.sasl.SaslServer; - -import junit.framework.TestCase; - -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.plugin.AuthenticationManagerFactory; -import org.apache.qpid.server.plugin.QpidServiceLoader; -import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.auth.AuthenticationResult; -import org.apache.qpid.server.security.group.GroupPrincipalAccessor; - -public class AuthenticationManagerRegistryTest extends TestCase -{ - @SuppressWarnings("unchecked") - private QpidServiceLoader<AuthenticationManagerFactory> _authManagerServiceLoader = mock(QpidServiceLoader.class); - - private ServerConfiguration _serverConfiguration = mock(ServerConfiguration.class); - private Configuration _serverCommonsConfig = mock(Configuration.class); - private Configuration _securityCommonsConfig = mock(Configuration.class); - - private GroupPrincipalAccessor _groupPrincipalAccessor = mock(GroupPrincipalAccessor.class); - - private TestAuthenticationManager1 _testAuthManager1 = new TestAuthenticationManager1(); - private TestAuthenticationManager2 _testAuthManager2 = new TestAuthenticationManager2(); - - private AuthenticationManagerFactory _testAuthManager1Factory = newMockFactoryProducing(_testAuthManager1); - private AuthenticationManagerFactory _testAuthManager2Factory = newMockFactoryProducing(_testAuthManager2); - - @Override - protected void setUp() throws Exception - { - super.setUp(); - when(_serverConfiguration.getConfig()).thenReturn(_serverCommonsConfig); - when(_serverCommonsConfig.subset("security")).thenReturn(_securityCommonsConfig); - } - - public void testNoAuthenticationManagerFactoryPluginsFound() throws Exception - { - String noServicesMessage = "mock exception - no services found"; - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenThrow(new RuntimeException(noServicesMessage)); - try - { - new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - fail("Exception not thrown"); - } - catch (RuntimeException ce) - { - // PASS - assertEquals(noServicesMessage, ce.getMessage()); - } - } - - public void testSameAuthenticationManagerSpecifiedTwice() throws Exception - { - List<AuthenticationManagerFactory> duplicateFactories = Arrays.asList(_testAuthManager1Factory, _testAuthManager1Factory); - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(duplicateFactories); - - try - { - new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - fail("Exception not thrown"); - } - catch (RuntimeException e) - { - assertTrue(e.getMessage().contains("Cannot configure more than one authentication manager with name")); - assertTrue(e.getMessage().contains(TestAuthenticationManager1.class.getSimpleName())); - } - } - - public void testMultipleAuthenticationManagersSpecifiedButNoDefaultSpecified() throws Exception - { - List<AuthenticationManagerFactory> factoryList = Arrays.asList(_testAuthManager1Factory, _testAuthManager2Factory); - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(null); - - try - { - new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - // PASS - assertEquals("If more than one authentication manager is configured a default MUST be specified.", - ce.getMessage()); - } - } - - public void testDefaultAuthenticationManagerNotKnown() throws Exception - { - String myDefaultAuthManagerSimpleClassName = "UnknownAuthenticationManager"; - - List<AuthenticationManagerFactory> factoryList = Arrays.asList(_testAuthManager1Factory, _testAuthManager2Factory); - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(myDefaultAuthManagerSimpleClassName); - - try - { - new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - // PASS - assertTrue("Unexpected message " + ce.getMessage(), - ce.getMessage().startsWith("No authentication managers configured of type " + myDefaultAuthManagerSimpleClassName + " which is specified as the default")); - } - } - - public void testPortMappedToUnknownAuthenticationManager() throws Exception - { - String myDefaultAuthManagerSimpleClassName = "UnknownAuthenticationManager"; - int portNumber = 1234; - - List<AuthenticationManagerFactory> factoryList = Arrays.asList(_testAuthManager1Factory); - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - when(_serverConfiguration.getPortAuthenticationMappings()).thenReturn(Collections.singletonMap(portNumber, myDefaultAuthManagerSimpleClassName)); - - try - { - new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - fail("Exception not thrown"); - } - catch (ConfigurationException ce) - { - // PASS - assertEquals("Unknown authentication manager class " + myDefaultAuthManagerSimpleClassName + " configured for port " + portNumber, ce.getMessage()); - } - } - - public void testGetAuthenticationManagerForInetSocketAddress() throws Exception - { - List<AuthenticationManagerFactory> factoryList = Arrays.asList(_testAuthManager1Factory); - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - - AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - - SubjectCreator subjectCreator = registry.getSubjectCreator(new InetSocketAddress(1234)); - assertSubjectCreatorUsingExpectedAuthManager(_testAuthManager1, subjectCreator); - } - - public void testGetAuthenticationManagerForNonInetSocketAddress() throws Exception - { - List<AuthenticationManagerFactory> factoryList = Arrays.asList(_testAuthManager1Factory); - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - - AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - - SubjectCreator subjectCreator = registry.getSubjectCreator(mock(SocketAddress.class)); - assertSubjectCreatorUsingExpectedAuthManager(_testAuthManager1, subjectCreator); - } - - public void testGetAuthenticationManagerWithMultipleAuthenticationManager() throws Exception - { - List<AuthenticationManagerFactory> factoryList = Arrays.asList(_testAuthManager1Factory, _testAuthManager2Factory); - - AuthenticationManager defaultAuthManger = _testAuthManager1; - AuthenticationManager unmappedAuthManager = _testAuthManager2; - - String defaultAuthMangerName = defaultAuthManger.getClass().getSimpleName(); - String mappedAuthManagerName = unmappedAuthManager.getClass().getSimpleName(); - - int unmappedPortNumber = 1234; - int mappedPortNumber = 1235; - - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(defaultAuthMangerName); - when(_serverConfiguration.getPortAuthenticationMappings()).thenReturn(Collections.singletonMap(mappedPortNumber, mappedAuthManagerName)); - - AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - - SubjectCreator subjectCreatorForDefaultAuthManager = registry.getSubjectCreator(new InetSocketAddress(unmappedPortNumber)); - assertSubjectCreatorUsingExpectedAuthManager(defaultAuthManger, subjectCreatorForDefaultAuthManager); - - SubjectCreator subjectCreatorForUnmappedAuthManager = registry.getSubjectCreator(new InetSocketAddress(mappedPortNumber)); - assertSubjectCreatorUsingExpectedAuthManager(unmappedAuthManager, subjectCreatorForUnmappedAuthManager); - } - - public void testAuthenticationManagersAreClosed() throws Exception - { - AuthenticationManager defaultAuthManager = mock(MockAuthenticationManager1.class); - AuthenticationManagerFactory defaultAuthManagerFactory = newMockFactoryProducing(defaultAuthManager); - - AuthenticationManager authManager2 = mock(MockAuthenticationManager2.class); - AuthenticationManagerFactory authManagerFactory2 = newMockFactoryProducing(authManager2); - - List<AuthenticationManagerFactory> factoryList = Arrays.asList(defaultAuthManagerFactory, authManagerFactory2); - - String defaultAuthMangerName = defaultAuthManager.getClass().getSimpleName(); - when(_serverConfiguration.getDefaultAuthenticationManager()).thenReturn(defaultAuthMangerName); - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - - AuthenticationManagerRegistry registry = new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - - registry.close(); - - verify(defaultAuthManager).close(); - verify(authManager2).close(); - } - - public void testAlreadyInitialisedAuthManagersAreClosedWhenAnotherAuthManagerInitFails() throws Exception - { - AuthenticationManager authManger1 = mock(MockAuthenticationManager1.class); - AuthenticationManagerFactory authManager1Factory = newMockFactoryProducing(authManger1); - - AuthenticationManager authManager2 = mock(MockAuthenticationManager2.class); - AuthenticationManagerFactory authManagerFactory2 = newMockFactoryProducing(authManager2); - - List<AuthenticationManagerFactory> factoryList = Arrays.asList(authManager1Factory, authManagerFactory2); - when(_authManagerServiceLoader.atLeastOneInstanceOf(AuthenticationManagerFactory.class)).thenReturn(factoryList); - - doThrow(new RuntimeException("mock auth manager 2 init exception")).when(authManager2).initialise(); - - try - { - new AuthenticationManagerRegistry(_serverConfiguration, _groupPrincipalAccessor, _authManagerServiceLoader); - fail("Exception not thrown"); - } - catch (RuntimeException e) - { - // PASS - } - - verify(authManger1).close(); - } - - - private AuthenticationManagerFactory newMockFactoryProducing(AuthenticationManager myAuthManager) - { - AuthenticationManagerFactory myAuthManagerFactory = mock(AuthenticationManagerFactory.class); - when(myAuthManagerFactory.createInstance(_securityCommonsConfig)).thenReturn(myAuthManager); - return myAuthManagerFactory; - } - - private void assertSubjectCreatorUsingExpectedAuthManager(AuthenticationManager expectedAuthenticationManager, SubjectCreator subjectCreator) - { - assertEquals( - "The subject creator should be using " + expectedAuthenticationManager + " so its mechanisms should match", - expectedAuthenticationManager.getMechanisms(), - subjectCreator.getMechanisms()); - } - - /** @see MockAuthenticationManager2 */ - private interface MockAuthenticationManager1 extends AuthenticationManager - { - } - - /** - * I only exist so that mock implementations of me have a different class to {@link MockAuthenticationManager1}, - * as mandated by {@link AuthenticationManagerRegistry} - */ - private interface MockAuthenticationManager2 extends AuthenticationManager - { - } - - /** - * We use a stub rather than a mock because {@link AuthenticationManagerRegistry} relies on {@link AuthenticationManager} class names, - * which are hard to predict for mocks. - */ - private abstract class TestAuthenticationManager implements AuthenticationManager - { - @Override - public void close() - { - // no-op - } - - @Override - public void initialise() - { - // no-op - } - - @Override - public SaslServer createSaslServer(String mechanism, String localFQDN, Principal externalPrincipal) throws SaslException - { - throw new UnsupportedOperationException(); - } - - @Override - public AuthenticationResult authenticate(SaslServer server, byte[] response) - { - throw new UnsupportedOperationException(); - } - - @Override - public AuthenticationResult authenticate(String username, String password) - { - throw new UnsupportedOperationException(); - } - } - - private class TestAuthenticationManager1 extends TestAuthenticationManager - { - @Override - public String getMechanisms() - { - return "MECHANISMS1"; - } - } - - private class TestAuthenticationManager2 extends TestAuthenticationManager - { - /** - * Needs to different from {@link TestAuthenticationManager1#getMechanisms()} to aid our test assertions. - */ - @Override - public String getMechanisms() - { - return "MECHANISMS2"; - } - } -} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java new file mode 100644 index 0000000000..04e09e073f --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/Base64MD5PasswordFileAuthenticationManagerFactoryTest.java @@ -0,0 +1,111 @@ +/* + * 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.security.auth.manager; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.security.auth.database.Base64MD5PasswordFilePrincipalDatabase; + +public class Base64MD5PasswordFileAuthenticationManagerFactoryTest extends TestCase +{ + AuthenticationManagerFactory _factory = new Base64MD5PasswordFileAuthenticationManagerFactory(); + private Map<String, Object> _configuration = new HashMap<String, Object>(); + private File _emptyPasswordFile; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + _emptyPasswordFile = File.createTempFile(getName(), "passwd"); + _emptyPasswordFile.deleteOnExit(); + } + + public void testBase64MD5InstanceCreated() throws Exception + { + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, Base64MD5PasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath()); + + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNotNull(manager); + assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager); + assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof Base64MD5PasswordFilePrincipalDatabase); + } + + public void testPasswordFileNotFound() throws Exception + { + //delete the file + _emptyPasswordFile.delete(); + + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, Base64MD5PasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath()); + + try + { + _factory.createInstance(_configuration); + } + catch (RuntimeException re) + { + assertTrue(re.getCause() instanceof FileNotFoundException); + } + } + + public void testReturnsNullWhenNoConfig() throws Exception + { + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNull(manager); + } + + public void testReturnsNullWhenConfigForOtherAuthManagerType() throws Exception + { + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, "other-auth-manager"); + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNull(manager); + } + + public void testReturnsNullWhenConfigForPlainPDImplementationNoPasswordFileValueSpecified() throws Exception + { + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, Base64MD5PasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNull(manager); + } + + @Override + protected void tearDown() throws Exception + { + try + { + if (_emptyPasswordFile == null && _emptyPasswordFile.exists()) + { + _emptyPasswordFile.delete(); + } + } + finally + { + super.tearDown(); + } + } +}
\ No newline at end of file diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java new file mode 100644 index 0000000000..d428f8b211 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java @@ -0,0 +1,111 @@ +/* + * 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.security.auth.manager; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.qpid.server.plugin.AuthenticationManagerFactory; +import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase; + +public class PlainPasswordFileAuthenticationManagerFactoryTest extends TestCase +{ + AuthenticationManagerFactory _factory = new PlainPasswordFileAuthenticationManagerFactory(); + private Map<String, Object> _configuration = new HashMap<String, Object>(); + private File _emptyPasswordFile; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + _emptyPasswordFile = File.createTempFile(getName(), "passwd"); + _emptyPasswordFile.deleteOnExit(); + } + + public void testPlainInstanceCreated() throws Exception + { + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath()); + + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNotNull(manager); + assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager); + assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof PlainPasswordFilePrincipalDatabase); + } + + public void testPasswordFileNotFound() throws Exception + { + //delete the file + _emptyPasswordFile.delete(); + + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath()); + + try + { + _factory.createInstance(_configuration); + } + catch (RuntimeException re) + { + assertTrue(re.getCause() instanceof FileNotFoundException); + } + } + + public void testReturnsNullWhenNoConfig() throws Exception + { + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNull(manager); + } + + public void testReturnsNullWhenConfigForOtherAuthManagerType() throws Exception + { + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, "other-auth-manager"); + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNull(manager); + } + + public void testReturnsNullWhenConfigForPlainPDImplementationNoPasswordFileValueSpecified() throws Exception + { + _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + + AuthenticationManager manager = _factory.createInstance(_configuration); + assertNull(manager); + } + + @Override + protected void tearDown() throws Exception + { + try + { + if (_emptyPasswordFile == null && _emptyPasswordFile.exists()) + { + _emptyPasswordFile.delete(); + } + } + finally + { + super.tearDown(); + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthManagerFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthManagerFactoryTest.java deleted file mode 100644 index d76ea2674c..0000000000 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthManagerFactoryTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.security.auth.manager; - -import java.io.File; -import java.io.FileNotFoundException; - -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.XMLConfiguration; -import org.apache.qpid.server.security.auth.database.Base64MD5PasswordFilePrincipalDatabase; -import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase; - -import junit.framework.TestCase; - -public class PrincipalDatabaseAuthManagerFactoryTest extends TestCase -{ - PrincipalDatabaseAuthManagerFactory _factory = new PrincipalDatabaseAuthManagerFactory(); - private Configuration _configuration = new XMLConfiguration(); - private File _emptyPasswordFile; - - @Override - protected void setUp() throws Exception - { - super.setUp(); - _emptyPasswordFile = File.createTempFile(getName(), "passwd"); - _emptyPasswordFile.deleteOnExit(); - } - - public void testPlainInstanceCreated() throws Exception - { - _configuration.setProperty("pd-auth-manager.principal-database.class", PlainPasswordFilePrincipalDatabase.class.getName()); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.name", "passwordFile"); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.value", _emptyPasswordFile.getAbsolutePath()); - - AuthenticationManager manager = _factory.createInstance(_configuration); - assertNotNull(manager); - assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager); - assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof PlainPasswordFilePrincipalDatabase); - } - - public void testBase64MD5nstanceCreated() throws Exception - { - _configuration.setProperty("pd-auth-manager.principal-database.class", Base64MD5PasswordFilePrincipalDatabase.class.getName()); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.name", "passwordFile"); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.value", _emptyPasswordFile.getAbsolutePath()); - - AuthenticationManager manager = _factory.createInstance(_configuration); - assertNotNull(manager); - assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager); - assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof Base64MD5PasswordFilePrincipalDatabase); - } - - public void testPasswordFileNotFound() throws Exception - { - _emptyPasswordFile.delete(); - - _configuration.setProperty("pd-auth-manager.principal-database.class", PlainPasswordFilePrincipalDatabase.class.getName()); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.name", "passwordFile"); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.value", _emptyPasswordFile.getAbsolutePath()); - - try - { - _factory.createInstance(_configuration); - } - catch (RuntimeException re) - { - assertTrue(re.getCause() instanceof FileNotFoundException); - } - } - - public void testReturnsNullWhenNoConfig() throws Exception - { - AuthenticationManager manager = _factory.createInstance(_configuration); - assertNull(manager); - } - - public void testReturnsNullWhenConfigForOtherPDImplementation() throws Exception - { - _configuration.setProperty("pd-auth-manager.principal-database.class", "mypdimpl"); - - AuthenticationManager manager = _factory.createInstance(_configuration); - assertNull(manager); - } - - public void testReturnsNullWhenConfigForPlainPDImplementationNoPasswordFileValueSpecified() throws Exception - { - _configuration.setProperty("pd-auth-manager.principal-database.class", PlainPasswordFilePrincipalDatabase.class.getName()); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.name", "passwordFile"); - // no pd-auth-manager.attributes.attribute.value - - AuthenticationManager manager = _factory.createInstance(_configuration); - assertNull(manager); - } - - public void testReturnsNullWhenConfigForPlainPDImplementationWrongArgumentName() throws Exception - { - _configuration.setProperty("pd-auth-manager.principal-database.class", PlainPasswordFilePrincipalDatabase.class.getName()); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.name", "wrong"); - _configuration.setProperty("pd-auth-manager.principal-database.attributes.attribute.value", "/does/not/matter"); - - AuthenticationManager manager = _factory.createInstance(_configuration); - assertNull(manager); - } - - - - @Override - protected void tearDown() throws Exception - { - try - { - if (_emptyPasswordFile == null && _emptyPasswordFile.exists()) - { - _emptyPasswordFile.delete(); - } - } - finally - { - super.tearDown(); - } - } -} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java index aab5f80836..1424bee611 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerFactoryTest.java @@ -19,20 +19,22 @@ */ package org.apache.qpid.server.security.auth.manager; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.XMLConfiguration; +import java.util.HashMap; +import java.util.Map; + import junit.framework.TestCase; public class SimpleLDAPAuthenticationManagerFactoryTest extends TestCase { private SimpleLDAPAuthenticationManagerFactory _factory = new SimpleLDAPAuthenticationManagerFactory(); - private Configuration _configuration = new XMLConfiguration(); + private Map<String, Object> _configuration = new HashMap<String, Object>(); public void testInstanceCreated() throws Exception { - _configuration.setProperty("simple-ldap-auth-manager.provider-url", "ldaps://example.com:636/"); - _configuration.setProperty("simple-ldap-auth-manager.search-context", "dc=example"); + _configuration.put(SimpleLDAPAuthenticationManagerFactory.ATTRIBUTE_TYPE, SimpleLDAPAuthenticationManagerFactory.PROVIDER_TYPE); + _configuration.put("providerUrl", "ldaps://example.com:636/"); + _configuration.put("searchContext", "dc=example"); AuthenticationManager manager = _factory.createInstance(_configuration); assertNotNull(manager); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java index 89580dc392..52b525dd80 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticatorTest.java @@ -21,17 +21,20 @@ package org.apache.qpid.server.security.auth.rmi; import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.security.Principal; +import java.util.regex.Pattern; import javax.security.auth.Subject; import junit.framework.TestCase; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.security.SubjectCreator; import org.apache.qpid.server.security.auth.AuthenticationResult; import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus; @@ -47,9 +50,8 @@ public class RMIPasswordAuthenticatorTest extends TestCase private static final String USERNAME = "guest"; private static final String PASSWORD = "password"; - private final ApplicationRegistry _applicationRegistry = mock(ApplicationRegistry.class); + private final Broker _broker = mock(Broker.class); private final SecurityManager _securityManager = mock(SecurityManager.class); - private final InetSocketAddress _jmxSocketAddress = new InetSocketAddress(8999); private final Subject _loginSubject = new Subject(); private final String[] _credentials = new String[] {USERNAME, PASSWORD}; @@ -60,9 +62,8 @@ public class RMIPasswordAuthenticatorTest extends TestCase protected void setUp() throws Exception { - _rmipa = new RMIPasswordAuthenticator(_applicationRegistry, _jmxSocketAddress); - - when(_applicationRegistry.getSecurityManager()).thenReturn(_securityManager); + when(_broker.getSecurityManager()).thenReturn(_securityManager); + _rmipa = new RMIPasswordAuthenticator(_broker, new InetSocketAddress(8999)); } /** @@ -70,7 +71,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase */ public void testAuthenticationSuccess() { - when(_applicationRegistry.getSubjectCreator(_jmxSocketAddress)).thenReturn(_usernamePasswordOkaySuvjectCreator); + when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(_usernamePasswordOkaySuvjectCreator); when(_securityManager.accessManagement()).thenReturn(true); Subject newSubject = _rmipa.authenticate(_credentials); @@ -82,7 +83,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase */ public void testUsernameOrPasswordInvalid() { - when(_applicationRegistry.getSubjectCreator(_jmxSocketAddress)).thenReturn(_badPasswordSubjectCreator); + when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(_badPasswordSubjectCreator); try { @@ -98,7 +99,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase public void testAuthorisationFailure() { - when(_applicationRegistry.getSubjectCreator(_jmxSocketAddress)).thenReturn(_usernamePasswordOkaySuvjectCreator); + when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(_usernamePasswordOkaySuvjectCreator); when(_securityManager.accessManagement()).thenReturn(false); try @@ -117,7 +118,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase { final Exception mockAuthException = new Exception("Mock Auth system failure"); SubjectCreator subjectCreator = createMockSubjectCreator(false, mockAuthException); - when(_applicationRegistry.getSubjectCreator(_jmxSocketAddress)).thenReturn(subjectCreator); + when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(subjectCreator); try { @@ -135,7 +136,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase */ public void testNullSubjectCreator() throws Exception { - when(_applicationRegistry.getSubjectCreator(_jmxSocketAddress)).thenReturn(null); + when(_broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(null); try { @@ -144,8 +145,7 @@ public class RMIPasswordAuthenticatorTest extends TestCase } catch (SecurityException se) { - assertEquals("Unexpected exception message", - "Can't get subject creator for 0.0.0.0/0.0.0.0:8999", se.getMessage()); + assertTrue("Unexpected exception message", Pattern.matches("Can't get subject creator for .*:8999", se.getMessage())); } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java index 5b04df2fc5..934c0082ea 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerFactoryTest.java @@ -18,23 +18,25 @@ */ package org.apache.qpid.server.security.group; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.XMLConfiguration; -import org.apache.qpid.test.utils.TestFileUtils; +import java.util.HashMap; +import java.util.Map; import junit.framework.TestCase; +import org.apache.qpid.server.model.GroupProvider; +import org.apache.qpid.test.utils.TestFileUtils; + public class FileGroupManagerFactoryTest extends TestCase { private FileGroupManagerFactory _factory = new FileGroupManagerFactory(); - private Configuration _configuration = new XMLConfiguration(); + private Map<String, Object> _configuration = new HashMap<String, Object>(); private String _emptyButValidGroupFile = TestFileUtils.createTempFile(this).getAbsolutePath(); public void testInstanceCreated() throws Exception { - _configuration.setProperty("file-group-manager.attributes.attribute.name", "groupFile"); - _configuration.setProperty("file-group-manager.attributes.attribute.value", _emptyButValidGroupFile); + _configuration.put(GroupProvider.TYPE, FileGroupManagerFactory.FILE_GROUP_MANAGER_TYPE); + _configuration.put(FileGroupManagerFactory.FILE, _emptyButValidGroupFile); GroupManager manager = _factory.createInstance(_configuration); assertNotNull(manager); @@ -49,32 +51,17 @@ public class FileGroupManagerFactoryTest extends TestCase public void testReturnsNullWhenConfigNotForThisPlugin() throws Exception { - _configuration.setProperty("other-group-manager", "config"); + _configuration.put(GroupProvider.TYPE, "other-group-manager"); GroupManager manager = _factory.createInstance(_configuration); assertNull(manager); } - public void testRejectsConfigThatHasUnexpectedAttributeName() throws Exception - { - _configuration.setProperty("file-group-manager.attributes.attribute.name", "unexpected"); - _configuration.setProperty("file-group-manager.attributes.attribute.value", _emptyButValidGroupFile); - - try - { - _factory.createInstance(_configuration); - fail("Exception not thrown"); - } - catch (RuntimeException re) - { - // PASS - } - } public void testRejectsConfigThatIsMissingAttributeValue() throws Exception { - _configuration.setProperty("file-group-manager.attributes.attribute.name", "groupFile"); - _configuration.setProperty("file-group-manager.attributes.attribute.value", null); + _configuration.put(GroupProvider.TYPE, FileGroupManagerFactory.FILE_GROUP_MANAGER_TYPE); + _configuration.put(FileGroupManagerFactory.FILE, null); try { diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java index e926d72607..b83d25b206 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/FileGroupManagerTest.java @@ -26,7 +26,7 @@ import java.security.Principal; import java.util.Properties; import java.util.Set; -import org.apache.commons.configuration.ConfigurationException; +import org.apache.qpid.server.configuration.IllegalConfigurationException; import org.apache.qpid.server.security.auth.UsernamePrincipal; import org.apache.qpid.test.utils.QpidTestCase; @@ -69,7 +69,7 @@ public class FileGroupManagerTest extends QpidTestCase _manager = new FileGroupManager(filePath); fail("expected exception was not thrown"); } - catch(ConfigurationException ce) + catch(IllegalConfigurationException ce) { assertNotNull(ce.getCause()); assertTrue(ce.getCause() instanceof FileNotFoundException); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java index 6f9b73845d..e58a1a01f8 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/group/GroupPrincipalAccessorTest.java @@ -29,15 +29,14 @@ import java.util.Set; import junit.framework.TestCase; -import org.apache.qpid.server.security.group.GroupManager; -import org.apache.qpid.server.security.group.GroupPrincipalAccessor; +import org.apache.qpid.server.model.GroupProvider; public class GroupPrincipalAccessorTest extends TestCase { private static final String USERNAME = "username"; - private GroupManager _groupManager1 = mock(GroupManager.class); - private GroupManager _groupManager2 = mock(GroupManager.class); + private GroupProvider _groupManager1 = mock(GroupProvider.class); + private GroupProvider _groupManager2 = mock(GroupProvider.class); private Principal _group1 = mock(Principal.class); private Principal _group2 = mock(Principal.class); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreCreatorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreCreatorTest.java new file mode 100644 index 0000000000..e74937dd1c --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreCreatorTest.java @@ -0,0 +1,39 @@ +/* + * + * 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; + +import org.apache.qpid.server.store.derby.DerbyMessageStore; +import org.apache.qpid.test.utils.QpidTestCase; + +public class MessageStoreCreatorTest extends QpidTestCase +{ + private static final String[] STORE_TYPES = {MemoryMessageStore.TYPE, DerbyMessageStore.TYPE}; + + public void testMessageStoreCreator() + { + MessageStoreCreator messageStoreCreator = new MessageStoreCreator(); + for (String type : STORE_TYPES) + { + MessageStore store = messageStoreCreator.createMessageStore(type); + assertNotNull("Store of type " + type + " is not created", store); + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreTest.java index 8b7807e999..ffd777243b 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/store/MessageStoreTest.java @@ -20,6 +20,7 @@ */ package org.apache.qpid.server.store; + import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.qpid.AMQException; @@ -38,6 +39,7 @@ import org.apache.qpid.server.exchange.ExchangeRegistry; import org.apache.qpid.server.exchange.TopicExchange; import org.apache.qpid.server.message.AMQMessage; import org.apache.qpid.server.message.MessageMetaData; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.UUIDGenerator; import org.apache.qpid.server.plugin.ExchangeType; import org.apache.qpid.server.queue.AMQPriorityQueue; @@ -48,11 +50,11 @@ import org.apache.qpid.server.queue.ConflationQueue; import org.apache.qpid.server.queue.IncomingMessage; import org.apache.qpid.server.queue.QueueRegistry; import org.apache.qpid.server.queue.SimpleAMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.txn.AutoCommitTransaction; import org.apache.qpid.server.txn.ServerTransaction; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; import org.apache.qpid.util.FileUtils; import java.io.File; @@ -66,7 +68,7 @@ import java.util.Map; * For persistent stores, it validates that Exchanges, Queues, Bindings and * Messages are persisted and recovered correctly. */ -public class MessageStoreTest extends InternalBrokerBaseCase +public class MessageStoreTest extends QpidTestCase { public static final int DEFAULT_PRIORTY_LEVEL = 5; public static final String SELECTOR_VALUE = "Test = 'MST'"; @@ -93,13 +95,15 @@ public class MessageStoreTest extends InternalBrokerBaseCase private AMQShortString queueOwner = new AMQShortString("MST"); - protected PropertiesConfiguration _config; + private PropertiesConfiguration _config; + + private VirtualHost _virtualHost; + private Broker _broker; public void setUp() throws Exception { - getConfigXml().addProperty("management.enabled", "false"); - super.setUp(); + BrokerTestHelper.setUp(); String storePath = System.getProperty("QPID_WORK") + File.separator + getName(); @@ -109,9 +113,38 @@ public class MessageStoreTest extends InternalBrokerBaseCase cleanup(new File(storePath)); + _broker = BrokerTestHelper.createBrokerMock(); + reloadVirtualHost(); } + @Override + public void tearDown() throws Exception + { + try + { + if (_virtualHost != null) + { + _virtualHost.close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + } + + public VirtualHost getVirtualHost() + { + return _virtualHost; + } + + public PropertiesConfiguration getConfig() + { + return _config; + } + protected void reloadVirtualHost() { VirtualHost original = getVirtualHost(); @@ -121,8 +154,6 @@ public class MessageStoreTest extends InternalBrokerBaseCase try { getVirtualHost().close(); - getVirtualHost().getApplicationRegistry(). - getVirtualHostRegistry().unregisterVirtualHost(getVirtualHost()); } catch (Exception e) { @@ -133,7 +164,7 @@ public class MessageStoreTest extends InternalBrokerBaseCase try { - setVirtualHost(ApplicationRegistry.getInstance().createVirtualHost(new VirtualHostConfiguration(getClass().getName(), _config))); + _virtualHost = BrokerTestHelper.createVirtualHost(new VirtualHostConfiguration(getClass().getName(), _config, _broker)); } catch (Exception e) { diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/QueueBrowserUsesNoAckTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/QueueBrowserUsesNoAckTest.java index 3272bd5447..d35a90e3c8 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/QueueBrowserUsesNoAckTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/QueueBrowserUsesNoAckTest.java @@ -21,14 +21,75 @@ package org.apache.qpid.server.subscription; import org.apache.qpid.AMQException; +import org.apache.qpid.common.AMQPFilterTypes; +import org.apache.qpid.exchange.ExchangeDefaults; import org.apache.qpid.framing.AMQShortString; +import org.apache.qpid.framing.FieldTable; +import org.apache.qpid.server.AMQChannel; +import org.apache.qpid.server.exchange.Exchange; import org.apache.qpid.server.protocol.InternalTestProtocolSession; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.queue.AMQQueue; +import org.apache.qpid.server.queue.SimpleAMQQueue; +import org.apache.qpid.server.store.MessageStore; +import org.apache.qpid.server.store.TestableMemoryMessageStore; +import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; import java.util.List; -public class QueueBrowserUsesNoAckTest extends InternalBrokerBaseCase +public class QueueBrowserUsesNoAckTest extends QpidTestCase { + private AMQChannel _channel; + private SimpleAMQQueue _queue; + private MessageStore _messageStore; + private String _queueName; + + @Override + public void setUp() throws Exception + { + super.setUp(); + BrokerTestHelper.setUp(); + _channel = BrokerTestHelper.createChannel(); + VirtualHost virtualHost = _channel.getVirtualHost(); + _queueName = getTestName(); + _queue = BrokerTestHelper.createQueue(_queueName, virtualHost); + _messageStore = virtualHost.getMessageStore(); + Exchange defaultExchange = virtualHost.getExchangeRegistry().getDefaultExchange(); + virtualHost.getBindingFactory().addBinding(_queueName, _queue, defaultExchange, null); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_channel != null) + { + _channel.getVirtualHost().close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + } + + private AMQChannel getChannel() + { + return _channel; + } + + private InternalTestProtocolSession getSession() + { + return (InternalTestProtocolSession)_channel.getProtocolSession(); + } + + private SimpleAMQQueue getQueue() + { + return _queue; + } public void testQueueBrowserUsesNoAck() throws AMQException { @@ -39,7 +100,7 @@ public class QueueBrowserUsesNoAckTest extends InternalBrokerBaseCase checkStoreContents(0); //Send required messsages to the queue - publishMessages(getSession(), getChannel(), sendMessageCount); + BrokerTestHelper.publishMessages(getChannel(), sendMessageCount, _queueName, ExchangeDefaults.DEFAULT_EXCHANGE_NAME.asString()); //Ensure they are stored checkStoreContents(sendMessageCount); @@ -74,4 +135,16 @@ public class QueueBrowserUsesNoAckTest extends InternalBrokerBaseCase .equals(Subscription.State.SUSPENDED)); } + private void checkStoreContents(int messageCount) + { + assertEquals("Message header count incorrect in the MetaDataMap", messageCount, ((TestableMemoryMessageStore) _messageStore).getMessageCount()); + } + + private AMQShortString browse(AMQChannel channel, AMQQueue queue) throws AMQException + { + FieldTable filters = new FieldTable(); + filters.put(AMQPFilterTypes.NO_CONSUME.getValue(), true); + + return channel.subscribeToQueue(null, queue, true, filters, false, true); + } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/SubscriptionFactoryImplTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/SubscriptionFactoryImplTest.java index e7b0725edf..89d434e95d 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/SubscriptionFactoryImplTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/subscription/SubscriptionFactoryImplTest.java @@ -23,20 +23,55 @@ package org.apache.qpid.server.subscription; import org.apache.qpid.common.AMQPFilterTypes; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.framing.FieldTable; +import org.apache.qpid.server.AMQChannel; import org.apache.qpid.server.flow.WindowCreditManager; +import org.apache.qpid.server.logging.UnitTestMessageLogger; +import org.apache.qpid.server.logging.actors.GenericActor; +import org.apache.qpid.server.protocol.AMQProtocolSession; import org.apache.qpid.server.protocol.ProtocolEngine_0_10; import org.apache.qpid.server.transport.ServerConnection; import org.apache.qpid.server.transport.ServerSession; import org.apache.qpid.server.transport.ServerSessionDelegate; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.test.utils.QpidTestCase; import org.apache.qpid.transport.Binary; import org.apache.qpid.transport.MessageAcceptMode; import org.apache.qpid.transport.MessageAcquireMode; import org.apache.qpid.transport.MessageFlowMode; import org.apache.qpid.transport.TestNetworkConnection; -public class SubscriptionFactoryImplTest extends InternalBrokerBaseCase +public class SubscriptionFactoryImplTest extends QpidTestCase { + private AMQChannel _channel; + private AMQProtocolSession _session; + + @Override + public void setUp() throws Exception + { + super.setUp(); + BrokerTestHelper.setUp(); + _channel = BrokerTestHelper.createChannel(); + _session = _channel.getProtocolSession(); + GenericActor.setDefaultMessageLogger(new UnitTestMessageLogger(false)); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_channel != null) + { + _channel.getVirtualHost().close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } + } + /** * Tests that while creating Subscriptions of various types, the * ID numbers assigned are allocated from a common sequence @@ -46,31 +81,31 @@ public class SubscriptionFactoryImplTest extends InternalBrokerBaseCase { //create a No-Ack subscription, get the first Subscription ID long previousId = 0; - Subscription noAckSub = SubscriptionFactoryImpl.INSTANCE.createSubscription(1, getSession(), new AMQShortString("1"), false, null, false, getChannel().getCreditManager()); + Subscription noAckSub = SubscriptionFactoryImpl.INSTANCE.createSubscription(1, _session, new AMQShortString("1"), false, null, false, _channel.getCreditManager()); previousId = noAckSub.getSubscriptionID(); //create an ack subscription, verify the next Subscription ID is used - Subscription ackSub = SubscriptionFactoryImpl.INSTANCE.createSubscription(1, getSession(), new AMQShortString("1"), true, null, false, getChannel().getCreditManager()); + Subscription ackSub = SubscriptionFactoryImpl.INSTANCE.createSubscription(1, _session, new AMQShortString("1"), true, null, false, _channel.getCreditManager()); assertEquals("Unexpected Subscription ID allocated", previousId + 1, ackSub.getSubscriptionID()); previousId = ackSub.getSubscriptionID(); //create a browser subscription FieldTable filters = new FieldTable(); filters.put(AMQPFilterTypes.NO_CONSUME.getValue(), true); - Subscription browerSub = SubscriptionFactoryImpl.INSTANCE.createSubscription(1, getSession(), new AMQShortString("1"), true, null, false, getChannel().getCreditManager()); + Subscription browerSub = SubscriptionFactoryImpl.INSTANCE.createSubscription(1, _session, new AMQShortString("1"), true, null, false, _channel.getCreditManager()); assertEquals("Unexpected Subscription ID allocated", previousId + 1, browerSub.getSubscriptionID()); previousId = browerSub.getSubscriptionID(); //create an BasicGet NoAck subscription - Subscription getNoAckSub = SubscriptionFactoryImpl.INSTANCE.createBasicGetNoAckSubscription(getChannel(), getSession(), new AMQShortString("1"), null, false, - getChannel().getCreditManager(),getChannel().getClientDeliveryMethod(), getChannel().getRecordDeliveryMethod()); + Subscription getNoAckSub = SubscriptionFactoryImpl.INSTANCE.createBasicGetNoAckSubscription(_channel, _session, new AMQShortString("1"), null, false, + _channel.getCreditManager(),_channel.getClientDeliveryMethod(), _channel.getRecordDeliveryMethod()); assertEquals("Unexpected Subscription ID allocated", previousId + 1, getNoAckSub.getSubscriptionID()); previousId = getNoAckSub.getSubscriptionID(); //create a 0-10 subscription ServerConnection conn = new ServerConnection(1); - ProtocolEngine_0_10 engine = new ProtocolEngine_0_10(conn, new TestNetworkConnection(), getRegistry()); - conn.setVirtualHost(getVirtualHost()); + ProtocolEngine_0_10 engine = new ProtocolEngine_0_10(conn, new TestNetworkConnection()); + conn.setVirtualHost(_session.getVirtualHost()); ServerSessionDelegate sesDel = new ServerSessionDelegate(); Binary name = new Binary(new byte[]{new Byte("1")}); ServerSession session = new ServerSession(conn, sesDel, name, 0); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/transport/ServerSessionTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/transport/ServerSessionTest.java index ac3c0d5722..3389773ff8 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/transport/ServerSessionTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/transport/ServerSessionTest.java @@ -18,12 +18,14 @@ */ package org.apache.qpid.server.transport; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.logging.actors.GenericActor; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.test.utils.QpidTestCase; import org.apache.qpid.transport.Binary; -public class ServerSessionTest extends InternalBrokerBaseCase +public class ServerSessionTest extends QpidTestCase { private VirtualHost _virtualHost; @@ -32,7 +34,26 @@ public class ServerSessionTest extends InternalBrokerBaseCase public void setUp() throws Exception { super.setUp(); - _virtualHost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHosts().iterator().next(); + BrokerTestHelper.setUp(); + _virtualHost = BrokerTestHelper.createVirtualHost(getName()); + GenericActor.setDefaultMessageLogger(CurrentActor.get().getRootMessageLogger()); + } + + @Override + public void tearDown() throws Exception + { + try + { + if (_virtualHost != null) + { + _virtualHost.close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } } public void testCompareTo() throws Exception diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java new file mode 100644 index 0000000000..3be8927224 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java @@ -0,0 +1,209 @@ +/* + * + * 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.util; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.SocketAddress; +import java.util.Collections; +import java.util.UUID; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.qpid.AMQException; +import org.apache.qpid.framing.AMQShortString; +import org.apache.qpid.framing.BasicContentHeaderProperties; +import org.apache.qpid.framing.ContentHeaderBody; +import org.apache.qpid.framing.abstraction.MessagePublishInfo; +import org.apache.qpid.server.AMQChannel; +import org.apache.qpid.server.configuration.VirtualHostConfiguration; +import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; +import org.apache.qpid.server.exchange.DefaultExchangeFactory; +import org.apache.qpid.server.exchange.Exchange; +import org.apache.qpid.server.logging.RootMessageLogger; +import org.apache.qpid.server.logging.SystemOutMessageLogger; +import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.logging.actors.GenericActor; +import org.apache.qpid.server.logging.actors.TestLogActor; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.UUIDGenerator; +import org.apache.qpid.server.protocol.AMQProtocolSession; +import org.apache.qpid.server.protocol.InternalTestProtocolSession; +import org.apache.qpid.server.queue.AMQQueueFactory; +import org.apache.qpid.server.queue.SimpleAMQQueue; +import org.apache.qpid.server.security.SecurityManager; +import org.apache.qpid.server.security.SubjectCreator; +import org.apache.qpid.server.stats.StatisticsGatherer; +import org.apache.qpid.server.store.TestableMemoryMessageStore; +import org.apache.qpid.server.virtualhost.VirtualHost; +import org.apache.qpid.server.virtualhost.VirtualHostImpl; +import org.apache.qpid.server.virtualhost.VirtualHostRegistry; + +public class BrokerTestHelper +{ + + protected static final String BROKER_STORE_CLASS_NAME_KEY = "brokerstore.class.name"; + protected static final String JSON_BROKER_STORE_CLASS_NAME = JsonConfigurationEntryStore.class.getName(); + + public static Broker createBrokerMock() + { + SubjectCreator subjectCreator = mock(SubjectCreator.class); + when(subjectCreator.getMechanisms()).thenReturn(""); + Broker broker = mock(Broker.class); + when(broker.getAttribute(Broker.SESSION_COUNT_LIMIT)).thenReturn(1); + when(broker.getAttribute(Broker.HOUSEKEEPING_CHECK_PERIOD)).thenReturn(10000l); + when(broker.getId()).thenReturn(UUID.randomUUID()); + when(broker.getSubjectCreator(any(SocketAddress.class))).thenReturn(subjectCreator); + RootMessageLogger rootMessageLogger = CurrentActor.get().getRootMessageLogger(); + when(broker.getRootMessageLogger()).thenReturn(rootMessageLogger); + when(broker.getVirtualHostRegistry()).thenReturn(new VirtualHostRegistry()); + when(broker.getSecurityManager()).thenReturn(new SecurityManager(null)); + GenericActor.setDefaultMessageLogger(rootMessageLogger); + return broker; + } + + public static void setUp() + { + CurrentActor.set(new TestLogActor(new SystemOutMessageLogger())); + } + + public static void tearDown() + { + CurrentActor.remove(); + } + + public static VirtualHost createVirtualHost(VirtualHostConfiguration virtualHostConfiguration, VirtualHostRegistry virtualHostRegistry) + throws Exception + { + StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); + VirtualHost host = new VirtualHostImpl(virtualHostRegistry, statisticsGatherer, new SecurityManager(null), virtualHostConfiguration); + virtualHostRegistry.registerVirtualHost(host); + return host; + } + + public static VirtualHost createVirtualHost(VirtualHostConfiguration virtualHostConfiguration) throws Exception + { + return new VirtualHostImpl(null, mock(StatisticsGatherer.class), new SecurityManager(null), virtualHostConfiguration); + } + + public static VirtualHost createVirtualHost(String name, VirtualHostRegistry virtualHostRegistry) throws Exception + { + VirtualHostConfiguration vhostConfig = createVirtualHostConfiguration(name); + return createVirtualHost(vhostConfig, virtualHostRegistry); + } + + public static VirtualHost createVirtualHost(String name) throws Exception + { + VirtualHostConfiguration configuration = createVirtualHostConfiguration(name); + return createVirtualHost(configuration); + } + + private static VirtualHostConfiguration createVirtualHostConfiguration(String name) throws ConfigurationException + { + VirtualHostConfiguration vhostConfig = new VirtualHostConfiguration(name, new PropertiesConfiguration(), createBrokerMock()); + vhostConfig.setMessageStoreClass(TestableMemoryMessageStore.class.getName()); + return vhostConfig; + } + + public static AMQChannel createChannel(int channelId, AMQProtocolSession session) throws AMQException + { + AMQChannel channel = new AMQChannel(session, channelId, session.getVirtualHost().getMessageStore()); + session.addChannel(channel); + return channel; + } + + public static AMQChannel createChannel(int channelId) throws Exception + { + InternalTestProtocolSession session = createSession(); + return createChannel(channelId, session); + } + + public static AMQChannel createChannel() throws Exception + { + return createChannel(1); + } + + public static InternalTestProtocolSession createSession() throws Exception + { + return createSession("test"); + } + + public static InternalTestProtocolSession createSession(String hostName) throws Exception + { + VirtualHost virtualHost = createVirtualHost(hostName); + return new InternalTestProtocolSession(virtualHost, createBrokerMock()); + } + + public static Exchange createExchange(String hostName) throws Exception + { + SecurityManager securityManager = new SecurityManager(null); + VirtualHost virtualHost = mock(VirtualHost.class); + when(virtualHost.getName()).thenReturn(hostName); + when(virtualHost.getSecurityManager()).thenReturn(securityManager); + DefaultExchangeFactory factory = new DefaultExchangeFactory(virtualHost); + return factory.createExchange("amp.direct", "direct", false, false); + } + + public static void publishMessages(AMQChannel channel, int numberOfMessages, String queueName, String exchangeName) throws AMQException + { + AMQShortString rouningKey = new AMQShortString(queueName); + AMQShortString exchangeNameAsShortString = new AMQShortString(exchangeName); + MessagePublishInfo info = mock(MessagePublishInfo.class); + when(info.getExchange()).thenReturn(exchangeNameAsShortString); + when(info.getRoutingKey()).thenReturn(rouningKey); + + Exchange exchange = channel.getVirtualHost().getExchangeRegistry().getExchange(exchangeName); + for (int count = 0; count < numberOfMessages; count++) + { + channel.setPublishFrame(info, exchange); + + // Set the body size + ContentHeaderBody _headerBody = new ContentHeaderBody(); + _headerBody.setBodySize(0); + + // Set Minimum properties + BasicContentHeaderProperties properties = new BasicContentHeaderProperties(); + + properties.setExpiration(0L); + properties.setTimestamp(System.currentTimeMillis()); + + // Make Message Persistent + properties.setDeliveryMode((byte) 2); + + _headerBody.setProperties(properties); + + channel.publishContentHeader(_headerBody); + } + channel.sync(); + } + + public static SimpleAMQQueue createQueue(String queueName, VirtualHost virtualHost) throws AMQException + { + SimpleAMQQueue queue = (SimpleAMQQueue) AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), queueName, false, null, + false, false, virtualHost, Collections.<String, Object>emptyMap()); + virtualHost.getQueueRegistry().registerQueue(queue); + return queue; + } + + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/util/InternalBrokerBaseCase.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/util/InternalBrokerBaseCase.java deleted file mode 100644 index d7a9078412..0000000000 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/util/InternalBrokerBaseCase.java +++ /dev/null @@ -1,372 +0,0 @@ -/* - * - * 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.util; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.XMLConfiguration; - -import org.apache.qpid.AMQException; -import org.apache.qpid.common.AMQPFilterTypes; -import org.apache.qpid.exchange.ExchangeDefaults; -import org.apache.qpid.framing.AMQShortString; -import org.apache.qpid.framing.BasicContentHeaderProperties; -import org.apache.qpid.framing.ContentHeaderBody; -import org.apache.qpid.framing.FieldTable; -import org.apache.qpid.framing.abstraction.MessagePublishInfo; -import org.apache.qpid.server.AMQChannel; -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.exchange.Exchange; -import org.apache.qpid.server.logging.SystemOutMessageLogger; -import org.apache.qpid.server.logging.actors.CurrentActor; -import org.apache.qpid.server.logging.actors.TestLogActor; -import org.apache.qpid.server.model.UUIDGenerator; -import org.apache.qpid.server.protocol.InternalTestProtocolSession; -import org.apache.qpid.server.queue.AMQQueue; -import org.apache.qpid.server.queue.AMQQueueFactory; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; -import org.apache.qpid.server.store.MessageStore; -import org.apache.qpid.server.store.TestableMemoryMessageStore; -import org.apache.qpid.server.virtualhost.VirtualHost; -import org.apache.qpid.test.utils.QpidTestCase; - - -public class InternalBrokerBaseCase extends QpidTestCase -{ - private IApplicationRegistry _registry; - private MessageStore _messageStore; - private AMQChannel _channel; - private InternalTestProtocolSession _session; - private VirtualHost _virtualHost; - private AMQQueue _queue; - private AMQShortString QUEUE_NAME; - private ServerConfiguration _configuration; - private XMLConfiguration _configXml = new XMLConfiguration(); - private boolean _started = false; - - public void setUp() throws Exception - { - super.setUp(); - - _configXml.addProperty("virtualhosts.virtualhost.name", "test"); - _configXml.addProperty("virtualhosts.virtualhost.test.store.class", TestableMemoryMessageStore.class.getName()); - - _configXml.addProperty("virtualhosts.virtualhost(-1).name", getName()); - _configXml.addProperty("virtualhosts.virtualhost(-1)."+getName()+".store.class", TestableMemoryMessageStore.class.getName()); - - createBroker(); - } - - protected void createBroker() throws Exception - { - _started = true; - CurrentActor.set(new TestLogActor(new SystemOutMessageLogger())); - - _configuration = new ServerConfiguration(_configXml); - - configure(); - - _registry = createApplicationRegistry(); - ApplicationRegistry.initialise(_registry); - _registry.getVirtualHostRegistry().setDefaultVirtualHostName(getName()); - _virtualHost = _registry.getVirtualHostRegistry().getVirtualHost(getName()); - - QUEUE_NAME = new AMQShortString("test"); - // Create a queue on the test Vhost.. this will aid in diagnosing duff tests - // as the ExpiredMessage Task will log with the test Name. - _queue = AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), QUEUE_NAME.asString(), false, "testowner", - false, false, _virtualHost, null); - - Exchange defaultExchange = _virtualHost.getExchangeRegistry().getDefaultExchange(); - _virtualHost.getBindingFactory().addBinding(QUEUE_NAME.toString(), _queue, defaultExchange, null); - - _virtualHost = _registry.getVirtualHostRegistry().getVirtualHost("test"); - _messageStore = _virtualHost.getMessageStore(); - - _queue = AMQQueueFactory.createAMQQueueImpl(UUIDGenerator.generateRandomUUID(), getName(), false, "testowner", - false, false, _virtualHost, null); - - _virtualHost.getQueueRegistry().registerQueue(_queue); - - defaultExchange = _virtualHost.getExchangeRegistry().getDefaultExchange(); - - _virtualHost.getBindingFactory().addBinding(getName(), _queue, defaultExchange, null); - - _session = new InternalTestProtocolSession(_virtualHost); - CurrentActor.set(_session.getLogActor()); - - _channel = new AMQChannel(_session, 1, _messageStore); - - _session.addChannel(_channel); - } - - protected IApplicationRegistry createApplicationRegistry() throws ConfigurationException - { - return new TestApplicationRegistry(_configuration); - } - - protected void configure() - { - // Allow other tests to override configuration - } - - protected void stopBroker() - { - try - { - //Remove the ProtocolSession Actor added during createBroker - CurrentActor.remove(); - } - finally - { - ApplicationRegistry.remove(); - _started = false; - } - } - - - public void tearDown() throws Exception - { - try - { - if (_started) - { - stopBroker(); - } - } - finally - { - super.tearDown(); - // Purge Any erroneously added actors - CurrentActor.removeAll(); - } - } - - protected void checkStoreContents(int messageCount) - { - assertEquals("Message header count incorrect in the MetaDataMap", messageCount, ((TestableMemoryMessageStore) _messageStore).getMessageCount()); - - //The above publish message is sufficiently small not to fit in the header so no Body is required. - //assertEquals("Message body count incorrect in the ContentBodyMap", messageCount, ((TestableMemoryMessageStore) _messageStore).getContentBodyMap().size()); - } - - protected AMQShortString subscribe(InternalTestProtocolSession session, AMQChannel channel, AMQQueue queue) - { - try - { - return channel.subscribeToQueue(null, queue, true, null, false, true); - } - catch (AMQException e) - { - e.printStackTrace(); - fail(e.getMessage()); - } - - //Keep the compiler happy - return null; - } - - protected AMQShortString browse(AMQChannel channel, AMQQueue queue) - { - try - { - FieldTable filters = new FieldTable(); - filters.put(AMQPFilterTypes.NO_CONSUME.getValue(), true); - - return channel.subscribeToQueue(null, queue, true, filters, false, true); - } - catch (AMQException e) - { - e.printStackTrace(); - fail(e.getMessage()); - } - - //Keep the compiler happy - return null; - } - - public void publishMessages(InternalTestProtocolSession session, AMQChannel channel, int messages) throws AMQException - { - MessagePublishInfo info = new MessagePublishInfo() - { - public AMQShortString getExchange() - { - return ExchangeDefaults.DEFAULT_EXCHANGE_NAME; - } - - public void setExchange(AMQShortString exchange) - { - - } - - public boolean isImmediate() - { - return false; - } - - public boolean isMandatory() - { - return false; - } - - public AMQShortString getRoutingKey() - { - return new AMQShortString(getName()); - } - }; - - for (int count = 0; count < messages; count++) - { - channel.setPublishFrame(info, _virtualHost.getExchangeRegistry().getExchange(info.getExchange())); - - //Set the body size - ContentHeaderBody _headerBody = new ContentHeaderBody(); - _headerBody.setBodySize(0); - - //Set Minimum properties - BasicContentHeaderProperties properties = new BasicContentHeaderProperties(); - - properties.setExpiration(0L); - properties.setTimestamp(System.currentTimeMillis()); - - //Make Message Persistent - properties.setDeliveryMode((byte) 2); - - _headerBody.setProperties(properties); - - channel.publishContentHeader(_headerBody); - } - channel.sync(); - } - - public void acknowledge(AMQChannel channel, long deliveryTag) - { - try - { - channel.acknowledgeMessage(deliveryTag, false); - } - catch (AMQException e) - { - e.printStackTrace(); - fail(e.getMessage()); - } - } - - public IApplicationRegistry getRegistry() - { - return _registry; - } - - public void setRegistry(IApplicationRegistry registry) - { - _registry = registry; - } - - public MessageStore getMessageStore() - { - return _messageStore; - } - - public void setMessageStore(MessageStore messageStore) - { - _messageStore = messageStore; - } - - public AMQChannel getChannel() - { - return _channel; - } - - public void setChannel(AMQChannel channel) - { - _channel = channel; - } - - public InternalTestProtocolSession getSession() - { - return _session; - } - - public void setSession(InternalTestProtocolSession session) - { - _session = session; - } - - public VirtualHost getVirtualHost() - { - return _virtualHost; - } - - public void setVirtualHost(VirtualHost virtualHost) - { - _virtualHost = virtualHost; - } - - public AMQQueue getQueue() - { - return _queue; - } - - public void setQueue(AMQQueue queue) - { - _queue = queue; - } - - public AMQShortString getQUEUE_NAME() - { - return QUEUE_NAME; - } - - public void setQUEUE_NAME(AMQShortString QUEUE_NAME) - { - this.QUEUE_NAME = QUEUE_NAME; - } - - public ServerConfiguration getConfiguration() - { - return _configuration; - } - - public void setConfiguration(ServerConfiguration configuration) - { - _configuration = configuration; - } - - public XMLConfiguration getConfigXml() - { - return _configXml; - } - - public void setConfigXml(XMLConfiguration configXml) - { - _configXml = configXml; - } - - public boolean isStarted() - { - return _started; - } - - public void setStarted(boolean started) - { - _started = started; - } -} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/util/TestApplicationRegistry.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/util/TestApplicationRegistry.java deleted file mode 100644 index fd3791a8e1..0000000000 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/util/TestApplicationRegistry.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * - * 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.util; - -import java.net.SocketAddress; -import java.util.Collections; -import java.util.Map; -import org.apache.commons.configuration.ConfigurationException; - -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.apache.qpid.server.logging.NullRootMessageLogger; -import org.apache.qpid.server.logging.actors.BrokerActor; -import org.apache.qpid.server.logging.actors.CurrentActor; -import org.apache.qpid.server.logging.actors.GenericActor; -import org.apache.qpid.server.logging.log4j.LoggingManagementFacade; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.security.SubjectCreator; -import org.apache.qpid.server.security.auth.database.PropertiesPrincipalDatabase; -import org.apache.qpid.server.security.auth.manager.AuthenticationManager; -import org.apache.qpid.server.security.auth.manager.IAuthenticationManagerRegistry; -import org.apache.qpid.server.security.auth.manager.PrincipalDatabaseAuthenticationManager; -import org.apache.qpid.server.security.group.GroupPrincipalAccessor; -import org.apache.qpid.test.utils.QpidTestCase; - -import java.util.Properties; - -public class TestApplicationRegistry extends ApplicationRegistry -{ - - public TestApplicationRegistry(ServerConfiguration config) throws ConfigurationException - { - super(config); - } - - @Override - public void initialise() throws Exception - { - CurrentActor.setDefault(new BrokerActor(new NullRootMessageLogger())); - GenericActor.setDefaultMessageLogger(new NullRootMessageLogger()); - LoggingManagementFacade.configure(QpidTestCase.LOG4J_CONFIG_FILE_PATH); - - super.initialise(); - } - - @Override - protected IAuthenticationManagerRegistry createAuthenticationManagerRegistry( - ServerConfiguration configuration, final GroupPrincipalAccessor groupPrincipalAccessor) - throws ConfigurationException - { - final Properties users = new Properties(); - users.put("guest","guest"); - users.put("admin","admin"); - - final PropertiesPrincipalDatabase ppd = new PropertiesPrincipalDatabase(users); - - final AuthenticationManager pdam = new PrincipalDatabaseAuthenticationManager(ppd); - pdam.initialise(); - - return new IAuthenticationManagerRegistry() - { - @Override - public void close() - { - pdam.close(); - } - - @Override - public SubjectCreator getSubjectCreator(SocketAddress address) - { - return new SubjectCreator(pdam, groupPrincipalAccessor); - } - - @Override - public Map<String, AuthenticationManager> getAvailableAuthenticationManagers() - { - return Collections.singletonMap(pdam.getClass().getName(), pdam); - } - - @Override - public void addRegistryChangeListener(RegistryChangeListener listener) - { - } - }; - } -} - - diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/MockVirtualHost.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/MockVirtualHost.java index 5e02fda40b..1d99d99820 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/MockVirtualHost.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/MockVirtualHost.java @@ -28,14 +28,12 @@ import org.apache.qpid.server.exchange.ExchangeFactory; import org.apache.qpid.server.exchange.ExchangeRegistry; import org.apache.qpid.server.protocol.v1_0.LinkRegistry; import org.apache.qpid.server.queue.QueueRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; import org.apache.qpid.server.security.SecurityManager; import org.apache.qpid.server.security.auth.manager.AuthenticationManager; import org.apache.qpid.server.stats.StatisticsCounter; import org.apache.qpid.server.store.MessageStore; import org.apache.qpid.server.txn.DtxRegistry; -import java.util.Map; import java.util.UUID; public class MockVirtualHost implements VirtualHost @@ -52,7 +50,8 @@ public class MockVirtualHost implements VirtualHost } - public IApplicationRegistry getApplicationRegistry() + @Override + public VirtualHostRegistry getVirtualHostRegistry() { return null; } @@ -67,11 +66,6 @@ public class MockVirtualHost implements VirtualHost return null; } - public UUID getBrokerId() - { - return null; - } - public DtxRegistry getDtxRegistry() { return null; diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/VirtualHostImplTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/VirtualHostImplTest.java index b8ba76e43d..559a7f8aaf 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/VirtualHostImplTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/VirtualHostImplTest.java @@ -20,15 +20,21 @@ */ package org.apache.qpid.server.virtualhost; +import static org.mockito.Mockito.mock; + +import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.XMLConfiguration; +import org.apache.commons.configuration.PropertiesConfiguration; + +import org.apache.qpid.server.configuration.VirtualHostConfiguration; -import org.apache.qpid.server.configuration.ServerConfiguration; import org.apache.qpid.server.exchange.Exchange; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.queue.AMQQueue; -import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.security.SecurityManager; +import org.apache.qpid.server.stats.StatisticsGatherer; import org.apache.qpid.server.store.MemoryMessageStore; -import org.apache.qpid.server.util.TestApplicationRegistry; +import org.apache.qpid.server.util.BrokerTestHelper; import org.apache.qpid.test.utils.QpidTestCase; import java.io.BufferedWriter; @@ -38,15 +44,31 @@ import java.io.IOException; public class VirtualHostImplTest extends QpidTestCase { - private ServerConfiguration _configuration; - private ApplicationRegistry _registry; + private VirtualHostRegistry _virtualHostRegistry; + + @Override + public void setUp() throws Exception + { + super.setUp(); + BrokerTestHelper.setUp(); + } @Override public void tearDown() throws Exception { - super.tearDown(); + try + { + if (_virtualHostRegistry != null) + { + _virtualHostRegistry.close(); + } + } + finally + { + BrokerTestHelper.tearDown(); + super.tearDown(); + } - ApplicationRegistry.remove(); } /** @@ -74,17 +96,23 @@ public class VirtualHostImplTest extends QpidTestCase */ public void testSpecifyingCustomBindingForDefaultExchangeThrowsException() throws Exception { - File config = writeConfigFile(getName(), getName(), null, false, new String[]{"custom-binding"}); + final String queueName = getName(); + final String customBinding = "custom-binding"; + File config = writeConfigFile(queueName, queueName, null, false, new String[]{customBinding}); try { - createVirtualHost(getName(), config); + createVirtualHost(queueName, config); fail("virtualhost creation should have failed due to illegal configuration"); } catch (RuntimeException e) { + assertNotNull(e.getCause()); + assertEquals(ConfigurationException.class, e.getCause().getClass()); - //expected + + Throwable configException = e.getCause(); + assertEquals("Illegal attempt to bind queue '" + queueName + "' to the default exchange with a key other than the queue name: " + customBinding, configException.getMessage()); } } @@ -96,6 +124,14 @@ public class VirtualHostImplTest extends QpidTestCase assertEquals(State.ACTIVE, vhost.getState()); } + public void testVirtualHostHavingStoreSetAsTypeBecomesActive() throws Exception + { + String virtualHostName = getName(); + VirtualHost host = createVirtualHostUsingStoreType(virtualHostName); + assertNotNull(host); + assertEquals(State.ACTIVE, host.getState()); + } + public void testVirtualHostBecomesStoppedOnClose() throws Exception { File config = writeConfigFile(getName(), getName(), getName() +".direct", false, new String[0]); @@ -107,22 +143,39 @@ public class VirtualHostImplTest extends QpidTestCase assertEquals(0, vhost.getHouseKeepingActiveCount()); } + public void testVirtualHostHavingStoreSetAsTypeBecomesStoppedOnClose() throws Exception + { + String virtualHostName = getName(); + VirtualHost host = createVirtualHostUsingStoreType(virtualHostName); + assertNotNull(host); + assertEquals(State.ACTIVE, host.getState()); + host.close(); + assertEquals(State.STOPPED, host.getState()); + assertEquals(0, host.getHouseKeepingActiveCount()); + } + /** * Tests that specifying an unknown exchange to bind the queue to results in failure to create the vhost */ public void testSpecifyingUnknownExchangeThrowsException() throws Exception { - File config = writeConfigFile(getName(), getName(), "made-up-exchange", true, new String[0]); + final String queueName = getName(); + final String exchangeName = "made-up-exchange"; + File config = writeConfigFile(queueName, queueName, exchangeName, true, new String[0]); try { - createVirtualHost(getName(), config); + createVirtualHost(queueName, config); fail("virtualhost creation should have failed due to illegal configuration"); } catch (RuntimeException e) { + assertNotNull(e.getCause()); + assertEquals(ConfigurationException.class, e.getCause().getClass()); - //expected + + Throwable configException = e.getCause(); + assertEquals("Attempt to bind queue '" + queueName + "' to unknown exchange:" + exchangeName, configException.getMessage()); } } @@ -154,12 +207,14 @@ public class VirtualHostImplTest extends QpidTestCase private VirtualHost createVirtualHost(String vhostName, File config) throws Exception { - _configuration = new ServerConfiguration(new XMLConfiguration(config)); + Broker broker = BrokerTestHelper.createBrokerMock(); + _virtualHostRegistry = broker.getVirtualHostRegistry(); - _registry = new TestApplicationRegistry(_configuration); - ApplicationRegistry.initialise(_registry); + VirtualHostConfiguration configuration = new VirtualHostConfiguration(vhostName, config, broker); + VirtualHost host = new VirtualHostImpl(_virtualHostRegistry, mock(StatisticsGatherer.class), new SecurityManager(null), configuration); + _virtualHostRegistry.registerVirtualHost(host); - return _registry.getVirtualHostRegistry().getVirtualHost(vhostName); + return host; } /** @@ -184,7 +239,6 @@ public class VirtualHostImplTest extends QpidTestCase BufferedWriter writer = new BufferedWriter(fstream); //extra outer tag to please Commons Configuration - writer.write("<configuration>"); writer.write("<virtualhosts>"); writer.write(" <default>" + vhostName + "</default>"); @@ -222,8 +276,6 @@ public class VirtualHostImplTest extends QpidTestCase writer.write(" </virtualhost>"); writer.write("</virtualhosts>"); - writer.write("</configuration>"); - writer.flush(); writer.close(); } @@ -234,4 +286,17 @@ public class VirtualHostImplTest extends QpidTestCase return tmpFile; } + + private VirtualHost createVirtualHostUsingStoreType(String virtualHostName) throws ConfigurationException, Exception + { + Broker broker = BrokerTestHelper.createBrokerMock(); + _virtualHostRegistry = broker.getVirtualHostRegistry(); + + Configuration config = new PropertiesConfiguration(); + config.setProperty("store.type", MemoryMessageStore.TYPE); + VirtualHostConfiguration configuration = new VirtualHostConfiguration(virtualHostName, config, broker); + VirtualHost host = new VirtualHostImpl(_virtualHostRegistry, mock(StatisticsGatherer.class), new SecurityManager(null), configuration); + _virtualHostRegistry.registerVirtualHost(host); + return host; + } } diff --git a/qpid/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm b/qpid/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm index 02bf155c44..cddfcfb581 100644 --- a/qpid/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm +++ b/qpid/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm @@ -23,8 +23,8 @@ package ${package}; import static org.apache.qpid.server.logging.AbstractRootMessageLogger.DEFAULT_LOG_HIERARCHY_PREFIX; import org.apache.log4j.Logger; +import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.logging.LogMessage; -import org.apache.qpid.server.registry.ApplicationRegistry; import java.text.MessageFormat; import java.util.Locale; @@ -44,7 +44,7 @@ import java.util.ResourceBundle; public class ${type.name}Messages { private static ResourceBundle _messages; - private static Locale _currentLocale; + private static Locale _currentLocale = BrokerProperties.getLocale(); public static final String ${type.name.toUpperCase()}_LOG_HIERARCHY = DEFAULT_LOG_HIERARCHY_PREFIX + "${type.name.toLowerCase()}"; #foreach( $message in ${type.list} ) @@ -58,24 +58,9 @@ public class ${type.name}Messages Logger.getLogger(${message.methodName.toUpperCase()}_LOG_HIERARCHY); #end - reload(); - } - - public static void reload() - { - if (ApplicationRegistry.isConfigured()) - { - _currentLocale = ApplicationRegistry.getInstance().getConfiguration().getLocale(); - } - else - { - _currentLocale = Locale.getDefault(); - } - _messages = ResourceBundle.getBundle("${resource}", _currentLocale); } - ## ## The list stored under key 'list' in the 'type' HashMap contains all the ## log messages that this class should contain. So for each entry in the list |
