Server-Side JavaScript
 with Project Phobos

 Roberto Chinnici

 Senior Staff Engineer
 Sun Microsystems, Inc.

 http://ph...
JavaScript is a real language




                                2
Small, terse language
          Closures
     Functional style OK
Prototype-based object model
        Object literals
   ...
JavaScript in the browser
    JSON on the wire
             +
cross-tier JavaScript code
 JavaScript on the server
JavaScr...
JavaScript On All Tiers




                          5
What is Phobos?
●   Lightweight web application framework
●   Running on the Java™ platform
●   All application logic is J...
Client Architecture

       JAVASCRIPT APPLICATION CODE



             JAVASCRIPT / AJAX
                LIBRARIES




  ...
Server Architecture

       JAVASCRIPT APPLICATION CODE



     JAVASCRIPT /         PHOBOS
         AJAX           JAVASC...
Core Phobos Functionality
●   URL mapping/dispatching
●   Request/session/application contexts
●   Asynchronous script exe...
Development Process
1. Start NetBeans
2. Create skeleton application using wizard
3. Run it in debug mode
4. Map out the U...
Tool Support
●   Phobos ships as NetBeans plugins
●   Project and component wizards
●   jMaki widget palette
●   Multithre...
URL Design
●   Plain scripts       /doSomething.js
●   Controller/action   /store/display_cart
●   Resource            /bo...
Defining New Patterns
●   Add new rules at startup or on the fly

    application.mapping.rules.push({
          url: ”/fe...
Application Layout
/application          /static
  /controller           /resources
     test.js               ...jMaki......
Plain Scripts
●   Servlet-like
●   request/response objects bound in context

response.status = 200;
response.contentType ...
Controllers
●     JavaScript “classes” with “methods”
// package
library.common.define(controller, quot;mainquot;, functio...
Using JavaScript Libraries
●   No special treatment for built-in libraries
●   Dojo 0.9, Prototype, YUI all usable
●   Loa...
Scripting Engine Integration
●   Optional compilation to bytecode
●   Compiled scripts are cached
●   Engines are pooled
●...
Using Java Libraries
●   Complete interoperability
    –   Call into any Java libraries
    –   Implement any Java interfa...
Mozilla Rhino + Extensions
●   Dynamic objects via JSAdapter
●   E4X
●   property get/set with Rhino 1.6R6
●   Lots of con...
E4X
●   ECMA 357
●   XML support at the language level
    –   literals, namespaces, interpolation
●   XPath like syntax
 ...
E4X - Examples
// HTML
var doc = <html/>;
doc.head.title = “Hello, world!”;
doc.body.@bgcolor = “#224466”;
doc.body.p = “W...
Views / Templating with EJS
●   PHP / ASP / RHTML-like syntax

    <html>
      <head>
        <title><%= model.title %></...
Response Post-Processing
●   Capture rendered page
●   HTML parsed to DOM via TagLib
●   Post-processing stage is similar ...
jMaki
●   Lightweight JavaScript framework
●   Spans multiple widget libraries
●   Unified interface to widgets
●   Event-...
jMaki Support in Phobos
●   Built-in library.jmaki
●   Configured with JSON
●   Use common resource layout
●   Widget pale...
REST Support
●   Not controller-based
●   Resources are classes
●   Methods are HTTP methods
●   Code deals with HTTP enti...
Asynchronous Tasks
●   Multithreaded runtime
●   Schedule scripts for execution in the
    background
●   Pass arguments i...
Persistence Options
●   Java solutions more mature
    –   JavaScript wrapper for Java Persistence
    –   Native JavaScri...
“Soft” Scripting Engines
●   Non-embedded DSL facility
●   Translate to favorite target language
●   Plug new languages
● ...
Narrative JavaScript
●   New “yield” operator (->) for async calls
●   Implemented as a preprocessor
●   Separate browser/...
IDE in the Browser
●   Ongoing work
●   Started with the “system apps” concept
●   In-browser debugger prototype
●   Runni...
Demo




       33
GlassFish V3
●   Next-generation application server
●   Open source, open development process
●   Small footpring, fast st...
Conclusions
●   JavaScript is a real language
●   Dynamic characteristics valuable on the
    server too
●   Implementatio...
Server-Side JavaScript
 with Project Phobos

 Roberto Chinnici

 roberto.chinnici@sun.com

 http://phobos.dev.java.net



...
Upcoming SlideShare
Loading in...5
×

Server Side Javascript

2,542
-1

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,542
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
39
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Server Side Javascript

  1. 1. Server-Side JavaScript with Project Phobos Roberto Chinnici Senior Staff Engineer Sun Microsystems, Inc. http://phobos.dev.java.net
  2. 2. JavaScript is a real language 2
  3. 3. Small, terse language Closures Functional style OK Prototype-based object model Object literals Creative DSL uses 3
  4. 4. JavaScript in the browser JSON on the wire + cross-tier JavaScript code JavaScript on the server JavaScript in the database 4
  5. 5. JavaScript On All Tiers 5
  6. 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. 7. Client Architecture JAVASCRIPT APPLICATION CODE JAVASCRIPT / AJAX LIBRARIES BROWSER 7
  8. 8. Server Architecture JAVASCRIPT APPLICATION CODE JAVASCRIPT / PHOBOS AJAX JAVASCRIPT LIBRARIES LIBRARIES MOZILLA RHINO JAVA™ LIBRARIES RDBMS PHOBOS RUNTIME JAVA™ PLATFORM 8
  9. 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. 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. 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. 12. URL Design ● Plain scripts /doSomething.js ● Controller/action /store/display_cart ● Resource /book/isbn/1234-5678 ● User-defined any regexp 12
  13. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 23. Views / Templating with EJS ● PHP / ASP / RHTML-like syntax <html> <head> <title><%= model.title %></title> </head> <body> <% generateBody() %> </body> </html> 23
  24. 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. 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. 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. 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. 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. 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. 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. 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. 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. 33. Demo 33
  34. 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. 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. 36. Server-Side JavaScript with Project Phobos Roberto Chinnici roberto.chinnici@sun.com http://phobos.dev.java.net 36
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×