June 2003 Archives
June 16, 2003

Commercial support for Apache Cocoon

It's really great to see the main commercial supporters of Apache Cocoon rallying together to create a much stronger force. Orixo is an organization of no less than 6 different European companies spread out in 6 different countries, that joined together to provide backing to Apache Cocoon, an open-source XML processing engine. Congratulations to everyone involved!

PS: Interesting to know what was happening back in November 2002 while we were eating pork ribs and drinking amazingly good beer ;) You naughty guys, you didn't say a word until now... Cheers!

Update: BTW, what does Orixo mean?

Update: Matthew provides the answer to it. Thanks!

Posted by ovidiu at 10:36 PM |
June 11, 2003

JavaOne pictures

Java
Java Duke at JavaOne.

I've uploaded some of the pictures I made at JavaOne in my picture gallery. Appologies for the poor quality, I didn't get too close to the speakers in most of the cases. I also updated the previous entries to contain a relevant picture.

Posted by ovidiu at 10:51 PM |

JavaOne - compiling programming languages to the JVM

Java | Open Source
Per Bothner explaining how Kawa works.

Per Bothner's talk on Kawa is about to start. Per worked on various things over the year. He used to work for Cygnus, before being acquired by RedHat; he worked on GCC and many other tools.

17:16 It started. Apparently Per is the only presenter not affiliated with any company, which an almost unheard of at JavaOne. The attendance is pretty light, it's either too late or people don't care about languages other than Java.

What do you do when you need a higher language than Java? Well, you can write an interpreter. However if you do repetitive computations, it can get pretty slow. Another approach is to compile the program in your language to Java source code.

The best approach is to compile directly to in-memory Java bytecodes. Per makes the interesting assertion that bytecodes are more general than Java source: you actually have goto statements.

Kawa was written while at Cygnus in 1996 and is a GNU project, with a more liberal license than GPL. Kawa can be run interactively from the command line, it can be compiled to a program file. Languages implemented in Kawa: Scheme, XQuery, XSLT, Emacs Lisp etc.

Short introduction to Scheme, an "impure" functional language because of assignment. You run Kawa by doing java kawa.repl and you get the interactive prompt. It supports big integers.You can write applets in Scheme's Kawa.

Another language supported is Common Lisp. Guy Steele was instrumental in the Scheme, Common Lisp and Java languages.

Emacs Lisp: "Emacs is still the most powerful text editor". Kawa compiles Elisp to Java bytecodes. Goal is modern Emacs re-implementation that can efficiently use existing Emacs packages. Uses Swing to represent and display text. A nice JEmacs screenshot, unfortunately not many people actually contribute to it.

XQuery is a very high-level language used for querying, combining and generating XML-like data sets. It is a superset of XPath. Kawa supports XQuery with Qexo, which is missing some features, but still very useful. Some example of XQuery to generate an HTML fragment: it uses HTML markup and XQuery syntax to generate the output page. The example can generate XHTML 1.0, HTML, Scheme from the same file. The example XQuery program can be compiled to a Java class with a main, or to a servlet which can be deployed on a servlet container.

XQuery can be considered as an alternative to JSP. An XQuery program can also be compiled to a CGI program, not very useful these days however. You can embed the XQuery engine in a Java program and take advantage of its power.

Next language shown is XSLT. The Kawa implementation compiles an XSLT stylesheet into a Java class. The project is incomplete, but it's a useful example.

BRL is Beautiful Report Language, a template language much like JSP. Instead of embedding Java, you embed Scheme. KRL - Kawa Report Language - is Per's implementation. The language uses square brackets to embed Scheme code. You can embed such code within HTML tags.

Nice is a strongly typed language with multi-methods, parametric types, anonymous functions, tuples and multiple implementation. KRL and Nice were both written by other people than Per.

Implementation. Each language is a subclass of Interpreter. Each Interpreter uses a Lexer to parse an expression or a program file. The result is an Excpression instance. There are many subclasses of Expression. Once you have an Expression object, you call the compile() method to compile the script. This method takes two arguments, a Compilation object for managing the state, and a Target object for specifying where to leave the result, usually the JVM's stack.

