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

    Execute Javascript, return the value of the last expression

    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;
    }
    
Search
    • Home