private ServiceResponse<KieScannerResource> startScanner(
     String id, KieScannerResource resource, KieContainerInstance kci) {
   if (kci.getScanner() == null) {
     ServiceResponse<KieScannerResource> response = createScanner(id, kci);
     if (ResponseType.FAILURE.equals(response.getType())) {
       return response;
     }
   }
   if (KieScannerStatus.STOPPED.equals(mapStatus(kci.getScanner().getStatus()))
       && resource.getPollInterval() != null) {
     kci.getScanner().start(resource.getPollInterval());
     return new ServiceResponse<KieScannerResource>(
         ServiceResponse.ResponseType.SUCCESS,
         "Kie scanner successfuly created.",
         new KieScannerResource(mapStatus(kci.getScanner().getStatus())));
   } else if (!KieScannerStatus.STOPPED.equals(mapStatus(kci.getScanner().getStatus()))) {
     return new ServiceResponse<KieScannerResource>(
         ServiceResponse.ResponseType.FAILURE,
         "Invalid kie scanner status: " + mapStatus(kci.getScanner().getStatus()),
         new KieScannerResource(mapStatus(kci.getScanner().getStatus())));
   } else if (resource.getPollInterval() == null) {
     return new ServiceResponse<KieScannerResource>(
         ServiceResponse.ResponseType.FAILURE,
         "Invalid polling interval: " + resource.getPollInterval(),
         new KieScannerResource(mapStatus(kci.getScanner().getStatus())));
   }
   return new ServiceResponse<KieScannerResource>(
       ServiceResponse.ResponseType.FAILURE,
       "Unknown error starting scanner. Scanner was not started." + resource,
       new KieScannerResource(mapStatus(kci.getScanner().getStatus())));
 }
 @Override
 public ServiceResponse<ExecutionResults> executeCommandsWithResults(String id, Command<?> cmd) {
   if (config.isRest()) {
     return makeHttpPostRequestAndCreateServiceResponse(
         loadBalancer.getUrl() + "/containers/instances/" + id,
         cmd,
         (Class) ExecutionResultImpl.class,
         getHeaders(cmd));
   } else {
     CommandScript script =
         new CommandScript(
             Collections.singletonList(
                 (KieServerCommand) new CallContainerCommand(id, serialize(cmd))));
     ServiceResponse response =
         executeJmsCommand(script, cmd.getClass().getName(), null, id).getResponses().get(0);
     if (shouldReturnWithNullResponse(response)) {
       return null;
     }
     if (response.getResult() instanceof String) {
       response.setResult(
           deserialize((String) response.getResult(), (Class) ExecutionResultImpl.class));
     }
     return response;
   }
 }
 protected void disposeAllContainers() {
   ServiceResponse<KieContainerResourceList> response = client.listContainers();
   Assert.assertEquals(ServiceResponse.ResponseType.SUCCESS, response.getType());
   List<KieContainerResource> containers = response.getResult().getContainers();
   if (containers != null) {
     for (KieContainerResource container : containers) {
       client.disposeContainer(container.getContainerId());
     }
   }
 }
  @Test
  public void testExecuteSpreadsheetRule() {
    Object person = createInstance(PERSON_CLASS_NAME, PERSON_AGE);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands);

    commands.add(commandsFactory.newInsert(person, PERSON_OUT_IDENTIFIER));
    commands.add(commandsFactory.newFireAllRules());

    ServiceResponse<ExecutionResults> response =
        ruleClient.executeCommandsWithResults(CONTAINER_ID, batchExecution);
    KieServerAssert.assertSuccess(response);
    ExecutionResults results = response.getResult();
    Object value = results.getValue(PERSON_OUT_IDENTIFIER);
    assertEquals(Boolean.TRUE, valueOf(value, PERSON_CAN_BUY_ALCOHOL_FIELD));
  }
 protected static void assertSuccess(ServiceResponse<?> response) {
   ServiceResponse.ResponseType type = response.getType();
   assertEquals(
       "Expected SUCCESS, but got " + type + "! Response: " + response,
       ServiceResponse.ResponseType.SUCCESS,
       type);
 }
 @Override
 public String call() {
   String payload =
       "<batch-execution lookup=\"kbase1.stateless\">\n"
           + "  <insert out-identifier=\"person1\">\n"
           + "    <org.kie.server.testing.Person>\n"
           + "      <firstname>Darth</firstname>\n"
           + "    </org.kie.server.testing.Person>\n"
           + "  </insert>\n"
           + "</batch-execution>";
   long threadId = Thread.currentThread().getId();
   ServiceResponse<String> reply;
   for (int i = 0; i < NR_OF_REQUESTS_PER_THREAD; i++) {
     logger.trace("Container call #{}, thread-id={}", i, threadId);
     reply = client.executeCommands(CONTAINER_ID, payload);
     logger.trace("Container reply for request #{}: {}, thread-id={}", i, reply, threadId);
     assertEquals(ServiceResponse.ResponseType.SUCCESS, reply.getType());
   }
   return "SUCCESS";
 }
  @Test
  public void testUseClassWithSameFQNInDifferentContainers() throws Exception {
    assertSuccess(
        client.createContainer(CONTAINER_1_ID, new KieContainerResource(CONTAINER_1_ID, kjar1)));

    Object person = createInstance(PERSON_CLASS_NAME);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    BatchExecutionCommand batchExecution1 =
        commandsFactory.newBatchExecution(commands, KIE_SESSION_1);

    commands.add(commandsFactory.newInsert(person, PERSON_OUT_IDENTIFIER));
    commands.add(commandsFactory.newFireAllRules());

    ServiceResponse<String> response1 = ruleClient.executeCommands(CONTAINER_1_ID, batchExecution1);
    assertSuccess(response1);
    String result1 = response1.getResult();
    assertTrue(
        "Person's id should be 'Person from kjar1'!. Got result: " + result1,
        result1.contains("Person from kjar1"));

    // now execute the same commands, but for the second container. The rule in there should set
    // different id
    // (namely "Person from kjar2") for the inserted person
    assertSuccess(
        client.createContainer(CONTAINER_2_ID, new KieContainerResource(CONTAINER_2_ID, kjar2)));

    person = createInstance(PERSON_CLASS_NAME);
    commands = new ArrayList<Command<?>>();
    BatchExecutionCommand batchExecution2 =
        commandsFactory.newBatchExecution(commands, KIE_SESSION_2);

    commands.add(commandsFactory.newInsert(person, PERSON_OUT_IDENTIFIER));
    commands.add(commandsFactory.newFireAllRules());

    ServiceResponse<String> response2 = ruleClient.executeCommands(CONTAINER_2_ID, batchExecution2);
    assertSuccess(response2);
    String result2 = response2.getResult();
    assertTrue(
        "Person's id should be 'Person from kjar2'!. Got result: " + result2,
        result2.contains("Person from kjar2"));
  }
 private ServiceResponse<KieScannerResource> disposeScanner(
     String id, KieScannerResource resource, KieContainerInstance kci) {
   if (kci.getScanner() == null) {
     return new ServiceResponse<KieScannerResource>(
         ServiceResponse.ResponseType.SUCCESS,
         "Invalid call. Scanner already disposed. ",
         new KieScannerResource(KieScannerStatus.DISPOSED));
   }
   if (KieScannerStatus.STARTED.equals(mapStatus(kci.getScanner().getStatus()))
       || KieScannerStatus.SCANNING.equals(mapStatus(kci.getScanner().getStatus()))) {
     ServiceResponse<KieScannerResource> response = stopScanner(id, resource, kci);
     if (ResponseType.FAILURE.equals(response.getType())) {
       return response;
     }
   }
   kci.getScanner().shutdown();
   kci.setScanner(null);
   return new ServiceResponse<KieScannerResource>(
       ServiceResponse.ResponseType.SUCCESS,
       "Kie scanner successfuly shutdown.",
       new KieScannerResource(KieScannerStatus.DISPOSED));
 }