summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/sca-client-javascript/src/main/java/org/apache/tuscany/sca/client/javascript/ScriptEngine.java
blob: 3db6a6d08cf26de6c3a9528530f5c0d8660d31ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.tuscany.sca.client.javascript;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.client.rmi.SCAClientFactoryImpl;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.apache.tuscany.sca.runtime.EndpointRegistry;

import sun.org.mozilla.javascript.internal.Context;
import sun.org.mozilla.javascript.internal.Function;
import sun.org.mozilla.javascript.internal.Scriptable;
import sun.org.mozilla.javascript.internal.ScriptableObject;
import sun.org.mozilla.javascript.internal.WrappedException;
/**
 * 
 * This class implements a Scripting Engine for the Tuscany runitme. This engine
 * uses the Rhino javaScript Engine.
 * 
 * <p> The default script functions supported by this engine are the following:
 * <ul>
 *   <li>echo</li>
 *   <li>version</li>
 *   <li>load</li>
 *   <li>help</li>
 *   <li>exit</li>
 *   <li>printStackTrace</li>
 *   <li>loadModule</li>
 *   <li>listComponents</li>
 *   <li>getComponent</li>
 *   <li>listServices</li>
 *   <li>getService</li>
 *  </ul>
 */
public class ScriptEngine extends ScriptableObject implements Runnable{
	private static final long serialVersionUID = 1L;

	/**
	 * The context with which this script engine will be run
	 */
	private Context jsContext = null;
    
	/**
	 * Set of parameters passes to the script engine
	 */
	private String[] params = null;
	
	/**
	 * instance of Tuscany runtime
	 */
	private SCAClientFactoryImpl tuscany;
	
	/**
	 * A reference to the old class loader
	 */
	private ClassLoader oldClassLoader = null;
	
	/**
	 * Flag to indicate, if the shoutdown has requested by the user
	 */
	private boolean shutdown;
	
	/**
	 * The current output stream 
	 */
	private static PrintStream out = System.out;

	/**
	 * The current error output stream 
	 */
	private PrintStream err = System.err;
	
	/**
	 * The current input stream 
	 */
	private InputStream in = System.in;
	
	/**
	 * The prompt used
	 */
	private String prompt = "js> ";
	
	/**
	 * The last exception that occured, used for debugging
	 */
	private Throwable lastException = null;
	
	/**
	 * Names of the global functions particular to the shell. Note that these functions 
	 * are not part of ECMA.
	 */
	public static final String[] SCRIPT_FUNCTIONS = { 
			"echo", 
			"version", 
			"load", 
			"help",
			"exit", 
			"printStackTrace",
			"loadModule",
			"listComponents",
			"getComponent",
			"listServices",
			"getService", };
	
	/**
	 * get the class name
	 * @return "global" 
	 */
	@Override
	public String getClassName() {
        return "global";
    }

	/**
	 * Construct a script engine
	 *
	 * @param command line arguments
	 */
	public ScriptEngine(String[] params) {
		this.params = params;
	}
	
	/**
	 * Run the script engine. Invoking this method will initialize the script engine,
	 * Tuscany Runtime and load any SCA modules if found in the classpath.
	 *
	 */
	public void run() {
		// Associate a new Context with this thread
        jsContext = Context.enter();
        try {
            // initialize standard objects
            jsContext.initStandardObjects(this);
            // define supported script functions
            this.defineFunctionProperties(SCRIPT_FUNCTIONS, 
            		ScriptEngine.class, 
            		ScriptableObject.DONTENUM);

            // init the tuscany runtime
            this.initTuscanyRuntime();
            // initialize the script engine
            this.startScriptEngine();
        } 
        finally {
            tuscany.stop();
            Thread.currentThread().setContextClassLoader(oldClassLoader);
            Context.exit();
        }
	}

