標籤

星期六, 7月 23, 2011

Java 5以上有關泛型集合拷貝操作的問題

  • 緣起:
昨天辦公室裡同事在問我有關JDK5以上有關泛型集合拷貝操作的問題,他的需求是希望能保留原始集合內所有物件之內容,因此先將原始的集合先行拷貝一份至記憶體內,然後針對這份拷貝出來的集合在行修改操作,這樣一來既可以保留原來的那一份,又可以毫無顧慮地對拷貝出來的這份恣意地修改。
  • 問題
但是問題是當他使用集合的拷貝方法時,如:ArrayList.clone()時,Java似乎並沒有真的在記憶體內,重新拷貝新的集合到另一快新的記憶體區塊內。導致他怎麼操作修改新的那份集合,都會連帶異動到原始集合內的資料。
  • 解法
簡單地說,以Dog這個類別為例,就是要實作Cloneable這個介面的clone()方法即可。
範例程式碼如下:
  • Dog.java
public class Dog implements Cloneable {

private String kind;
private String color;
public Dog(String kind, String color) {
super();
this.kind = kind;
this.color = color;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
  • ListCloneTest.java
import java.util.ArrayList;

public class ListCloneTest {

/**
* @param args
* @throws CloneNotSupportedException
*/
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
ArrayList oldDogs = new ArrayList();
ArrayList newDogs;
oldDogs.add(new Dog("a", "white"));
oldDogs.add(new Dog("b", "block"));

newDogs = new ArrayList(oldDogs.size());
for(Dog dog : oldDogs){
newDogs.add((Dog) dog.clone());
}
newDogs.get(0).setColor("yellow");
newDogs.get(1).setKind("d");
System.out.println("Old Dogs#1 : kink="+oldDogs.get(0).getKind()+" color="+oldDogs.get(0).getColor());
System.out.println("New Dogs#1 : kink="+newDogs.get(0).getKind()+" color="+newDogs.get(0).getColor());
System.out.println("Old Dogs#2 : kink="+oldDogs.get(1).getKind()+" color="+oldDogs.get(1).getColor());
System.out.println("New Dogs#2 : kink="+newDogs.get(1).getKind()+" color="+newDogs.get(1).getColor());
}

}
  • 輸出結果:
Old Dogs#1 : kink=a color=white
New Dogs#1 : kink=a color=yellow
Old Dogs#2 : kink=b color=block
New Dogs#2 : kink=d color=block

星期四, 4月 14, 2011

Oracle 10g Install on CetOS5.6

Oracle 10g Install on CetOS5.6

1

Get Oracle 10g Installing File


a.

      Download the Complete Files




        Filename : 10201_database_linux_x86_64.cpio.gz


b.

      Unzip the file




        $gunzip 10201_database_linux_x86_64.cpio.gz


c.

      Extract the file




