static {
    RULE_REGISTRY.addNamedTemplate(
        "rules",
        TemplateCompiler.compileTemplate(
            AbstractJavaProcessBuilder.class.getResourceAsStream("javaRule.mvel"), null));
    INVOKER_REGISTRY.addNamedTemplate(
        "invokers",
        TemplateCompiler.compileTemplate(
            AbstractJavaProcessBuilder.class.getResourceAsStream("javaInvokers.mvel"), null));

    /** Process these templates */
    TemplateRuntime.execute(RULE_REGISTRY.getNamedTemplate("rules"), null, RULE_REGISTRY);
    TemplateRuntime.execute(INVOKER_REGISTRY.getNamedTemplate("invokers"), null, INVOKER_REGISTRY);
  }
Example #2
0
 /**
  * Generates a dynamic description for the given policy and stores the result on the policy bean
  * instance. This should be done prior to returning the policybean back to the user for a REST
  * call to the management API.
  *
  * @param policy the policy
  * @throws Exception any exception
  */
 public static void generatePolicyDescription(PolicyBean policy) throws Exception {
   PolicyDefinitionBean def = policy.getDefinition();
   PolicyDefinitionTemplateBean templateBean = getTemplateBean(def);
   if (templateBean == null) {
     return;
   }
   String cacheKey = def.getId() + "::" + templateBean.getLanguage(); // $NON-NLS-1$
   CompiledTemplate template = templateCache.get(cacheKey);
   if (template == null) {
     template = TemplateCompiler.compileTemplate(templateBean.getTemplate());
     templateCache.put(cacheKey, template);
   }
   try {
     // TODO hack to fix broken descriptions - this util should probably not know about encrypted
     // data
     String jsonConfig = AesEncrypter.decrypt(policy.getConfiguration());
     Map<String, Object> configMap = mapper.readValue(jsonConfig, Map.class);
     configMap = new PolicyConfigMap(configMap);
     String desc = (String) TemplateRuntime.execute(template, configMap);
     policy.setDescription(desc);
   } catch (Exception e) {
     // TODO log the error
     policy.setDescription(templateBean.getTemplate());
   }
 }
  @PostConstruct
  protected void init() {
    formTemplate =
        TemplateCompiler.compileTemplate(getClass().getResourceAsStream(formTemplatePath));

    for (InputTemplateProvider provider : providers) {
      provider.registerTemplates(registry);
    }
  }
  /**
   * Creates a representation of the profile based on the assigned item for the specified
   * {@linkTaskContext}.
   *
   * @param context
   * @return
   */
  private ProfileData createProfileData(TaskContext context) {
    ProfileData profileData = new ProfileData();
    Set<WorkItem> workItems = assignedWorkItems.get(context);
    if (workItems.isEmpty()) {
      return profileData;
    }

    Container current = fabricService.get().getCurrentContainer();
    Version version = current.getVersion();
    String templateProfileName =
        String.valueOf(context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME));
    Profile templateProfile = version.getProfile(templateProfileName);
    Set<String> allFiles = templateProfile.getFileConfigurations().keySet();
    Iterable<String> mvelFiles = Iterables.filter(allFiles, MvelPredicate.INSTANCE);
    Iterable<String> plainFiles =
        Iterables.filter(allFiles, Predicates.not(MvelPredicate.INSTANCE));

    for (String mvelFile : mvelFiles) {
      Key key = new Key(templateProfile.getId(), mvelFile);
      synchronized (templates) {
        CompiledTemplate template = templates.get(key);
        if (template == null) {
          template =
              TemplateCompiler.compileTemplate(
                  new String(templateProfile.getFileConfigurations().get(mvelFile)), parserContext);
          templates.put(key, template);
        }
      }
    }

    for (WorkItem workItem : workItems) {
      Map<String, WorkItem> data = new HashMap<String, WorkItem>();
      data.put(WorkItem.ITEM, workItem);

      // Render templates
      for (String fileTemplate : mvelFiles) {
        String file = renderTemplateName(fileTemplate, workItem);
        Key key = new Key(templateProfile.getId(), fileTemplate);
        try {
          String renderedTemplate =
              TemplateRuntime.execute(templates.get(key), parserContext, data).toString();
          updateProfileData(file, renderedTemplate, profileData);
        } catch (Exception ex) {
          LOGGER.warn("Failed to render {}. Ignoring.", fileTemplate);
        }
      }

      // Copy plain files.
      for (String file : plainFiles) {
        String content = new String(templateProfile.getFileConfigurations().get(file));
        updateProfileData(file, content, profileData);
      }
    }
    return profileData;
  }
Example #5
0
  public String init(
      TypeOracle oracle,
      JField targetWidgetField,
      JType targetType,
      JField targetEntityMember,
      JField targetEntityField,
      String variable,
      List<JField> fields) {
    JClassType widgetCollectionType = targetWidgetField.getType().isClassOrInterface();
    JClassType entityCollectionType = targetEntityMember.getType().isClassOrInterface();

    JParameterizedType paramaterizedType = widgetCollectionType.isParameterized();

    if (paramaterizedType == null) {
      throw new RuntimeException(
          "cannot generateGetField mappers for collection of widgets (the collection is not properly parameterized: eg. List<Widget>)");
    }

    JClassType widgetType = paramaterizedType.getTypeArgs()[0];
    varName =
        targetEntityField.getType().isClassOrInterface().getName()
            + targetWidgetField.getName()
            + "Mapper";

    if (compiledTemplate == null) {
      InputStream istream = this.getClass().getResourceAsStream("CollectionFMGenerator.mv");
      compiledTemplate = TemplateCompiler.compileTemplate(istream, null);
    }

    Map vars = new HashMap();
    vars.put("typeOracle", oracle);
    vars.put("targetWidgetField", targetWidgetField);
    vars.put("targetEntityField", targetEntityField);
    vars.put("targetEntityMember", targetEntityMember);
    vars.put("widgetType", widgetType);
    vars.put("entityCollectionType", entityCollectionType);
    vars.put("widgetCollectionType", widgetCollectionType);
    vars.put("varName", varName);

    return String.valueOf(TemplateRuntime.execute(compiledTemplate, vars));
  }