The implementation uses the gnu.bytecode package written by Per for handling bytecodes and .class files: code generation, reading, writing, printing and disassembling. This is a library for dealing with the very low level bytecodes. Apache has the equivalent BCEL, but Per claims gnu.bytecode is more efficient because it doesn't generate a Java object for each bytecode being outputted.

In summary, Kawa includes a good compiler and useful libraries. The Scheme and XQuery languages on Kawa are the most popular languages. The license is either GPL or a more liberal license that allows you to include Kawa in a commercial application if you don't do any modifications to the original code. If you do such changes, you are required to submit them back to Kawa.

Kawa is available at http://www.gnu.org/software/kawa, Qexo could be found at http://www.gnu.org/software/qexo/.

Questions. XQuery was started in summer 2001, and is still in works. Per works on it part-time, he's day-time job is working for Apple (as a contractor at Apple - working on GCC?). Kawa's Scheme is not tail-call elimination. Writing parsers with Yacc sucks, Per prefers writing them by hand. GCC is replacing the Yacc parser with a hand-written descendent recursive parser. Kawa is an optimizing compiler, sort of. It doesn't do common subexpression elimination, it uses a simple minded register allocation. Errors generated at runtime will have an exception stacktrace that refers to the original source file.

Posted by ovidiu at 06:15 PM |

Apple WWDC

Apple

Got this in my mail:

Steve Job's invitation to WWDC
Posted by ovidiu at 03:35 PM |

JavaOne - XML parsing (with StAX)

Java

Even though I've heard about XML pull parsers, I haven't had time to play with them. From the abstract, this session seems to be a good introduction to the technology. The presenters, Ping Guo, Mark Scardina and K Karun are from Oracle.

I'm actually interested in more higher level XML processors, similar to XSLT, but that don't require a DOM tree representation of the input XML document. I looked at STX, Streaming Transformations for XML, and Joost, an open-source Java implementation of STX. The idea is intruiging and this particular implementation seems to be very clean. The CVS version seems to also have support for hooking up with the Java classes running outside the processor.

The Joost STX implementation uses a SAX parser to do the work, which makes it difficult to abort the processing of large XML documents. This could come in handy when in the middle of the processing, the program discovers it has nothing else to do. This would be a very useful feature for a streaming XML processor. StAX could solve this problem.

14:45 It started. Ping is the lead developer and the presenter of this session, K is an expert group member of JSR 173, and Mark is a manager. StAX is the subject of JSR 173.

Why do such presentations have to start with what is XML? Or examples of what an XML document is? I find this a waste of time, especially for a session marked as "intermediary" complexity. You would expect the audience to know these things.

Next is an introduction to different XML parsing methodologies. She introduces DOM now. She jumps to JAXP now, the Java API for accessing a DOM parser. She now presents a DOM parser example, which I found to be flawed because it uses getElementsByName(). The reason why this is bad, especially in the context of an input document conforming to a well known DTD, is because this method will traverse all the nodes in the document. She goes into explaining the cons of using DOM.

15:01 She now introduces SAX, explaining the model and giving a small example.

15:09 Finally, the meat of the talk, StAX. This new parsing technology uses a different event model. It has an XMLEvent abstract event, with StartDocument, StartElement etc. concrete events. The interface with the parse is very similar to JAXP. The parsing model is different from SAX: the application tells the parser to get the next event and the parser returns the concreate event objects.

The cursor style example

while (reader.hasNext()) {
  int eventType = reader.next();
  if (eventType == XMLEvent.START_ELEMENT && reader.getLocalName().equals("title")) {
    reader.next();
    println(reader.getText());
  }
}

The next example (iterator style) she presents asks the parser to return the actual event object, which may be easier to program but uses more memory because the parser has to create the event object (an actual XMLEvent instance).

The nice part about StAX is that you can process multiple XML documents at the same time, by simply sucking data from multiple parsers.

She now jumps into Web Services and JAX-RPC. This seems to be going over the scope of this talk, so I'll head out of the talk. StAX and XML pull-parsers seem to be a really good idea. I need to check them out.

Posted by ovidiu at 03:23 PM |

JavaOne - Design patterns for high-load Web sites

Java

