Upload
rajivmordani
View
134
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Server-Side JavaScriptwith Project Phobos
Roberto Chinnici
Senior Staff EngineerSun Microsystems, Inc.
http://phobos.dev.java.net
2
JavaScript is a real language
3
Small, terse languageClosures
Functional style OKPrototype-based object model
Object literalsCreative DSL uses
4
JavaScript in the browser
JSON on the wire+
cross-tier JavaScript code
JavaScript on the server
JavaScript in the database
5
JavaScript On All Tiers
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
7
Client Architecture
JAVASCRIPT APPLICATION CODE
JAVASCRIPT / AJAXLIBRARIES
BROWSER
8
Server Architecture
PHOBOSJAVASCRIPTLIBRARIES
JAVASCRIPT APPLICATION CODE
JAVASCRIPT /AJAX
LIBRARIES
JAVA™LIBRARIES
MOZILLA RHINO
PHOBOS RUNTIMERDBMS
JAVA™ PLATFORM
9
Core Phobos Functionality
● URL mapping/dispatching● Request/session/application contexts ● Asynchronous script execution● Container independence● Full-featured debugger● JSR-223 scripting engine integration
10
Development Process
1. Start NetBeans2. Create skeleton application using wizard3. Run it in debug mode4. Map out the URLs for pages, services, Ajax5. Attach logic to them6. Test out interactively7. Go back to step 4, repeat8. Stop the application, you're done
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
12
URL Design
● Plain scripts /doSomething.js● Controller/action /store/display_cart● Resource /book/isbn/1234-5678● User-defined any regexp
13
Defining New Patterns
● Add new rules at startup or on the fly
application.mapping.rules.push({url: ”/feed/@id”,factory: "module.atom.createFeedResource",fn: "library.mapping.maybeREST"
});
Named functions in yellow
14
Application Layout/application
/controller
test.js
/dynamic
sample.ejsp
/module
application.js
/script
index.js
/template
/view
test.ejs
/static /resources ...jMaki... /css main.css faq.html release_notes.html
/environment development.js startup-webapp.js
15
Plain Scripts
response.status = 200;response.contentType = “text/html”;var writer = response.writer;writer.println(“<html><head>....</body></html>”);writer.flush();
● Servlet-like● request/response objects bound in context
/sample.js → /application/script/sample.js
16
Controllers
/@controller/@action → /main/show
// packagelibrary.common.define(controller, "main", function() { function Main() { } // class Main.prototype.show = function() { // method library.view.render(“show.ejs”); } this.Main = Main; // export});
● JavaScript “classes” with “methods”
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
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
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
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:
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
22
E4X - Examples
// ATOM
var nsATOM = new Namespace("atom", "http://www.w3.org/2005/Atom");
default xml namespace = nsATOM;
var doc = <entry><title>{args.title}</title><summary>{args.summary} /summary></entry>;
// HTMLvar doc = <html/>;doc.head.title = “Hello, world!”;doc.body.@bgcolor = “#224466”;doc.body.p = “What's up, doc?”;
23
Views / Templating with EJS
● PHP / ASP / RHTML-like syntax
<html> <head> <title><%= model.title %></title> </head> <body> <% generateBody() %> </body></html>
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
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
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
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
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
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**
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)
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
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...
33
Demo
34
GlassFish V3
● Next-generation application server● Open source, open development process● Small footpring, fast startup● Modular, extensible● Ideal container for scripting applications
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?
36
Roberto Chinnici
http://phobos.dev.java.net
Server-Side JavaScriptwith Project Phobos