• Intro Headers Instructions Macros Commands
Fork me on GitHub
    • Introduction
    • How to install bnd
    • Guided Tour
    • Guided Tour Workspace & Projects
    • Concepts
    • Best practices
    • Build
    • Project Setup
    • Generating JARs
    • Versioning
    • Baselining
    • Service Components
    • Metatype
    • Contracts
    • Bundle Annotations
    • Accessor Properties
    • SPI Annotations
    • Resolving Dependencies
    • Launching
    • Startlevels
    • Testing
    • Testing with Launchpad
    • Packaging Applications
    • JPMS Libraries
    • Wrapping Libraries to OSGi Bundles
    • Generating Documentation
    • Commands
    • For Developers
    • Tips for Windows users
    • Tools bound to bnd
    • Reference
    • Headers
    • Instruction Reference
    • Instruction Index
    • Macro Reference
    • Macro Index
    • Plugins
    • External Plugins
    • Settings
    • Errors
    • Warnings
    • Frequently Asked Questions
  • js (';' JAVASCRIPT )*
    Macro

    ScriptEngine	engine		= new ScriptEngineManager().getEngineByName("javascript");
    ScriptContext	context		= null;
    Bindings		bindings	= null;
    StringWriter	stdout		= new StringWriter();
    StringWriter	stderr		= new StringWriter();
    
    static String	_js			= "${js [;<js expr>...]}";
    
    public Object _js(String args[]) throws Exception {
    	verifyCommand(args, _js, null, 2, Integer.MAX_VALUE);
    
    	StringBuilder sb = new StringBuilder();
    
    	for (int i = 1; i < args.length; i++)
    		sb.append(args[i]).append(';');
    
    	if (context == null) {
    		context = engine.getContext();
    		bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    		bindings.put("domain", domain);
    		String javascript = domain.mergeProperties("javascript", ";");
    		if (javascript != null && javascript.length() > 0) {
    			engine.eval(javascript, context);
    		}
    		context.setErrorWriter(stderr);
    		context.setWriter(stdout);
    	}
    	Object eval = engine.eval(sb.toString(), context);
    	StringBuffer buffer = stdout.getBuffer();
    	if (buffer.length() > 0) {
    		domain.error("Executing js: %s: %s", sb, buffer);
    		buffer.setLength(0);
    	}
    
    	if (eval != null) {
    		return toString(eval);
    	}
    
    	String out = stdout.toString();
    	stdout.getBuffer().setLength(0);
    	return out;
    }
    
    • GitHub