java 6 [mustang] - features and enchantments

49
1 Visit me @ www.Pavel-Kaminsky.com Java 6 : Mustang Unleashed!

Upload: pavel-kaminsky

Post on 19-Jan-2015

5.074 views

Category:

Education


2 download

DESCRIPTION

find the full lecture on http://www.pavel-kaminsky.com/uncategorized/java-6-mustang/

TRANSCRIPT

Page 1: Java 6 [Mustang] - Features and Enchantments

1Visit me @ www.Pavel-Kaminsky.com

Java 6 : Mustang

Unleashed!

Page 2: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 2

Source Code SVN

All Sources can be checked out (SVN) from here:

http://code.google.com/p/java6-features/

Page 3: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 3

Page 4: Java 6 [Mustang] - Features and Enchantments

Language Enhancements in Java SE 6

No language changes were introduced in Java SE 6 !!!!!!!!!

where is my “syntactic sugar”?!

Visit me @ www.Pavel-Kaminsky.com 4

Page 5: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 5

Page 6: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 6

JAX-WS 2.0 (JSR 224)• includes– “Java Architecture for XML Binding” (JAXB) 2.0.

– “SOAP with Attachments API for Java” (SAAJ) 1.3.

Page 7: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 7

@WebServicepackage webServices;

import javax.jws.WebService;

import javax.xml.ws.Endpoint;

/**

* @Author: [email protected]

* @Date: 11/09/11

*/

@WebService

public class CalculatorWS {

public int add(int a , int b) {

return a+b;

}

public static void main(String[] args) {

Endpoint.publish("http://localhost:8080/calculator",new CalculatorWS());

}

}

Page 8: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 8

Page 9: Java 6 [Mustang] - Features and Enchantments

Scripting (JSR 223)• Scripting language independent framework for using script engines from Java code

• Write customizable/extendable applications in the Java language and leave the customization scripting language choice to the end user

• The Java application developer need not choose the extension language during development

• Sun's implementation of JDK 6 is co-bundled with the Mozilla Rhino based JavaScript script engine.

Visit me @ www.Pavel-Kaminsky.com 9

Page 10: Java 6 [Mustang] - Features and Enchantments

Scripting – examplepackage scripting.example2;

/**

* @Author: [email protected]

* @Date: 31/08/11

*/

import javax.script.*;

import java.io.File;

public class InvokeScriptMethod {

public static void main(String[] args) throws Exception {

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("JavaScript");

engine.eval(new java.io.FileReader(new File("src/scripting/example2/script.js").getAbsoluteFile()));

Invocable inv = (Invocable) engine;

// get script object on which we want to call the method

Object obj = engine.get("obj");

// invoke the method named "hello" on the script object "obj"

inv.invokeMethod(obj, "rock", "Pavel-Kaminsky.com" );

}

}

Visit me @ www.Pavel-Kaminsky.com 10

Page 11: Java 6 [Mustang] - Features and Enchantments

Scripting – example

• Scripts.jsvar obj = new Object();

obj.rock = function(name){

result = name + ',Are you ready to rock?!!'; print(result); return result;

}

• Output : Pavel Kaminsky , Are you ready to rock?!!

• Answer: HAIL YEAH!!!

Visit me @ www.Pavel-Kaminsky.com 11

Page 12: Java 6 [Mustang] - Features and Enchantments

Importing Java Packagespackage scripting.example3;

/** * @Author: [email protected] * @Date: 31/08/11 */

import javax.script.Invocable;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import java.io.File;import java.util.ArrayList;

public class ScriptMethodImportPackages { public static void main(String[] args) throws Exception {

ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); engine.eval(new java.io.FileReader(new File("src/scripting/example3/script.js").getAbsoluteFile())); Invocable inv = (Invocable) engine; String myArray[] = (String[]) engine.get("myArray"); ArrayList yourArray = (ArrayList)engine.get("yourArray");

System.out.println(myArray[myArray.length-1]); System.out.println(yourArray.get(yourArray.size()-1));

}}

