15.01.2015 Views

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

to use an existing object instance instead. For example, users of<br />

our mechanism can write the following Java codes to persist a<br />

UserClass object:<br />

UserClass s = new UserClass();<br />

encoder.setPersistenceDelegate(UserClass.class,<br />

new UserClassDelegate() );<br />

encoder.getInterpreter().defineVariable<br />

("myUserClass", s);<br />

encoder.setCacheEnabled(true);<br />

encoder.writeObject(s, "//executescripthere"); ….<br />

The result is shown in Figure 2. Note that the decisi<strong>on</strong> about using a<br />

cached object is made in the encoding process. If the<br />

setCacheEnabled(true) method is called by the encoder, the<br />

decoder must call the corresp<strong>on</strong>ding readObject(true) method to<br />

use the cached object in the decoding process.<br />

...<br />

<br />

<br />

UserClass0<br />

myUserClass <br />

<br />

<br />

<br />

<br />

…<br />

<br />

<br />

...<br />

<br />

<br />

<br />

myUserClass<br />

<br />

<br />

<br />

<br />

<br />

// put script here<br />

....<br />

Figure 2. Enabling cache pool.<br />

Name of the<br />

Cached Object<br />

Properties of the<br />

Cached Object<br />

Mapping of<br />

Name Spaces<br />

Before the encoder writes all the properties of the UserClass in the<br />

XML text file, a hash table must be written. In the decoding stage,<br />

the XMLDecoder will use this table to decide which object should<br />

not be instantiated. It will then use an existing object instance from<br />

the interpreter’s object pool instead, <strong>and</strong> reset all the properties of<br />

the UserClass object. For users to get the UserClass object with new<br />

properties in the decoding process, the Java codes may look like<br />

this:<br />

UserClass s = new UserClass();<br />

decoder.getInterpreter().defineVariable<br />

("myUserClass", s);<br />

…<br />

UserClass s1 = (UserClass)<br />

decoder.readObject(true); ….<br />

Here, the readObject(true) method is used to notify the<br />

XMLDecoder that the object is cached <strong>and</strong> therefore it should not<br />

try to instantiate a new object. In this manner, new properties<br />

recorded in the XML text for this cached object will be set up, <strong>and</strong><br />

then returned. In the above example, the variables s <strong>and</strong> s1 refer to<br />

the same object.<br />

Note that because a Singlet<strong>on</strong> object does not have a public<br />

c<strong>on</strong>structor, its instance cannot be persisted using the<br />

XMLEncoder’s writeObject() method. On the other h<strong>and</strong>, users<br />

cannot write a PersistenceDelegate class for a Singlet<strong>on</strong> because in<br />

order to let a PersistenceDelegate work, more than <strong>on</strong>e instance of<br />

the class must be created, which violate the nature of a Singlet<strong>on</strong>.<br />

Our mechanism can be used to solve this problem.<br />

Users can write a wrapper class to access the Singlet<strong>on</strong> object. The<br />

wrapper object is used to hold all the properties of the Singlet<strong>on</strong><br />

object in the encoding process so that the wrapper object is<br />

persisted. In the decoding process, users can instantiate a wrapper<br />

object bound to the Singlet<strong>on</strong>, <strong>and</strong> cache it in the object pool.<br />

Hence, all properties stored in the wrapper object can be retrieved<br />

from the XML text <strong>and</strong> set for the Singlet<strong>on</strong> object via running the<br />

script (if any) al<strong>on</strong>g with the wrapper object. This process is<br />

suitable for argument passing of Web services or remote procedure<br />

calls. It can also be applied when an undo/redo functi<strong>on</strong> is added to<br />

Java applicati<strong>on</strong>s or Java integrated development envir<strong>on</strong>ments.<br />

4. API IMPLEMENTATION<br />

This secti<strong>on</strong> presents the API implementati<strong>on</strong> of the extensible<br />

mechanism. The key features of the implementati<strong>on</strong> are (1) an<br />

interpreter is integrated with the XMLEncoder <strong>and</strong> the<br />

XMLDecoder; (2) the name spaces of the XMLEncoder <strong>and</strong> the<br />

interpreter are mapped; <strong>and</strong> (3) cached objects can be used, instead<br />

of instantiating new objects.<br />

The result of the implemented mechanism is a Java API package,<br />

which is available for download publicly from the web site<br />

http://sml-109.iis.sinica.edu.tw/eLTP/.<br />

4.1 Integrati<strong>on</strong> of a Java source interpreter<br />

In order to run scripts in the decoding process, a Java source code<br />

interpreter must be utilized. Any interpreters that can interact with a<br />

Java virtual machine are suitable; therefore we have designed an<br />

interface XMLScriptInterpreter for these interpreters, as shown in<br />

Figure 3. A c<strong>on</strong>crete class that implements this interface is regarded<br />

as a proxy of the practical interpreter.<br />

The XMLScriptInterpreter interface defines five methods. The<br />

interpret() method is used to run the script, the two defineVariable()<br />

methods are used to define variables in the interpreter envir<strong>on</strong>ment,<br />

the setVariable() method is used to set the value of a variable, <strong>and</strong><br />

the getVariable() method is used to get the value of a specified<br />

variable.<br />

interface XMLScriptInterpreter<br />

{<br />

void interpret(String script);<br />

void defineVariable(String var, Object obj);<br />

void defineVariable(String var, Object obj, Class c);<br />

void setVariable(String name, Object value);<br />

Object getVariable(String name);<br />

}<br />

Figure 3. The XMLScriptInterpreter interface.<br />

97

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!