36
Server-Side JavaScript with Project Phobos Roberto Chinnici Senior Staff Engineer Sun Microsystems, Inc. http://phobos.dev.java.net

Server Side Javascript

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Server Side Javascript

Server-Side JavaScriptwith Project Phobos

Roberto Chinnici

Senior Staff EngineerSun Microsystems, Inc.

http://phobos.dev.java.net

Page 2: Server Side Javascript

2

JavaScript is a real language

Page 3: Server Side Javascript

3

Small, terse languageClosures

Functional style OKPrototype-based object model

Object literalsCreative DSL uses

Page 4: Server Side Javascript

4

JavaScript in the browser

JSON on the wire+

cross-tier JavaScript code

JavaScript on the server

JavaScript in the database

Page 5: Server Side Javascript

5

JavaScript On All Tiers

Page 6: Server Side Javascript

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

Page 7: Server Side Javascript

7

Client Architecture

JAVASCRIPT APPLICATION CODE

JAVASCRIPT / AJAXLIBRARIES

BROWSER

Page 8: Server Side Javascript

8

Server Architecture

PHOBOSJAVASCRIPTLIBRARIES

JAVASCRIPT APPLICATION CODE

JAVASCRIPT /AJAX

LIBRARIES

JAVA™LIBRARIES

MOZILLA RHINO

PHOBOS RUNTIMERDBMS

JAVA™ PLATFORM

Page 9: Server Side Javascript

9

Core Phobos Functionality

● URL mapping/dispatching● Request/session/application contexts ● Asynchronous script execution● Container independence● Full-featured debugger● JSR-223 scripting engine integration

Page 10: Server Side Javascript

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

Page 11: Server Side Javascript

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

Page 12: Server Side Javascript

12

URL Design

● Plain scripts /doSomething.js● Controller/action /store/display_cart● Resource /book/isbn/1234-5678● User-defined any regexp

Page 13: Server Side Javascript

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

Page 14: Server Side Javascript

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

Page 15: Server Side Javascript

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

Page 16: Server Side Javascript

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”

Page 17: Server Side Javascript

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

Page 18: Server Side Javascript

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

Page 19: Server Side Javascript

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

Page 20: Server Side Javascript

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:

Page 21: Server Side Javascript

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

Page 22: Server Side Javascript

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?”;

Page 23: Server Side Javascript

23

Views / Templating with EJS

● PHP / ASP / RHTML-like syntax

<html> <head> <title><%= model.title %></title> </head> <body> <% generateBody() %> </body></html>

Page 24: Server Side Javascript

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

Page 25: Server Side Javascript

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

Page 26: Server Side Javascript

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

Page 27: Server Side Javascript

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

Page 28: Server Side Javascript

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

Page 29: Server Side Javascript

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**

Page 30: Server Side Javascript

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)

Page 31: Server Side Javascript

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

Page 32: Server Side Javascript

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...

Page 33: Server Side Javascript

33

Demo

Page 34: Server Side Javascript

34

GlassFish V3

● Next-generation application server● Open source, open development process● Small footpring, fast startup● Modular, extensible● Ideal container for scripting applications

Page 35: Server Side Javascript

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?

Page 36: Server Side Javascript

36

Roberto Chinnici

[email protected]

http://phobos.dev.java.net

Server-Side JavaScriptwith Project Phobos