JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

21
javaONE 2007 - Kjartan Aanestad (Objectware) 1 JavaONE 2007

Transcript of JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

Page 1: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

javaONE 2007 - Kjartan Aanestad (Objectware) 1

JavaONE 2007

Page 2: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

2javaONE 2007 - Kjartan Aanestad (Objectware)

Agenda•Reverse Ajax - DWR

•GlassFish V3

•Effective Java Reloaded: This Time it's NOT for Real

Page 3: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

3javaONE 2007 - Kjartan Aanestad (Objectware)

Reverse Ajax - DWR

•Joe Walker/Geert Bevin (TS-6410)

•New feature in DWR 2.0

•CommunityOne: ICEfaces and Ajax Push

Page 4: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

4javaONE 2007 - Kjartan Aanestad (Objectware)

Reversed Ajax – DWR:Overview•The ability to asynchronously send data from a web-server

to a browser

Page 5: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

5javaONE 2007 - Kjartan Aanestad (Objectware)

Reversed Ajax – DWR:Overview

•Supports 3 methods of pushing the data to the browser:

• Polling – Browser makes regular request to the server

• Piggyback – Puts the response with the next request

• Comet (aka Long lived http) – Keeps the communication channel open to pass down information when time comes.

Page 6: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

6javaONE 2007 - Kjartan Aanestad (Objectware)

Reversed Ajax – DWR:”live” coding demo

Page 7: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

7javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3

•Jerome Dochez (TS-6503)

•Loosely based on the work for JSR 277 (Java Module System )

•Due in Java SE 7

•Architecture based on IoC, modules and maven 2

Page 8: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

8javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3:Module subsystem

•The module subsystem is called HK2 (Hundred Kb Kernel)

•Module system based on maven

•Identified by name and version

•One classloader per module of 1 to n jars

•Exports a subset of its content (Service Provider Interface)

•Imports other modules (listed in manifest file)

Page 9: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

9javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3:Module instances• Modules identified by module instances at runtime

• 2 classloaders (public/private)

• Runtime network of class loaders

Page 10: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

10javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3:Repository

•Repositories hold modules

•Add and remove at runtime

•Different types supported

• Directory based

• Maven

•Modules can be added/removed/updated from repositories

Page 11: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

11javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3:Bootstrapping

•Module subsystems can bootstrap itself

•No need to define a classpath at invocation

•Packaged in a jar

•Implement the ApplicationStartup interface

•Declare dependencies in manifest

Page 12: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

12javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3:Build System: Maven

•Each module is build from a maven project (pom.xml)

<groupId>com.sun.enterprise.glassfish</groupId><artifactId>gf-web-connector</artifactId><version>1.2.1</version><packaging>hk2-jar</packaging>

•Running GlassFish retrieves the modules from the maven repository

$ mvn gf:run

Page 13: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

13javaONE 2007 - Kjartan Aanestad (Objectware)

GlassFish V3:Services

•GlassFish V3 use extensively Services to identify extension points like

• Application containers like web-app, Jruby, Phobos..)

• Administrative commands

•Services in V3:

• Interfaces are declared with @Contract

• Implementations are declared with @Service

@Contractpublic interface AdminCommand {...}

@Service(name=”deploy”)public class DeployCommand implements AdminCommand {...}

Page 14: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

14javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: This Time it's NOT for Real

•Joshua Bloch (TS-2689)

•Effective Java still hasn’t been reloaded - It will be done later this year for sure..

•Object creation

•Generics

Page 15: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

15javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: Object creation

•Static factories

Map<String, List<String>> m = new HashMap<String, List<String>>();

Map<String, List<String>> m = HashMap.newInstance();

•Regrettably HashMap has no such method (yet)

• write your own, your generic classes can and should:

public static <K, V> HashMap<K, V> newInstance() { return new HashMap<K, V>();}

Page 16: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

16javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: Object creation•Builder pattern

• Ugly when constructors have many optional parameters

new NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int

carbohydrate, 15 more optional params!);

• Builder constructor takes all required params

• One setter for each optional parameter

• Setter returns the builder to allow for chaining

NutritionFacts locoCola = new NutritionFacts.Builder(240, 8)

.sodium(30).carbohydrate(28).build();

Page 17: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

17javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: Object creationpublic class NutritionFacts { public static class Builder { public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } ... // 15 more setters

public NutritionFacts build() { return new NutritionFacts(this); } }

private NutritionFacts(Builder builder) { .. }}

Page 18: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

18javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: Generics – bounded wildcards•Use bounded wildcards to increase applicability of APIs

public interface Shop<T> { void buy(int numToBuy, Collection<T> myColl); void sell(Collection<T> myLot);}

•Collection subtyping doesn’t work!

public interface Shop<T> { void buy(int numToBuy, Collection<? super T> myColl); void sell(Collection<? extends T> myLot);}

•Use <? extends T> when parameterized instance is a T producer (“for read/input”)

•Use <? super T> when parameterized instance is a T consumer (“for write/output”)

Page 19: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

19javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: Generics – wildcard capture•Control Wildcard-Capture

•Type system doesn’t know captured types are identical

public static void rotate(List<?> list) { if (list.size() == 0) return; list.add(list.remove(0));}

•Solution:

public static void rotate(List<?> list) { rotateHelper(list);}// Generic helper method captures wildcard onceprivate static <E> void rotateHelper(List<E> list) { if (list.size() == 0) return; list.add(list.remove(0));}

Page 20: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

20javaONE 2007 - Kjartan Aanestad (Objectware)

Effective Java Reloaded: Generics – miscellania

•final is the new private

• Minimizes mutability

• Clearly thread-safe—one less thing to worry about

•Use the @Override annotation every time you want to override

• Avoid overriding my mistake

Page 21: JavaONE 2007 - Kjartan Aanestad (Objectware)1 JavaONE 2007.

javaONE 2007 - Kjartan Aanestad (Objectware) 21

JavaONE 2007Kjartan Aanestad - Objectware