關於JDK6新特性資料

JDK6的新特性
JDK6的新特性之一_Desktop類和SystemTray類
JDK6的新特性之七_用Console開發控制檯程序
JDK6的新特性之三_理解StAX
JDK6的新特性之九_CommonAnnotations
JDK6的新特性之二_使用JAXB2來實現對象與XML之間的映射
JDK6的新特性之五_輕量級HttpServer
JDK6的新特性之八_嵌入式數據庫Derby
JDK6的新特性之六_插入式註解處理API
JDK6的新特性之十_Web服務元數據
JDK6的新特性之十一_更簡單強大的JAX-WS
JDK6的新特性之十三_JTable的排序和過濾
JDK6的新特性之十二_腳本語言支持
JDK6的新特性之四_使用Compiler API

JDK6的新特性之一_Desktop類和SystemTray類

JDK6.0發佈有段時間了,新的JDK也有不少新的特性,我去網上搜集了一下,列在下面和大家一起學習.
1.Desktop和SystemTray. 在JDK6中 ,AWT新增加了兩個類:Desktop和SystemTray,前者可以用來打開系統默認瀏覽器瀏覽指定的URL,打開系統默認郵件客戶端給指定的郵箱發郵件,用默認應用程序打開或編輯文件(比如,用記事本打開以txt爲後綴名的文件),用系統默認的打印機打印文檔;後者可以用來在系統托盤區創建一個托盤程序。

我隨便找了幾張圖,在Tray裏面都是空的,沒有圖,可能是圖太大,有xdjm知道希望告訴我.

 

Java代碼
import java.awt.AWTException;   
import java.awt.Desktop;   
import java.awt.Image;   
import java.awt.MenuItem;   
import java.awt.PopupMenu;   
import java.awt.SystemTray;   
import java.awt.Toolkit;   
import java.awt.TrayIcon;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import java.io.File;   
import java.io.IOException;   
import java.net.URI;   
import java.net.URISyntaxException;   
  
public class DesktopTrayTest{   
    private static Desktop desktop;   
    private static SystemTray st;   
    private static PopupMenu pm;   
       
    public static void main( String[] args ) {   
        if( Desktop.isDesktopSupported() ) {   
            desktop = Desktop.getDesktop();   
        }   
        if( SystemTray.isSupported() ) {   
            st = SystemTray.getSystemTray();   
            Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );   
            createPopupMenu();   
            TrayIcon ti = new TrayIcon( image, "Demo", pm );   
            try{   
                st.add( ti );   
            } catch( AWTException awte ) {   
                awte.printStackTrace();   
            }   
        }   
    }   
    public static void sendMail( String mail ) {   
        if( desktop != null &&   
            desktop.isSupported( Desktop.Action.MAIL ) ) {   
            try {   
                desktop.mail( new URI( mail ) );   
            } catch (IOException e) {   
                e.printStackTrace();   
            } catch (URISyntaxException e) {   
                e.printStackTrace();   
            }   
        }   
    }   
    public static void openBrowser( String url ) {   
        if( desktop != null &&   
            desktop.isSupported( Desktop.Action.BROWSE )) {   
            try {   
                desktop.browse( new URI( url ) );   
            } catch (IOException e) {   
                e.printStackTrace();   
            } catch (URISyntaxException e) {   
                e.printStackTrace();   
            }   
        }   
    }   
    public static void edit() {   
        if( desktop != null &&   
            desktop.isSupported( Desktop.Action.EDIT ) ) {   
            File file = new File( "test.txt" );   
            try {   
                if( file.exists() == false ) {   
                    file.create();   
                }   
                desktop.edit( file );   
            } catch( IOException ioe ) {   
                ioe.printStackTrace();   
            }   
        }   
    }   
    public static void createPopupMenu() {   
        pm = new PopupMenu();   
        MenuItem ob = new MenuItem( "Open url" );   
        ob.addActionListener( new ActionListener() {   
            public void actionPerformed( ActionEvent ae ) {   
                openBrowser( "http://blog.csdn.net/xumingming64398966" );   
            }   
        });   
        MenuItem sm = new MenuItem( "Send Mail" );   
        sm.addActionListener( new ActionListener() {   
            public void actionPerformed( ActionEvent ae ) {   
                sendMail( "[email protected]" );   
            }   
        });   
        MenuItem ed = new MenuItem( "Edit" );   
        ed.addActionListener( new ActionListener() {   
            public void actionPerformed( ActionEvent ae ) {   
                edit();   
            }   
        });   
        MenuItem ex = new MenuItem( "Exit" );   
        ex.addActionListener( new ActionListener() {   
            public void actionPerformed( ActionEvent ae ) {   
                System.exit( 0 );   
            }   
        });   
        pm.add( ob );   
        pm.add( sm );   
        pm.add( ed );   
        pm.addSeparator();   
        pm.add( ex );   
    }   
}  
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class DesktopTrayTest{
    private static Desktop desktop;
    private static SystemTray st;
    private static PopupMenu pm;
   
    public static void main( String[] args ) {
        if( Desktop.isDesktopSupported() ) {
            desktop = Desktop.getDesktop();
        }
        if( SystemTray.isSupported() ) {
            st = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );
            createPopupMenu();
            TrayIcon ti = new TrayIcon( image, "Demo", pm );
            try{
                st.add( ti );
            } catch( AWTException awte ) {
                awte.printStackTrace();
            }
        }
    }
    public static void sendMail( String mail ) {
        if( desktop != null &&
            desktop.isSupported( Desktop.Action.MAIL ) ) {
            try {
                desktop.mail( new URI( mail ) );
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    public static void openBrowser( String url ) {
        if( desktop != null &&
            desktop.isSupported( Desktop.Action.BROWSE )) {
            try {
                desktop.browse( new URI( url ) );
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    public static void edit() {
        if( desktop != null &&
            desktop.isSupported( Desktop.Action.EDIT ) ) {
            File file = new File( "test.txt" );
            try {
                if( file.exists() == false ) {
                    file.create();
                }
                desktop.edit( file );
            } catch( IOException ioe ) {
                ioe.printStackTrace();
            }
        }
    }
    public static void createPopupMenu() {
        pm = new PopupMenu();
        MenuItem ob = new MenuItem( "Open url" );
        ob.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                openBrowser( "http://blog.csdn.net/xumingming64398966" );
            }
        });
        MenuItem sm = new MenuItem( "Send Mail" );
        sm.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                sendMail( "[email protected]" );
            }
        });
        MenuItem ed = new MenuItem( "Edit" );
        ed.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                edit();
            }
        });
        MenuItem ex = new MenuItem( "Exit" );
        ex.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                System.exit( 0 );
            }
        });
        pm.add( ob );
        pm.add( sm );
        pm.add( ed );
        pm.addSeparator();
        pm.add( ex );
    }
}