Visit me @ www.Pavel-Kaminsky.com 12

Page 13: Java 6 [Mustang] - Features and Enchantments

Scripting – example 1

• Scripts.js

var myArray = java.lang.reflect.Array.newInstance(java.lang.String, 1);var yourArray = new java.util.ArrayList();

myArray[0] = "this is MY Array!";yourArray.add("this is YOUR Array!");

• Output :

this is MY Array!this is YOUR Array!

Visit me @ www.Pavel-Kaminsky.com 13

Page 14: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 14

Page 15: Java 6 [Mustang] - Features and Enchantments

Database• Shipped with a Java DB ( = DB Apache Derby)

– A transactional, relational database.– storage of a database archived in a JAR file,

which allows you to simply distribute the JAR file.

• Start/Stop locally :• java jar derbyrun.jar server start 192.168.0.1 1555�

• Accessed via JDBC

• The $JAVA_HOME/db subdirectory contains class libraries for Java DB, Sun Microsystems's distribution of the Apache Derby database technology.

Visit me @ www.Pavel-Kaminsky.com 15

Page 16: Java 6 [Mustang] - Features and Enchantments

Database Examplepackage database;

/** * @Author: [email protected] * @Date: 31/08/11 */

import java.sql.*;

public class JavaDBExample { static Connection conn;

public static void main(String[] args) throws Exception {

if (args.length != 2) { System.out.println("Usage: <Name> <Address>"); System.exit(1); }

String dbName = "UsersDB"; String connectionURL = "jdbc:derby:" + dbName + ";create=true"; Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); conn = DriverManager.getConnection(connectionURL);

// create table Statement stmt = conn.createStatement(); stmt.executeUpdate("CREATE TABLE USERS (ID NUMBER NOT NULL, UserName VARCHAR(250) NOT NULL)");

Visit me @ www.Pavel-Kaminsky.com 16

Page 17: Java 6 [Mustang] - Features and Enchantments

// insert user PreparedStatement psInsert = conn.prepareStatement("insert into USERS

values (?,?)"); psInsert.setInt(1, 1); psInsert.setString(2, "Lenny Kravitz"); psInsert.executeUpdate();

// load user Statement usetStmt = conn.createStatement(); ResultSet rs = usetStmt.executeQuery("select * from USERS");

while (rs.next()) { System.out.println("Id: " + rs.getInt(1) + " Name" +

rs.getString(2)); } rs.close(); }}

• Output :

Id:1 Name:Lenny Kravitz

Visit me @ www.Pavel-Kaminsky.com 17

Page 18: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 18

Page 19: Java 6 [Mustang] - Features and Enchantments

java.awt.Desktop class

• Minimize the difference between the performance and integration of native applications and Java applications.

