Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

50
2007 JavaOne SM Conference | Session TS-6957 | TS-6957 Project Phobos: Server-Side Scripting for the Java™ Platform Roberto Chinnici Ludovic Champenois Senior Staff Engineers Sun Microsystems, Inc. http://phobos.dev.java.net/

Transcript of Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

Page 1: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 |

TS-6957

Project Phobos: Server-Side Scripting for the Java™ PlatformRoberto ChinniciLudovic ChampenoisSenior Staff EngineersSun Microsystems, Inc.http://phobos.dev.java.net/

Page 2: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 2

Learn how to build modern web applications the quick and easy way using Phobos

Goal of This Talk

Page 3: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 3

Agenda

What Is Phobos?Programming Model (With Demo)Ajax Using jMaki (With Demo)Extensibility, Persistence (With Demo)Conclusions

Page 4: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 4

Agenda

What Is Phobos?Programming Model (With Demo)Ajax Using jMaki (With Demo)Extensibility, Persistence (With Demo)Conclusions

Page 5: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 5

What Is Phobos?

● Lightweight application framework● Running on the Java platform● Supporting multiple scripting languages● Current focus is on JavaScript™ technology● Deploy to any Servlet container

Page 6: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 6

What Problem Does It Address?

● Scripting languages growing in popularity● Ajax places new emphasis on interactive

development—avoid the compile/deploy cycle● A scripting engine by itself is not enough● Tooling is an important aspect

Page 7: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 7

Key Functionality

● URL mapping● Java Specification Request (JSR)-223

scripting engine integration● Context management (scopes)● Container independence● Server-side JavaScript technology support

Page 8: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 8

Goal: Productivity and Performance

● Be more productive by developing selected parts of your web application in a scripting language

● Remove the impedance mismatch from Ajax● Glue together Java libraries and components● Deploy to a proven platform

Page 9: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 9

JavaScript Technology?

“JavaScript (technology) on servers will emerge as one

of several programming models popularized by Web

platforms by 2009 (0.7 probability)”

Gartner Report, November 21, 2006

Page 10: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 10

Quick Guide

● Language: JavaScript programming language, others

● URL dispatching: ordered regexps● Templates: EJS, FreeMarker, anything● ORM: Java Persistence API● Ajax: jMaki integration, other toolkits● Extras: all Java libraries

Page 11: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 11

Installing Phobos

● Set of NetBeans™ software plug-ins● Bootstrapped using the Ajax update center● Or use the Sun™ Web Developer Pack

Page 12: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 12

Agenda

What Is Phobos?Programming Model (With Demo)Ajax Using jMaki (With Demo)Extensibility, Persistence (With Demo)Conclusions

Page 13: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 13

Development Process

1.Start your IDE2.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, generate a war file9.Done!

Page 14: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 14

Application Layout/application

/controller

main.js

/dynamic

sample.ejsp

/module

application.js

resource.js

/script

index.js

/template

/view

main.ejs

/static

/resources

...jMaki... /css

main.css

faq.html

release_notes.html

/environment

development.js

startup-webapp.js

Page 15: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 15

● External “appearance” of your application● Keep URLs clean● Recognize certain patterns

Plain script: /doSomething.jsQualified operation: /store/display_cartResource: /catalog/isbn/1234-5678-90

● All can take query arguments ?view=html● Natural mapping to implementation logic

URL Design

Page 16: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 16

/application/scriptPlain Scripts

● Servlet-like, but written in any language