2.Console. JDK6中提供了java.io.Console類專用來訪問基於字符的控制檯設備. 你的程序如果要與Windows下的cmd或者Linux下的Terminal交互,就可以用Console類代勞. 但我們不總是能得到可用的Console, 一個JVM是否有可用的Console依賴於底層平臺和JVM如何被調用. 如果JVM是在交互式命令行(比如Windows的cmd)中啓動的,並且輸入輸出沒有重定向到另外的地方,那麼就可以得到一個可用的Console實例. 下面代碼演示了Console類的用法:

Java代碼
import java.io.Console;   
  
public class ConsoleTest {   
    public static void main( String[] args ) {   
        Console console = System.console();   
        if( console != null ) {   
            String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) );   
            String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] ));   
            console.printf( "User name is:%s", new Object[]{user} );   
            console.printf( "Password is:%s", new Object[]{pwd} );   
        } else {   
            System.out.println( "No Console!" );   
        }   
    }   
}  
import java.io.Console;

public class ConsoleTest {
    public static void main( String[] args ) {
        Console console = System.console();
        if( console != null ) {
            String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) );
            String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] ));
            console.printf( "User name is:%s", new Object[]{user} );
            console.printf( "Password is:%s", new Object[]{pwd} );
        } else {
            System.out.println( "No Console!" );
        }
    }
}

你如果是在一個IDE中如eclipse, netbeans中運行你將得到:
No Console!
因爲只有在命令行中才能得到Console對象。

3.Compiler API. 現在我們可以用JDK6 的Compiler API(JSR 199)去動態編譯Java源文件,Compiler API結合反射功能就可以實現動態的產生Java代碼並編譯執行這些代碼,有點動態語言的特徵。這個特性對於某些需要用到動態編譯的應用程序相當有用,比如JSP Web Server,當我們手動修改JSP後,是不希望需要重啓Web Server纔可以看到效果的,這時候我們就可以用Compiler API來實現動態編譯JSP文件,當然,現在的JSP Web Server也是支持JSP熱部署的,現在的JSP Web Server通過在運行期間通過Runtime.exec或ProcessBuilder來調用javac來編譯代碼,這種方式需要我們產生另一個進程去做編譯工作,不夠優雅而且容易使代碼依賴與特定的操作系統;Compiler API通過一套易用的標準的API提供了更加豐富的方式去做動態編譯,而且是跨平臺的。 下面代碼演示了Compiler API的使用:

