private static Optional<StepFunction> mvel(String source) { try { final Serializable mvel = MVEL.compileExpression(source); MVEL.executeExpression(mvel, ImmutableMap.of("c", 1)); StepFunction step = new StepFunction() { public int step(int from) { return (Integer) MVEL.executeExpression(mvel, ImmutableMap.of("c", from)); } }; return Optional.of(step); } catch (Exception e) { return Optional.absent(); } }
private Map loadEnum(String mvelSource) { if (mvelSource == null || (mvelSource.trim().equals(""))) { return Collections.EMPTY_MAP; } if (mvelSource.startsWith("=")) { mvelSource = mvelSource.substring(1); } else { mvelSource = "[ " + addCommasForNewLines(mvelSource) + " ]"; } final Object mvelData; try { mvelData = MVEL.eval(mvelSource, new HashMap()); } catch (RuntimeException e) { addError("Unable to load enumeration data."); addError(e.getMessage()); addError("Error type: " + e.getClass().getName()); return Collections.EMPTY_MAP; } if (!(mvelData instanceof Map)) { addError("The expression is not a map, it is a " + mvelData.getClass().getName()); return Collections.EMPTY_MAP; } Map map = (Map) mvelData; Map newMap = new HashMap(); for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { String key = (String) iter.next(); Object list = map.get(key); if (!(list instanceof List || list instanceof String)) { if (list == null) { addError("The item with " + key + " is null."); } else { addError( "The item with " + key + " is not a list or a string, it is a " + list.getClass().getName()); } return new HashMap(); } else if (list instanceof String) { newMap.put(key, new String[] {(String) list}); } else { List items = (List) list; String[] newItems = new String[items.size()]; for (int i = 0; i < items.size(); i++) { Object listItem = items.get(i); if (!(listItem instanceof String)) { newItems[i] = listItem.toString(); } else { newItems[i] = (String) listItem; } } newMap.put(key, newItems); } } return newMap; }
public void testPackageWithRuleflow() throws Exception { RulesRepository repo = getRepo(); PackageItem pkg = repo.createPackage("testPackageWithRuleFlow", ""); AssetItem model = pkg.addAsset("model", "qed"); model.updateFormat(AssetFormats.MODEL); model.updateBinaryContentAttachment(this.getClass().getResourceAsStream("/billasurf.jar")); model.checkin(""); ServiceImplementation.updateDroolsHeader( "import com.billasurf.Board\n global com.billasurf.Person customer", pkg); AssetItem rule1 = pkg.addAsset("rule_1", ""); rule1.updateFormat(AssetFormats.DRL); rule1.updateContent("rule 'rule1' \n when Board() \n then customer.setAge(42); \n end"); rule1.checkin(""); AssetItem ruleFlow = pkg.addAsset("ruleFlow", ""); ruleFlow.updateFormat(AssetFormats.RULE_FLOW_RF); ruleFlow.updateBinaryContentAttachment(this.getClass().getResourceAsStream("/ruleflow.rfm")); ruleFlow.checkin(""); ContentPackageAssembler asm = new ContentPackageAssembler(pkg); assertFalse(asm.hasErrors()); Map flows = asm.getBinaryPackage().getRuleFlows(); assertNotNull(flows); assertEquals(1, flows.size()); Object flow = flows.values().iterator().next(); assertNotNull(flow); assertTrue(flow instanceof RuleFlowProcess); // now check we can do some MVEL stuff from the classloader... List<JarInputStream> jars = BRMSPackageBuilder.getJars(pkg); PackageBuilder builder = BRMSPackageBuilder.getInstance(jars); ClassLoader newCL = builder.getPackageBuilderConfiguration().getClassLoader(); ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); // set the CL for the current thread so MVEL can find it Thread.currentThread().setContextClassLoader(newCL); Object o = MVEL.eval("new com.billasurf.Board()"); assertEquals("com.billasurf.Board", o.getClass().getName()); System.err.println(o.toString()); Thread.currentThread().setContextClassLoader(oldCL); builder.addPackageFromDrl(new StringReader("package foo\n import com.billasurf.Board")); Object o2 = builder.getPackageRegistry("foo").getTypeResolver().resolveType("Board"); assertNotNull(o2); assertEquals("com.billasurf.Board", ((Class) o2).getName()); }
public Map insertFilter(Source source) { JavaResult result = new JavaResult(); // preserve the previous classloader, While we make Smooks aware of the Drools classloader ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread() .setContextClassLoader( ((InternalRuleBase) this.session.getRuleBase()).getRootClassLoader()); ExecutionContext executionContext = this.smooks.createExecutionContext(); // Filter the input message to extract, using the execution context... smooks.filter(source, result, executionContext); Thread.currentThread().setContextClassLoader(previousClassLoader); Map handles = new HashMap<FactHandle, Object>(); Object object = result.getBean(this.configuration.getRoodId()); if (object == null) { return handles; } if (this.getterExpr != null) { Iterable it = (Iterable) MVEL.executeExpression(this.getterExpr, object); if (it != null) { for (Object item : it) { FactHandle handle = this.session.insert(item); handles.put(handle, object); } } } else { FactHandle handle = this.session.insert(object); handles.put(handle, object); } return handles; }
protected String doProcess(RenderContext rc, Map<String, String> parameters) { log.debug("run script: " + script); RenderContext rcTarget = rc.getTarget(); Map map = new HashMap(); map.put("targetPage", rcTarget.page); map.put("params", parameters); map.put("rc", rc); map.put("request", RequestParams.current()); map.put("services", new Services()); Object o = org.mvel.MVEL.eval(script, map); commit(); if (o == null) { return null; } else if (o instanceof String) { String url = (String) o; return url; } else if (o instanceof CommonTemplated) { CommonTemplated ct = (CommonTemplated) o; return ct.getHref(); } else { log.warn("unhandled return type: " + o.getClass()); return rcTarget.page.getHref(); } }