67
What’s new in DWR v3 Joe Walker DWR Lead Developer SitePen UK

What's new in DWR version 3

Embed Size (px)

DESCRIPTION

A presentation of the new features in DWR version 3.

Citation preview

Page 1: What's new in DWR version 3

What’s new in DWR v3

Joe WalkerDWR Lead DeveloperSitePen UK

Page 2: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

RecapSince we last talked ...

Named ParametersBinary Files

JavaScript Extending JavaBetter Reverse Ajax

Data Sync: Dojo Data StoreJSON / JSONP / JSON-RPC

Varargs and Overloaded MethodsWhat’s Next

Page 3: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Recap

Page 4: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Page 5: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Marshalling Types

Primitive types, and their Object counterpartsint, boolean, long, float, double, etc

Obvious classesString, Date, BigDecimal, BigInteger, Enum, etc

Arrays and CollectionsMap, List, Set, Iterator, ...

JavaBeans and ObjectsXML objects

DOM, XOM, JDom, Dom4J

Page 6: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Page 7: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Since we last talked ...

TIBCO General InterfaceSitePenhttp://svn.directwebremoting.org

Page 8: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Named Parameters

Page 9: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Named Parameters

DWR will create client-side classes to look like server-side classes to make passing parameters easy

Page 10: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

public interface Person { ... }public class Employee implements Person { ... }public class Manager extends Employee { ... }

public HumanResources { public void addPerson(Person p) { ... }}

JavaScript:

Manager m = new Manager();HumanResources.addPerson(m);

Named Parameters

Page 11: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Named Parameters

Why?• Inheritance is useful in places• It saves creating addEmployee() and addManager()

methods

Page 12: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Lightweight Named Parameters

Page 13: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Lightweight Named Parameters

DWR also allows a lighter-weight method of declaring types

Page 14: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

public interface Person { ... }public class Employee implements Person { ... }public class Manager extends Employee { ... }

public HumanResources { public void addPerson(Person p) { ... }}

JavaScript:

var m = { $dwrClassName:'Manager', firstname:'Joe', ...};HumanResources.addPerson(m);

Lightweight Named Parameters

Page 15: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Lightweight Named Parameters

Why?• Everything as for Named Parameters• But sometimes you get an object from somewhere

else

Page 16: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: File Upload

Page 17: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: File Upload

DWR has always had a long list of things that it will marshall including Dates, DOM trees, etc

In addition, DWR will now marshall binary files just as if they were the text resources it handles now

Page 18: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

public Remoted { public void receiveBinaryFile(byte[] uploaded) { ... }}

HTML:

<input id='fileId' type='file'/>

JavaScript:

var binary = dwr.util.getValue('fileId');Remoted.receiveBinaryFile(binary);

Binary Files: File Upload

Page 19: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: File Upload

Will marshall to:• byte[]• java.awt.BufferedImage• java.io.InputStream• org.directwebremoting.io.FileTransfer

(gives access to filename and mime-type in addition to the contents)

Page 20: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: File Upload

Why?• This is a lot easier than using commons-fileupload

or similar• We can provide integration with progress bar

widgets

Page 21: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: Download

Page 22: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: Download

Binary file handling is 2 way. It’s good for:• Images• PDF files• Word, Excel documents• etc.

Page 23: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:public Remoted { public void getPDF(String contents) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); Document doc = new Document(); PdfWriter.getInstance(doc, buf); doc.open(); doc.add(new Paragraph(contents)); doc.close(); return new FileTransfer("ex.pdf", "application/pdf", buf.toByteArray());

}

JavaScript:Remoted.getPDF('Joe', function(data) { dwr.engine.openInDownload(data);});

Binary Files: Download

Page 24: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Binary Files: Download

Why?• This is a lot easier than creating a special PDF/

image/etc serving servlet

Page 25: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Javascript extending Java

Page 26: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Javascript extending Java