    /**
     * Print the help message.
     *
     * This method is defined as a script function.
     */
    public void help() {
    	final String helpMsg = 
    		"Command                Description \n" +
    		"=======                =========== \n" +
    		"help()                 Display this message. \n" + 
    		"load(['f1.js', ...])   Load and execute javaScript source files named f1.js, etc. \n" +
    		"echo([expr ...])       Evaluate and print a variable or an expressions. \n" +
    		"exit()                 Exit this shell. \n" +
    		"version([number])      Get or set the javaScript version. \n" +
    		"printStackTrace()      Print the stacktrace of the last exception \n" +
    		"loadModule(jar|war)    Not yet implemented. \n" +
    		"listComponents()       Not yet implemented. \n" +
    		"getComponent('name')   Get an instance of the component with the given name. \n" +
    		"listServices()         Not yet implemented. \n" +
    		"getService('name')     Not yet implemented. \n";
    	
    	out.println();
    	out.println(helpMsg);
    }

    /**
     * Load a given module with the tuscany runtime
     * 
     * This method is defined as a script function.
     * 
     * @param moduleName absolute path of the module
     */
    public void loadModule(final String moduleName) {
        try {
            File file = new File(moduleName);
            URL[] urls = new URL[] {file.toURL()};
            final ClassLoader newCL = new URLClassLoader(urls);
            Thread.currentThread().setContextClassLoader(newCL);
        } catch (Exception e) {
                err.println(e.getMessage());
                lastException = e;
        }
    	// TODO complete this method
    	/*
    	ModuleContext context = CurrentModuleContext.getContext();
    	out.println(context.toString());
    	tuscany.stop();
    	
    	try {
	    	File file = new File(moduleName);
	    	URL[] urls = new URL[] {file.toURL(), };
	    	final ClassLoader newCL = new URLClassLoader(urls);
	    	Thread.currentThread().setContextClassLoader(newCL);
	    	
	    	//Class c = Class.forName("customerinfo.CustomerInfoClient", true, newCL);
	    	//out.println("c = " + c);
	    	
	    	tuscany = new TuscanyRuntime(moduleName, null);
	        tuscany.start();
	        
	        context = CurrentModuleContext.getContext();
	    	out.println(context.toString());
	    	
	    	//Object component = context.locateService("CustomerInfoServiceComponent");
	    	//out.println(component);
    	}
    	catch (Exception e) {
    		err.println(e.getMessage());
    		lastException = e;
    	}
    	*/
    }
    
    /**
     * List the components available within the current module
     * 
     * This method is defined as a script function. 
     */
    public void listComponents() {
    	// TODO complete this method
    }
    
    /**
     * Get the component available within the current module with a 
     * given name
     * 
     * This method is defined as a script function.
     * 
     * @param name name of the component
     * @return service component object represented by the given name 
     */
    public Object getComponent(String name) {
//        ModuleContext tuscanyContext = CurrentModuleContext.getContext();
//        Object component = tuscanyContext.locateService(name);
        
//        return component;
        return null;
    }
    
    /**
     * List the external services available within the current module
     * 
     * This method is defined as a script function. 
     */
    public void listServices() {
        EndpointRegistry epr = tuscany.getEndpointRegistry();
        for (Endpoint e : epr.getEndpoints()) {
            out.println(e.getURI());
        }
    }
    
    public Object getService(String serviceName) {
        ExtensionPointRegistry extensionsRegistry = tuscany.getExtensionsRegistry();
        FactoryExtensionPoint factories = extensionsRegistry.getExtensionPoint(FactoryExtensionPoint.class);
        AssemblyFactory assemblyFactory = factories.getFactory(AssemblyFactory.class);

        EndpointReference endpointReference = assemblyFactory.createEndpointReference();
        endpointReference.setReference(assemblyFactory.createComponentReference());
        Endpoint targetEndpoint = assemblyFactory.createEndpoint();
        targetEndpoint.setURI(serviceName);
        endpointReference.setTargetEndpoint(targetEndpoint);
        EndpointRegistry epr = tuscany.getEndpointRegistry();
        List<Endpoint> er = epr.findEndpoint(endpointReference);
        if (er.size() < 1) {
            err.println("service not found " + serviceName);
            return null;
        }
        JavaInterface ifac = ((JavaInterface)er.get(0).getInterfaceContract().getInterface());
        return getService(serviceName, ifac.getName());
    }

    /**
     * Get an external service defined within the current module with a given name
     * 
     * This method is defined as a script function.
     * 
     * @param name name of the service
     * @return service object represented by the given name 
     */
    protected Object getService(String serviceName, String type) {
        try {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Class<?> serviceInterface = cl.loadClass(type);
            Object service = tuscany.getService(serviceInterface, serviceName);
            printService(serviceInterface);
            return service;
        } catch (Exception e) {
            err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
            lastException = e;
            return null;
        }
    }

