@Override
  protected RegisterableItemsFactory getRegisterableItemsFactory(
      AuditEventBuilder auditLoggerBuilder, KieContainer kieContainer, KModuleDeploymentUnit unit) {

    return InjectableRegisterableItemsFactory.getFactory(
        beanManager, auditLoggerBuilder, kieContainer, unit.getKsessionName());
  }
 protected RegisterableItemsFactory getRegisterableItemsFactory(
     AuditEventBuilder auditLoggerBuilder, KieContainer kieContainer, KModuleDeploymentUnit unit) {
   KModuleRegisterableItemsFactory factory =
       new KModuleRegisterableItemsFactory(kieContainer, unit.getKsessionName());
   factory.setAuditBuilder(auditLoggerBuilder);
   return factory;
 }
  @Test
  public void deploymentObjectsTest() throws Exception {
    Assume.assumeFalse(getType().equals(TestType.YAML));

    // for test at end, fill during test
    JaxbDeploymentUnitList depUnitList = new JaxbDeploymentUnitList();

    // dep jobs
    JaxbDeploymentJobResult jaxbJob = new JaxbDeploymentJobResult();
    testRoundTrip(jaxbJob);

    // complex dep jobs
    KModuleDeploymentUnit kDepUnit =
        new KModuleDeploymentUnit("org", "jar", "1.0", "kbase", "ksession");
    kDepUnit.setStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE);

    JaxbDeploymentUnit depUnit =
        new JaxbDeploymentUnit(
            kDepUnit.getGroupId(), kDepUnit.getArtifactId(), kDepUnit.getArtifactId());
    depUnit.setKbaseName(kDepUnit.getKbaseName());
    depUnit.setKsessionName(kDepUnit.getKsessionName());
    depUnit.setStrategy(kDepUnit.getStrategy());
    depUnit.setStatus(JaxbDeploymentStatus.NONEXISTENT);
    depUnitList.getDeploymentUnitList().add(depUnit);

    jaxbJob = new JaxbDeploymentJobResult(null, "test", depUnit, "deploy");
    jaxbJob.setIdentifier(23L);
    jaxbJob.setSuccess(false);
    JaxbDeploymentJobResult copyJaxbJob = testRoundTrip(jaxbJob);
    ComparePair.compareObjectsViaFields(jaxbJob, copyJaxbJob, "jobId", "identifier");

    depUnit = new JaxbDeploymentUnit("g", "a", "v");
    depUnit.setKbaseName("kbase");
    depUnit.setKsessionName("ksession");
    depUnit.setStatus(JaxbDeploymentStatus.DEPLOY_FAILED);
    depUnit.setStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE);
    depUnitList.getDeploymentUnitList().add(depUnit);

    JaxbDeploymentUnit copyDepUnit = testRoundTrip(depUnit);

    ComparePair.compareObjectsViaFields(depUnit, copyDepUnit, "identifier");

    JaxbDeploymentJobResult depJob =
        new JaxbDeploymentJobResult(null, "testing stuff", copyDepUnit, "test");
    depJob.setSuccess(true);
    JaxbDeploymentJobResult copyDepJob = testRoundTrip(depJob);

    ComparePair.compareObjectsViaFields(copyDepJob, depJob, "jobId", "identifier");

    JaxbDeploymentUnitList roundTripUnitList = testRoundTrip(depUnitList);
    ComparePair.compareObjectsViaFields(
        depUnitList.getDeploymentUnitList().get(0),
        roundTripUnitList.getDeploymentUnitList().get(0),
        "jobId",
        "identifier");
  }
  @Override
  public void deploy(DeploymentUnit unit) {
    super.deploy(unit);
    if (!(unit instanceof KModuleDeploymentUnit)) {
      throw new IllegalArgumentException(
          "Invalid deployment unit provided - " + unit.getClass().getName());
    }
    KModuleDeploymentUnit kmoduleUnit = (KModuleDeploymentUnit) unit;
    DeployedUnitImpl deployedUnit = new DeployedUnitImpl(unit);
    KieServices ks = KieServices.Factory.get();
    MavenRepository repository = getMavenRepository();
    repository.resolveArtifact(kmoduleUnit.getIdentifier());

    ReleaseId releaseId =
        ks.newReleaseId(
            kmoduleUnit.getGroupId(), kmoduleUnit.getArtifactId(), kmoduleUnit.getVersion());
    KieContainer kieContainer = ks.newKieContainer(releaseId);

    String kbaseName = kmoduleUnit.getKbaseName();
    if (StringUtils.isEmpty(kbaseName)) {
      KieBaseModel defaultKBaseModel =
          ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
      if (defaultKBaseModel != null) {
        kbaseName = defaultKBaseModel.getName();
      } else {
        kbaseName = DEFAULT_KBASE_NAME;
      }
    }
    InternalKieModule module =
        (InternalKieModule) ((KieContainerImpl) kieContainer).getKieModuleForKBase(kbaseName);
    if (module == null) {
      throw new IllegalStateException("Cannot find kbase with name " + kbaseName);
    }

    Map<String, String> formsData = new HashMap<String, String>();
    Collection<String> files = module.getFileNames();
    for (String fileName : files) {
      if (fileName.matches(".+bpmn[2]?$")) {
        ProcessDesc process;
        try {
          String processString = new String(module.getBytes(fileName), "UTF-8");
          process = bpmn2Service.findProcessId(processString, kieContainer.getClassLoader());
          process.setEncodedProcessSource(Base64.encodeBase64String(processString.getBytes()));
          process.setDeploymentId(unit.getIdentifier());
          process.setForms(formsData);
          deployedUnit.addAssetLocation(process.getId(), process);
        } catch (UnsupportedEncodingException e) {
          logger.warn("Unable to load content for file '{}' : {}", fileName, e);
        }
      } else if (fileName.matches(".+ftl$")) {
        try {
          String formContent = new String(module.getBytes(fileName), "UTF-8");
          Pattern regex = Pattern.compile("(.{0}|.*/)([^/]*?)\\.ftl");
          Matcher m = regex.matcher(fileName);
          String key = fileName;
          while (m.find()) {
            key = m.group(2);
          }
          formsData.put(key, formContent);
        } catch (UnsupportedEncodingException e) {
          logger.warn("Unable to load content for form '{}' : {}", fileName, e);
        }
      } else if (fileName.matches(".+form$")) {
        try {
          String formContent = new String(module.getBytes(fileName), "UTF-8");
          Pattern regex = Pattern.compile("(.{0}|.*/)([^/]*?)\\.form");
          Matcher m = regex.matcher(fileName);
          String key = fileName;
          while (m.find()) {
            key = m.group(2);
          }
          formsData.put(key + ".form", formContent);
        } catch (UnsupportedEncodingException e) {
          logger.warn("Unable to load content for form '{}' : {}", fileName, e);
        }
      }
    }

    KieBase kbase = kieContainer.getKieBase(kbaseName);

    AbstractAuditLogger auditLogger = getAuditLogger();
    ServicesAwareAuditEventBuilder auditEventBuilder = new ServicesAwareAuditEventBuilder();
    auditEventBuilder.setIdentityProvider(identityProvider);
    auditEventBuilder.setDeploymentUnitId(unit.getIdentifier());
    auditLogger.setBuilder(auditEventBuilder);

    RuntimeEnvironmentBuilder builder =
        RuntimeEnvironmentBuilder.getDefault()
            .entityManagerFactory(getEmf())
            .knowledgeBase(kbase)
            .classLoader(kieContainer.getClassLoader());
    if (beanManager != null) {
      builder.registerableItemsFactory(
          InjectableRegisterableItemsFactory.getFactory(
              beanManager, auditLogger, kieContainer, kmoduleUnit.getKsessionName()));
    }
    commonDeploy(unit, deployedUnit, builder.get());
  }