        $cpio -idmv < 10201_database_linux_x86_64.cpio

2

Installation Guide


a.

Preinstallation Tasks



i.

Install JDK





$vi /etc/profile

JAVA_HOME=/usr/local/jdk1.6.0_24

export JAVA_HOME

PATH=$JAVA_HOME/bin:$PATH

export CLASSPATH=.:/usr/local/jdk1.6.0_24/lib:/usr/local/jdk1.6.0_24/jre/lib



ii.

Checking the Software Requirements





$uname -r

$rpm -qa



iii.

Checking the Network Setup




1

Configuring Name Resolution






$vi /etc/sysconfig/network

HOSTNAME=myhost.ora.mycompany.com






$vi /etc/hosts

10.10.0.102 myhost.ora.mycompany.com myhost




2

Setting the ORACLE_HOSTNAME Environment Variable






$vi /etc/profile

ORACLE_HOSTNAME=myhost.ora.mycompany.com

export ORACLE_HOSTNAME



iv.

Creating Required Operating System Groups and Users




1

Need to create several operating system groups and users






The OSDBA group : dba

The OSOPER group : oper

The Oracle Inventory group : oinstall

The Oracle software owner user : oracle




2

Creating the Oracle Inventory Group






$/usr/sbin/groupadd oinstall

$/usr/sbin/groupadd dba

$/usr/sbin/groupadd oper

$/usr/sbin/useradd -g oinstall -G dba,oper oracle

$/usr/sbin/usermod -g oinstall -G dba,oper oracle

$passwd oracle

${password}

$id nobody




3

Configuring Kernel Parameters






$/sbin/sysctl -a | grep sem

$/sbin/sysctl -a | grep shm

$/sbin/sysctl -a | grep file-max

$/sbin/sysctl -a | grep ip_local_port_range

$/sbin/sysctl -a | grep rmem_default

$/sbin/sysctl -a | grep rmem_max

$/sbin/sysctl -a | grep wmem_default

$/sbin/sysctl -a | grep wmem_max

$vi /etc/sysctl.conf

kernel.shmall = 2097152

kernel.shmmax = 2147483648

kernel.shmmni = 4096

kernel.sem = 250 32000 100 128

fs.file-max = 65536

net.ipv4.ip_local_port_range = 1024 65000

net.core.rmem_default = 262144

net.core.rmem_max = 262144

net.core.wmem_default = 262144

net.core.wmem_max = 262144

Restart the system




4

Setting Shell Limits for the oracle User






$vi /etc/security/limits.conf

oracle soft nproc 2047

oracle hard nproc 16384

oracle soft nofile 1024

oracle hard nofile 65536

$vi /etc/pam.d/login

session required pam_limits.so

$vi /etc/profile

if [ $USER = "oracle" ]; then

if [ $SHELL = "/bin/ksh" ]; then

ulimit -p 16384

ulimit -n 65536

else

ulimit -u 16384 -n 65536

fi

fi



v.

Identifying Required Software Directories




1

Identifying or Creating an Oracle Base Directory





a.

Identifying an Existing Oracle Base Directory







$more /etc/oraInst.loc





b.

Creating an Oracle Base Directory







$mkdir -p /ora/app/oracle

$chown -R oracle:oinstall /ora/app/oracle

$chmod -R 775 /ora/app/oracle

$mkdir -p /ora/app/oracle/product/10.2.0

$chown -R oracle:oinstall /ora/app/oracle/product/10.2.0

$chmod -R 775 /ora/app/oracle/product/10.2.0



vi.

Creating Directories for Oracle Database or Recovery Files




1

Creating Required Directories





a.

Database files







$mkdir /ora/app/oracle/product/10.2.0/oradata

$chown -R oracle:oinstall /ora/app/oracle/product/10.2.0/oradata

$chmod -R 775 /ora/app/oracle/product/10.2.0/oradata





b.

Recovery files







$mkdir /ora/app/oracle/product/10.2.0/flash_recovery_area

$chown -R oracle:oinstall /ora/app/oracle/product/10.2.0/flash_recovery_area

$chmod -R 775 /ora/app/oracle/product/10.2.0/flash_recovery_area



vii.

Configuring the oracle User's Environment





$mkdir -p /ora/app/oracle/tmp

$chmod a+wr /ora/app/oracle/tmp

$su - oracle

$vi .bash_profile

umask 022

TEMP=/ora/app/oracle/tmp

TMPDIR=/ora/app/oracle/tmp

export TEMP TMPDIR

ORACLE_BASE=/ora/app/oracle

ORACLE_HOME=/ora/app/oracle/product/10.2.0/db_1

ORACLE_SID=orcl

export ORACLE_BASE ORACLE_HOME ORACLE_SID

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib

$. ./.bash_profile

$vi /etc/redhat-release

redhat 4

$yum -y install libXp


b.

Installation Tasks




$xhost +

$xterm

$su - oracle

$export DISPLAY=:0.0

$./database/runInstall

3

Database Administration


a.

Tasks of a Database Administrator



i.

Create and Open the Database





Use the Database Configuration Assistant (DBCA)






$xhost +

$xterm

$su - oracle

$cd /ora/app/oracle/product/10.2.0/db_1/bin

$./dbca



ii.

Starting Up and Shutting Down





Starting Up a Database






$su - oracle

$cd /ora/app/oracle/product/10.2.0/db_1/bin

$./sqlplus /nolog

conn sys/orcl as sysdba

startup force

exit

$./lsnrctl start

$./emctl start dbconsole





Shutting Down a Database






$su - oracle

$cd /ora/app/oracle/product/10.2.0/db_1/bin

$./sqlplus /nolog

conn sys/orcl as sysdba

shutdown immediate

exit

$./lsnrctl stop

$./emctl stop dbconsole

星期一, 3月 14, 2011

Eclipse IDE相關設定

Case1. 如何指定檔案編碼格式為utf-8
In eclipse.ini file appended : -Dfile.encoding=UTF-8

星期四, 1月 13, 2011

MySQL Knowlage - Memo

簡介:
MySQL是一套靈巧的資料庫伺服器軟體,適合應用在中、小型資訊系統。其特性是支持標準的ANSI SQL語句,以及支持多種作業平台(Linux/Windows)。而在Unix/Linux系統上,MySQL更支持多線程執行(Multi-Threads)方式,從而獲得相當好的效能。而且它也是屬於開放源原始碼軟体。

說明:
本文摘要一些使用MySQL的相關概念與使用情境來展示對應之指令用法,以及一些進階的資料供後續參考使用。

版本:
5.1.53-community MySQL Community Server (GPL)

官網文件:

重要概念說明:
1.何謂 Character Sets and Collations
在資料存取時,最重要的就是編碼問題,這是中文比英文麻煩之處
MySQL 有二個與編碼相關的設定 : Character Sets(文字編碼) & Collations(連線校對)
  • Character Sets(文字編碼)
是一組符號及其編碼(is a set of symbols and encodings)
舉例來說:有四個字元『A, B, a, b』,這些字元即所謂的符號 (symbols),而這些符號存在資料庫有特定的編碼方式 (encodings)。譬如:'0' 代表 A, '1' 代表B。
  • Collations(連線校對)
是一組用來比較上述文字編碼的規則(is a set of rules for comparing characters in a character set)
譬如:上述 A 與 B 進行比較,我們知道 A <>
  • Character Sets 與 Collations 的設置
預設的編碼與校對都是 Latin1
如何查詢相關設定值?
  • show variables like "%character%";
  • show variables like "%collation%";
2.MyISAM 與 InnoDB 之差異
  • MyISAM
use Table Locking mechanism was the default storage engine for the MySQL relational database management system versions prior to 5.5. The major deficiency of MyISAM is the absence of transactions support.
  • InnoDB
use Row Locking mechanism is the default storage engine for MySQL as of MySQL 5.5 provides the standard ACID-compliant transaction features along with foreign key support (Declarative Referential Integrity)

情境與命令:
1.設定使用者密碼:
  • SET PASSWORD FOR 'user'@'%.domain' = PASSWORD('newpass');
2.允許遠端連線管理資料庫:
舉例:若MySQL安裝於IP: 10.10.0.103之主機上面,管理者希望由遠端IP:10.10.0.183(xxx-PC)之電腦上面欲連線至10.10.0.103之資料庫主機上進行管理

