There's an old saying I like to quote: 92.6 percent of all statistics are made up on the spot. I took statistics in school (never mind how long ago that was) and I never saw anything to make me doubt that old chestnut.
Last month, I let an assumption color my logic and Pete Neill—a mathematically astute reader—was kind enough to point it out to me. If you recall, I mentioned that some large percentage of drivers consider themselves better than average. I said that this could not be possible. As Pete pointed out, it is certainly possible to have a population where most members are above average. For example, suppose out of 100 people, 99 make $75/hour and 1 person makes $1/hour. This is a case where most people are above average.
Of course, I was assuming the relative merit of drivers is normally distributed. While that's probably a pretty good assumption, it is an assumption. It makes me wonder what other assumptions might creep into my programming.
I find I program in two different modes. When I'm working on something quick and dirty, I make a lot of assumptions. I assume user input will be correct and indices are in range. My second mode is when I know my code is will be actually used by other people. Then I'm a bit more cautious. Of course, this causes me more work when I start on something quick and dirty and by the end of the project, I realize the program will be used by others for some reason.
Java's run time exceptions do help protect you from some of your bad assumptions. Of course, they work better if you catch them and respond to them instead of letting them abort your program.
But I think the most dangerous assumptions we all make are in the fundamental way that things should work. Real innovation occurs when you don't assume what a solution will look like based on past experience. For example, a banker had to think of something different to invent the automatic teller machine. Of course, you have to temper this with some common sense. For every automatic teller machine invented there are probably quite a few "better ideas" that don't fly (think New Coke or DIVX pay per view DVDs).
To this end I like to start most projects with a few minutes of reflection. I try to think of completely new ways to do whatever my task is. Most of the time, these ideas turn out to be rather silly, but once in awhile something will spark a good idea that will either be incorporated in the final design, or even become the final design.
Sun Merges Web Resources
Sun announced they would join the developer-oriented Web sites that are
part of the Sun Developer Network. All developer resources, including
wireless and Solaris information will now be at sun.com/developers.
In addition to bringing all developer content together, the new site
adds an improved forum system that allows you to set up sophisticated
"watches" on forums so that you can receive notifications when
users post messages of interest.
There are still other entry points into Sun's information, though. For example, Linux developers will be interested in linux.java.net.
Speaking of Portals
JBoss' Web site is based on Nukes—a Java (and, of course, JBoss) rewrite
of the popular PostNuke content management system. Now, you can download
Nukes from their Web site. Download it from the project page at www.jboss.org.
According to a study of 15,000 developers conducted by BZ Research (the "Third Annual Java Use and Awareness Study," conducted in November 2003), 26.9 percent of respondents reported using JBoss in their company or at companies for which they consult.
Magnolia
Obinary recently announced the availability of Magnolia 1.1, a free Java-based
Enterprise-Content Management System (CMS). The new release features high-performance
caching and server-side image resizing. Magnolia installers and updaters
for all major operating systems are available for download at www.magnolia.info.
WebSphere V6 Preview
IBM unveiled a technical preview of the next version of WebSphere Application
Server. This version of WebSphere has full support for J2EE 1.4. In addition
to improvements in JSP and servlet processing, the new version sports
many interfaces required for J2EE 1.4 compliance like the Java Connector
Architecture 1.5 (JCA). The preview is available at 106.ibm.com/developerworks/websphere/downloads/.
API Focus: JAXP
XML is a great way to represent data in a standard portable format. Of
course, portability is a Java watch word, so it is not surprising there
are many ways to read and use XML from Java. To get the Sun APIs for XML
you can install the Java Web Services Developer Pack at java.sun.com/webservices/webservicespack.
JavaServer Faces (JSF) v1.0 EA4
XML and Web Services Security v1.0 EA2
Java Architecture for XML Binding (JAXB) v1.0.2
Java API for XML Processing (JAXP) v1.2.4
Java API for XML Registries (JAXR) v1.0.5
Java API for XML-based RPC (JAX-RPC) v1.1
SOAP with Attachments API for Java (SAAJ) v1.2
JavaServer Pages Standard Tag Library (JSTL) v1.1 EA
Java WSDP Registry Server v1.0_06
Ant Build Tool 1.5.4
Apache Tomcat v5 development container
Ws-I Supply Chain Management Sample Application 1.0
In particular, JAXP lets you write SAX- or DOM-based Java programs to interpret XML data. Which model you choose will depend largely on what you want to do with the XML. A SAX-based parser is best when you want to do element-by-element parsing of XML data. DOM-based parsers build an entire tree that represents the document, and therefore they are well-suited to tasks where you need to understand the document as a whole.
The idea behind a SAX parser is very simple. You simply acquire a SAXParserFactory (by calling SAXParserFactory.newInstance) and use it to create a new parser instance (by calling newSAXParser). Once you have this parser object, you can call one of the parse methods that cause the parser to work through a stream of XML data.
When you call parse, you include a reference to a handler object
that you provide (usually an object that extends DefaultHandler).
In this object you override methods that the parser calls when it finds
certain elements. For example, the parser will call startElement
when it finds an element or characters when it finds character
data.
The parser notifies when it finds just about anything that has meaning.
However, it doesn't ordinarily send information about things that don't
have direct meaning, like comments. If you are, say, importing XML into
a database, that's not a problem. But if you are trying to transform an
XML file into another XML file, you may want to keep these items that
the parser considers meaningless.
If you want to receive notification of additional items you have to implement
the LexicalHandler interface. This interface allows the parser
to call you when it detects things like comments, CDATA boundaries, entity
boundaries, and DTD boundaries.
However, there is a slight problem. JAXP is not actually an XML parser.
It is an interface to an underlying XML parser (and you can select different
parsers). This is similar to AWT, for example. It isn't a windowing interface,
but an interface to an underlying windowing interface. The problem arises
because the real parsers are not required to implement LexicalHandler.
You can ask the parser factory to provide different parsers, including
ones that validate a document against a DTD, or one that is aware of namespaces.
So SAX is a very easy way to write simple code that processes an XML data
stream. The other alternative is DOM, which builds a tree structure that
represents the entire document. We'll save DOM for another month.
Hey Martians! What Time is It?
Here in Houston, NASA is big business. Although the manned program that
operates from Houston is still trying to recover from last year's tragedy,
the unmanned program has made two landings on Mars last month. If you'd
like to know what time it is on Mars, and where day and night are, visit
www.giss.nasa.gov/tools/mars24/.
There you'll find a Java program and a corresponding applet that will
make sure you keep your Martian appointments. Even if you don't know anyone
on Mars, the graphics alone are worth a look. Unfortunately, I couldn't
find the source code for the program, but it is a nice use of Java.
It has been widely reported that NASA uses Java software to plot the Mars journey. The Science Activity Planner takes all the raw data from the mission data base and builds a 3-D terrain visualization. A civilian version of the software—Maestro—is available at mars.telascience.org/.
Tech: UDP Packets
Everyone knows it is simple to make TCP sockets in Java—that's one of
the library's biggest strengths. TCP sockets—the kind that Web servers,
for example, use—work like a telephone. You connect directly to another
computer and have a conversation. Like a telephone, that conversation
is fairly reliable. Barring technical problems both parties will hear
everything that is said during the call.
However, there is another type of packet that is sometimes used: a UDP socket. Java can handle these as well. If a TCP socket is a telephone, then a UDP socket is a radio transmitter. UDP sockets don't revolve around connections, so you can send datagrams to multiple receivers. However, you have no idea if your messages were received or by how many receivers. In addition, there is no assurance that interested receivers will hear any given packet or that they will arrive in the same order that the sender transmitted them.
So why use UDP sockets? The simple answer would be that some services require UDP. But a better answer has two parts. First, UDP is more efficient than TCP which might be important for, say, streaming audio, video, or telemetry. Second, UDP can do tasks that would be difficult to do using TCP. For example, suppose you want a monitoring computer to broadcast temperature telemetry to any interested workstation on the network? That would be a good job for a UDP socket.
Of course, if you find yourself building a system that acknowledges data, resends missing packets, and reorders them you should probably switch to TCP—after all, that's exactly what TCP does.
I wrote a book on Java networking (Java 2 Network Protocols Black Book from Paraglyph Press). In that book there are examples of UDP used for obtaining time updates, for example. TFTP (trivial file transfer protocol) can also use UDP sockets. In another book, Embedded Internet Design from McGraw Hill, I wrote about using UDP to communicate from embedded processors to other Internet hosts which is an ideal use for UDP.
So how do you use UDP sockets? The key are two classes in the java.net package: DatagramSocket and DatagramPacket. The datagram packet contains state that indicates an IP address (which could be a broadcast address) and a port number. When you are sending a packet, you set these values. When your receive data, you only set the port number. The IP address will indicate the source of the received data. Here's an excerpt from the Java 2 Network Protocols Black Book:
byte ary[]= new byte[128];
DatagramPacket pack=new DatagramPacket(ary,128);
if (reading) {
// read
DatagramSocket sock=new DatagramSocket(portnum);
sock.receive(pack);
String word=new String(pack.getData());
System.out.println("From: " + pack.getAddress() +
" Port: " + pack.getPort());
System.out.println(word);
sock.close();
} else { // writing
DatagramSocket sock=new DatagramSocket();
pack.setAddress(InetAddress.getByName(hostname));
pack.setData(dataString.getBytes());
pack.setPort(portnum);
sock.send(pack);
sock.close();
}
Although UDP sockets are not as common as TCP, they are still a useful tool to have in your networking arsenal.
Microsoft Wants You!
Microsoft has issued a beta release of Version 3.0 of the JLCA (Java Language
Conversion Assistant) tool. This tool can move Java code to Microsoft's
Visual C# .Net and .Net Framework.
Featured in version 3.0 is the ability to convert J2EE 1.3 applications
including JSP and EJB. J2EE 1.3 and JDK libraries can be converted. Microsoft
also claims the tool will handle code that uses EJB, JAAS (Java Authentication
and Authorization Service), JCE (Java Cryptography Extension), JMS (Java
Message Service), JNDI (Java Naming and Directory Interface), and RMI
(Remote Method Invocation). The new version of JLCA also has improved
Swing support.
If you want to check it out, go to http://www.beta.microsoft.com, and
select "Sign in as Guest." Enter "JLCA3" as the guest
ID, and select the "Java Language Conversion 3.0" link. Personally,
I'll stick with Java.
Apple releases Java 1.4.2 for Mac OS X
Apple says this release provides drawing and stability improvements. Download
it at www.apple.com/java/.
Coming up in DDJ
In the April issue of Dr. Dobb's Journal, you'll find two Java books reviewed:
Pragmatic Unit Testing: In Java with JUnit
by Andy Hunt and Dave Thomas
Developing Games in Java
by David Brackeen
In Closing
Earlier, I mentioned taking some time to think about offbeat ideas when
you start a design. Of course, sometimes it is difficult to make that
extra time for "blue skying" when faced with deadlines and other
projects.
This year, of course, is a leap year—so you have that extra day! Have you ever considered that if you could put aside 45 minutes a week for something (perhaps something you want to do, instead of something you have to do) that you'd have an extra 39 hours a year! That's nearly one work week. Maybe I'll take my 45 minutes and finally learn Spanish.
What would you do with an extra week in your year? Drop me a note at [email protected] and let me know.
See you next month!
Al Williams
Dr. Dobb's Journal