| |
"Pervasive Computing:
The Next Generation of Consumer Applications"
Vol. 7, Issue 4, p. 70
Listing 1
/**
* Returns the text of the document specified in documentName.
*/
public String getDocument(String documentName)
{
System.out.println("getDocument(" + documentName +")");
StringBuffer documentText = new StringBuffer();
try
{
// We prepend /tmp to prevent creative people
// from hosing our system...
BufferedReader inStream =
new BufferedReader(new FileReader("/tmp/JDJTemp/" + documentName));
String tmp = inStream.readLine();
while (tmp != null)
{
documentText.append(tmp);
tmp = inStream.readLine();
}
}
catch (IOException e)
{
documentText.append("<Error reading document>");
}
return documentText.toString();
}
Listing 2
<?xml version="1.0"?>
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:TextEditorService">
<isd:provider type="org.apache.soap.providers.StatelessEJBProvider"
scope="Application"
methods="getDocument saveDocument">
<isd:option key="JNDIName"
value="TextEditorServiceHome"/>
<isd:option key="FullHomeInterfaceName"
value="com.pilone.texteditor.TextEditorServiceHome" />
<isd:option key="ContextProviderURL"
value="nova.slac.com:1099" />
<isd:option key="FullContextFactoryName"
value="org.jnp.interfaces.NamingContextFactory" />
</isd:provider>
<isd:faultListener>
org.apache.soap.server.DOMFaultListener
</isd:faultListener>
</isd:service>
Listing 3
import org.ksoap.*;
import org.ksoap.transport.*;
public class TextEditorServiceProxy
{
public String getDocument(String documentName)
{
String document = null;
try {
SoapObject rpc = new SoapObject("urn:TextEditorService", "getDocument");
rpc.addProperty ("documentName", documentName);
HttpTransport transport = new HttpTransport("http://www.sys-con.com/soap/servlet/rpcrouter", "");
document = transport.call(rpc).toString();
}
catch (Exception e) { e.printStackTrace(); document = e.toString(); }
return document;
}
public void saveDocument(String documentName, String documentText)
{
try {
SoapObject rpc = new SoapObject("urn:TextEditorService", "saveDocument");
rpc.addProperty("documentName", documentName);
rpc.addProperty("documentText", documentText);
HttpTransport transport = new HttpTransport("http://www.sys-con.com/soap/servlet/rpcrouter", "");
transport.call(rpc);
}
catch (Exception e) { e.printStackTrace(); }
}
}
Listing 4
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
// SOAP imports...
import org.ksoap.*;
import org.ksoap.transport.*;
public class TextEditorClient extends MIDlet
implements CommandListener
{
public TextEditorClient ()
{
// Create the main form
mainForm.append(m_DocumentName);
mainForm.addCommand(m_GetCommand);
mainForm.addCommand(m_SaveCommand);
// Create the document viewing form
m_DocumentText.addCommand(m_SaveCommand);
m_DocumentText.addCommand(m_CancelCommand);
// We'll handle button presses for both
mainForm.setCommandListener (this);
m_DocumentText.setCommandListener(this);
}
public void startApp ()
{
Display.getDisplay (this).setCurrent (mainForm);
}
public void pauseApp ()
{
}
public void destroyApp (boolean unconditional)
{
// We could persist our current document
// to a record store here if needed
}
public void commandAction (Command c, Displayable d) {
// Find out which command was activated
if (c == m_GetCommand)
{
// Retrieve the document name, then retrieve the
// document from our proxy.
String documentName = m_DocumentName.getString ();
m_DocumentText.setString (m_TEProxy.getDocument(documentName));
Display.getDisplay(this).setCurrent(m_DocumentText);
}
else if (c == m_SaveCommand)
{
// Ask our proxy to save the object,
// then return to the main form
m_TEProxy.saveDocument(m_DocumentName.getString(),
m_DocumentText.getString());
Display.getDisplay(this).setCurrent(mainForm);
}
else if (c == m_CancelCommand)
{
// Simply redisplay the main form. We can ignore
// changes that were made to the document since
// the only way to return to this screen is to
// try to load a document that will refresh the
// contents of our document variable
Display.getDisplay(this).setCurrent(mainForm);
}
}
private Form mainForm = new Form("Text Editor");
private TextField m_DocumentName =
new TextField("Document Name:", "foo", 100, TextField.ANY);
private TextBox m_DocumentText =
new TextBox("", "", 1000, TextField.ANY);
private Command m_GetCommand =
new Command("Load", Command.SCREEN, 1);
private Command m_SaveCommand =
new Command("Save", Command.SCREEN, 1);
private Command m_CancelCommand =
new Command("Cancel", Command.SCREEN, 1);
private TextEditorServiceProxy m_TEProxy =
new TextEditorServiceProxy();
}
|
|