Archive for April, 2006
Init hooks into Apache XML-RPC3 user handlers
Here are some lite mods to the Apache XML-RPC v3 code that allow you to take explicit control of initializing your user handlers when used inside a Java servlet.
In this new scheme, a handler artifact declared in XmlRpcServlet.properties
artifact=mypackage.Handler
must implement the (new, proposed) UserHandler interface
package org.apache.xmlrpc.server;
import javax.servlet.http.HttpServlet;
public interface UserHandler {
public void init(HttpServlet s);
}
In your servlet init() method, we acquire the reference to the handler and allow it to do whatever configuration it should do before requests are made of it.
public class MyServlet extends XmlRpcServlet {
...
public void init() {
// put useful stuff in ServletContext here...
// then init the handler
getUserHandler("artifact").init(this);
...
}
...
}
Without the init hooks, and because the Handler is instantiated within the parent class servlet constructor, you have no way of preparing the handler in a special way before the servlet begins taking requests.
The model I’ve hinted at here suggests that prior to calling init() on the user handler, you have put useful things in the servlet context for handler.init()’s consumption and use. Acquiring inherited Log4J logger instances and DataSource instances come to mind.
There may be a good way to do what my mods do — without the mods — but I couldn’t immediately see it.
[tags]xmlrpc,xmlrpcservlet,servlet[/tags]
Welcome to the Voice 2.0 Big House
There is Good News in the IP voice services world this morning. It’s not new news to some of us, but to many I know it is.
The Good News is that the rest of us have been invited into The Big House. The Big House where you don’t need Session Border Controllers, Media Gateways, or a room-filling Advanced Intelligent Network platform. Or captive customers. You don’t need any of these to be a member of the Voice 2.0 producer economy.
All you need is an XML-RPC server process. And Java or Perl or Python or Ruby or Groovy, or whatever scripting language in which you think and which makes you happy.
Then, implement your philosophy.
Take an award winning company like Televolution, to whom I consult [disclaimer], that wants to send you as a developer voice event and signalling data, and award winning Iotum with their call relevance brainchild, and we’ve two companies with complementary services who have shown the way.
And it goes like this: Bob is a customer of both PhoneGnome and Iotum. Bob as an Iotum subscriber is continuously relaying his state and availability information to his Iotum service, via a network plugin that monitors his Outlook calendaring and MSN Messenger state. Bob has also configured call routing rules in his Iotum service. When a call comes in for Bob on his PhoneGnome line, PhoneGnome sends a short XML-RPC message to the Iotum server indicating who is calling Bob. The Iotum server applies Bob’s rules to his realtime state, and instructs PhoneGnome in a simple XML-RPC response in how to dispose of the call: desk phone, mobile phone, or voicemail. Bob takes control of where, when, and under what conditions his calls are terminated.
Because we are programmers, let’s look at the two messages that pass between the PhoneGnome and Iotum platforms on the wire.
Given an inbound call from 9185551212 to Bob at 8165551212, PhoneGnome sends, in realtime
<?xml version="1.0"?>
<methodCall>
<methodName>theMethod</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>From</name>
<value>
<string>"9185551212" <sip:9185551212@televolution.net></string>
</value>
</member>
<member>
<name>Contact</name>
<value>
<string>PHONEGNOME TEST</string>
</value>
</member>
<member>
<name>To</name>
<value>
<string>8165551212</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>
and after applying Bob’s rules, Iotum replies
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>action</name>
<value>
<string>ACCEPT</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
In this ACCEPT example, Iotum tells Phonegnome to put the call through its normal processing. But it could have been REDIRECTed had Bob’s rules indicated it.
That’s it. PhoneGnome doing what it does best, Iotum doing what it does best, and both doing that best extremely well because the technology and federation allow that focus.
Two text messages announce the Good News: if you know any language for which there exists an XML-RPC implementation — and they are many — you are a member of the modern Voice over IP developer community.
For the first hundred years of voice telephony, you were not invited. You didn’t have the money. You didn’t own a Network.
Welcome. You are now invited — we have running code to prove it.
[tags]phonegnome,iotum,voip,xmlrpc[/tags]
Dialing MIDP2.0 phones programmatically
A user interested in the PhoneGnome midlet pointed out something interesting and quite useful today: how to induce a Java MIDP2.0 phone to dial out under MIDlet control.
The Javadoc for MIDlet.platformRequest(String URL) says
If the URL specified is of the form tel:, as specified in RFC2806, then the platform MUST interpret this as a request to initiate a voice call. The request MUST be passed to the “phone” application to handle if one is present in the platform. The “phone” application, if present, MUST be able to set up local and global phone calls and also perform DTMF post dialing.
See also this link for additional discussion.
Very cool.
Thanks, Richard.
[tags]jme,j2me,midp,phonegnome[/tags]
Running an XML-RPC server as a Java servlet
Roll your own XML-RPC servlet using Apache XML-RPC v3.0.
The essentials:
The servlet.
The build.xml file.
The web.xml file.
The XmlRpcServlet.properties file, which defines which of your classes handle which remote method calls.
The handler class for this example.
And finally, the Log4J log4j.properties file, which I always deploy early in this sort of project - because I know I will eventually want it. See initLogger() in servlet.init(). The log4j.properties and initLogger() method immediately allow for the creation of rolling appenders for log output to a disk file.
You will also need the ws-commons jar file. This is an unsettling URL, what with the string literal SNAPSHOT in it. If anyone has a better version that bespeaks stability, I would appreciate knowing. It was the only jar of its type I could find to satisfy the VM (argh).
Here is what the war file looks like after ant dist
images/ META-INF/ META-INF/MANIFEST.MF WEB-INF/ WEB-INF/classes/ WEB-INF/classes/org/ WEB-INF/classes/org/apache/ WEB-INF/classes/org/apache/xmlrpc/ WEB-INF/classes/org/apache/xmlrpc/webserver/ WEB-INF/classes/org/apache/xmlrpc/webserver/XmlRpcServlet.properties WEB-INF/classes/org/carpediem/ WEB-INF/classes/org/carpediem/phonegnome/ WEB-INF/classes/org/carpediem/phonegnome/crm/ WEB-INF/classes/org/carpediem/phonegnome/crm/Handler.class WEB-INF/classes/org/carpediem/phonegnome/crm/log4j.properties WEB-INF/classes/org/carpediem/phonegnome/crm/PGXMLRPCServer.class WEB-INF/lib/ WEB-INF/lib/log4j-1.2.13.jar WEB-INF/lib/ws-commons-util-1.0-SNAPSHOT.jar WEB-INF/lib/xmlrpc-3.0a1.jar WEB-INF/web.xml
The war file is copied, of course, into /webapps of your servlet container (e.g., Tomcat, jetty), and the container started.
The resulting example servlet can service a remote call of the form crm.inBound(string1,string2).
[tags]xmlrpc,xml-rpc,servlet,tomcat[/tags]
Chroot’ing Tomcat
Something that deserves more attention that it gets: running Tomcat in a chroot jail.
Update: 5/11/2006
Some notes on following the Brittain/Darwin procedure from their fine book referenced above.
The platform: SuSe 8.2
- In addition to what ldd java outputs, I needed /lib/libm.so.6 because libjvm.so needed it. I also needed /lib/libnsl.so.1.
- I had to do cd /usr/local/chroot; mkdir proc; mount –bind /proc proc. This is unsettling, but the jvm needs access to /proc to start. Otherwise you get find_vma failed when the vm attempts to start. I’m not sure this mount step is staying in my process, but for now it’s there. I’d love to hear from anyone who knows of a better way to deal with this.
If you continue to have problems chrooting Tomcat, temporarily copy /usr/bin/strace into /usr/local/chroot so you can do
/usr/local/bin/jbchroot -U tomcat -- /usr/local/chroot /strace /usr/local/jdk1.5.0_06/bin/java -version > /tmp/strace.out 2>&1
Then, file by file, examine the output of
parsetrace.pl < /tmp/strace.out
where parsetrace.pl is here and looks like
#!/usr/bin/perl -w
use Uniq; # See http://search.cpan.org/~syamal/Uniq-0.01/Uniq.pm
while( ) {
if( /^open\(\"(.*)\".*ENOENT/ ) {
push @files, "$1";
}
}
foreach $j (uniq (sort @files) ) {
if ( -e $j ) {
print "$j ";
$r = `file $j`;
chop($r);
print $r . "\n";
}
}
The script shows you what files could not be found running chroot that can be found on a non-chrooted system. It’s a matter of working your way through the output found files to determine which ones whose absence chroot’d are killing the vm during startup.
Update 5/13/2006
Here is a scripted method of creating a chroot jail, and placing Tomcat and Java in it. The only external dependencies are a binary distribution of Tomcat and a binary distribution of Java (the shell-based install, not the RPM version).
With the binary Tomcat and Java distributions in the current working directory, do
$ sh makeChroot
$ /etc/init.d/tomcat start
If you end up running the script multiple times as you tune and tweak for your system, you will first find the need to do
$ umount $chroot/proc
where $chroot is the chroot prefix
Also included in the script are options to run with -security with or without Java security policy debugging.
One bothersome feature is the need to replicate /proc into the chroot jail. If anyone has a better way of dealing with the JVM’s need for /proc, I would appreciate knowing.
[tags]tomcat,find_vma<webapps[/tags]
Mating Tomcat to JXTA
Modernizing a neat idea discovered while reading Sean Kelly’s RegistryVous, comes a rough work called BridgeServlet.
BridgeServlet is a Java servlet that has a view into a JXTA peer to peer network. In fact, BridgeServlet functions as a servlet and JXTA peer simultaneously, processing HTTP requests on one side and JXTA peergroup services on the other. Very cool, Sean.
The BridgeServlet main class file
ant dist builds dist/bridge.war, which is copied into Tomcat’s webapps/

directory in the usual way. ConfigurationFactory.java, found in the servlet code, is basically what you would find in platform/binding/java/contrib/rendezvous/. It’s a quick way to configure the platform without going through the GUI that JXTA config is (in)famous for.
Thus far, what does BridgeServlet actually do? Very little, beyond using modern JXTA APIs, starting the JXTA Platform, joining a sample peergroup — in this case the AE6RT Beacon peergroup — and serve up a list of peers joined to the group. The goal might be to set the JXTA side of the bridge toiling away, monitoring advertisements, or even accept advertisements originated as HTTP POSTs and publishing them to the JXTA network. Seti@Home does something like this, giving web clients a way to browse the productions of its network.
Note how the servlet.destroy() method is overridden to affect a resignation from the application peergroup, followed by stopping the application and Net peergroups. Stopping the Net peergroup causes the JXTA platform to shutdown.
Finally, a screenshot, showing a list of peers currently joined to the group. This sort of production rapidly approaches a situation where Struts or JSF or Grails are needed to do the really interesting apps.
Update: New uploads include ConfigurationFactory.java, log4j.properties, and a sample peergroup advertisement so that BridgeServlet.java can compile and run.
Update 8 Dec 2006: The ConfigurationFactory class should be replaced with its near-equivalent NetworkConfigurator, which is now part of the JXTA platform distribution.
[tags]p2p,jxta,servlet,tomcat[/tags]
JXTA meets HTTP
James and I have been discussing meerkat, a scheme he’s been developing for some time. “JXTA Meerkat”, not to be confused with O’Reilly’s Meerkat, seeks to bridge clients with HTTP-only capabilities to a JXTA backend network.
JXTA Meerkat mirrored some thoughts I was having about how to bridge the world of browsers-only and a JXTA network. My original fuzzy approach involved Tomcat as a front end to a JXTA network on the backend, while James took a more direct approach and immediately moved to a lightweight HTTP frontend to the same backend p2p network.
Why bother bridging HTTP clients to JXTA networks? Simple: there may be a sea of users who are interested in the productions of the JXTA network who are not actually participating in it. For example.
If I have Meerkat’s semantics right, and I’m not at all sure I do, it would need a way to specify a peergroup to query using standard CGI or Servlet-like technology
For example, to query for peer advertisements we might say
http://www.portal.example.com/cgi-bin/jxtaportal.cgi?pgid=urn:jxta:uuid-C75F3A11542D493CA07BE47E1F151BBF02?adv=peers
for peergroup ID urn:jxta:uuid-C75F3A11542D493CA07BE47E1F151BBF02.
Such simple queries duplicate what one can do with the JXTA Shell. But Meerkat based services would be a true bridge: http on the front, JXTA on the back.
One might imagine this query syntax growing quite elaborate and expressive, in a good way. It could easily grow to accomodate HTTP clients injecting work or data into the JXTA network in perhaps some network-for-hire scenario. All without the http client being a JXTA peer per se.
Update: I knew such a project existed, but couldn’t put my finger on it at the time: Sean Kelly’s registryVous. It uses a Tomcat frontend to a JXTA network. The JXTA facing portion of the bridge is started in the servlet.init() function. Clever.
[tags]jxta,p2p,meerkat,tomcat,servlet[/tags]
JXTA Shell Extension for generating platform IDs
A simple JXTA Shell extension for generating useful platform IDs, typically occurring in the course of developing new pipes or services.
Usage and typical output are included in source comments.
A command line equivalent that allows the PeerID and PipeID to be based on an input PeerGroupID. Usage is in the source comments.
The utility is intended to be used not in a dynamic runtime situation, where IDs are created on the fly as needed, but as a tool to generate well-known IDs to be statically embedded in application code.
[tags]jxta,p2p[/tags]
JXTA Advertisement code generator
JXTA relies heavily on the concept of advertisements, which are XML documents that describe what you have to offer the network or what you seek to acquire from it. A typical JXTA app will have a number of advertisement types, and each needs to be marshalled to/from XML via code you must supply.
Here is a utility and work-in-progress to create that Java marshalling code based on a declarative XML description of what the advertisement looks like.
To compile (under JSE 5)
javac AdvMill.java
To run
java AdvMill infile
The utility generates from the XML input both the abstract Advertisement Java code and the implementation Advertisement Java code.
A shortcoming of the utility is that it is not smart enough to know how to deal with non-String type properties of the advertisement. So, for example, if an advertisement has a member whose natural type is CodatID, the utility doesn’t know enough to use the IDFactory to take the String representation and produce the native CodatID type. That may be the subject of future work, but for now, the utility does nicely bootstrap creating advertisement code, and in some sense formalizes the description in a bit of XML. Meaning that if you have non-String types in your definition, the impl Adv code will not compile without tweaking.
At some point you not only want to compile the generated code, but also to look at it. Given the tedious nature of outputting the generated code, indenting in the utility quickly became more trouble that it was worth. I found myself running the generated code through astyle for formatting purposes.
Update: The AdvMill.java utility code has been contributed to the JXTA Commons Project.
[tags]jxta,p2p[/tags]