response.status = 200;response.contentType = "text/html";var writer = response.writer;writer.println("<html><head><title>Hello from Javascript</title></head><body>”);for (var i = 0; i < 10; ++i) {

writer.println(“Hello from Javascript!<br>”);}writer.println(“</body></html>");writer.flush();

Page 17: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 17

/application/controller - /application/view - modelControllers

● MVC pattern, in JavaScript programming language

library.common.define(controller, “main”, function() {

// constructor this.Main = function() {}

// action method this.Main.prototype.show = function() { library.view.render(“main.ejs”); }}

● /main/show parsed as /@controller/@action

Page 18: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 18

Views—Embedded JavaScript Technology Files

● Always rendered by controllers● Simple templating system, PHP-like● Embedded JavaScript technology statements

<% ... statements ... %>● Embedded JavaScript technology expressions

<%= ... expression ... %>

/application/view - .ejs extension

Page 19: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 19

System Apps

● http://myserver:8888/system● In-browser development helpers

● Code generation, URL mapping, CRUD, ...● Part of the running application● “Eat your own dog food”● IDE in a browser?

Page 20: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 20

DEMOSample application using NetBeans IDE

Page 21: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 22: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 23: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 24: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 25: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 26: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 27: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 28: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 29: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform
Page 30: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 30

Agenda

What Is Phobos?Programming Model (With Demo)Ajax Using jMaki (With Demo)Extensibility, Persistence (With Demo)Conclusions

Page 31: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 31

Architecture

SCRIPTSVIEWS

CONTROLLERS

TEMPLATES,STATIC CONTENT,

ADDITIONAL LIBRARIES...

ANY CONTAINER

SCRIPTING LIBRARIES AJAXLIBRARIES

JAVALIBRARIES

JAVA PLATFORM

SCRIPTING ENGINES

PHOBOS ADAPTER

Page 32: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 32

Multiple Extension Points

● Adapter to swap in a new container● JSR 223 for scripting engines● Java libraries● Ajax/client libraries● Extensions at the JavaScript technology level● Fully customizable URL mappings

Page 33: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 33

Default URL Mappings

● Several predefined patterns● No configuration needed

Index pageStatic contentScriptControllerPHP-like content

//[path/]static_content/[path/]scriptname/controller[/action][/id]/[path/]dynamic_content

Page 34: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 34

/application/moduleResources

● REST framework● Resources are classes● Methods are HTTP methods: GET, PUT, ...● Code deals with HTTP entities

● Content type, payload, extension headers● Many HTTP aspects offloaded to framework

Page 35: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 35

/application/module/application.js - onStartupDeclaring New URL Mappings

● Add a new rule at startup

application.mapping.rules.push({ url: ”/collection/@id”, factory: "module.atom.createCollectionResource", fn: "library.mapping.maybeREST"});

application.mapping.rules.push({ url: “/”, script: “index.rb”});

Page 36: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 36

Phobos on GlassFish™ Build v.3

● New, modular application server runtime● Phobos as a lightweight container● No dependency on the Servlet container● Fast startup, small memory footprint

Page 37: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 37

JSR 223 Scripting Engines

● Automated engine discovery● Just drop a new engine in the classpath● Engine selected based on the file extension

● .js .rb .py .groovy .xslt .scm ...● Many engines available on java.net

http://scripting.dev.java.net/● Practically all of them have the ability to call

from scripting into Java code

Page 38: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 38

JavaScript Technology in Phobos

● Mozilla Rhino 1.6R4● Robust, fast implementation● Optional compilation to bytecode● Built-in debugging support● Powerful interface to Java code● Many language extensions

Page 39: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 39

Accessing Java Libraries

● Integrated JavaScript-Java programming language bridge

● Bean properties become JavaScript technology properties

● Often can copy and paste Java source codevar builder = new Packages.org.jdom.input.SAXBuilder();var doc = builder.build( new java.io.FileInputStream(“a.xml”));

// response is a javax.servlet.http.HttpServletResponseresponse.setStatus(200);response.status = 200; // equivalent

Page 40: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 40

JavaScript Technology Extensions in Phobos

● Continuations● Dynamic objects● Allow many advanced constructs:

● Multiple inheritance● Autoloaded modules● Builders● DoesNotUnderstand: / missing_method

● E4X

Page 41: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 41

E4X

● XML support at the language level● XPath like search syntax// HTML examplevar doc = <html/>;doc.head.title = “Hello, world!”;doc.body.@bgcolor = “#224466”;doc.body.p = “This is all the text on this page.”;

// Atom exampledefault xml namespace = new Namespace("atom", "http://www.w3.org/2005/Atom");var feed = <feed><title>{title}</title><author><name>{author}</name></author></feed>;

Page 42: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 42

Persistence in Phobos

● Thin wrapper around Java Persistence API● JavaScript technology model, view, controller

generated based on JPA entity classes

1.Create a JPA library project2.Add it to the Phobos classpath3.Run the generator4.Customize ad lib

Page 43: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 43

Persistence Mapping

module.myPU.Customer

demo.Customer

CUSTOMER Database table

Entity class in apersistence unit

JavaScripttechnology module

JPA

Phobos

Views / Controllers

Page 44: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 44

DEMOPersistence

Page 45: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 45

Agenda

What Is Phobos?Programming Model (With Demo)Ajax Using jMaki (With Demo)Extensibility, Persistence (With Demo)Conclusions

Page 46: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 46

Phobos Summary

● Fast, interactive development model● Targeted at rich web applications (Ajax)● Complementary to existing Java technologies

● Persistence, web services, JavaServer Faces™ platform, Enterprise JavaBeans™ (EJB™), ...

● Full IDE support in NetBeans IDE

Page 47: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 47

For More Information

● Phoboshttp://phobos.dev.java.net/

● Project jMakihttp://ajax.dev.java.net/● Sessions TS-6375, TS-9516

● Project GlassFishhttp://glassfish.dev.java.net/

● Sun Web Developer Packhttp://developers.sun.com/web/swdp/

Page 48: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 48

Q&ARoberto ChinniciLudovic Champenois

Page 49: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 |

TS-6957

Project Phobos: Server-Side Scripting for the Java™ PlatformRoberto ChinniciLudovic ChampenoisSenior Staff EngineersSun Microsystems, Inc.http://phobos.dev.java.net/

Page 50: Project Phobos: Server-Side Scripting for the Javaâ„¢ Platform

2007 JavaOneSM Conference | Session TS-6957 | 50

Shortcut—Self-Rendering Views

● Views that don’t need a controller● Useful to add dynamic behavior to existing,

static HTML pages● Complete analogy with PHP● Unlike PHP, you don't have to use them

all the time!

/application/dynamic - .ejsp extension