private DatabusRegistration findRegistration(DatabusRequest request, String prefix)
      throws RequestProcessingException {
    String category = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
    String registrationIdStr = category.substring(prefix.length());
    RegistrationId regId = new RegistrationId(registrationIdStr);

    Collection<DatabusRegistration> regs = _client.getAllRegistrations();

    for (DatabusRegistration r : regs) {
      if (regId.equals(r.getRegistrationId())) return r;

      if (r instanceof DatabusMultiPartitionRegistration) {
        Map<DbusPartitionInfo, DatabusRegistration> childRegs =
            ((DatabusMultiPartitionRegistration) r).getPartitionRegs();
        for (Entry<DbusPartitionInfo, DatabusRegistration> e : childRegs.entrySet())
          if (regId.equals(e.getValue().getRegistrationId())) return e.getValue();
      }
    }
    throw new RequestProcessingException("Unable to find registration (" + regId + ") ");
  }
  @Override
  public synchronized void shutdown() throws IllegalStateException {
    if (!_state.isRunning())
      throw new IllegalStateException(
          "Registration ("
              + _id
              + ") is not in running state to be shutdown. Current state :"
              + _state);

    _sourcesConnection.unregisterMbeans();
    _sourcesConnection.stop();
    _status.shutdown();
    _state = RegistrationState.SHUTDOWN;

    // remove this registration stats from client stats Collector list.
    _client.getBootstrapEventsStats().removeStatsCollector(_id.getId());
    _client.getInBoundStatsCollectors().removeStatsCollector(_id.getId());
    _client.getRelayConsumerStatsCollectors().removeStatsCollector(_id.getId());
    _client.getBootstrapConsumerStatsCollectors().removeStatsCollector(_id.getId());
    _client.getUnifiedClientStatsCollectors().removeStatsCollector(_id.getId());
  }
  /**
   * Factory method to create sources connection
   *
   * @param connConfig
   * @param subs
   * @param candidateRelays
   * @param candidateBootstrapServers
   * @param eventBuffer
   * @param bootstrapBuffer
   * @return
   */
  protected synchronized DatabusSourcesConnection createConnection(
      StaticConfig connConfig,
      List<DatabusSubscription> subs,
      Set<ServerInfo> candidateRelays,
      Set<ServerInfo> candidateBootstrapServers,
      DbusEventBuffer eventBuffer,
      DbusEventBuffer bootstrapBuffer) {
    _log.info(
        "Creating Sources Connection : Candidate Relays :"
            + candidateRelays
            + ", CandidateBootstrapServers :"
            + candidateBootstrapServers
            + ", Subscriptions :"
            + subs);

    ConnectionStateFactory connStateFactory =
        new ConnectionStateFactory(DatabusSubscription.getStrList(subs));
    DatabusSourcesConnection sourcesConnection =
        new DatabusSourcesConnection(
            connConfig,
            subs,
            candidateRelays,
            candidateBootstrapServers,
            _streamConsumerRawRegistrations,
            _bootstrapConsumerRawRegistrations,
            eventBuffer,
            bootstrapBuffer,
            _client.getDefaultExecutorService(),
            _client.getContainerStatsCollector(),
            _inboundEventsStatsCollector,
            _bootstrapEventsStatsCollector,
            _relayConsumerStats,
            _bootstrapConsumerStats,
            _unifiedClientStats,
            _checkpointPersistenceProvider,
            _client.getRelayConnFactory(),
            _client.getBootstrapConnFactory(),
            _client.getHttpStatsCollector(),
            null, // This should make sure the checkpoint directory structure is compatible with V2.
            _client,
            _id.toString(), // Used to uniquely identify logs and mbean name
            _client.getEventFactory(),
            null,
            connStateFactory);
    return sourcesConnection;
  }
 // TODO:  make private?  no other Databus callers except two ctors above; is this a public
 // (external) API?
 public DatabusV2RegistrationImpl(
     RegistrationId id,
     DatabusHttpClientImpl client,
     CheckpointPersistenceProvider ckptProvider,
     String[] sources,
     AbstractDatabusCombinedConsumer[] consumers) {
   _id = id;
   _status = new Status();
   _client = client;
   _checkpointPersistenceProvider = ckptProvider;
   _state = RegistrationState.INIT;
   _sources = new ArrayList<String>();
   _consumers = new ArrayList<DatabusCombinedConsumer>();
   final String loggerName = (_id != null) ? _id.getId() : getClass().getName();
   _log = Logger.getLogger(loggerName);
   if (null != sources) _sources.addAll(Arrays.asList(sources));
   if (null != consumers) _consumers.addAll(Arrays.asList(consumers));
 }
  private RegInfo getRegistration(RegistrationId regId) throws RequestProcessingException {
    Collection<DatabusRegistration> regs = _client.getAllRegistrations();

    for (DatabusRegistration r : regs) {
      if (regId.equals(r.getRegistrationId())) {
        if (r instanceof DatabusMultiPartitionRegistration) {
          Map<DbusPartitionInfo, DatabusRegistration> childRegs =
              ((DatabusMultiPartitionRegistration) r).getPartitionRegs();
          Map<DbusPartitionInfo, RegInfo> childR = new HashMap<DbusPartitionInfo, RegInfo>();
          for (Entry<DbusPartitionInfo, DatabusRegistration> e : childRegs.entrySet()) {
            childR.put(
                e.getKey(),
                new RegInfo(
                    e.getValue().getState(),
                    e.getValue().getRegistrationId(),
                    e.getValue().getStatus(),
                    e.getValue().getFilterConfig(),
                    e.getValue().getSubscriptions()));
          }

          return new RegInfo(
              r.getState(),
              r.getRegistrationId(),
              r.getStatus(),
              r.getFilterConfig(),
              r.getSubscriptions(),
              true,
              childR);
        } else {
          return new RegInfo(
              r.getState(),
              r.getRegistrationId(),
              r.getStatus(),
              r.getFilterConfig(),
              r.getSubscriptions());
        }
      }
    }

    throw new RequestProcessingException("Unable to find regId (" + regId + ")");
  }
  @Override
  public synchronized DatabusRegistration withRegId(RegistrationId regId)
      throws DatabusClientException, IllegalStateException {
    if ((_id != null) && (_id.equals(regId))) return this;

    if (!RegistrationIdGenerator.isIdValid(regId))
      throw new DatabusClientException(
          "Another registration with the same regId (" + regId + ") already present !!");

    if (_state.isRunning())
      throw new IllegalStateException(
          "Cannot update regId when registration is in running state. RegId :"
              + _id
              + ", State :"
              + _state);

    _id = regId;
    _status = new Status(); // Component Status should use the correct component name

    return this;
  }
  /** Initialize Statistics Collectors */
  protected synchronized void initializeStatsCollectors() {
    MBeanServer mbeanServer = null;

    if (null != _client) {
      mbeanServer = _client.getMbeanServer();
    }

    int ownerId = null == _client ? -1 : _client.getContainerStaticConfig().getId();
    String regId = null != _id ? _id.getId() : "unknownReg";

    initializeStatsCollectors(regId, ownerId, mbeanServer);

    if (null != _client) {
      _client.getBootstrapEventsStats().addStatsCollector(regId, _bootstrapEventsStatsCollector);
      _client.getInBoundStatsCollectors().addStatsCollector(regId, _inboundEventsStatsCollector);
      _client.getRelayConsumerStatsCollectors().addStatsCollector(regId, _relayConsumerStats);
      _client
          .getBootstrapConsumerStatsCollectors()
          .addStatsCollector(regId, _bootstrapConsumerStats);
      _client.getUnifiedClientStatsCollectors().addStatsCollector(regId, _unifiedClientStats);
    }
  }
 protected synchronized String getStatusName() {
   return "Status" + ((_id != null) ? "_" + _id.getId() : "");
 }