DWR will allow you to implement Java interfaces using JavaScript

Page 27: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:public interface BazListener { void somethingChanged(String msg);}public class Remote { public void addBazListener(BazListener bl) { ... }

public void calledLater() { for (BazListener bl : listeners) bl.somethingChanged("JS objects can implement Java interfaces"); } ...}

JavaScript:function BazListener() {this.$dwrByRef;}BazListener.prototype.somethingChanged = function(msg){alert(msg);};

var bl = new BazListener();Remote.addBazListener(bl);

Javascript extending Java

Page 28: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Javascript extending Java

Why?• Intuitive way to interact• Easy Pub-sub• Allows interaction with existing APIs

Page 29: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Scalable Reverse Ajax

Page 30: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Scalable Reverse Ajax

Previously there were some scalability limitations with the 2.0 reverse ajax API.3.0 deprecates the problem areas.

Page 31: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Scalable Reverse Ajax

Reverse Ajax proxies no longer take a list of ScriptSessions in the constructor, they just write to the current ‘destination’

The Browser API allows you to change the current ‘destination’

Page 32: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// The default destination is the browser// that caused the current action to happen.Window.alert("Hello");

// Non DWR thread have no default destinationThread t = new Thread(new Runnable()) { public void run() { // Error Window.alert("Hello"); }});

Scalable Reverse Ajax

Page 33: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Set the destination to be all browsers that// are looking at the current pageBrowser.withCurrentPage(new Runnable()) { public void run() { Window.alert("Hello"); }});

// Set the destination to be all browsers that// are looking at the current pageBrowser.withCurrentPage("index.html", new Runnable()) { public void run() { Window.alert("Hello"); }});

Scalable Reverse Ajax

Page 34: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Broadcast to everyoneBrowser.withAllSessions(...);

// Broadcast to subsetsBrowser.with*Filtered(scriptSesssionFilter, ...);

// To a known individualBrowser.withSession(sessionId, ...);

Scalable Reverse Ajax

Page 35: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Scalable Reverse Ajax

Why?• It’s generally easier to use• It decouples generation from routing• It scales

Page 36: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Reverse Ajax APIs

Page 37: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Reverse Ajax APIs

Reverse Ajax != Comet

Reverse Ajax == Comet + Polling + Piggyback

Page 38: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Low level: Use server-side W3C DOM methodsElement ele = doc.createElement("p");

ScriptSessions.addFunctionCall("document.body.appendChild",ele);

Reverse Ajax APIs: JS Level

Page 39: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Low level: Any arbitrary JavaScriptString s = "if (document.all) window.alert('IE');";

ScriptSessions.addScript(s);

Reverse Ajax APIs: JS Level

Page 40: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Some methods from Window and Documentimport javax.servlet.http.Cookie;import org.directwebremoting.ui.browser.Document;

Cookie c = new Cookie("name", "value");Document.setCookie(c);

Reverse Ajax APIs: DOM Level

Page 41: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// dwr.util in Javaimport org.directwebremoting.ui.dwr.Util;

String[] opts = new String[] {"one","two",...};Util.addOptions("li", opts);

Reverse Ajax APIs: dwr.util

Page 42: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Scriptaculous Effects in Javaimport org.directwebremoting.ui.scriptaculous.Effect;

Effect.fade("someId");

Reverse Ajax APIs: Scriptaculous

Page 43: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// TIBCO General Interface in Javaimport jsx3.GI;import jsx3.app.*;import jsx3.gui.*;

Server server = GI.getServer("servername");TextBox phoneNum = server.getJSXByName("phoneNum", TextBox.class);

phoneNum.setValue("servername");

Reverse Ajax APIs: TIBCO GI

Page 44: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

// Dojo in Javaimport org.dojotoolkit.dijit.Dijit;import org.dojotoolkit.dijit.Editor;

Editor e = Dijit.byId("price", Editor.class);e.setValue(42);

