BrokerAddressMap { public static BrokerAddressMap createFromMappingFile(AMQPConnectionParameters defaultParams, String mappingFilePath) { File file = new File(mappingFilePath); if (!file.isFile()) { throw new IllegalArgumentException("File " + file.getAbsolutePath() + " not found"); } Properties properties = new Properties(); try { FileReader reader = new FileReader(file); properties.load(reader); reader.close(); } catch (IOException e) { throw new IllegalArgumentException("Failed to load configuration properties from the " + file.getAbsolutePath(), e); } Map<String, AMQPConnectionParameters> mapping = new HashMap<String, AMQPConnectionParameters>(); for (Object key : properties.keySet()) { String property = key.toString(); if (property.startsWith(BROKER_KEY_PREFIX)) { String brokerName = property.substring(BROKER_KEY_PREFIX.length()); if (brokerName.isEmpty()) { throw new IllegalArgumentException("Broker name is empty: " + property); } String brokerKey = properties.getProperty(property); if (brokerKey == null) { throw new IllegalArgumentException("Broker's host:port is empty for property: " + property); } String host = properties.getProperty(brokerName + ".host", AMQPConstants.DEFAULT_BROKER_HOST); String port = properties.getProperty(brokerName + ".port", String.valueOf(AMQPConstants.DEFAULT_BROKER_PORT)); String username = properties.getProperty(brokerName + ".user", AMQPConstants.DEFAULT_USER); String password = properties.getProperty(brokerName + ".password", AMQPConstants.DEFAULT_PASSWORD); String vhost = properties.getProperty(brokerName + ".vhost", AMQPConstants.DEFAULT_VHOST); try { mapping.put(brokerKey, new AMQPConnectionParameters(host, Integer.valueOf(port), username, password, vhost)); } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid broker port value: " + port, e); } } } BrokerAddressMap brokerAddressMap = new BrokerAddressMap(defaultParams, mapping); return brokerAddressMap; } BrokerAddressMap(AMQPConnectionParameters defaultParams, Map<String, AMQPConnectionParameters> mapping); static BrokerAddressMap createFromMappingFile(AMQPConnectionParameters defaultParams,
            String mappingFilePath); AMQPConnectionParameters getBrokerForObject(URI objectURI);  }
@Test public void test() throws Exception { AMQPConnectionParameters defaultParams = new AMQPConnectionParameters("defaultHost", 1, "defaultUser", "defaultPassword", "defaultVhost"); try { BrokerAddressMap.createFromMappingFile(defaultParams, "invalid_file_name"); fail(); } catch (IllegalArgumentException e) { } BrokerAddressMap addressMap; String config; config = "broker_key.broker1=host1:123\n" + "broker1.host=host2\n" + "broker1.port=456\n" + "broker1.user=user1\n" + "broker1.password=p1\n" + "broker1.vhost=vhost1"; addressMap = createForConfig(defaultParams, config); assertMapping(addressMap, "host1", 123, "host2", 456, "user1", "p1", "vhost1"); assertMapping(addressMap, "host1", 124, "defaultHost", 1, "defaultUser", "defaultPassword", "defaultVhost"); assertMapping(addressMap, "unknown_host", 123, "defaultHost", 1, "defaultUser", "defaultPassword", "defaultVhost"); config = "broker_key.broker1=host1:123\n" + "broker1.host=host2\n" + "broker1.port=456"; addressMap = createForConfig(defaultParams, config); assertMapping(addressMap, "host1", 123, "host2", 456, AMQPConstants.DEFAULT_USER, AMQPConstants.DEFAULT_PASSWORD, AMQPConstants.DEFAULT_VHOST); assertMapping(addressMap, "unknown_host", 789, "defaultHost", 1, "defaultUser", "defaultPassword", "defaultVhost"); config = "broker_key.broker1=host1:12\n" + "broker1.host=host2\n" + "broker1.port=34\n" + "broker1.user=user1\n" + "broker1.password=p1\n" + "broker_key.broker2=host3:56\n" + "broker2.host=host4\n" + "broker2.port=78\n"; addressMap = createForConfig(defaultParams, config); assertMapping(addressMap, "host1", 12, "host2", 34, "user1", "p1", AMQPConstants.DEFAULT_VHOST); assertMapping(addressMap, "host3", 56, "host4", 78, AMQPConstants.DEFAULT_USER, AMQPConstants.DEFAULT_PASSWORD, AMQPConstants.DEFAULT_VHOST); assertMapping(addressMap, "unknown_host", 123, "defaultHost", 1, "defaultUser", "defaultPassword", "defaultVhost"); }