public void safeDestroy() {
    if (rootContext != null) {
      // deactivate
      rootContext.deactivate();
      factory.getStateCache().removeContext(contextName);

      for (Map.Entry<Integer, ContextControllerTreeAgentInstanceList> entryCP :
          agentInstances.entrySet()) {
        StatementAgentInstanceUtil.stopAgentInstances(
            entryCP.getValue().getAgentInstances(), null, servicesContext, true);
      }
      agentInstances.clear();
      contextPartitionIdManager.clear();
      statements.clear();
    }
  }
 private Collection<Integer> getAgentInstancesForSelector(ContextPartitionSelector selector) {
   Collection<Integer> agentInstanceIds;
   if (selector instanceof ContextPartitionSelectorById) {
     ContextPartitionSelectorById byId = (ContextPartitionSelectorById) selector;
     Set<Integer> ids = byId.getContextPartitionIds();
     if (ids == null || ids.isEmpty()) {
       return Collections.emptyList();
     }
     agentInstanceIds = new ArrayList<Integer>(ids);
   } else if (selector instanceof ContextPartitionSelectorAll) {
     agentInstanceIds = new ArrayList<Integer>(agentInstances.keySet());
   } else {
     agentInstanceIds = rootContext.getSelectedContextPartitionPathIds(selector);
     if (agentInstanceIds == null || agentInstanceIds.isEmpty()) {
       return Collections.emptyList();
     }
   }
   return agentInstanceIds;
 }
  private AgentInstance startStatement(
      int contextId,
      ContextControllerStatementDesc statementDesc,
      ContextController originator,
      Object partitionKey,
      Map<String, Object> contextProperties,
      boolean isRecoveringResilient) {

    // build filters
    IdentityHashMap<FilterSpecCompiled, List<FilterValueSetParam>> filterAddendum =
        new IdentityHashMap<FilterSpecCompiled, List<FilterValueSetParam>>();
    originator
        .getFactory()
        .populateFilterAddendums(filterAddendum, statementDesc, partitionKey, contextId);
    AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(filterAddendum);

    // build built-in context properties
    contextProperties.put(ContextPropertyEventType.PROP_CTX_NAME, contextName);
    contextProperties.put(ContextPropertyEventType.PROP_CTX_ID, contextId);
    MappedEventBean contextBean =
        (MappedEventBean)
            servicesContext
                .getEventAdapterService()
                .adapterForTypedMap(
                    contextProperties,
                    contextDescriptor.getContextPropertyRegistry().getContextEventType());

    // activate
    StatementAgentInstanceFactoryResult result =
        StatementAgentInstanceUtil.start(
            servicesContext,
            statementDesc.getStatement(),
            false,
            contextId,
            contextBean,
            proxy,
            isRecoveringResilient);

    // save only instance data
    return new AgentInstance(
        result.getStopCallback(), result.getAgentInstanceContext(), result.getFinalView());
  }
 private void activate() {
   rootContext.activate(null, null, null, null);
 }
 public void setContextPartitionRange(List<NumberSetParameter> partitionRanges) {
   rootContext.setContextPartitionRange(partitionRanges);
 }