Reverse Ajax APIs: Dojo

Page 45: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Scalable Reverse Ajax

Why?• A full range of APIs for dynamically updating client

data• DWR doesn’t do widgets, but it does talk to the

people that do

Drapgen can be used to create and maintain large APIs

Page 46: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Dojo Data Store

Page 47: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Dojo Data Store

DWR now implements all 4 interfaces to allow Dojo to sync data with Java code on the server

Page 48: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

// Load the data somehowMap<String, Person> ppl = ...;

// Create an implementation of StoreProvider to hold the dataMapStoreProvider provider = new MapStoreProvider(ppl, Person.class);

// Tell DWR to expose the data to the internetDirectory.register("testServerData", provider);

Dojo Data Store

Page 49: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

HTML:

<table id="grid" dojoType="dojox.grid.DataGrid" ><tr> <th field="name" width="120px" editable="true">Name</th> ...</tr></table>

JavaScript:

dojo.registerModulePath("dwr", "path/from/dojo/to/dwr");dojo.require("dwr.data.Store");

dwrStore = new dwr.data.Store("testServerData", { subscribe:true });dijit.byId("grid").setStore(dwrStore);

Dojo Data Store

Page 50: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

// The StoreProvider from earlierMapStoreProvider provider = ...

// Get a representation of the internal dataMap<String, Person> data = provider.asMap();

// Mutate itdata.addPerson(new Person(...));

// The browsers viewing the data automagically update

Dojo Data Store

Page 51: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Dojo Data Store

Why?• Data-Sync APIs are hard to get right, but are really

simple to use• There is lots of potential for network level

optimization

Page 52: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

JSON / JSONP / JSON-RPC

Page 53: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Dojo Data Store

DWR now supports:• plain JSON• JSONP• JSON-RPC

Page 54: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

public class Demo { public sayHello(String name) { return "Hello, " + name; }}

Shell:

$ wget http://example.com/app/dwr/jsonp/Demo/sayHello? ↩ callback=callback&param0="Joe"

-> callback("Hello, Joe");

JSONP

Page 55: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Dojo:

dojo.io.script.get({ url:'http://example.com/app/dwr/jsonp/Demo/sayHello', content:{param:'Joe'}}).addCallback(function() { ... });

JQuery:

$.ajax({ dataType:'jsonp', data:'param=Joe', url:'http://example.com/app/dwr/jsonp/Demo/sayHello', success:function () { ... },});

JSONP

Page 56: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

JSON / JSONP / JSON-RPC

Why?• To allow DWR to remote functions to things other

than a DWR client• ‘DWRP’ is designed to be something we can change

without a long deprecation process

Page 57: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Varargs

Page 58: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Varargs

You can now call methods with a vararg parameter

Page 59: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:

public Remoted { public void method(String... arg) { ... }}

JavaScript:

Remoted.method("One", "Two", "Three");

Varargs

Page 60: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Varargs

Why?• It saves the hassle of wrapping options in an array

or collection before a method is called

Alert:• It could break some corner cases when mixing

servlet parameters with normal parameters

Page 61: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Overloaded Methods

Page 62: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Overloaded Methods

Previously DWR prevented you from reliably calling overloaded methods

Page 63: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Java:public Remoted { public void method(int num) { log.debug("int method called with " + num); } public void method(String str) { log.debug("String method called with " + str); }}

JavaScript:Remoted.method("String Param");Remoted.method(42);

Overloaded Methods

Page 64: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Overloaded Methods

Why?• It saves you from creating multiple proxy methods

to existing APIs

Page 65: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

What’s Next

Page 66: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Top Directions for DWR 3.1

Shorter release cycleGearsSMDDojo:

• Reverse Ajax API support• Auto-build

Rest

Page 67: What's new in DWR version 3

© SitePen, Inc. 2008. All Rights Reserved

Any Questions?

• http://directwebremoting.org• http://sitepen.com