• Adopted JDesktop Integration Components (JDIC) API (http://jdic.dev.java.net/)

Visit me @ www.Pavel-Kaminsky.com 19

Page 20: Java 6 [Mustang] - Features and Enchantments

Java.awt.Desktoppublic static enum Action { /** * Represents an "open" action. * @see Desktop#open(java.io.File) */

OPEN, /** * Represents an "edit" action. * @see Desktop#edit(java.io.File) */

EDIT, /** * Represents a "print" action. * @see Desktop#print(java.io.File) */

PRINT, /** * Represents a "mail" action. * @see Desktop#mail() * @see Desktop#mail(java.net.URI) */

MAIL, /** * Represents a "browse" action. * @see Desktop#browse(java.net.URI) */

BROWSE };

Visit me @ www.Pavel-Kaminsky.com 20

Page 21: Java 6 [Mustang] - Features and Enchantments

• Desktop.isDesktopSupported() – checks whether Desktop functionality is available.

• Desktop desktop = Desktop.getDesktop(); desktop.isSupported(Desktop.Action) – check whether the functionality is available.

• 5 New functionalities : public void mail() throws IOException public void open(File file) throws IOException public void edit(File file) throws IOException public void print(File file) throws IOException public void browse(URI uri) throws IOException

Let’s See a DEMO!

Visit me @ www.Pavel-Kaminsky.com 21

Page 22: Java 6 [Mustang] - Features and Enchantments

System Tray Functionality

• Now it’s possible to hook the “System Tray”– SystemTray tray = SystemTray.getSystemTray();

public TrayIcon(Image image, String tooltip, PopupMenu popup)

public static boolean isSupported() public void add(TrayIcon trayIcon) throws AWTException public void remove(TrayIcon trayIcon)

Let’s See a DEMO!

Question : why there is no AWTException thrown on “remove” method?

Visit me @ www.Pavel-Kaminsky.com 22

Page 23: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 23

Page 24: Java 6 [Mustang] - Features and Enchantments

Monitoring and Management

• Improvements in jconsle• “jps” tool - Roll call of all the names and

numbers of all the Java processes on your machine. Good for other command lines as they required id’s.

>C:\Users\kaminskp>jps.exe>5484>5324>7816 Jps

• “jhat” - Java Heap Analysis Tool

Visit me @ www.Pavel-Kaminsky.com 24

Page 25: Java 6 [Mustang] - Features and Enchantments

Usage• Find java application id with jps.• Create a dump with jmap into a file.

C:\Users\kaminskp>jmap -dump:file=output.txt <id>

• Jhat the dump file.C:\Users\kaminskp>jhat output.txt Reading from output.txt...Dump file created Thu Sep 01 13:18:09 IDT 2011Snapshot read, resolving...Resolving 45981 objects...Chasing references, expect 9 dots.........Eliminating duplicate references.........Snapshot resolved.Started HTTP server on port 7000Server is ready.• Browse to http://localhost:7000

Visit me @ www.Pavel-Kaminsky.com 25

Let’s see an Example!

Page 26: Java 6 [Mustang] - Features and Enchantments

Personal Recommendation

• Don’t use JHat.• Use IBM’s MAT (Memory Analyzing Tool)

instead , download here.

Visit me @ www.Pavel-Kaminsky.com 26

Page 27: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 27

Page 28: Java 6 [Mustang] - Features and Enchantments

Compiler Access (JSR 199)

• Compile java source files from within java files• No need to fork into new javac.exe process

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

int run(InputStream in, OutputStream out, OutputStream err, String... arguments);

• Good for IDE’s (compiling on the fly)

Visit me @ www.Pavel-Kaminsky.com 28

compiler.run(null, null, null, “One.java”, “Two.java”);

Let’s see an Example!

Page 29: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 29

Page 30: Java 6 [Mustang] - Features and Enchantments

Pluggable Annotations (JSR 269)

• @Annotation – remember that ?!?• Common annotations :

@SuppressWarnings@Override @Deprecated

• Integrate custom annotation with compiler!

Visit me @ www.Pavel-Kaminsky.com 30

Page 31: Java 6 [Mustang] - Features and Enchantments

Example (included in the example’s code)

• Let’s create a new annotation

package pluggableAnnotations;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention( RetentionPolicy.SOURCE )

@Target( ElementType.METHOD )

public @interface Borat {

}

Visit me @ www.Pavel-Kaminsky.com 31

Page 32: Java 6 [Mustang] - Features and Enchantments

BoratBuilderProcessorpackage pluggableAnnotations;

import javax.annotation.processing.AbstractProcessor;import javax.annotation.processing.RoundEnvironment;import javax.annotation.processing.SupportedAnnotationTypes;import javax.annotation.processing.SupportedSourceVersion;import javax.lang.model.SourceVersion;import javax.lang.model.element.Element;import javax.lang.model.element.TypeElement;import javax.tools.Diagnostic;import java.util.Set;

@SupportedAnnotationTypes("pluggableAnnotations.Borat")@SupportedSourceVersion(SourceVersion.RELEASE_6)

public class BoratBuilderProcessor extends AbstractProcessor {

@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

for (TypeElement typeElement : annotations) { for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) { this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,

"I Like! but we don't use this method in Kazakhstan", element); } } return true; }}