Java代碼
import java.io.BufferedWriter;   
import java.io.FileWriter;   
import java.io.IOException;   
import java.util.Iterator;   
  
import javax.tools.JavaCompiler;   
import javax.tools.JavaFileObject;   
import javax.tools.StandardJavaFileManager;   
import javax.tools.ToolProvider;   
  
public class CompilerAPITest {   
    private final static String srcFileName = "Test.java";   
    private final static String classFileName = "Test.class";   
    private final static String className = "Test";   
       
    public static void main( String[] args ) {   
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();   
        if( compiler == null ) {   
            System.err.println( "Compiler is null!" );   
            return;   
        }   
        StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );   
        generateJavaClass();   
           
        Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } );   
        compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call();   
        try {   
            fileManager.close();   
            Class.forName( className ).newInstance();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } catch (InstantiationException e) {   
            e.printStackTrace();   
        } catch (IllegalAccessException e) {   
            e.printStackTrace();   
        } catch (ClassNotFoundException e) {   
            e.printStackTrace();   
        }   
    }   
       
    public static void generateJavaClass() {   
        try {   
            FileWriter rw = new FileWriter( srcFileName );   
            BufferedWriter bw = new BufferedWriter( rw );   
            bw.write( "public class " + className + " {" );   
            bw.newLine();   
               
            bw.write( "public " + className + "() {");   
            bw.newLine();   
            bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );   
            bw.write( "}" );   
            bw.newLine();   
               
            bw.write( "}" );   
            bw.flush();   
            bw.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }   
}  
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class CompilerAPITest {
    private final static String srcFileName = "Test.java";
    private final static String classFileName = "Test.class";
    private final static String className = "Test";
   
    public static void main( String[] args ) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if( compiler == null ) {
            System.err.println( "Compiler is null!" );
            return;
        }
        StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
        generateJavaClass();
       
        Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } );
        compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call();
        try {
            fileManager.close();
            Class.forName( className ).newInstance();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
   
    public static void generateJavaClass() {
        try {
            FileWriter rw = new FileWriter( srcFileName );
            BufferedWriter bw = new BufferedWriter( rw );
            bw.write( "public class " + className + " {" );
            bw.newLine();
           
            bw.write( "public " + className + "() {");
            bw.newLine();
            bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );
            bw.write( "}" );
            bw.newLine();
           
            bw.write( "}" );
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
JDK1.6.0新特性詳解與代碼示例
JDK6.0發佈有段時間了,新的JDK也有不少新的特性,我去網上搜集了一下,列在下面和大家一起學習.
1.Desktop和SystemTray. 在JDK6中 ,AWT新增加了兩個類:Desktop和SystemTray,前者可以用來打開系統默認瀏覽器瀏覽指定的URL,打開系統默認郵件客戶端給指定的郵箱發郵件,用默認應用程序打開或編輯文件(比如,用記事本打開以txt爲後綴名的文件),用系統默認的打印機打印文檔;後者可以用來在系統托盤區創建一個托盤程序。

我隨便找了幾張圖,在Tray裏面都是空的,沒有圖,可能是圖太大,有xdjm知道希望告訴我.


import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class DesktopTrayTest {
    private static Desktop desktop;
    private static SystemTray st;
    private static PopupMenu pm;
   
    public static void main( String[] args ) {
        if( Desktop.isDesktopSupported() ) {
            desktop = Desktop.getDesktop();
        }
        if( SystemTray.isSupported() ) {
            st = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );
            createPopupMenu();
            TrayIcon ti = new TrayIcon( image, "Demo", pm );
            try{
                st.add( ti );
            } catch( AWTException awte ) {
                awte.printStackTrace();
            }
        }
    }
    public static void sendMail( String mail ) {
        if( desktop != null &&
            desktop.isSupported( Desktop.Action.MAIL ) ) {
            try {
                desktop.mail( new URI( mail ) );
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    public static void openBrowser( String url ) {
        if( desktop != null &&
            desktop.isSupported( Desktop.Action.BROWSE )) {
            try {
                desktop.browse( new URI( url ) );
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    public static void edit() {
        if( desktop != null &&
            desktop.isSupported( Desktop.Action.EDIT ) ) {
            File file = new File( "test.txt" );
            try {
                if( file.exists() == false ) {
                    file.create();
                }
                desktop.edit( file );
            } catch( IOException ioe ) {
                ioe.printStackTrace();
            }
        }
    }
    public static void createPopupMenu() {
        pm = new PopupMenu();
        MenuItem ob = new MenuItem( "Open url" );
        ob.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                openBrowser( "http://blog.csdn.net/xumingming64398966" );
            }
        });
        MenuItem sm = new MenuItem( "Send Mail" );
        sm.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                sendMail( "[email protected]" );
            }
        });
        MenuItem ed = new MenuItem( "Edit" );
        ed.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                edit();
            }
        });
        MenuItem ex = new MenuItem( "Exit" );
        ex.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ) {
                System.exit( 0 );
            }
        });
        pm.add( ob );
        pm.add( sm );
        pm.add( ed );
        pm.addSeparator();
        pm.add( ex );
    }
}