    private void printService(Class<?> serviceInterface) {
        out.println(serviceInterface.getName());
        for (Method m  : serviceInterface.getMethods()) {
            out.print("   " + m.getReturnType().getName() + " " + m.getName() + "(");
            boolean one = false;
            for (Class arg  : m.getParameterTypes()) {
                if (one) {
                    out.print(", ");
                }
                out.print(arg.getName());
                one = true;
            }
            out.println(")");
        }
    }
    
    /**
     * Echo a variable or a message.
     *
     * This method is defined as a script function.
     *
     * @param jsContext
     * @param thisObj
     * @param args
     * @param function
     */
    public static void echo(Context jsContext, Scriptable thisObj, Object[] args, Function function)
    {
    	if (args.length > 0) {
	    	out.println(args[0]);
	        for (int i=1; i < args.length; i++) {
	            out.print(" " + Context.toString(args[i]));
	        }
    	}
        out.println();
    }

    /**
     * print the stacktrace of the last exception
     *
     * This method is defined as a script function.
     */
    public void printStackTrace() {
    	if (lastException == null) {
    		out.println("No stacktrace available");
    	}
    	else {
    		err.println(getStackTrace(lastException));
    	}
    }
    
    /**
     * Exit the shell when it is in the interactive mode.
     *
     * This method is defined as a script function.
     */
    public void exit(){
        shutdown = true;
    }

    /**
     * Get and set the language version.
     *
     * This method is defined as a JavaScript function.
     * 
     * @param jsContext
     * @param thisObj
     * @param args
     * @param function
     */
    public static double version(Context jsContext, Scriptable thisObj, Object[] args, Function function)
    {
        if (args.length > 0) {
            int i = (int)Context.toNumber(args[0]);
            jsContext.setLanguageVersion(i);
        }
        return (double) jsContext.getLanguageVersion();
    }

    /**
     * Load and execute a set of JavaScript source files.
     *
     * This method is defined as a JavaScript function.
     *
     * @param jsContext
     * @param thisObj
     * @param args
     * @param function
     */
    public static void load(Context jsContext, Scriptable thisObj, Object[] args, Function function)
    {
    	ScriptEngine engine = (ScriptEngine)getTopLevelScope(thisObj);
        for (int i = 0; i < args.length; i++) {
           engine.processFile(Context.toString(args[i]));
        }
    }

    /**
     * Get the current output stream
     * 
     * @return current output stream
     */
	public PrintStream getOut() {
		return out;
	}

	/**
	 * Set the current output stream
	 * @param out the new output stream
	 */
	public void setOut(PrintStream out) {
		ScriptEngine.out = out;
	}

	/**
	 * Get the current error output stream
	 * 
	 * @return Returns the current error output stream.
	 */
	public PrintStream getErr() {
		return err;
	}

	/**
	 * Set the current error output stream
	 * @param err The new error output stream to set.
	 */
	public void setErr(PrintStream err) {
		this.err = err;
	}

	/**
	 * Get the current input stream
	 * 
	 * @return Returns the current input stream.
	 */
	public InputStream getIn() {
		return in;
	}

	/**
	 * Set the current input stream
	 * @param in The new input stream to set.
	 */
	public void setIn(InputStream in) {
		this.in = in;
	}

	/**
	 * initialize the Tuscany runtime
	 *
	 */
	protected void initTuscanyRuntime() {
		
        // Required to allow the SDO runtime to use the correct classloader
        oldClassLoader = Thread.currentThread().getContextClassLoader();
//        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

        tuscany = new SCAClientFactoryImpl(URI.create("tribes:foo"));        
        
//        // Obtain and start the Tuscany runtime
//        try {
//	        tuscany = new TuscanyRuntime("TuscanyClient", null);
//	        tuscany.start();
//	        	        
//	        // this will get the module name; either specified by user or from the classpath
//	        String moduleName = getCurrentModuleName();
//	        // TODO how do we get all the components, services exposed by this module ? 
//	        
//        }
//        catch (ConfigurationException ce) {
//        	err.println("Failed to start Tuscany runtime: " + ce.getMessage());
//        	//ce.printStackTrace(err);
//        	lastException = ce;
//        }
	}


