public void listScripts(PrintWriter out) throws Exception { Iterator<RuleScript> iterator = scriptRepository.currentRules().iterator(); if (!iterator.hasNext()) { out.println("no rules installed"); } else { while (iterator.hasNext()) { RuleScript ruleScript = iterator.next(); ruleScript.writeTo(out); synchronized (ruleScript) { List<Transform> transforms = ruleScript.allTransforms(); for (Transform transform : transforms) { transform.writeTo(out); } } } } }
protected void dumpScript(RuleScript ruleScript) { String file = ruleScript.getFile(); int line = ruleScript.getLine(); if (file != null) { Helper.out("# " + file + " line " + line); } Helper.out("RULE " + ruleScript.getName()); if (ruleScript.isInterface()) { Helper.out("INTERFACE " + ruleScript.getTargetClass()); } else { Helper.out("CLASS " + ruleScript.getTargetClass()); } Helper.out("METHOD " + ruleScript.getTargetMethod()); if (ruleScript.getTargetHelper() != null) { Helper.out("HELPER " + ruleScript.getTargetHelper()); } Helper.out(ruleScript.getTargetLocation() + ""); Helper.out(ruleScript.getRuleText()); Helper.out("ENDRULE"); }
protected void collectAffectedNames(List<RuleScript> ruleScripts, List<String> classList, List<String> interfaceList, List<String> superClassList, List<String> superInterfaceList) { for (RuleScript ruleScript : ruleScripts) { String targetClassName = ruleScript.getTargetClass(); boolean isOverride = ruleScript.isOverride(); if (ruleScript.isInterface()) { if (!interfaceList.contains(targetClassName)) { interfaceList.add(targetClassName); if (isOverride) { superInterfaceList.add(targetClassName); } } } else { if (!classList.contains(targetClassName)) { classList.add(targetClassName); if (isOverride) { superClassList.add(targetClassName); } } } } }
String name = script.getName(); RuleScript current; current = ruleNameIndex.get(name); if (current == script) { ruleNameIndex.remove(current.getName()); boolean isDeleted = current.setDeleted(); if (isDeleted) { Map<String, List<RuleScript>> index = (current.isInterface() ? targetInterfaceIndex : targetClassIndex); unindexTarget(current, index); boolean wasOverride = current.isOverride();
out.println("ERROR remove failed to find script " + ruleScript.getName()); for (Transform transform : ruleScript.allTransforms()) { RuleScript newRuleScript = scriptRepository.scriptForRuleName(oldRuleScript.getName()); for (TransformSet oldTransformSet : oldRuleScript.getTransformSets()) { TransformSet newTransformSet = newRuleScript.lookupTransformSet(oldTransformSet.getLoader(), oldTransformSet.getTriggerClass()); if(newTransformSet == null || newTransformSet.isInstalled()) { if(oldTransformSet.isInstalled()) { newTransformSet.setInstalled(oldTransformSet.getInstalledRule()); } else { newRuleScript.ensureTransformSet(oldTransformSet.getLoader(), oldTransformSet.getTriggerClass(), oldTransformSet.getInstalledRule()); for (TransformSet oldTransformSet : oldRuleScript.getTransformSets()) { if(oldTransformSet.isInstalled()) { out.println("uninstall RULE " + oldRuleScript.getName());
String targetClassName = script.getTargetClass(); Class targetClass = null; try { error("ERROR : Could not load class " + targetClassName + " declared in rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); continue; if (script.isInterface() && !targetClass.isInterface()) { error("ERROR : Found class instead of interface for rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); continue; if (!script.isInterface() && targetClass.isInterface()) { error("ERROR : Found interface instead of class for rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); continue; if (!script.isInterface()) { String resourceName = targetClass.getName().replace(".", "/") + ".class"; byte[] bytes = null; error("ERROR : Could not load bytecode for class " + targetClassName + " declared in rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); continue; error("ERROR : Could not load bytecode for class " + targetClassName + " declared in rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine(), e); continue; info("Checking rule " + script.getName() + " against class " + targetClass.getName()); bytes = transformer.transform(script, loader, targetClass.getName(), bytes); if (script.hasTransform(targetClass)) {
String targetMethodName = script.getTargetMethod(); String targetName = TypeHelper.parseMethodName(targetMethodName); String targetDesc = TypeHelper.parseMethodDescriptor(targetMethodName); warning("WARNING : Cannot type check <clinit> rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); return; if (script.isInterface()) { error("ERROR : Invalid target method <init> for interface rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); return; } else { error("ERROR : Invalid target method <init> for overriding rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine()); return; rule.compile(); } catch (TypeWarningException te) { typeWarning("WARNING : Unable to type check rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine(), te); System.out.println(te); System.out.println(); return; } catch (TypeException te) { typeError("ERROR : Failed to type check rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine(), te); System.out.println(te); System.out.println(); return; } catch (CompileException ce) { typeError("ERROR : Failed to compile rule \"" + script.getName() + "\" loaded from " + script.getFile() + " line " + script.getLine(), ce); System.out.println(ce); System.out.println();
try { String file = getFile(); ECATokenLexer lexer = new ECATokenLexer(new StringReader(ruleScript.getRuleText())); lexer.setStartLine(getLine()); lexer.setFile(file); Symbol parse = (debugParse ? parser.debug_parse() : parser.parse()); if (parser.getErrorCount() != 0) { String message = "rule " + ruleScript.getName(); message += parser.getErrors(); throw new ParseException(message); throw pe; } catch (Throwable th) { String message = "rule " + ruleScript.getName(); if (parser != null && parser.getErrorCount() != 0) { message += parser.getErrors(); String helperName = ruleScript.getTargetHelper(); String[] imports = ruleScript.getImports(); if (helperName != null || isCompileToBytecode() || imports.length > 0) {
List<RuleScript> ruleScripts = scriptRepository.processScripts(scriptText, file); for (RuleScript ruleScript : ruleScripts) { String name = ruleScript.getName(); RuleScript previous = scriptRepository.scriptForRuleName(name); if (previous == null) { buffer.append(name); buffer.append("in file "); buffer.append(ruleScript.getFile()); buffer.append(" line "); buffer.append(ruleScript.getLine()); buffer.append("\n previously defined in file "); buffer.append(previous.getFile()); buffer.append(" line "); buffer.append(previous.getLine()); Exception ex = new Exception(buffer.toString()); throw ex;
stringWriter.write(getTargetLocation().toString()); stringWriter.write('\n'); String[] imports = ruleScript.getImports(); if (imports != null) { for (int i = 0; i < imports.length; i++) { String targetHelper = ruleScript.getTargetHelper(); if (targetHelper != null) { stringWriter.write("HELPER "); stringWriter.write('\n'); if (ruleScript.isCompileToBytecode()) { stringWriter.write("COMPILE\n"); } else {
public byte[] transform(byte[] targetClassBytes) final Location handlerLocation = ruleScript.getTargetLocation(); String ruleName = ruleScript.getName(); ruleScript.purge(loader, triggerClassName); } catch (Throwable th) { Helper.verbose("org.jboss.byteman.agent.Transformer : unexpected error applying rule " + ruleScript.getName() + " to class " + triggerClassName + "\n" + th); Helper.verboseTraceException(th); Helper.verbose("org.jboss.byteman.agent.Transformer : possible trigger for rule " + ruleScript.getName() + " in class " + triggerClassName); Helper.verbose("org.jboss.byteman.agent.Transformer : unexpected error injecting trigger for rule " + ruleScript.getName() + " into class " + triggerClassName + "\n" + th); Helper.verboseTraceException(th); Helper.verbose("org.jboss.byteman.agent.Transformer : inserted trigger for " + ruleScript.getName() + " in class " + triggerClassName);
/** * insert a script into the index using the script target class name as the index key. * @param script * @param index */ private void indexTarget(RuleScript script, Map<String, List<RuleScript>> index) { String key = script.getTargetClass(); // synchronize on the new script to avoid ant race with a deleting thread synchronized(script) { if (script.isDeleted()) { return; } synchronized (index) { List<RuleScript> entry = index.get(key); // always create a new list so that we don't affect any in progress iteration of the previous value if (entry == null) { entry = new ArrayList(); add(entry, script); } else { entry = new ArrayList(entry); add(entry, script); } index.put(key, entry); } } }
public String getTargetClass() { return ruleScript.getTargetClass(); }
/** * record the fact that a trigger call has failed to install into bytecode associated with a specific * class and loader * @param loader the loader of the class for which injection was attempted * @param internalClassName the internal Java name of the class * @param triggerMethodName the name of the method injected into * @param desc the descriptor of the method injected into * @param rule the rule which was injected * @param th throwable generated during the attempt to parse the rule text or inject code at the trigger point * @return true if the successful injection was recorded false if not */ public synchronized boolean recordTransform(ClassLoader loader, String internalClassName, String triggerMethodName, String desc, Rule rule, Throwable th) { if (deleted) { return false; } String fullMethodName = null; if (triggerMethodName != null) { fullMethodName = triggerMethodName + TypeHelper.internalizeDescriptor(desc); } // make sure we know about this specific loader and classname combination TransformSet transformSet = ensureTransformSet(loader, internalClassName, null); // and install the transform in the set transformSet.add(new Transform(loader, internalClassName, fullMethodName, rule, th)); return true; }
/** * retrieve the name of the file containing this rule * @return the name of the file containing this rule */ public String getFile() { return ruleScript.getFile(); }
/** * retrieve the start line for the rule * @return the start line for the rule */ public int getLine() { return ruleScript.getLine(); }
public String getName() { return ruleScript.getName(); }
/** * install helper class used to execute this rule. this may involve generating a compiled helper class * for the rule and, if compilation to bytecode is enabled, generating bytecode for a method of this class * used to execute the rule binding, condition and action expressions. If the rule employ sthe default helper * without enabling compilation to bytecode then no class need be generated. the installed helper class will * be the predefined class InterpretedHelper. * @throws CompileException if the rule cannot be compiled */ public void compile() throws CompileException { boolean doCompileToBytecode = doCompileToBytecode(); String[] imports = ruleScript.getImports(); Class<?> helperClass = getHelperClass(); if (helperClass == Helper.class && !doCompileToBytecode && imports.length == 0) { // we can use the builtin interpreted helper adapter for class Helper helperImplementationClass = InterpretedHelper.class; helperImplementationClassName = Type.internalName(helperImplementationClass, true); } else { // we need to generate a helper adapter class which either interprets or compiles helperImplementationClassName = Compiler.getHelperAdapterName(helperClass, doCompileToBytecode); helperImplementationClass = Compiler.getHelperAdapter(this, helperClass, helperImplementationClassName, doCompileToBytecode); } }
targetImports = (defaultImports != null) ? defaultImports : new String[0]; RuleScript ruleScript = new RuleScript(name, targetClass, isInterface, isOverride, targetMethod, targetHelper, targetImports, targetLocation, nextRule, startNumber, scriptFile, ruleCompileToBytecode); ruleScripts.add(ruleScript);
out.println("ERROR remove failed to find script " + ruleScript.getName()); for (Transform transform : ruleScript.allTransforms()) { RuleScript newRuleScript = scriptRepository.scriptForRuleName(oldRuleScript.getName()); for (TransformSet oldTransformSet : oldRuleScript.getTransformSets()) { TransformSet newTransformSet = newRuleScript.lookupTransformSet(oldTransformSet.getLoader(), oldTransformSet.getTriggerClass()); if(newTransformSet == null || newTransformSet.isInstalled()) { if(oldTransformSet.isInstalled()) { newTransformSet.setInstalled(oldTransformSet.getInstalledRule()); } else { newRuleScript.ensureTransformSet(oldTransformSet.getLoader(), oldTransformSet.getTriggerClass(), oldTransformSet.getInstalledRule()); for (TransformSet oldTransformSet : oldRuleScript.getTransformSets()) { if(oldTransformSet.isInstalled()) { out.println("uninstall RULE " + oldRuleScript.getName());