@Override public Object compile(String expression, Map<String, Object> parameters) { logger.debug("About to compile mvel expression {}", expression); ClassLoader classLoader = (ClassLoader) parameters.get("classloader"); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(classLoader); ParserContext context = new ParserContext(config); if (parameters != null) { @SuppressWarnings("unchecked") Set<String> imports = (Set<String>)parameters.get("imports"); if (imports != null) { for(String clazz : imports) { try { Class<?> cl = Class.forName(clazz, true, classLoader); context.addImport(cl.getSimpleName(), cl); } catch (ClassNotFoundException e) { logger.warn("Unable to load class {} due to {}", clazz, e.getException()); }; } } } return MVEL.compileExpression(expression, context); }
@Override public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) { Object instance = null; InternalRuntimeManager manager = null; if (contextParams.containsKey("runtimeManager")) { manager = (InternalRuntimeManager) contextParams.get("runtimeManager"); instance = manager.getCacheManager().get(model.getIdentifier()); if (instance != null) { return instance; } } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(cl); ParserContext ctx = new ParserContext(config); if (contextParams != null) { for (Map.Entry<String, Object> entry : contextParams.entrySet()) { ctx.addVariable(entry.getKey(), entry.getValue().getClass()); } } Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx); instance = MVELSafeHelper.getEvaluator().executeExpression( compiledExpression, contextParams ); if (manager != null && instance instanceof Cacheable) { manager.getCacheManager().add(model.getIdentifier(), instance); } return instance; }
@Override public Object extract(String valueAsString, Event event) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); if (!mvelExpressions.containsKey(valueAsString)) { ParserConfiguration parserConfiguration = new ParserConfiguration(); parserConfiguration.setClassLoader(getClass().getClassLoader()); mvelExpressions.put(valueAsString, MVEL.compileExpression(valueAsString, new ParserContext(parserConfiguration))); } Map<String, Object> ctx = new HashMap<>(); ctx.put("event", event); ctx.put("session", event.getSession()); ctx.put("profile", event.getProfile()); return MVEL.executeExpression(mvelExpressions.get(valueAsString), ctx); } finally { Thread.currentThread().setContextClassLoader(tccl); } } });
@Override public Object extract(String valueAsString, Event event) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); if (!mvelExpressions.containsKey(valueAsString)) { ParserConfiguration parserConfiguration = new ParserConfiguration(); parserConfiguration.setClassLoader(getClass().getClassLoader()); mvelExpressions.put(valueAsString, MVEL.compileExpression(valueAsString, new ParserContext(parserConfiguration))); } Map<String, Object> ctx = new HashMap<>(); ctx.put("event", event); ctx.put("session", event.getSession()); ctx.put("profile", event.getProfile()); return MVEL.executeExpression(mvelExpressions.get(valueAsString), ctx); } finally { Thread.currentThread().setContextClassLoader(tccl); } } });
this.parserConfiguration.setImports( this.imports ); this.parserConfiguration.setPackageImports( this.packageImports ); this.parserConfiguration.setClassLoader( classLoader );
@Override public Object compile(String expression, Map<String, Object> parameters) { logger.debug("About to compile mvel expression {}", expression); ClassLoader classLoader = (ClassLoader) parameters.get("classloader"); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(classLoader); ParserContext context = new ParserContext(config); if (parameters != null) { @SuppressWarnings("unchecked") Set<String> imports = (Set<String>)parameters.get("imports"); if (imports != null) { for(String clazz : imports) { try { Class<?> cl = Class.forName(clazz, true, classLoader); context.addImport(cl.getSimpleName(), cl); } catch (ClassNotFoundException e) { logger.warn("Unable to load class {} due to {}", clazz, e.getException()); }; } } } return MVEL.compileExpression(expression, context); }
public EnumFieldPopulator(Object factObject, String fieldName, String fieldValue, TypeResolver typeResolver, ClassLoader classLoader) { super( factObject, fieldName ); this.typeResolver = typeResolver; this.fieldValue = fieldValue; this.pconf = new ParserConfiguration(); pconf.setClassLoader( classLoader ); this.pctx = new ParserContext( pconf ); pctx.setStrongTyping( true ); }
public FactFieldValueVerifier(Map<String, Object> populatedData, String factName, Object factObject, final TypeResolver resolver, final ClassLoader classLoader) { this.populatedData = populatedData; this.factName = factName; this.factObject = factObject; this.resolver = resolver; this.pconf = new ParserConfiguration(); pconf.setClassLoader(classLoader); this.pctx = new ParserContext(pconf); pctx.setStrongTyping(true); }
public FactFieldValueVerifier(Map<String, Object> populatedData, String factName, Object factObject, final TypeResolver resolver, final ClassLoader classLoader) { this.populatedData = populatedData; this.factName = factName; this.factObject = factObject; this.resolver = resolver; this.pconf = new ParserConfiguration(); pconf.setClassLoader(classLoader); this.pctx = new ParserContext(pconf); pctx.setStrongTyping(true); }
public EnumFieldPopulator(Object factObject, String fieldName, String fieldValue, TypeResolver typeResolver, ClassLoader classLoader) { super( factObject, fieldName ); this.typeResolver = typeResolver; this.fieldValue = fieldValue; this.pconf = new ParserConfiguration(); pconf.setClassLoader( classLoader ); this.pctx = new ParserContext( pconf ); pctx.setStrongTyping( true ); }
pconf.setClassLoader(classLoader);
pconf.setClassLoader(classLoader);
public EnumFieldPopulator(Object factObject, String fieldName, String fieldValue, TypeResolver typeResolver) { super(factObject, fieldName); this.typeResolver = typeResolver; this.fieldValue = fieldValue; this.pconf = new ParserConfiguration(); pconf.setClassLoader(typeResolver.getClassLoader()); // classLoader ); this.pctx = new ParserContext(pconf); pctx.setStrongTyping(true); }
public FactFieldValueVerifier(Map<String, Object> populatedData, String factName, Object factObject, final TypeResolver resolver) { this.populatedData = populatedData; this.factName = factName; this.factObject = factObject; this.resolver = resolver; this.pconf = new ParserConfiguration(); pconf.setClassLoader(resolver.getClassLoader()); this.pctx = new ParserContext(pconf); pctx.setStrongTyping(true); }
public void testCollectionAccessWithInvalidThreadClassLoader() { String expression = "['A', 'B', 'C'] contains 'B'"; ParserConfiguration parserConfiguration = new ParserConfiguration(); parserConfiguration.setClassLoader(MVEL.class.getClassLoader()); Serializable compiledExpression = MVEL.compileExpression(expression, new ParserContext(parserConfiguration)); ClassLoader currentCl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(NO_MVEL_CL); for (int i = 0; i <= DynamicOptimizer.tenuringThreshold; i++) { Object result = MVEL.executeExpression(compiledExpression); assertEquals(Boolean.TRUE, result); } } finally { Thread.currentThread().setContextClassLoader(currentCl); } } }
@Override public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) { Object instance = null; InternalRuntimeManager manager = null; if (contextParams.containsKey("runtimeManager")) { manager = (InternalRuntimeManager) contextParams.get("runtimeManager"); instance = manager.getCacheManager().get(model.getIdentifier()); if (instance != null) { return instance; } } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(cl); ParserContext ctx = new ParserContext(config); if (contextParams != null) { for (Map.Entry<String, Object> entry : contextParams.entrySet()) { ctx.addVariable(entry.getKey(), entry.getValue().getClass()); } } Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx); instance = MVELSafeHelper.getEvaluator().executeExpression( compiledExpression, contextParams ); if (manager != null && instance instanceof Cacheable) { manager.getCacheManager().add(model.getIdentifier(), instance); } return instance; }
ParserConfiguration conf = data.getParserConfiguration(); conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() );
public void testNestedEnumFromJar() throws ClassNotFoundException, SecurityException, NoSuchFieldException { String expr = "EventRequest.Status.ACTIVE"; // creating a classloader for the jar URL resource = getClass().getResource("/eventing-example.jar"); assertNotNull(resource); URLClassLoader loader = new URLClassLoader(new URL[]{resource}, getClass().getClassLoader()); // loading the class to prove it works Class<?> er = loader.loadClass("org.drools.examples.eventing.EventRequest"); assertNotNull(er); assertEquals("org.drools.examples.eventing.EventRequest", er.getCanonicalName()); // getting the value of the enum to prove it works: Class<?> st = er.getDeclaredClasses()[0]; assertNotNull(st); Field active = st.getField("ACTIVE"); assertNotNull(active); // now, trying with MVEL ParserConfiguration pconf = new ParserConfiguration(); pconf.setClassLoader(loader); pconf.addImport(er); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); Serializable compiled = MVEL.compileExpression(expr, pctx); Object result = MVEL.executeExpression(compiled); assertNotNull(result); }
MVELDialectRuntimeData data = ( MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" ); ParserConfiguration conf = data.getParserConfiguration(); conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() );
mvelConfiguration.setClassLoader(classLoader);