private static DataFormatDefinition processDataFormatType(
      RouteContext routeContext, String ref, DataFormatDefinition dformatDefinition) {
    if (dformatDefinition == null) {
      if ("json".equals(ref)) {
        dformatDefinition = new XStreamDataFormat();
        ((XStreamDataFormat) dformatDefinition).setDriver("json");
      } else if ("xstream".equals(ref)) {
        dformatDefinition = new XStreamDataFormat();
      } else if ("jaxb".equals(ref)) {
        dformatDefinition = new JaxbDataFormat();
      } else {
        dformatDefinition = routeContext.getCamelContext().resolveDataFormatDefinition(ref);
      }
    }

    // always clone before changing
    dformatDefinition = new FastCloner().deepClone(dformatDefinition);

    if (dformatDefinition instanceof JaxbDataFormat) {
      dformatDefinition = augmentJaxbDataFormatDefinition((JaxbDataFormat) dformatDefinition);
    } else if (dformatDefinition instanceof XStreamDataFormat) {
      XStreamDataFormat xstreamDataFormat = (XStreamDataFormat) dformatDefinition;
      if ("json".equals(xstreamDataFormat.getDriver())) {
        dformatDefinition = XStreamJson.newJSonMarshaller(xstreamDataFormat);
        ;
      } else {
        dformatDefinition = XStreamXml.newXStreamMarshaller((XStreamDataFormat) dformatDefinition);
      }
    }
    return dformatDefinition;
  }
  private void restoreContainers() {
    DirectoryStream<Path> ds = null;
    try {
      Path containersPath = fs.getPath("/containers");
      if (ios.exists(containersPath)) {
        ds =
            ios.newDirectoryStream(
                containersPath,
                new Filter<Path>() {

                  @Override
                  public boolean accept(Path entry) throws IOException {
                    return Files.isDirectory(entry);
                  }
                });
        if (ds != null) {
          XStream xs = XStreamXml.newXStreamMarshaller(KieServerImpl.class.getClassLoader());
          for (Path entry : ds) {
            BufferedReader reader = null;
            try {
              logger.info("Restoring state of kie container '" + entry.getFileName() + "'");
              reader =
                  Files.newBufferedReader(
                      entry.resolve(CONTAINER_STATE_FILE), Charset.forName("UTF-8"));
              KieContainerResource resource = (KieContainerResource) xs.fromXML(reader);
              restore(resource);
            } catch (Exception e) {
              logger.error("Error restoring kie container state", e);
            } finally {
              if (reader != null) {
                try {
                  reader.close();
                } catch (java.io.IOException e) {
                }
              }
            }
          }
        }
      }
    } catch (Exception e) {
      logger.error("Error restoring kie server state", e);
    } finally {
      if (ds != null) ds.close();
    }
  }
 private void persistContainer(KieContainerInstance ci) {
   if (fs != null) {
     BufferedWriter writer = null;
     try {
       logger.info("Persisting state for kie container '" + ci.getContainerId() + "'");
       XStream xs = XStreamXml.newXStreamMarshaller(KieServerImpl.class.getClassLoader());
       Path file = fs.getPath("/containers/" + ci.getContainerId() + "/" + CONTAINER_STATE_FILE);
       writer = Files.newBufferedWriter(file, Charset.forName("UTF-8"));
       xs.toXML(ci.getResource(), writer);
     } catch (Exception e) {
       logger.error("Error persisting state for kie container '" + ci.getContainerId() + "'", e);
     } finally {
       if (writer != null) {
         try {
           writer.close();
         } catch (java.io.IOException e) {
         }
       }
     }
   }
 }
  public ServiceResponse<String> callContainer(String containerId, String payload) {
    if (payload == null) {
      return new ServiceResponse<String>(
          ServiceResponse.ResponseType.FAILURE,
          "Error calling container " + containerId + ". Empty payload. ");
    }
    try {
      KieContainerInstance kci = (KieContainerInstance) context.getContainer(containerId);
      // the following code is subject to a concurrent call to dispose(), but the cost of
      // synchronizing it
      // would likely not be worth it. At this point a decision was made to fail the execution if a
      // concurrent
      // call do dispose() is executed.
      if (kci != null && kci.getKieContainer() != null) {
        String sessionId = null;
        // this is a weak way of finding the lookup, but it is the same used in kie-camel. Will keep
        // it for now.
        Matcher m = LOOKUP.matcher(payload);
        if (m.find()) {
          sessionId = m.group(1);
        }

        KieSession ks = null;
        if (sessionId != null) {
          ks = kci.getKieContainer().getKieSession(sessionId);
        } else {
          ks = kci.getKieContainer().getKieSession();
        }
        if (ks != null) {
          ClassLoader moduleClassLoader = kci.getKieContainer().getClassLoader();
          XStream xs = XStreamXml.newXStreamMarshaller(moduleClassLoader);
          Command<?> cmd = (Command<?>) xs.fromXML(payload);

          if (cmd == null) {
            return new ServiceResponse<String>(
                ServiceResponse.ResponseType.FAILURE,
                "Body of in message not of the expected type '" + Command.class.getName() + "'");
          }
          if (!(cmd instanceof BatchExecutionCommandImpl)) {
            cmd =
                new BatchExecutionCommandImpl(
                    Arrays.asList(new GenericCommand<?>[] {(GenericCommand<?>) cmd}));
          }

          ExecutionResults results = ks.execute((BatchExecutionCommandImpl) cmd);
          String result = xs.toXML(results);
          return new ServiceResponse<String>(
              ServiceResponse.ResponseType.SUCCESS,
              "Container " + containerId + " successfully called.",
              result);
        } else {
          return new ServiceResponse<String>(
              ServiceResponse.ResponseType.FAILURE,
              "Session '" + sessionId + "' not found on container '" + containerId + "'.");
        }
      } else {
        return new ServiceResponse<String>(
            ServiceResponse.ResponseType.FAILURE,
            "Container " + containerId + " is not instantiated.");
      }
    } catch (Exception e) {
      logger.error("Error calling container '" + containerId + "'", e);
      return new ServiceResponse<String>(
          ServiceResponse.ResponseType.FAILURE,
          "Error calling container "
              + containerId
              + ": "
              + e.getClass().getName()
              + ": "
              + e.getMessage());
    }
  }