I came in really late at this one, as I had to eat something and post the previous Weblog entries.

This session looks like a product presentation. It's a Windows RAD application for developing Web sites and Web services using Struts and presumable other J2EE frameworks. I didn't get the presenter's name or the company he works for.

Posted by ovidiu at 02:03 PM |

JavaOne - More programming puzzles

Java
Neal Gafter (left) and Joshua Bloch during their "Programming Puzzles" session.

This session promises to be a lot of fun. In the past years, I missed similar presentations considering them entry level, only to hear from colleagues that attended that the sessions were really good. The session is presented by Joshua Bloch and Neal Gafter of Sun Microsystems. After this session I need to get some food, I'm really hungry.

12:15pm It started. The first problem is "Random Behavior".

public class RandomSet {
  public static  void main(String[] args) {
    Set s = new HashSet();
    for (int i = 0; i < 100; i++)
      s.add(randomInteger());
    System.out.println(s.size());
  }

  private static Integer randomInteger() {
    return new Integer(new Random().nextInt());
  }
}

What is the size of the set printed at the end: 1, 50 or 100? Because the Random is initialized each time in randomInteger(), and that is always initialized with the current time of day in miliseconds, on fast machines the seed is pretty much the same. By sharing the Random object, you can actually get real random numbers.

Second problem is "Making a Hash of It".

public class Name {
  private String first, last;

  public Name(String first, String last) {
    if (first == null || last == null)
      throw new NullPointerException();
    this.first == first; this.last = last;
  }
  public boolean equals(Name o) {
    return first.equals(o.first) && last.equals(o.last);
  }

  public int hashCode() {
    return 31 * first.hashCode() + last.hashCode();
  {

  public static void main(String[] args)
    Set S = new HashSet();
    s.add(new Name("Mickey", "Mouse"));
    System.ouyt.println(s.contains(new Name("Mickey", "Mouse"));
  }
}

What does this program output, true or false? Because equals() takes as argument a Name and not an Object, the method is not actually overriding equals(Object), but overloads equals(). Thus the method is never called, so the program always returns false. The solution is to change the signature of equals() to take an Object as argument.

Next problem is "Ping Pong":

class PingPong {
  public static synchronized void main(String[] a) {
    Thread t = new Thread() {
      public void run() {
        pong();
      }
    };

    t.run();
    System.out.println("Ping");
  }

  static synchronized void pong() {
    System.out.println("Pong");
  }
}

What is the output of this program? PingPong, PongPing or it varies?

The answer is PongPing, because by the time pong() executes the class already holds the lock. Well, if the programmer actually wanted to start the thread, he should have written t.start() instead of t.run(). In that case the output would be PingPong. The issue is that Thread implements Runnable, which it shouldn't because it's difficult to catch these problems.

Next problem is "Reflection Infection":

public class Reflector {
  public static void main(String[] args) throws Exception {
    Set s = new HashSet();
    s.add("foo");
    Iterator i = s.iterator();
    Method m = i.getClass().getMethod("hasNext", new Class[0]);
    System.out.println(m.invoke(i, new Object[0]));
  }
}

Won't compile? true? Throws exception? None of the above?

Most people think it will throw an exception, which is correct: attempt to invoke a method on a private class. The problem is that i.getClass() is not an Iterator, which is an interface, but a private class of the JVM. The correct way to do it is rewrite it to Iterator.class.getMethod() instead.

Next problem is "String Cheese":

public class StringCheese {
  public static void main(String[] args) throws Exception {
    byte b[] = new byte[256];
    for (int i = 0; i < 256; i++)
      b[i] = (byte)i;
    String str = new String(b);
    for (int i = 0; i < str.length(); i++)
      System.out.print((int)str,charAt(i) + " ");
  }
}

The numbers from 0 to 255? The number from 0 to 127 then -128 to -1? It varies? None of the above?

The answer is it varies because the sequence depends on the default charset, which depends on OS and locale. The problem is String(byte[] bytes) which constructs a String according to the default charset and locale. To fix it you have to explicitly specify the charset new String(b, "ISO-8553-1")

Next problem is "All Strung Out":

public class Puzzle {
  public static void main(String[] args) {
    String s = new String("bla");
    System.out.println(s);
  }
}

class String {
  java.lang.String s;

  public String(java.lang.String s) {
    this.s = s;
  }

  public java.lang.String toString() {
    return s;
  }
}

Won't compile? blah? Throws an exception? Other?

It throws an exception at runtime. NoSuchMethodError is throwsn because Puzzling is missing a main method. The String argument of main() is the String class defined by us. To fix the problem you have to declare java.lang.String or rename the our String class to MyString. Moral: don't reuse names by hiding, shadowing or overloading.

Next problem is "Elvis Lives":

public class Elvis {
  public static final Elvis INSTANCE = new Elvis();
  private final int beltSize;

  private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);

  private Elvis() { beltSize = CURRENT_YEAR - 1930; }
  public static void main(String[] args) {
    System.out.println("Elvis wears size " + INSTANCE.beltSize() + " belt.");
  }
}

Elvis wears size 0 belt? Elvis wears size 73 belt? Elvis wears size -1930 belt? None of the above?

The answer is -1930 because of a recursive initialization in the initialization of INSTANCE. In the constructor, the value of CURRENT_YEAR is actually 0, because it was not yet initialized. To fix it, you need to move the initialization of INSTANCE after the initialization of CURRENT_YEAR. Moral: watch out for circular initialization.

Next problem is "What's the point":

class Point {
  protected final int x, y;
  private final String name; // Cached at construction time
  protected String makeName() { return "[" + x + ", " + y + "]"; }
  public final String toString() { return name; }
  Point(int x, int y) {
    this.x = x; this.y = y;
    this.name = makeName();
  }
}

public class ColorPoint extends Point {
  private final String color ;
  protected String makeName() { return super.makeName() + ":" + color; }
  ColorPoint(int x, int y, String color) {
    super(x, y);
    this.color = color;
  }

