예제 #1
0
  /**
   * Creates default configuration of <code>RuntimeManager</code> with given <code>strategy</code>
   * and all <code>resources</code> being added to knowledge base. <br>
   * There should be only one <code>RuntimeManager</code> created during single test.
   *
   * @param strategy - selected strategy of those that are supported
   * @param resources - resources that shall be added to knowledge base
   * @param identifier - identifies the runtime manager
   * @return new instance of RuntimeManager
   */
  protected RuntimeManager createRuntimeManager(
      Strategy strategy, Map<String, ResourceType> resources, String identifier) {
    if (manager != null) {
      throw new IllegalStateException("There is already one RuntimeManager active");
    }

    RuntimeEnvironmentBuilder builder = null;
    if (!setupDataSource) {
      builder =
          RuntimeEnvironmentBuilder.getEmpty()
              .addConfiguration(
                  "drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName())
              .addConfiguration(
                  "drools.processInstanceManagerFactory",
                  DefaultProcessInstanceManagerFactory.class.getName());
    } else if (sessionPersistence) {
      builder = RuntimeEnvironmentBuilder.getDefault().entityManagerFactory(emf);
    } else {
      builder = RuntimeEnvironmentBuilder.getDefaultInMemory();
    }
    builder.userGroupCallback(new JBossUserGroupCallbackImpl("classpath:/usergroups.properties"));

    for (Map.Entry<String, ResourceType> entry : resources.entrySet()) {
      builder.addAsset(ResourceFactory.newClassPathResource(entry.getKey()), entry.getValue());
    }

    return createRuntimeManager(strategy, resources, builder.get(), identifier);
  }
예제 #2
0
  /**
   * Creates default configuration of <code>RuntimeManager</code> with given <code>strategy</code>
   * and all <code>resources</code> being added to knowledge base. <br>
   * There should be only one <code>RuntimeManager</code> created during single test.
   *
   * @param strategy - selected strategy of those that are supported
   * @param resources - resources that shall be added to knowledge base
   * @return new instance of RuntimeManager
   */
  protected RuntimeManager createRuntimeManager(
      Strategy strategy, Map<String, ResourceType> resources) {
    if (manager != null) {
      throw new IllegalStateException("There is already one RuntimeManager active");
    }

    RuntimeEnvironmentBuilder builder = null;
    if (!setupDataSource) {
      builder =
          RuntimeEnvironmentBuilder.getEmpty()
              .addConfiguration(
                  "drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName())
              .addConfiguration(
                  "drools.processInstanceManagerFactory",
                  DefaultProcessInstanceManagerFactory.class.getName());
    } else if (sessionPersistence) {
      builder = RuntimeEnvironmentBuilder.getDefault().entityManagerFactory(emf);
    } else {
      builder = RuntimeEnvironmentBuilder.getDefaultInMemory();
    }
    builder.userGroupCallback(new JBossUserGroupCallbackImpl("classpath:/usergroups.properties"));

    for (Map.Entry<String, ResourceType> entry : resources.entrySet()) {
      builder.addAsset(ResourceFactory.newClassPathResource(entry.getKey()), entry.getValue());
    }
    switch (strategy) {
      case SINGLETON:
        manager = managerFactory.newSingletonRuntimeManager(builder.get());
        break;
      case REQUEST:
        manager = managerFactory.newPerRequestRuntimeManager(builder.get());
        break;
      case PROCESS_INSTANCE:
        manager = managerFactory.newPerProcessInstanceRuntimeManager(builder.get());
        break;
      default:
        manager = managerFactory.newSingletonRuntimeManager(builder.get());
        break;
    }

    return manager;
  }
예제 #3
0
  @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());
  }