@Activate
  protected void activate(ComponentContext componentContext) throws Exception {
    LOG.info("Starting Activation of AMQ Broker ");
    try {
      Dictionary<?, ?> properties = componentContext.getProperties();
      BundleContext bundleContext = componentContext.getBundleContext();
      String brokerUrl = (String) properties.get(ACTIVEMQ_BROKER_URL);

      broker = new BrokerService();

      // generate a full path
      String slingHome = bundleContext.getProperty("sling.home");
      String dataPath = slingHome + "/activemq-data";
      LOG.info("Setting Data Path to  [{}] [{}] ", new Object[] {slingHome, dataPath});
      broker.setDataDirectory(dataPath);

      String federatedBrokerUrl = (String) properties.get(ACTIVEMQ_FEDERATED_BROKER_URL);

      if (federatedBrokerUrl != null && federatedBrokerUrl.length() > 0) {
        LOG.info("Federating ActiveMQ  [" + federatedBrokerUrl + "]");
        NetworkConnector connector = broker.addNetworkConnector(federatedBrokerUrl);
        connector.setDuplex(true);
      }

      // configure the broker
      LOG.info("Adding ActiveMQ connector [" + brokerUrl + "]");
      broker.addConnector(brokerUrl);

      broker.start();
    } catch (Exception e) {
      LOG.info(e.getMessage(), e);
      throw e;
    }
  }
  @Test
  public void testMod() throws Exception {
    final BrokerService brokerService = new BrokerService();
    startBroker(brokerService);
    assertTrue("broker alive", brokerService.isStarted());
    assertEquals("no network connectors", 0, brokerService.getNetworkConnectors().size());

    DiscoveryNetworkConnector nc = createNetworkConnector();
    javaConfigBroker.addNetworkConnector(nc);
    TimeUnit.SECONDS.sleep(SLEEP);

    assertEquals("one network connectors", 1, brokerService.getNetworkConnectors().size());

    // track the original
    NetworkConnector networkConnector = brokerService.getNetworkConnectors().get(0);
    assertEquals("network ttl is default", 1, networkConnector.getNetworkTTL());

    nc.setNetworkTTL(2);
    javaConfigBroker.updateNetworkConnector(nc);
    TimeUnit.SECONDS.sleep(SLEEP);
    assertEquals("still one network connectors", 1, brokerService.getNetworkConnectors().size());

    NetworkConnector modNetworkConnector = brokerService.getNetworkConnectors().get(0);
    assertEquals("got ttl update", 2, modNetworkConnector.getNetworkTTL());

    // apply again - ensure no change
    javaConfigBroker.updateNetworkConnector(nc);
    assertEquals("no new network connectors", 1, brokerService.getNetworkConnectors().size());
    assertSame("same instance", modNetworkConnector, brokerService.getNetworkConnectors().get(0));
  }
  @Test
  public void testNew() throws Exception {
    final BrokerService brokerService = new BrokerService();
    startBroker(brokerService);
    assertTrue("broker alive", brokerService.isStarted());
    assertEquals("no network connectors", 0, brokerService.getNetworkConnectors().size());

    DiscoveryNetworkConnector nc = createNetworkConnector();

    javaConfigBroker.addNetworkConnector(nc);

    assertTrue(
        "new network connectors",
        Wait.waitFor(
            new Wait.Condition() {
              @Override
              public boolean isSatisified() throws Exception {
                return 1 == brokerService.getNetworkConnectors().size();
              }
            }));

    NetworkConnector networkConnector = brokerService.getNetworkConnectors().get(0);
    javaConfigBroker.addNetworkConnector(nc);
    TimeUnit.SECONDS.sleep(SLEEP);
    assertEquals("no new network connectors", 1, brokerService.getNetworkConnectors().size());
    assertSame("same instance", networkConnector, brokerService.getNetworkConnectors().get(0));

    // verify nested elements
    assertEquals("has exclusions", 2, networkConnector.getExcludedDestinations().size());

    assertEquals(
        "one statically included", 1, networkConnector.getStaticallyIncludedDestinations().size());
    assertEquals(
        "one dynamically included",
        1,
        networkConnector.getDynamicallyIncludedDestinations().size());
    assertEquals("one durable", 1, networkConnector.getDurableDestinations().size());
  }
  @Test
  public void testRemove() throws Exception {
    final BrokerService brokerService = new BrokerService();
    startBroker(brokerService);
    assertTrue("broker alive", brokerService.isStarted());
    assertEquals("no network connectors", 0, brokerService.getNetworkConnectors().size());

    DiscoveryNetworkConnector nc1 = new DiscoveryNetworkConnector();
    nc1.setUri(new URI("static:(tcp://localhost:5555)"));
    nc1.setNetworkTTL(1);
    nc1.setName("one");

    DiscoveryNetworkConnector nc2 = new DiscoveryNetworkConnector();
    nc2.setUri(new URI("static:(tcp://localhost:5555)"));
    nc2.setNetworkTTL(1);
    nc2.setName("one");

    javaConfigBroker.addNetworkConnector(nc1);
    javaConfigBroker.addNetworkConnector(nc2);

    TimeUnit.SECONDS.sleep(SLEEP);
    assertEquals("correct network connectors", 2, brokerService.getNetworkConnectors().size());

    javaConfigBroker.removeNetworkConnector(nc2);

    assertTrue(
        "expected mod on time",
        Wait.waitFor(
            new Wait.Condition() {
              @Override
              public boolean isSatisified() throws Exception {
                return 1 == brokerService.getNetworkConnectors().size();
              }
            }));

    NetworkConnector remainingNetworkConnector = brokerService.getNetworkConnectors().get(0);
    assertEquals("name match", "one", remainingNetworkConnector.getName());
  }
  private BrokerService createBrokerService(
      final String brokerName, final String uri1, final String uri2) throws Exception {
    final BrokerService brokerService = new BrokerService();

    brokerService.setBrokerName(brokerName);
    brokerService.setPersistent(false);
    brokerService.setUseJmx(true);

    final SystemUsage memoryManager = new SystemUsage();
    // memoryManager.getMemoryUsage().setLimit(10);
    brokerService.setSystemUsage(memoryManager);

    final List<PolicyEntry> policyEntries = new ArrayList<>();

    final PolicyEntry entry = new PolicyEntry();
    entry.setQueue(">");
    // entry.setMemoryLimit(1);
    policyEntries.add(entry);

    final PolicyMap policyMap = new PolicyMap();
    policyMap.setPolicyEntries(policyEntries);
    brokerService.setDestinationPolicy(policyMap);

    final TransportConnector tConnector = new TransportConnector();
    tConnector.setUri(new URI(uri1));
    tConnector.setName(brokerName + ".transportConnector");
    brokerService.addConnector(tConnector);

    if (uri2 != null) {
      final NetworkConnector nc = new DiscoveryNetworkConnector(new URI("static:" + uri2));
      nc.setBridgeTempDestinations(true);
      nc.setBrokerName(brokerName);
      // nc.setPrefetchSize(1);
      brokerService.addNetworkConnector(nc);
    }

    return brokerService;
  }
 private void bridgeAndConfigureBrokers(String local, String remote) throws Exception {
   NetworkConnector bridge = bridgeBrokers(local, remote, false, 1, false);
   bridge.setDecreaseNetworkConsumerPriority(true);
   bridge.setDuplex(true);
 }
 private void bridgeBroker(BrokerService localBroker, String remoteURI) throws Exception {
   String uri = "static:(" + remoteURI + ")";
   NetworkConnector connector = new DiscoveryNetworkConnector(new URI(uri));
   connector.setName("bridge-" + bridgeCount++);
   localBroker.addNetworkConnector(connector);
 }