java.rmi.MarshalledObject´Â ConstructorÀÇ argument¿¡ ÃëÇÏ´Â ObjectÀÇ Serialized dataÀÎ
byte streamÀ» °®°í ÀÖ´Â °´Ã¼ÀÌ´Ù.
Áï MarshalledObject´Â object¿¡ ´ëÇÑ Serialized representationÀ» °®°í ÀÖ´Â Container°´Ã¼ÀÌ´Ù.
Member methodÀÎ get() method´Â °®°íÀÖ´Â byte streamÀÇ deserializationÀ¸·Î original objectÀÇ
º¹»çº» °´Ã¼¸¦ ¸¸µé¾î return ÇÑ´Ù.
MarshalledObject°¡ serialized dataÀÎ byte streamÀ» createÇÒ¶§ °¢°¢ÀÇ classµéÀº ³ªÁß¿¡
deserializedµÉ¶§ Äڵ尡 loadingµÉ¼öÀÖ´Â codebase URLÀÌ Ç¥½ÃµÇ°í, marshalled object³»¿¡
ÀÖ´Â remote object(ºÐ»ê°´Ã¼)µéÀº serialized µÈ stub instance·Î µÈ´Ù.
Serialization½Ã¿¡ ¾î¶² °´Ã¼¸¦ serializeÇÒ¼öµµ ÀÖ°ÚÁö¸¸, ±× °´Ã¼ÀÇ serialized representation(byte strrem)À»
°®°í ÀÖ´Â MarshalledObject¸¦ serializeÇÒ¼öµµ ÀÖ´Ù. MarshalledObject¸¦ serializeÇÏ´Â °ÍÀº deserialize½Ã
MarshalledObject¸¦ ¾ò´Â JVM¿¡¼´Â MarshalledObject³» °´Ã¼¿¡ ´ëÇÑ class¸¦ ¹Ýµå½Ã °®°í ÀÖ¾î¾ß ÇÒ
ÇÊ¿ä°¡ ¾ø´Ù´Â ÀåÁ¡À» °®°ÔµÈ´Ù. ±×¸®°í, MarshalledObject´Â °´Ã¼¿¡ ´ëÇÑ codebase urlÀ» °®°í
ÀÖÀ»¼ö Àֱ⠶§¹®¿¡ ÇÊ¿ä¿¡ µû¶ó¼ dynamic class loadedµÉ¼ö°¡ ÀÖ´Ù.
(RMIÀÇ dynamic stub loadedµµ ÀÌ¿Í °°Àº°ÍÀÌ°ÚÁÒ!!)
ÀÚ ±×·¯¸é, MarshalledObjectÀÇ serialization°ú deserilizationÀ» »ìÆì º¸±â Àü¿¡, Object serializationÀ» ¸ÕÀú
¿¹Á¦·Î¼ º¸±â·Î ÇÏ°Ú½À´Ï´Ù.
¢ºObject Serialization and Deserialization
//Serializable object Á¤ÀÇ
import java.io.Serializable;
public class ObjectContainer implements Serializable
{
private Object value;
public ObjectContainer(Object value) {
this.value=value;
}
public String toString() {
return "This container holds " + value;
}
}
//Serialization Example
import java.io.*;
public class WriteObject
{
public static void main(String args[]) throws Exception
{
if(args.length!=1) {
System.out.println("java -cp classes WriteObject [object.ser]");
return;
}
FileOutputStream fo=null;
ObjectOutputStream oos=null;
try {
fo = new FileOutputStream(args[0]);
oos = new ObjectOutputStream(fo);
oos.writeObject(new ObjectContainer("HongSeong"));
} finally {
if(fo!=null) fo.close();
if(oos!=null) oos.close();
}
System.out.println("Wrote to " + args[0]);
}
}
//Deserialization Example
import java.io.*;
public class ReadObject
{
public static void main(String args[]) throws Exception
{
if(args.length!=1) {
System.out.println("java -cp classes ReadObject [object.ser]");
return;
}
FileInputStream fi=null;
ObjectInputStream ois=null;
try {
fi = new FileInputStream(args[0]);
ois = new ObjectInputStream(fi);
Object tmp = ois.readObject();
System.out.println("Read from file " + args[0]+ ":");
System.out.println(tmp);
} finally {
if(fi!=null) fi.close();
if(ois!=null) ois.close();
}
}
}
[Test]
c:\work>mkdir classes
c:\work>javac -d classes *.java
c:\work>java -cp classes WriteObject object.ser
Wrote to object.ser
c:\work>java -cp classes ReadObject object.ser
Read from file object.ser:
This container holds HongSeong
Deserialization½Ã¿¡ ObjectContainer.class ÆÄÀÏÀÌ ¹Ýµå½Ã ÀÖ¾î¾ß ÇÑ´Ù´Â °Í Á¤µµ´Â ¾Æ½Ã°ÚÁÒ.!!!
Áï °´Ã¼¸¦ Serialization½Ãų°æ¿ì¿¡´Â Deserialization½Ã data typeÀÎ class fileÀÇ locationÀ» classpath°æ·Î
¿Ü¿¡´Â ¾Ë¼ö ÀÖ´Â ¹æ¹ýÀÌ ¾ø´Ù´Â °ÍÀÔ´Ï´Ù.
[Test]
c:\work>mkdir otherdir
c:\work>copy classes\ReadObject.class otherdir
c:\work>java -cp otherdir ReadObject object.ser
java.lang.ClassNotFoundException: ObjectContainer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
(etc...)
¢º2.MarshalledObject Serialization and Deserialization
//file WriteMarshalled.java
import java.io.*;
import java.rmi.*;
public class WriteMarshalled {
public static void main(String[] args) throws IOException
{
FileOutputStream fo=null;
ObjectOutputStream oos = null;
try {
fo = new FileOutputStream(args[0]);
oos = new ObjectOutputStream(fo);
ObjectContainer container = new ObjectContainer("specular");
MarshalledObject mo = new MarshalledObject(container);
oos.writeObject(mo);
} finally {
if (fo != null) fo.close();
if (oos != null) oos.close();
}
System.out.println("Wrote to " + args[0]);
}
}
//file ReadMarshalled.java
import java.io.*;
import java.rmi.*;
public class ReadMarshalled {
public static void main(String[] args) throws Exception
{
FileInputStream fi = null;
ObjectInputStream ois = null;
try {
fi = new FileInputStream(args[0]);
ois = new ObjectInputStream(fi);
MarshalledObject o = (MarshalledObject) ois.readObject();
System.out.println("Read MarshalledObject from file " +
args[0]+ ", contents are:");
Object containee = o.get();
System.out.println(containee);
System.out.println("Marshalled object loaded by: " +
o.getClass().getClassLoader());
System.out.println("Containee loaded by: " +
containee.getClass().getClassLoader());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fi != null) fi.close();
if (ois != null) ois.close();
}
}
}
[Test]
c:\work>javac -d classes *.java
Å×½ºÆ®¸¦ ÇϱâÀü¿¡ ´Ù½Ã À§¿¡¼ ¼³¸íÇÑ ³»¿ëÀ» »ý°¢ÇØ ºÁ¾ß °Ú´Ù.
MarshalledObject¸¦ ÀÌ¿ëÇÒ °æ¿ì¿¡´Â Deserialization °á°ú´Â MarshalledObjce°¡
µÇ°í, ÀÌ °´Ã¼ÀÇ get() method¸¦ ÅëÇØ ¿øÇÑ´Â °´Ã¼¸¦ ¾ò¾î ³¾¼ö°¡ ÀÖ´Â °ÍÀÌ´Ù.
À̶§, À§¿¡¼ ¾ð±ÞÇÑ ºÁ¿Í °°ÀÌ, deserialization½Ã classÀÇ loactionÁöÁ¤À»
Serialization½Ã MarashalledObject¸¦ »ý¼ºÇÒ¶§ Ç¥½Ã ÇÒ¼ö°¡ ÀÖ´Ù°í ÀÖ´Ù.
[Test]
c:\work>java -Djava.rmi.server.codebase=http://localhost:8080/ \
-cp classes WriteMarshalled marshalled.ser
Wrote to marshalled.ser
java.rmi.server.codebase property¿¡ ÀÇÇØ MarshalledObject³»ÀÇ
°´Ã¼¿¡ ´ëÇÑ classÀÇ locationÁ¤º¸¸¦ ÁÙ¼ö°¡ Àִ°ÍÀÌ´Ù.
°á±¹ http://localhost:8080/ °æ·Îµµ runtime¿¡ deserialization½Ã class¸¦
loadingÇϱâ À§ÇØ Ã£´Â °æ·Î¿¡ Æ÷ÇÔÀ» ÇÏ°Ô µÇ´Â °ÍÀÌÁÒ.
ÀÌ°ÍÀº RMI¿¡¼ Remote Loaded StubÀÇ ±â´ÉÀ» ÀÌ¿ëÇÒ¶§ .. RMIServer¸¦
start½ÃÅ°¸é¼ ÁÖ´Â property±â´É°ú µ¿ÀÏÇÑ°ÍÀÌ´Ù. MarshalledObject¸¦ »ý¼ºÇϸé¼
java.rmi.server.codebase property´Â MarshalledObject³» Á¤º¸°¡ ³Ñ¾î°¡°Ô
µÇ´Â °ÍÀÌ´Ù. ÀÌ°ÍÀº ³ªÁß¿¡ Deserialization½Ã class fileÀ» loadingÇϱâÀ§ÇÑ
url·Î ÂüÁ¶°¡ µÇ¾î Áø´Ù.
´ÙÀ½Àº DeserializationÀ» Å×½ºÆ® ÇØ¾ß Çϴµ¥.....
[Test]
1. web server¸¦ ½ÇÇàÇÏ°í, document root¿¡ ObjectContainer.class fileÀ»
°®´Ù ³õ´Â´Ù.
2. otherdir¿¡ ReadMarshalled.class file, marshalled.ser fileÀ» copyÇÑ´Ù.
c:\work>copy marshalled.ser otherdir
c:\work>copy classes\ReadMarshalled.class otherdir
c:\work>cd otherdir
c:\work\otherdir>dir
2001-05-10 07:49p 1,059 ReadObject.class
2001-05-10 07:55p 1,468 ReadMarshalled.class
2001-05-11 10:40a 241 marshalled.ser
3. ReadMarshalled ½ÇÇà
c:\work\otherdir>java ReadMarshalled marshalled.ser
¢º ´ç¿¬È÷ ClassNotFoundExceptionÀÌ ¹ß»ý ÇÏ°ÚÁÒ. ¿Ö³Ä¸é,
MarshalledObject o = (MarshalledObject) ois.readObject();
System.out.println("Read MarshalledObject from file " +
args[0]+ ", contents are:");
Object containee = o.get();//Exception ¹ß»ý
À§¿¡¼ o.get() ÇÒ¶§, object¿¡ ´ëÇÑ byte streamÀ» ÀÌ¿ëÇؼ
original object¸¦ ¸¸µé¾î¾ß Çϴµ¥. class fileÀ» classpath
°æ·Î¿¡¼ ãÀ»¼ö°¡ ¾ø±â ¶§¹®¿¡ ClassNotFoundExceptionÀÌ ¹ß»ýÇÏ´Â
°ÍÀÌÁÒ.
¢º Serialization½Ã ³Ñ°ÜÁØ codebase url¸¦ ÂüÁ¶Çϱâ À§Çؼ´Â SecurityManager°¡
SettingµÇ¾î Á®¾ß ÇÑ´Ù. classpath°æ·Î¿¡¼ class fileÀ» loadingÇÏ´Â ClassLoader
°´Ã¼´Â classpath°æ·Î ¿ÜÀÇ ´Ù¸¥ location¿¡ ÀÖ´Â class fileÀ» loadingÇÒ¼ö°¡ ¾ø´Ù.
codebase°æ·ÎÀÇ class¸¦ loadingÇÒ¼ö ÀÖ´Â ClassLoader´Â JVM³»ÀÇ RMI system¿¡¼
Á¦°øÀ» Çϴµ¥ ÀÌ°ÍÀº SecurityManager°¡ settingµÇ¾î Á®¾ßÇÏ´Â °ÍÀÌÁÒ.
±×·¡¼, command line¿¡ property optionÀ¸·Î security manager¸¦ ´ÙÀ½°ú °°ÀÌ
settingÇؼ ½ÇÇà½ÃŲ´Ù.
c:\work\otherdir>java -Djava.security.manager ReadMarshalled marshalled.ser
java.lang.ClassNotFoundException: java.security.AccessControlException:
access denied (java.net.SocketPermission localhost:8080 connect,resolve)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:272)
at java.security.AccessController.checkPermission(AccessController.java:399)
¢º À§ÀÇ ¿¡·¯¸¦ º¸¸é, codebase urlÁ¤º¸¸¦ Àоù´Ù´Â °ÍÀ» ¾Ë¼ö°¡ ÀÖ°í, ±× °æ·Î·Î ºÎÅÍ class fileÀ»
loadingÇÏ´Ù securityÁ¦ÇÑÀ¸·Î AccessControlExceptionÀÌ ¹ß»ýµÈ°ÍÀ» ¾Ë¼ö°¡ ÀÖ´Ù.
classpath°æ·Î¿ÜÀÇ ´Ù¸¥ systemÀ¸·Î ºÎÅÍ class fileÀ» loadingÇϱâ À§Çؼ´Â Scoket
permissionÁß¿¡ connect¿Í resolve permissionÀÌ ºÎ¿© µÇ¾î¾ß ÇÏ´Â °ÍÀÌÁÒ.
±×·¡¼, policy fileÀ» ´ÙÀ½°ú °°ÀÌ ¸¸µé¾î ÇöÀç dir¿¡ ÀúÀå ÇÑ´Ù.
/* AUTOMATICALLY GENERATED ON Fri May 11 09:38:40 GMT+09:00 2001*/
/* DO NOT EDIT simple.policy file*/
grant {
permission java.net.SocketPermission "localhost:1024-", "resolve, connect";
};
c:\work\otherdir>java -Djava.security.policy=simple.policy \
-Djava.security.manager ReadMarshalled marshalled.ser
manager ReadMarshalled marshalled.ser
Read MarshalledObject from file marshalled.ser, contents are:
This container holds specular
Marshalled object loaded by:null
Containee loaded by: sun.rmi.server.LoaderHandler$Loader@7c4bec
¢º ReadMarshalled source¿Í ½ÇÇà °á°ú¸¦ º¸¸é, Remote¿¡ ÀÖ´ø ObjectContainer.class
fileÀ» LoadingÇÑ ClassLoader´Â rmi system¿¡¼ Á¦°øÇÏ´Â LoaderHadlerÀÇ inner
classÀÎ Loader¶ó´Â °ÍÀ» ¾Ë¼ö°¡ ÀÖ´Ù. - Dynamic ClassLoader¶ó°í ÇÕ´Ï´Ù.
MarshalledObjectÀÇ SerializationÀ» Á¤¸®ÇØ ºÃ´Âµ¥, sun siteÀÇ developer site¿¡ Tech.
TIPÀÇ ³»¿ëÀ»Á¤¸®ÇÑ°ÍÀÔ´Ï´Ù.
Remote Method InvocationÀÇ ³»ºÎ Mechanism ÀÌÇØ¿¡ µµ¿òÀÌ µÇ¾úÀ¸¸é ÇÕ´Ï´Ù.
2001.05.11 written by Jeon HongSeong
|