Template for Bachelor Thesis, Master Thesis, Course Paper, Seminar Paper
Total Page:16
File Type:pdf, Size:1020Kb
MASARYK UNIVERSITY FACULTY OF INFORMATICS Scripting for the Java Platform (JSR-223) Bachelor's Thesis Student: Matej Sabo Studies: Applied Informatics Student ID: 325124 E-Mail: [email protected] Brno 2011 Declaration Hereby I declare that this paper is my original authorial work, which I have worked out by my own. All sources, references and literature used or excerpted during elabor- ation of this work are properly cited and listed in complete reference to the due source. i Abstract The aim of this work is to evaluate the potential of scripting languages on the Java Platform. The work includes a summary of strengths and weaknesses of Java and scripting languages, and an explanation of the specification JSR-223 Scripting in Java. An example application that uses the API described in the specification is presented. Selected script engine implementations are compared. In the conclusion I propose a possible direction of future development. ii Keywords Java, scripting languages, JSR-223, multilanguage programming, script engine, Groovy, Quercus, Rhino, JRuby, Jython, Clojure iii Table of Contents 1 INTRODUCTION ..................................................................................................1 2 SCRIPTING LANGUAGES AND JAVA...............................................................3 2.1 TECHNOLOGY......................................................................................................................3 2.2 JAVA..................................................................................................................................4 2.3 SCRIPTING LANGUAGES..........................................................................................................5 2.4 STRENGTHS AND WEAKNESSES OF SCRIPTING LANGUAGES..............................................................5 2.4.1 Runtime Performance.............................................................................................6 2.4.2 Development Speed................................................................................................7 2.4.3 Bugs........................................................................................................................7 2.4.4 Maintenance...........................................................................................................8 2.4.5 Scaling....................................................................................................................8 2.5 COMMON USE-CASES OF SCRIPTING LANGUAGES ON THE JAVA PLATFORM........................................8 2.5.1 Extreme programming............................................................................................8 2.5.2 Wiring together existing components.....................................................................9 2.5.3 End-user-customization..........................................................................................9 2.5.4 Prototyping.............................................................................................................9 2.5.5 Testing....................................................................................................................9 2.5.6 Administration and Management ..........................................................................9 2.5.7 User Interface Programming................................................................................10 2.5.8 Web applications .................................................................................................10 2.6 OVERVIEW OF SELECTED SCRIPTING LANGUAGES.......................................................................10 2.6.1 Groovy..................................................................................................................10 2.6.2 JavaScript – Rhino ..............................................................................................10 2.6.3 Jython ..................................................................................................................11 2.6.4 JRuby....................................................................................................................11 2.6.5 Clojure..................................................................................................................12 3 SCRIPTING IN JAVA...........................................................................................13 3.1 JSR-223: SCRIPTING FOR THE JAVA PLATFORM.......................................................................13 3.1.1 The general scripting API....................................................................................13 3.1.2 Calling scripts .....................................................................................................14 3.1.3 Calling procedures and functions within scripts..................................................15 3.1.4 Sharing objects.....................................................................................................15 3.1.5 Web Scripting Framework....................................................................................16 3.2 CUSTOM SCRIPTING IMPLEMENTATIONS....................................................................................16 3.3 SUMMARY........................................................................................................................17 4 SCRIPTING IN JAVA – EXAMPLE APPLICATION.......................................17 4.1 ANALYSIS, DESIGN AND IMPLEMENTATION ...............................................................................18 4.1.1 Requirements........................................................................................................19 4.1.2 Design..................................................................................................................20 iv 4.1.3 Implementation.....................................................................................................20 4.2 TECHNOLOGY USED IN THE APPLICATION..................................................................................20 4.2.1 Object-relational mapping...................................................................................20 4.2.2 Scripting engines..................................................................................................21 4.2.3 RSyntaxTextArea...................................................................................................22 4.3 EXAMPLE SCRIPTS..............................................................................................................22 4.4 COMPARISON OF SCRIPTING ENGINES USED IN THE APPLICATION....................................................24 4.4.1 Script Engine implementations – basic information.............................................24 4.4.2 Execution speed....................................................................................................26 4.4.3 Popularity.............................................................................................................27 4.5 SUMMARY........................................................................................................................28 5 CONCLUSION......................................................................................................29 6 ATTACHMENT.......................................................................................................I 7 BIBLIOGRAPHY..................................................................................................II v List of Figures Fig. 1: The categorization of dynamic, dynamically typed, and scripting languages.....5 Fig. 2: The performance of runtimes of scripting languages..........................................7 Fig. 3: Screenshot of the main window of the application. .........................................18 Fig. 4: The incremental development model...............................................................19 Fig. 5: The execution time for “Hello World” scripts run with Java Script Engines...27 Fig. 6: The popularity of Script Engines based on Google hits (in million)................28 List of Tables Tab. 1: The Mozilla Rhino JavaScript engine............................................................24 Tab. 2: Groovy Scripting Engine................................................................................25 Tab. 3: Jython Scripting Engine.................................................................................25 Tab. 4: JRuby Scripting Engine..................................................................................25 Tab. 5: Clojure Scripting Engine................................................................................26 Tab. 6: Quercus PHP Scripting Engine.......................................................................26 Attachment CD-ROM: Scripting Languages on the Java Platform (JSR-223).................................I vi List of Abbreviations JVM Java Virtual Machine JRE Java Runtime Environment JDK Java Development Kit JSR Java Specification Request API Application Programming Interface GUI Graphical User Interface IDE Integrated Developing Environment 7 1 Introduction The common programming practice is to choose a programming language, best suited for the project. But this is now beginning to change. The multilanguage or hybrid ap- proach allows developers to use a different language for each individual development task within one project. You can, for example, use dynamic languages with their mod- ern features, and still retain access to Java libraries, or you can use the robust