Visit me @ www.Pavel-Kaminsky.com 32

Page 33: Java 6 [Mustang] - Features and Enchantments

Packaging

• Create an “META-INF/Services” Folder• Add a text file named

javax.annotation.processing.Processor• Insert the path to BoratBuilderProcessor into

the file• Package into Jar!

Visit me @ www.Pavel-Kaminsky.com 33

Page 34: Java 6 [Mustang] - Features and Enchantments

Visual ..

Visit me @ www.Pavel-Kaminsky.com 34

Page 35: Java 6 [Mustang] - Features and Enchantments

Create a TestApp

• Link the jar into your project (I use IntelliJ)

Visit me @ www.Pavel-Kaminsky.com 35

Page 36: Java 6 [Mustang] - Features and Enchantments

Tester.javaimport pluggableAnnotations.Borat;import javax.annotation.processing.Processor;import java.util.ServiceLoader;

public class Tester {

@Borat public static String annotateMe() { return "Welcome to America"; }

public static void main(String[] args) { ServiceLoader<Processor> processors =

ServiceLoader.load(Processor.class); for (Processor processor : processors) { System.out.println(processor.getClass().getSimpleName() + " : " +

processor.getClass().getCanonicalName()); }

System.out.println(Tester.annotateMe()); }}

Visit me @ www.Pavel-Kaminsky.com 36

Page 37: Java 6 [Mustang] - Features and Enchantments

javac

• What do you think would happen?

Visit me @ www.Pavel-Kaminsky.com 37

Page 38: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 38

Page 39: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com

Desktop Deployment• Swing Improvements for Windows and GTK look and Feel

• Can you tell the difference?!

29

Page 40: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 40

More “Perks”• JTable Sorting and Filtering

no more customs implemantations.

JTable table = new JTable(model);final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);table.setRowSorter(sorter);

sorter.setRowFilter(RowFilter.regexFilter(text));

• New Modality – Modeless – no modality– Document-modal - blocks all windows from the same document , except those from its

child hierarchy– Application-modal - blocks all windows from the same application , except those from its

child hierarchy– Toolkit-modal - blocks all windows that run in the same toolkit , except those from its child

hierarchy

Page 41: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 41

More “Perks”• Splash Screen – revamped

• Security Dialogs

Page 42: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 42

Page 43: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 43

Security

• Kerberos Integration• Support for Smart Card I/O API• JAAS-based authentication using LDAP

Page 44: Java 6 [Mustang] - Features and Enchantments

What’s New?• Web Services• Scripting• Database• More Desktop APIs• Monitoring and Management• Compiler Access• Pluggable Annotations• Desktop Deployment• Security• Performance and Quality

Visit me @ www.Pavel-Kaminsky.com 44

Page 45: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 45

Performance

Page 46: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 46

SPECjbb2005 Test• SPECjbb2005 is an industry-standard benchmark designed

to measure the server-side performance of Java runtime environments

• SPECjbb2005 bops (business operations per second), obtained by averaging the total transaction rate in a SPECjbb2005 run from the expected peak number of warehouses, to twice the peak number of warehouses

• Bops metric measures the overall throughput achieved by all the JVMs in a benchmark run. SPECjbb2005 bops/JVM reflects the contribution of a single JVM to the overall metric , and is thus a measure of the performance and scaling of a single JVM

• http://www.spec.org/jbb2005/results/

Page 47: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 47

Server Side Performance

Page 48: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 48

Server Side Performance

Page 49: Java 6 [Mustang] - Features and Enchantments

Visit me @ www.Pavel-Kaminsky.com 49

Thank You!Visit and Subscribe