  main() {
    System.out.println(new ColorPoint(4, 2, "purple"));
  }
}

The output is [4, 2]: null because the subclass method executes before subclass constructor body. To fix it use a lazy initialization, by removing the this.name assignment in Point and introducing a synchronized String toString() in Point, that check the value of name ivar and assigns to it the first time. Moral: never call overridable methods from constructors.

Next is "Shifty":

public class Shifty {
  public static void main(String[] args) {
    int distance = 0;
    while ((-1 << distance) != 0)
      distance++;
    System.out.println(distance);
  }
}

31? 32? 33? None of the above?

None of the above: infinite loop because (-1 << 32) == -1. Moral shift distances are computed mod 32 or 64. It's impossible to shift out an entire int using any shift operator.

Next is "Line Printer":

public class LinePrinter {
  public static void main(String[] args) {
    char c = 0x000A; // \u000A is Unicode for newline
    System.out.println(c);
  }
}

Two blank lines? 10? Won't compile? It varies?

The character \u000A will be broken by the compiler in two, essentially making the comment line break at the Unicode word. Moral: Unicode escapes are dangerous, always use escape sequences.

Posted by ovidiu at 01:49 PM |

JavaOne - MacOS X Java session

Apple | Java
Marratech Pro collaboration for MacOS X

10:55am I'm now in the MacOS X Java Technology Overview session - The Ultimate Java Development Plan - presented by Alan Samuel, a "Java Technologies Evangelist" at Apple. Pretty good attendance, the room although small, is packed (more than 100 people I think).

11:02am It started. Most of the people in the room develop in Java on MacOS X, a lot more since few years ago, when the presenter asked the same question and got only 4-5 people doing Java development on MacOS X. Lots of talk about why use Apple hardware and software and develop Java on it. Tools: Apple's ProjectBuilder as well as third-party tools. Enterprise applications: Oracle 11i, SAP etc. Server strategy: WebObjects, Tomcat, 3rd party tools and J2EE servers running on Xserve. JDK 1.4.1 had over 1 million downloads to date.

In 1.3.1 there were 1842 Java classes and 900 custom Apple classes. In 1.4.1 there are 2991 Java classes and only 300 custom Apple classes. The AWT is re-implemented in Cocoa (rotated tabs, translucent windows). The JDK plug-in is now supported in Safari. MacOS X supports both JVM versions at the same time, with 1.3.1 being the default one. The JVM could be specified on a per-application basis.

JDK is optimized for MacOS X: Aqua look-and-feel, access to Quartz Extreme using just the standard Java APIs. The result is more responsive UIs, live re-sizing, smoother scrolling and snappy feedback. The Java Internet Plug-in has faster loading of applets, enhanced applet caching, integrated with Keychain and printing. Java WebStart updated to version 1.2, works with Keychain and adds automatic app creation. Keychain implemented using Secure Transport (?), certificates stored in Keychain. Good accessibility support, no need for Java Access Bridge: full screen reader support, full keyboard access and visual notification (hm, I wonder why the Tab key doesn't work to switch between most controls, including buttons). Support for handwriting support using Inkwell and for sound input. The new JDK version includes AppleScript support: scriptable UIs widgets. Shared system jars with system objects being shared between each VM (I guess very similar to shared libraries in the C world). This saves 3-6Mb per application with savings mainly in Swing and AWT-based apps - even more optimizations coming.

11:32am He's now giving a demo using Marratech Pro 3.4, an impressive Java collaborative tool that integrates video and a board for sharing documents. The software is built by a Swedish company. The presenter is Serge Lachapelle and he's talking from Sweden together with two other people, Magnus, Rolf and Rickard also online. The communication protocol is encrypted using 128bit keys. The application ran initially only on expensive Solaris boxes, but was easy to port to MacOS X on Java 1.4.1, including the native libraries written in C. (BTW, the sound quality is great, even though the video stream is pretty crapy, only 1-2fps).

11:42am Back to the presentation. Few tips on how to write Java apps for MacOS X, all of them documented on the ADC Web site. Don't use modifier keys directly, use Toolkit.getMenuShortcutKeyMask(). Not all look and feels are the same: use layout managers instead of explicitly placing the components. Query UIManager, toolkit for colors and icons.

Few words on Panther: we'll be "shocked" by the new operating system. It will be announced two weeks from now at WWDC at Moscone Center in San Francisco (the same location as JavaOne).

Posted by ovidiu at 01:47 PM |

JavaOne day

Java

Cold this morning in San Francisco, I almost regret I left home in just a T-shirt. I carpooled with Costin and the traffic was really good, no delays anywhere on the highway or getting into the city.

This year the number of attendees seems significantly smaller than that of the past years. The corridors are not as crowded as they used to be. The exhibit area has fewer exhibitors and smaller booths. Sun has the biggest booth; Borland, Motorola and few others have slightly larger booths that the rest of the companies. Lots of small companies occupy the rest of the smaller booths.

I arrived late for this morning's keynote. The part I caught was really boring, with a presentation of a RAD tool for Java. I can't believe people still do live coding during such shows. The developer using the tool had some minor problems with the code, making the presentation stale for a while. At this point I went out looking for wireless connectivity. I couldn't find any, but I did spot a place where I can hook up with Powerbook to a wired Ethernet.

Posted by ovidiu at 01:46 PM |
June 10, 2003

JavaOne

Java

Tomorrow I'll be at JavaOne, I'll probably be there for one day only, as I have a lot of work to do at office. I'll attend few sessions on Java in the Web Tier

  • design patterns for high-load Web sites (TS-3588)
  • StAX for efficient parsing of XML (TS-1195)
  • an always fun session on Java programming puzzles (TS-1496)
  • Per Bothner's session on Kawa (TS-2907)

Last year the wireless connectivity was pretty bad, with few base stations available and those were really overloaded by the number of attendees trying to read email, browse the Web, trade stocks, whatever. I hope this year is going to be better, otherwise I'll have to resort to using Starbucks' wireless connection (fortunately there are two shops right in the area).

If you're in the area and want to meet me, send me an email SMS at javaone@webweavertech.com, I will read email SMS at this address on my cell phone. Send me your cell phone number and I'll call you back.

Posted by ovidiu at 11:40 PM |
 
Cool stuff
  Arduino TinyWebServer: part 3 and part 2
Search
More from me
Picture gallery
Admin
Copyright © 2002-2016 Ovidiu Predescu.