2.Console. JDK6中提供了java.io.Console類專用來訪問基於字符的控制檯設備. 你的程序如果要與Windows下的cmd或者Linux下的Terminal交互,就可以用Console類代勞. 但我們不總是能得到可用的Console, 一個JVM是否有可用的Console依賴於底層平臺和JVM如何被調用. 如果JVM是在交互式命令行(比如Windows的cmd)中啓動的,並且輸入輸出沒有重定向到另外的地方,那麼就可以得到一個可用的Console實例. 下面代碼演示了Console類的用法:


import java.io.Console;

public class ConsoleTest {
    public static void main( String[] args ) {
        Console console = System.console();
        if( console != null ) {
            String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) );
            String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] ));
            console.printf( "User name is:%s", new Object[]{user} );
            console.printf( "Password is:%s", new Object[]{pwd} );
        } else {
            System.out.println( "No Console!" );
        }
    }
}


你如果是在一個IDE中如eclipse, netbeans中運行你將得到:
No Console!
因爲只有在命令行中才能得到Console對象。

3.Compiler API. 現在我們可以用JDK6 的Compiler API(JSR 199)去動態編譯Java源文件,Compiler API結合反射功能就可以實現動態的產生Java代碼並編譯執行這些代碼,有點動態語言的特徵。這個特性對於某些需要用到動態編譯的應用程序相當有用,比如JSP Web Server,當我們手動修改JSP後,是不希望需要重啓Web Server纔可以看到效果的,這時候我們就可以用Compiler API來實現動態編譯JSP文件,當然,現在的JSP Web Server也是支持JSP熱部署的,現在的JSP Web Server通過在運行期間通過Runtime.exec或ProcessBuilder來調用javac來編譯代碼,這種方式需要我們產生另一個進程去做編譯工作,不夠優雅而且容易使代碼依賴與特定的操作系統;Compiler API通過一套易用的標準的API提供了更加豐富的方式去做動態編譯,而且是跨平臺的。 下面代碼演示了Compiler API的使用:


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class CompilerAPITest {
    private final static String srcFileName = "Test.java";
    private final static String classFileName = "Test.class";
    private final static String className = "Test";
   
    public static void main( String[] args ) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if( compiler == null ) {
            System.err.println( "Compiler is null!" );
            return;
        }
        StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
        generateJavaClass();
       
        Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } );
        compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call();
        try {
            fileManager.close();
            Class.forName( className ).newInstance();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
   
    public static void generateJavaClass() {
        try {
            FileWriter rw = new FileWriter( srcFileName );
            BufferedWriter bw = new BufferedWriter( rw );
            bw.write( "public class " + className + " {" );
            bw.newLine();
           
            bw.write( "public " + className + "() {");
            bw.newLine();
            bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );
            bw.write( "}" );
            bw.newLine();
           
            bw.write( "}" );
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我在運行這個例子的時候發現ToolProvider.getSystemJavaCompiler得到的是NULL,後來上網一查,原來是一個Bug!鏈接如下:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844

Closed, not reproducible
那爲什麼我一直在reproduce阿?

4.Http Server API. JDK6提供了一個簡單的Http Server API,據此我們可以構建自己的嵌入式Http Server,它支持Http和Https協議,提供了HTTP1.1的部分實現,沒有被實現的那部分可以通過擴展已有的Http Server API來實現,程序員必須自己實現HttpHandler接口,HttpServer會調用HttpHandler實現類的回調方法來處理客戶端請求,在這裏,我們把一個Http請求和它的響應稱爲一個交換,包裝成HttpExchange類,HttpServer負責將HttpExchange傳給 HttpHandler實現類的回調方法.下面代碼演示了怎樣創建自己的Http Server .
Java代碼
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.net.InetSocketAddress;   
  
import com.sun.net.httpserver.HttpExchange;   
import com.sun.net.httpserver.HttpHandler;   
import com.sun.net.httpserver.HttpServer;   
  
public class HttpServerAPITest {   
    private static int count = 0;   
    public static void main( String[] args ) {   
        try {   
            HttpServer hs = HttpServer.create( new InetSocketAddress( 8888 ), 0 );   
            hs.createContext( "/", new MyHandler() );   
            hs.createContext( "/java", new MyHandler() );   
            hs.setExecutor( null );   
            hs.start();   
            System.out.println( "---begin---" );   
            System.out.println( "Listening on " + hs.getAddress() );   
        } catch( IOException ioe ) {   
            ioe.printStackTrace();   
        }   
    }   
    static class MyHandler implements HttpHandler {   
        public void handle( HttpExchange he ) throws IOException {   
            System.out.println( "Request " + count++  );   
            System.out.println( he.getHttpContext().getPath() );   
               
            InputStream is = he.getRequestBody();   
            String response = "<font color='blue'>Happy Spring Festerval</font>";   
            he.sendResponseHeaders( 200, response.length() );   
            OutputStream os = he.getResponseBody();   
            os.write( response.getBytes() );   
            os.close();   
        }   
    }   
}  
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class HttpServerAPITest {
    private static int count = 0;
    public static void main( String[] args ) {
        try {
            HttpServer hs = HttpServer.create( new InetSocketAddress( 8888 ), 0 );
            hs.createContext( "/", new MyHandler() );
            hs.createContext( "/java", new MyHandler() );
            hs.setExecutor( null );
            hs.start();
            System.out.println( "---begin---" );
            System.out.println( "Listening on " + hs.getAddress() );
        } catch( IOException ioe ) {
            ioe.printStackTrace();
        }
    }
    static class MyHandler implements HttpHandler {
        public void handle( HttpExchange he ) throws IOException {
            System.out.println( "Request " + count++  );
            System.out.println( he.getHttpContext().getPath() );
           
            InputStream is = he.getRequestBody();
            String response = "<font color='blue'>Happy Spring Festerval</font>";
            he.sendResponseHeaders( 200, response.length() );
            OutputStream os = he.getResponseBody();
            os.write( response.getBytes() );
            os.close();
        }
    }
}
5.對腳本語言的支持如: ruby, groovy, javascript.
Java代碼
import java.io.FileReader;   
  
import javax.script.Invocable;   
import javax.script.ScriptEngine;   
import javax.script.ScriptEngineManager;   
  
public class ScriptTest {   
    public static void main( String[] args ) {   
        ScriptEngineManager manager = new ScriptEngineManager();   
        ScriptEngine engine = manager.getEngineByName( "ECMAScript" );   
        try {   
            engine.eval( new FileReader( "C:/test.js" ) );   
            Invocable invocableEngine = (Invocable)engine;   
            Object ret = invocableEngine.invokeFunction( "test", null );   
            System.out.println( "The result is :" + (Double)ret );   
        } catch( Exception e ) {   
            e.printStackTrace();   
        }   
    }   
}   
  
test.js如下:   
  
function test(){   
    return Math.round( 11.2 );   
}  
import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ScriptTest {
    public static void main( String[] args ) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName( "ECMAScript" );
        try {
            engine.eval( new FileReader( "C:/test.js" ) );
            Invocable invocableEngine = (Invocable)engine;
            Object ret = invocableEngine.invokeFunction( "test", null );
            System.out.println( "The result is :" + (Double)ret );
        } catch( Exception e ) {
            e.printStackTrace();
        }
    }
}

test.js如下:

function test(){
    return Math.round( 11.2 );
}
 6.插入式註解處理API(Pluggable Annotation Processing API),插入式註解處理API(JSR 269)提供一套標準API來處理Annotations.JSR 269用Annotation Processor在編譯期間而不是運行期間處理Annotation, Annotation Processor相當於編譯器的一個插件,所以稱爲插入式註解處理.如果Annotation Processor處理Annotation時(執行process方法)產生了新的Java代碼,編譯器會再調用一次Annotation Processor,如果第二次處理還有新代碼產生,就會接着調用Annotation Processor,直到沒有新代碼產生爲止.每執行一次process()方法被稱爲一個"round",這樣整個Annotation processing過程可以看作是一個round的序列.
舉個例子:們想建立一套基於Annotation的單元測試框架(如TestNG),在測試類裏面用Annotation來標識測試期間需要執行的測試方法,如下所示:

Java代碼
@TestMethod  
 public void testCheckName(){   
       //do something here   
 }  
@TestMethod
 public void testCheckName(){
       //do something here
 }
這時我們就可以用JSR 269提供的API來處理測試類,根據Annotation提取出需要執行的測試方法.


再舉個例子: 下面我用代碼演示如何來用JSR 269提供的API來處理Annotations和讀取Java源文件的元數據(metadata)

Java代碼
import java.util.List;   
import java.util.Map;   
import java.util.Set;   
  
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.AnnotationMirror;   
import javax.lang.model.element.AnnotationValue;   
import javax.lang.model.element.Element;   
import javax.lang.model.element.ExecutableElement;   
import javax.lang.model.element.TypeElement;   
import javax.lang.model.util.ElementFilter;   
import javax.tools.Diagnostic.Kind;   
  
@SupportedAnnotationTypes( "ToBeTested" )   
@SupportedSourceVersion( SourceVersion.RELEASE_6 )   
public class MyAnnotationProcessor extends AbstractProcessor {   
    private void note( String msg ) {   
        processingEnv.getMessager().printMessage( Kind.NOTE, msg );   
    }   
    public boolean process( Set< ? extends TypeElement > annotations, RoundEnvironment roundEnv ) {   
        for( TypeElement te : annotations ) {   
            note( "annotation: " + te.toString() );   
        }   
        Set< ? extends Element > elements = roundEnv.getRootElements();   
        for( Element e : elements ) {   
            List< ? extends Element > enclosedElems = e.getEnclosedElements();   
            List< ? extends ExecutableElement > ees = ElementFilter.methodsIn( enclosedElems );   
            for( ExecutableElement ee : ees ) {   
                note( "Executable Element Name: " + ee.getSimpleName() );   
                List< ? extends AnnotationMirror > as = ee.getAnnotationMirrors();   
                note( " as: " + as );    
                for( AnnotationMirror am : as ){   
                    Map< ? extends ExecutableElement, ? extends AnnotationValue > map = am.getElementValues();   
                    Set< ? extends ExecutableElement > ks = map.keySet();   
                    for( ExecutableElement k : ks ) {   
                        AnnotationValue av = map.get( k );   
                        note("----"+ee.getSimpleName()+"."+k.getSimpleName()+"="+av.getValue());   
                    }   
                }   
            }   
        }   
        return false;   
    }   
}   
  
    
  
  
public class Testing {   
    @ToBeTested(group="A")   
    public void m1(){   
    }   
    @ToBeTested(group="B",owner="QQ")   
    public void m2(){   
    }       
}   
  
    
  
import java.lang.annotation.ElementType;   
import java.lang.annotation.Retention;   
import java.lang.annotation.RetentionPolicy;   
import java.lang.annotation.Target;   
  
@Retention( RetentionPolicy.RUNTIME )   
@Target( ElementType.METHOD )   
public @interface ToBeTested {   
    String owner() default "Chinajash";   
    String group();   
}  
import java.util.List;
import java.util.Map;
import java.util.Set;

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.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic.Kind;

@SupportedAnnotationTypes( "ToBeTested" )
@SupportedSourceVersion( SourceVersion.RELEASE_6 )
public class MyAnnotationProcessor extends AbstractProcessor {
    private void note( String msg ) {
        processingEnv.getMessager().printMessage( Kind.NOTE, msg );
    }
    public boolean process( Set< ? extends TypeElement > annotations, RoundEnvironment roundEnv ) {
        for( TypeElement te : annotations ) {
            note( "annotation: " + te.toString() );
        }
        Set< ? extends Element > elements = roundEnv.getRootElements();
        for( Element e : elements ) {
            List< ? extends Element > enclosedElems = e.getEnclosedElements();
            List< ? extends ExecutableElement > ees = ElementFilter.methodsIn( enclosedElems );
            for( ExecutableElement ee : ees ) {
                note( "Executable Element Name: " + ee.getSimpleName() );
                List< ? extends AnnotationMirror > as = ee.getAnnotationMirrors();
                note( " as: " + as );
                for( AnnotationMirror am : as ){
                    Map< ? extends ExecutableElement, ? extends AnnotationValue > map = am.getElementValues();
                    Set< ? extends ExecutableElement > ks = map.keySet();
                    for( ExecutableElement k : ks ) {
                        AnnotationValue av = map.get( k );
                        note("----"+ee.getSimpleName()+"."+k.getSimpleName()+"="+av.getValue());
                    }
                }
            }
        }
        return false;
    }
}

 


public class Testing {
    @ToBeTested(group="A")
    public void m1(){
    }
    @ToBeTested(group="B",owner="QQ")
    public void m2(){
    }   
}

 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention( RetentionPolicy.RUNTIME )
@Target( ElementType.METHOD )
public @interface ToBeTested {
    String owner() default "Chinajash";
    String group();
}
 7.StAX. StAX是The Streaming API for XML的縮寫,是繼DOM(Document Object Model)和SAX(Simple API for XML)之後的又一種處理xml的api,一種利用拉模式解析(pull-parsing)XML文檔的API.StAX通過提供一種基於事件迭代器(Iterator)的API讓程序員去控制xml文檔解析過程,程序遍歷這個事件迭代器去處理每一個解析事件,解析事件可以看做是程序拉出來的,也就是程序促使解析器產生一個解析事件然後處理該事件,之後又促使解析器產生下一個解析事件,如此循環直到碰到文檔結束符;SAX也是基於事件處理xml文檔,但卻是用推模式解析,解析器解析完整個xml文檔後,才產生解析事件,然後推給程序去處理這些事件;DOM採用的方式是將整個xml文檔映射到一顆內存樹,這樣就可以很容易地得到父節點和子結點以及兄弟節點的數據,但如果文檔很大,將會嚴重影響性能。

下面是個例子:
Java代碼
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
  
import javax.xml.namespace.QName;   
import javax.xml.stream.XMLEventReader;   
import javax.xml.stream.XMLInputFactory;   
import javax.xml.stream.XMLOutputFactory;   
import javax.xml.stream.XMLStreamException;   
import javax.xml.stream.XMLStreamWriter;   
import javax.xml.stream.events.StartElement;   
import javax.xml.stream.events.XMLEvent;   
  
public class StaxTest {   
    public static void main( String[] arg ) throws XMLStreamException, FileNotFoundException {   
        readXMLByStAX();   
        writeXMLByStAX();   
    }   
    public static void readXMLByStAX() throws XMLStreamException, FileNotFoundException {   
        XMLInputFactory factory = XMLInputFactory.newInstance();   
        XMLEventReader  reader = factory.createXMLEventReader( StaxTest.class.getResourceAsStream( "test.xml" ) );   
        XMLEvent event;   
        StringBuffer  parsingResult = new StringBuffer();   
        while( reader.hasNext() ) {   
            event = reader.nextEvent();   
            if( event.isStartElement() ) {   
                StartElement se = event.asStartElement();   
                parsingResult.append( "<" );   
                parsingResult.append( se.getName() );   
                if( se.getName().getLocalPart().equals( "catalog" ) ) {   
                    parsingResult.append( "id="" );   
                    parsingResult.append( se.getAttributeByName( new QName( "id" ) ).getValue());   
                    parsingResult.append( """ ) ;   
                }   
                parsingResult.append( ">" );   
            } else if( event.isCharacters() ) {   
                parsingResult.append( event.asCharacters().getData() );   
            } else if( event.isEndElement() ) {   
                parsingResult.append( "</" );   
                parsingResult.append( event.asEndElement().getName() );   
                parsingResult.append( ">" );   
            }   
        }   
        System.out.println( parsingResult );   
    }   
       
    public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException {   
        XMLOutputFactory factory = XMLOutputFactory.newInstance();   
        XMLStreamWriter writer = factory.createXMLStreamWriter( new FileOutputStream( "output.xml" ) );   
        writer.writeStartDocument();   
        writer.writeCharacters( " " );   
        writer.writeComment( "testing comment" );   
        writer.writeCharacters( " " );   
        writer.writeStartElement( "catalogs" );   
        writer.writeNamespace( "myNS", "http://blog.csdn.net/Chinajash" );   
        writer.writeAttribute( "owner", "sina" );   
        writer.writeCharacters( " " );   
        writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog");   
        writer.writeAttribute("id","007");   
        writer.writeCharacters("Apparel");   
        // 寫入catalog元素的結束標籤   
        writer.writeEndElement();   
        // 寫入catalogs元素的結束標籤   
        writer.writeEndElement();   
        // 結束 XML 文檔   
        writer.writeEndDocument();            
        writer.close();   
  
    }   
}   
  
test.xml:   
  
    
  
<?xml version="1.0" encoding="UTF-8"?>   
<catalogs>   
    <catalog id="001">Book</catalog>   
    <catalog id="002">Video</catalog>   
</catalogs>  
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StaxTest {
    public static void main( String[] arg ) throws XMLStreamException, FileNotFoundException {
        readXMLByStAX();
        writeXMLByStAX();
    }
    public static void readXMLByStAX() throws XMLStreamException, FileNotFoundException {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader  reader = factory.createXMLEventReader( StaxTest.class.getResourceAsStream( "test.xml" ) );
        XMLEvent event;
        StringBuffer  parsingResult = new StringBuffer();
        while( reader.hasNext() ) {
            event = reader.nextEvent();
            if( event.isStartElement() ) {
                StartElement se = event.asStartElement();
                parsingResult.append( "<" );
                parsingResult.append( se.getName() );
                if( se.getName().getLocalPart().equals( "catalog" ) ) {
                    parsingResult.append( "id="" );
                    parsingResult.append( se.getAttributeByName( new QName( "id" ) ).getValue());
                    parsingResult.append( """ ) ;
                }
                parsingResult.append( ">" );
            } else if( event.isCharacters() ) {
                parsingResult.append( event.asCharacters().getData() );
            } else if( event.isEndElement() ) {
                parsingResult.append( "</" );
                parsingResult.append( event.asEndElement().getName() );
                parsingResult.append( ">" );
            }
        }
        System.out.println( parsingResult );
    }
   
    public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException {
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = factory.createXMLStreamWriter( new FileOutputStream( "output.xml" ) );
        writer.writeStartDocument();
        writer.writeCharacters( " " );
        writer.writeComment( "testing comment" );
        writer.writeCharacters( " " );
        writer.writeStartElement( "catalogs" );
        writer.writeNamespace( "myNS", "http://blog.csdn.net/Chinajash" );
        writer.writeAttribute( "owner", "sina" );
        writer.writeCharacters( " " );
        writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog");
        writer.writeAttribute("id","007");
        writer.writeCharacters("Apparel");
        // 寫入catalog元素的結束標籤
        writer.writeEndElement();
        // 寫入catalogs元素的結束標籤
        writer.writeEndElement();
        // 結束 XML 文檔
        writer.writeEndDocument();        
        writer.close();

    }
}

test.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<catalogs>
    <catalog id="001">Book</catalog>
    <catalog id="002">Video</catalog>
</catalogs>
8. Web Service. 由於Web服務日趨流行,利用Web服務的功能性的API特徵正從最新的Java EE版本中向Java SE 6平臺遷移。換言之,針對Web服務不需另外加入額外的工具,在Java EE和Java SE平臺擁有相同的API。野馬將大把不同的Web服務相關的API加到標準的工具櫃中:以JSR 181針對Java 平臺的Web服務元數據,通過JSR 224的基於XML 的Web服務Java API(JAX-WS);針對Java的帶有附件的SOAP API(SAAJ)作爲JSR 67 。與三個Web服務API相關的包新增到Java SE 6.0裏:JAX-WS API 放置到javax.xml.ws包; SAAJ類在javax.xml.soap 包; Web服務的元數據類放置在javax.jws包裏。 下面是一個簡單的例子, 下面的代碼是要作爲web service發佈的類。
Java代碼
package hello;   
import javax.jws.WebService;   
import javax.xml.ws.Endpoint;   
  
@WebService  
public class CircleFunctions {   
    public double getArea( int radius ) {   
        return Math.PI * radius * radius;   
    }   
    public double getCircumference( int radius ) {   
        return Math.PI * radius * 2;   
    }   
       
    public static void main( String[] args ) {   
        Endpoint.publish( "http://localhost:8888/WebServiceExample/circlefunctions", new CircleFunctions());   
    }   
}  
package hello;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class CircleFunctions {
    public double getArea( int radius ) {
        return Math.PI * radius * radius;
    }
    public double getCircumference( int radius ) {
        return Math.PI * radius * 2;
    }
   
    public static void main( String[] args ) {
        Endpoint.publish( "http://localhost:8888/WebServiceExample/circlefunctions", new CircleFunctions());
    }
}
處理的方法如下:
javac -d ./ CircleFunctions.java
wsgen hello.CircleFunctions
java hello.CircleFunctions
然後在瀏覽器中輸入如下url,你將得到一個xml頁面:
http: //localhost:8888/WebServiceExample/circlefunctions?WSDL

參考網頁:
1.Desktop和SystemTray. http://dev.yesky.com/411/3019911.shtml
2.Console. http://dev.yesky.com/133/3032133.shtml
3.Compiler API. http://developer.51cto.com/art/200701/37359.htm
4.HttpServer API. http://www.testage.net/QA/Dev/200701/1396.htm
5. 對腳本語言的支持http://blog.edwardro.com/read.php?167
6. 插入式註解處理API. http://ourconan.com.cn/article.php?itemid-2113-type-blog.html
7.StAX. http://ourconan.com.cn/article.php?itemid-2111-type-blog.html
8.Web Service. http://www.360doc.com/showWeb/0/0/298124.aspx
9.JDK1.5的Annotation

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/stevexk/archive/2008/03/26/2219158.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章