	/**
	 * start the script engine
	 *
	 */
	protected void startScriptEngine() {
		
		out.println("Tuscany SCA Shell 0.1, type help() for a list of supported functions.");
		
		// set up "arguments" in the global scope 
         if (params.length > 1) {
            int length = params.length - 1;
            Object obj[] = new Object[length];
            System.arraycopy(params, 1, obj, 0, length);
            
            Scriptable argsObj = jsContext.newArray(this, obj);
            this.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
        }

        if (params.length == 0) {
        	// no file name specified, go to interactive mode
        	this.startInteractiveMode();
        }
        else {
        	// TODO this could be a jar file or a script file
        	// process the specified file
        	this.processFile(params[0]);
        }
        System.gc();
	}
	


    /**
     * Evaluate JavaScript source in the interactive mode.
     *
     */
    private void startInteractiveMode()
    {
    	// see if org.mozilla.javascript.tools.shell.Main.processSource() can handle this
    	//Main.setErr(this.getErr());
    	//Main.setOut(this.getOut());
    	//Main.setIn(this.getIn());
    	//Main.processSource(jsContext, filename);
    	
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        int currentLine = 1;
        boolean done = false;
        while (!shutdown && !done) {
            out.print(prompt);
            out.flush();
            int lineToCompile = currentLine;
            try {
                StringBuffer source = new StringBuffer();
                // read the next line entered by user
                boolean compilable = false;
                while(!compilable) {
                    String nextLine = reader.readLine();
                    if (nextLine != null) {
                        currentLine += 1;
                    	source.append(nextLine).append("\n");
                    	compilable = jsContext.stringIsCompilableUnit(source.toString());
                    }
                    else {
                    	// TODO check if this can handle Ctrl+C
                    	System.out.println("=== Ctrl+C pressed ==");
                    	// break both the loops
                    	done = true;
                    	compilable = true;
                    }
                }
                Object result = jsContext.evaluateString(this, source.toString(), "<stdin>", lineToCompile, null);
                if (result != Context.getUndefinedValue()) {
                    out.println(Context.toString(result));
                }
            }
            catch (Exception e) {
            	if (e instanceof WrappedException) {
            		WrappedException we = (WrappedException)e;
            		err.println(we.getWrappedException().getMessage());
            	}
            	err.println(e.getMessage());
            	//e.printStackTrace(err);
            	lastException = e;
            }
        }
        out.println();
    }

    /**
     * Read the contents from a script file and execute it.
     *
     * @param filename the name of the file to compile
     */
    private void processFile(String fileName) {
    	BufferedReader reader = null;
        try {
        	reader = new BufferedReader(new FileReader(fileName));
        	jsContext.evaluateReader(this, reader, fileName, 1, null);
        }
        catch (Exception e) {
        	if (e instanceof WrappedException) {
        		WrappedException we = (WrappedException)e;
        		err.println(we.getWrappedException().getMessage());
        	}
        	err.println(e.getLocalizedMessage());
        	//e.printStackTrace(err);
        	lastException = e;
        }
        finally {
        	try {
        		reader.close();
            }
            catch (IOException ioe) {
            	err.println(ioe.getLocalizedMessage());
                //ioe.printStackTrace(err);
            	lastException = ioe;
            }
        }
    }

	/**
	 * Find the SCA module name. If the user have specified one, use that,
	 * otherwise, if there is a module definition in the path, pick it up. 
	 * 
	 * @return name of the module
	 */
	private String getCurrentModuleName() {
		String moduleName = "";
		
		// TODO parse the arguments to see if there is an SCA module name
		return moduleName;
	}
	
	/**
	 * Get the stacktrace for a give exception
	 * 
	 * @param t Throwable from which the stacktrace need to be obtained
	 * @return char array containing the stack trace
	 */
	private char[] getStackTrace(Throwable t) {
		CharArrayWriter charWriter = new CharArrayWriter(2048);
		PrintWriter writer = new PrintWriter(charWriter);
		t.printStackTrace(writer);

		return charWriter.toCharArray();
	}
}