Server Side Javascript

  • 2,346 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,346
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
37
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Server-Side JavaScript with Project Phobos Roberto Chinnici Senior Staff Engineer Sun Microsystems, Inc. http://phobos.dev.java.net
  • 2. JavaScript is a real language 2
  • 3. Small, terse language Closures Functional style OK Prototype-based object model Object literals Creative DSL uses 3
  • 4. JavaScript in the browser JSON on the wire + cross-tier JavaScript code JavaScript on the server JavaScript in the database 4
  • 5. JavaScript On All Tiers 5
  • 6. What is Phobos? ● Lightweight web application framework ● Running on the Java™ platform ● All application logic is JavaScript ● Most of the framework itself is in JavaScript ● Deploy to any servlet container ● IDE support for ease of development 6
  • 7. Client Architecture JAVASCRIPT APPLICATION CODE JAVASCRIPT / AJAX LIBRARIES BROWSER 7
  • 8. Server Architecture JAVASCRIPT APPLICATION CODE JAVASCRIPT / PHOBOS AJAX JAVASCRIPT LIBRARIES LIBRARIES MOZILLA RHINO JAVA™ LIBRARIES RDBMS PHOBOS RUNTIME JAVA™ PLATFORM 8
  • 9. Core Phobos Functionality ● URL mapping/dispatching ● Request/session/application contexts ● Asynchronous script execution ● Container independence ● Full-featured debugger ● JSR-223 scripting engine integration 9
  • 10. Development Process 1. Start NetBeans 2. Create skeleton application using wizard 3. Run it in debug mode 4. Map out the URLs for pages, services, Ajax 5. Attach logic to them 6. Test out interactively 7. Go back to step 4, repeat 8. Stop the application, you're done 10
  • 11. Tool Support ● Phobos ships as NetBeans plugins ● Project and component wizards ● jMaki widget palette ● Multithreaded debugger – Breakpoints, inspectors, watches, ... ● Server runs embedded in the IDE 11
  • 12. URL Design ● Plain scripts /doSomething.js ● Controller/action /store/display_cart ● Resource /book/isbn/1234-5678 ● User-defined any regexp 12
  • 13. Defining New Patterns ● Add new rules at startup or on the fly application.mapping.rules.push({ url: ”/feed/@id”, factory: quot;module.atom.createFeedResourcequot;, fn: quot;library.mapping.maybeRESTquot; }); Named functions in yellow 13
  • 14. Application Layout /application /static /controller /resources test.js ...jMaki... /dynamic /css sample.ejsp main.css /module faq.html application.js release_notes.html /script index.js /environment /template development.js /view startup-webapp.js test.ejs 14
  • 15. Plain Scripts ● Servlet-like ● request/response objects bound in context response.status = 200; response.contentType = “text/html”; var writer = response.writer; writer.println(“<html><head>....</body></html>”); writer.flush(); /sample.js /application/script/sample.js 15
  • 16. Controllers ● JavaScript “classes” with “methods” // package library.common.define(controller, quot;mainquot;, function() { function Main() { } // class Main.prototype.show = function() { // method library.view.render(“show.ejs”); } this.Main = Main; // export }); /@controller/@action /main/show 16
  • 17. Using JavaScript Libraries ● No special treatment for built-in libraries ● Dojo 0.9, Prototype, YUI all usable ● Load them in a controller (or using an event handler) library.dojo.load(“/application/dojo0.9”); library.scripting.run(“prototype.js”); ● Be careful in modifying Object.prototype and friends in the presence of pooling ● Other globals OK 17
  • 18. Scripting Engine Integration ● Optional compilation to bytecode ● Compiled scripts are cached ● Engines are pooled ● Works with any JSR-223 engine ● JavaScript engine hooked to debugger API 18
  • 19. Using Java Libraries ● Complete interoperability – Call into any Java libraries – Implement any Java interfaces – Subclass any Java classes ● Phobos can coexist with regular servlets and any Java web framework ● Tap into any Java libraries you need 19
  • 20. Mozilla Rhino + Extensions ● Dynamic objects via JSAdapter ● E4X ● property get/set with Rhino 1.6R6 ● Lots of constructs become possible: – autoloaded modules – builders – multiple inheritance – missing_method / doesNotUnderstand: 20
  • 21. E4X ● ECMA 357 ● XML support at the language level – literals, namespaces, interpolation ● XPath like syntax doc..*::customer.@name ● for each construct ● Powerful, not entirely intuitive 21
  • 22. E4X - Examples // HTML var doc = <html/>; doc.head.title = “Hello, world!”; doc.body.@bgcolor = “#224466”; doc.body.p = “What's up, doc?”; // ATOM var nsATOM = new Namespace(quot;atomquot;, quot;http://www.w3.org/2005/Atomquot;); default xml namespace = nsATOM; var doc = <entry><title>{args.title}</title><summary>{args.summary} /summary></entry>; 22
  • 23. Views / Templating with EJS ● PHP / ASP / RHTML-like syntax <html> <head> <title><%= model.title %></title> </head> <body> <% generateBody() %> </body> </html> 23
  • 24. Response Post-Processing ● Capture rendered page ● HTML parsed to DOM via TagLib ● Post-processing stage is similar to the browser model – scripts traverse/modify the document object ● Unified programming model 24
  • 25. jMaki ● Lightweight JavaScript framework ● Spans multiple widget libraries ● Unified interface to widgets ● Event-based component “glueing” ● CSS-based layouts ● Server-agnostic: JSP, JSF, Phobos, PHP 25
  • 26. jMaki Support in Phobos ● Built-in library.jmaki ● Configured with JSON ● Use common resource layout ● Widget palette in NetBeans ● Small footprint – Widget libraries added on first use 26
  • 27. REST Support ● Not controller-based ● Resources are classes ● Methods are HTTP methods ● Code deals with HTTP entities ● Framework takes care of HTTP details .e.g. ETags, conditional operations, etc. ● Sample AtomPub server 27
  • 28. Asynchronous Tasks ● Multithreaded runtime ● Schedule scripts for execution in the background ● Pass arguments in JSON format ● library.lang.invokeAsync calls a named function in a new thread ● Currently prototyping an Actor library ● Alternative: XMLHttpRequest-like object 28
  • 29. Persistence Options ● Java solutions more mature – JavaScript wrapper for Java Persistence – Native JavaScript DB API on top of JDBC ● Pure JavaScript solutions being developed – ActiveRecord port to JavaScript – Port of low-level Gears API – Port of GearsORM** 29
  • 30. “Soft” Scripting Engines ● Non-embedded DSL facility ● Translate to favorite target language ● Plug new languages ● Implemented by one JavaScript function ● Inherit debugging support from lower layer ● Example: Narrative JavaScript (.njs files) 30
  • 31. Narrative JavaScript ● New “yield” operator (->) for async calls ● Implemented as a preprocessor ● Separate browser/server runtime ● End goals: – Painless asynchronous processing – Continuation-like behavior without the cost 31
  • 32. IDE in the Browser ● Ongoing work ● Started with the “system apps” concept ● In-browser debugger prototype ● Running in the same space as the debuggee ● Some careful separation is in order... 32
  • 33. Demo 33
  • 34. GlassFish V3 ● Next-generation application server ● Open source, open development process ● Small footpring, fast startup ● Modular, extensible ● Ideal container for scripting applications 34
  • 35. Conclusions ● JavaScript is a real language ● Dynamic characteristics valuable on the server too ● Implementations getting more sophisticated every day ● Great integration with Java libraries and frameworks ● The question is: why not? 35
  • 36. Server-Side JavaScript with Project Phobos Roberto Chinnici roberto.chinnici@sun.com http://phobos.dev.java.net 36