  • 從遠端10.10.0.183(xxx-PC) ping該資料庫主機,會出現底下錯誤訊息:
>mysqladmin --host=10.10.0.103 -u root ping
mysqladmin: connect to server at '10.10.0.103' failed
error: 'Host 'xxx-PC' is not allowed to connect to this MySQL server'
  • 回到資料庫主機(IP:10.10.0.103)執行底下命令,執行授權:
>mysql -u root -ppass
CREATE USER 'root'@'%' IDENTIFIED BY 'pass';
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'pass' WITH GRANT OPTION;
ps. 'root'@'%'代表管理者可以從任何一台遠端機器連線進來,若是只限定某些電腦則「%」可以換成指定IP或Hostname
3.刪除遠端授權:
>mysql -u root -p pass
REVOKE ALL PRIVILEGES ON *.* FROM root@`%`;
DELETE FROM user WHERE user="root" AND host="%";
FLUSH PRIVILEGES;
進階閱讀:

星期日, 1月 09, 2011

Maven 2.2.1 - Memo

前言:為了學習使用Maven,先將相關文件資源找齊以備後續學習效率。雖說Maven 3已經釋出,在此還是先以Maven 2為學習對象。

版本號:2.2.1

參考資源:

Use JBoss Seam Framework 2.2.0 GA - Memo

前言:預備使用Seam Framework 2.2.0 GA來寫點東西,在開工前先把一些需要參考的資訊做一下記錄,避免開發中還要東找西找、或是突然閃神甚麼都想不起來! ^/\^

開發環境版本:
1.JBoss Application Server 5.1.0 GA
2.Seam Framework 2.2.0 GA(以下3~8項都是從Seam裡頭查出所依賴相依之部分重要框架版本)
3.Hibernate Core 3.3.1 GA
4.Hibernate Annotations 3.4.0 GA
5.Hibernate EntityManager 3.4.0 GA
6.Hibernate Validator 3.1.0 GA
7.RichFaces JSF components library 3.3.1 GA
8.Java Server Faces AJAX framework API 3.3.1 GA
9.RichFaces Extra Resource
10.JBoss Tools 3.0.1.GA Documentation

參考文件:(所有JBoss開源專案相關資源:http://www.jboss.org/projects/matrix/)
1.JBoss 5.1.0 GA
2.Seam 2.2.0 GA
3.Hibernate Core 3.3.1 GA
4.Hibernate Annotations 3.4.0 GA
5.Hibernate EntityManager 3.4.0 GA
6.Hibernate Validator 3.1.0 GA
7.JavaServer Faces 1.2 Reference Documentation
8.RichFaces JSF components library 3.3.1 GA
9.RichFaces JSF-AJAX framework API 3.3.1.GA
10.RichFaces Extra Resource :
11.JBoss Tools 3.0.1.GA Documentation

重點提示:
  • Seam Framework學習重點在Configruation與Annotation、Context與Component這幾個概念上面
  • Hiberante Framework學習重點在要能區分EJB3/JPA與Hibernate之間的關聯、Mapping Classes(Inheritance Mapping Strategies)、Classes Association、Transactions、Efficient Storing And Loading等等

星期六, 1月 08, 2011

Eclipse安裝JAD插件 ─ Jadclipse

前言:
JAD是眾所熟知的Java反編譯工具,可快速地將許多一時半載找不到原始程式碼的第三方程式庫加以反編譯程可讀的Java程式代碼。有這麼好用的工具,那有沒有辦法把它跟知名的IDE─Eclipse加以整合而不必再手動下指令呢?

Jadclipse就是幫忙解決我們這個需求,而產生的Eclipse插件。所以底下就簡單地將安裝的步驟條列出來:
1.先下載Jad.exe,請參考前一篇po文
2.Jadclipse的Eclipse Update Site位置:http://jadclipse.sourceforge.net/update
插件安裝完畢以後,接下來就是在IDE上設定相關參數:
3.Window→Preferences→Java→Decompilers→Jad→Path to decompiler: 填入步驟1.Jad.exe的目錄位置 ( Ex. : D:/javatools/ide/jad/jad.exe )
4.Window→Preferences→General→Editors→File Associations點選File types: *.class在Associated editors內選擇Decompiled Class File Viewer壓按Default按鈕

以上四個步驟簡簡單單就搞定囉!

Java 反編譯工具 - JAD

在Java界非常出名的反編譯工具-JAD,上網查都是建議你去http://www.kpdus.com下載,但是該網址已經很久不能使用了,好不容易找到一個可以下載的映射站http://www.varaneckas.com/jad。從這裡你可以找到各種版本的JAD。

星期日, 1月 02, 2011

JBoss 5.0.1 相關使用情境說明二三事

版本:JBoss 5.0.1 Community
主機IP:10.10.0.182
情境一:同一台機器上如何設定啟動多個JBoss應用伺服器呢?
步驟:
Step1.你可從${jboss_home}/server/底下選擇你要的部署模式對應之目錄,將其copy→past→rename(本例將其更名為"newsite")
Step2.錯開各個端口,請修改
${jboss_home}/server/newsite/conf/bindingservice.beans/META-INF/bindings-jboss-beans.xml組態設定檔內,找著有「<property name="port">」標籤的各個端口設定,將其指定之預設之端口號碼一一修改(我是將各端口號碼都再加上100)後存檔即可
Step3.啟動並綁定指定IP位置,命令如右:run.bat -c newsite -b 10.10.0.182

情境二:如何遠端關閉特定一個JBoss應用伺服器呢?
步驟:
Step1.先找到Naming Service之端口號碼,從${jboss_home}/server/newsite/conf/bindingservice.beans/META-INF/bindings-jboss-beans.xml組態檔案內尋找「<!-- Naming Service -->」底下「<property name="bindingName">Port</property>」在此範例中端口號碼為1099
Step2.執行指令為:shutdown -s jnp://10.10.0.182:1099