10/29/2009

Java Reflection 動態調用物件的方法

近來接觸Java Reflection,參考良葛格的例子做了些練習,下面是實作的code。

Student Class:
package com.iden.reflection;

public class Student {
 private String name;
    private int score; 

    public Student() { 
        name = "N/A"; 
    } 

    public Student(String name, int score) { 
        this.name = name; 
        this.score = score; 
    } 

    public void setName(String name) {
        this.name = name;
    }
    
    public void setScore(int score) {
        this.score = score;
    }

    public String getName() { 
        return name; 
    } 

    public int getScore() { 
        return score; 
    } 

    public void showData() { 
        System.out.println("name: " + name); 
        System.out.println("score: " + score); 
    }
}


InvokeMethodDemo Class 調用Student object實作方法:
package com.iden.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InvokeMethodDemo {
 
 private static String className;
 
    public static void main(String[] args) {
     //動態載入與呼叫方法
        try {
          
         if(args.length == 0)
          className = "com.iden.reflection.Student";
         else
          className = args[0];
          
            Class c = Class.forName(className);//呼叫Class
            Object targetObj = c.newInstance();//動態實作物件
            
            Class[] param1 = {String.class};
            Method setName = c.getMethod("setName", param1);//指定物件的方法            
            Object[] paramObjs1 = {"caterpillar"};
            setName.invoke(targetObj, paramObjs1);//將參數帶入方法
            
            
            Class[] param2 = {Integer.TYPE};
            Method setScore = c.getMethod("setScore", param2);            
            Object[] paramObjs2 = {new Integer(90)};
            setScore.invoke(targetObj, paramObjs2);
            
            
            Method showData = c.getMethod("showData", new Class[0]);
            showData.invoke(targetObj, new Object[0]);// call showData method to show name and score.
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
} 


感謝良葛格做了這麼棒的教學。
良葛格範例

10/28/2009

Oracle 系統 object 相關

--查出所有的objects:
select * from all_objects

--找Column名稱是ID的所有table
select * from all_tab_columns
where column_name = 'ID';

updated: Nov 12, 2009
--Oracle show Year, Month and Date
select TO_CHAR(sysdate,'Y') as YY ,TO_CHAR(sysdate,'MM') as MM,TO_CHAR(sysdate,'DD') as DD from dual

updated:Dec 17, 2009
--查詢自建Table: PT_LTDFT 有沒有欄位 DF_TYPE
SELECT COLUMN_NAME from USER_TAB_COLUMNS p WHERE p.table_name = 'PT_LTDFT' and p.column_name = 'DF_TYPE'

10/26/2009

VirtualBox中 GuestOS:Ubuntu 自動掛載 shared folder

Ubuntu開機後要自動掛載磁碟需設定fstab,做法約如下:

1.進入command console,etc裡用sudo vi打開fstab檔(權限不足會不能修改fstab)
cd /
cd etc
sudo vi fstab

2.在最下面加入以下資訊
Download /media/Download vboxsf defaults 0 0

其中Download是VirtualBox中設定的shared folder名稱
/media/Download是要掛載的folder

3.reboot,完成。

VirtualBox裡掛載Shared Folder在GuestOS Ubuntu 9.10

Host OS: windows
Guest OS: Ubuntu 9.10

前提:
在VirtualBox設定裡先設定好HostOS的shared folder

1. 安裝VirtualBox Additions:
cd /media/cdrom
sudo ./VBoxLinuxAdditions-x86.run

2. vboxvfs module載入核心:
sudo modprobe vboxvfs

3.建立要掛載的folder
mkdir /media/Download

4.掛載Shared Folder
sudo mount -t vboxsf [SharedFolderName] /media/Download

5.完成

9/29/2009

Java code加入proxy

加上下面的code就可以了。這是設在系統的方式。
    System.setProperty("http.proxySet", "true");
    System.setProperty("http.proxyHost", "10.180.0.53");
    System.setProperty("http.proxyPort", "8080");

6/03/2009

Windows中 Java都會自動使用的lib folder

只要將jar放到 C:\Windows\Sun\Java\Deployment\Lib\Trusted 裡,jre會自行取用而且只會load一次

Java Applet 需 Sign才能在client使用IO等安全性功能

這陣子在做Applet的東西才發現,Applet在client執行時,需要client簽核才可以使用IO等等資源,所以Applet寫好後打包成jar,這jar必須做成signed jar才行。

1.用jdk command產生憑証檔到keystore
keytool -genkey -alias [myKeyName]

2.用jdk command將打包好的jar 做成sign jar
jarsigner myJar.jar [myKeyName]


參造了原文文章:

Sign All Applet JAR files

Applets are back! And now applets can do more than ever before thanks to signed JAR files. By signing your JARs, you can get access to the filesystem and other resources that were previously off-limits, provided the user grants your applet those privileges. And signing JAR files is now very easy thanks to tools bundled with the JDK.

However, be certain to sign all JAR files used by your Java applet. If you sign the JAR file with your main applet class, your applet will launch. If it later uses classes from another JAR file, though, you can run into trouble. If the newly-loaded class tries a restricted operation and its JAR file isn't signed, your applet will fail at that point with a security exception. Rather than waiting for this and debugging it when it occurs, save yourself the trouble and sign all of your JAR files up front.

You can create your own certificate using tools provided by the JDK. keytool -genkey -alias mykey lets you create your own certificate. Be sure to specify an expiration date far in the future with -validity 1000. The default is only 6 months.

Sign your JAR files with jarsigner my.jar mykey (where my.jar is the name of the jar file to sign).

Deploy all of your JAR files to a folder on your web server, add an HTML page with the applet tag, and let the world enjoy your new applet with powerful permissions.

來自:http://frequal.com/java/SignAllAppletJars.html