Sunday 30 November 2008

Java New IO: ByteBuffers

java.nio.ByteBuffer. Provides a range of operations:

get, put - read and write single byte, wrap - wraps a byte array into a buffer.



ByteBuffer is derived from the abstract class Buffer (aka java.nio.Buffer). "A buffer is a linear, finite sequence of elements of a specific primitive type". Properties include: capacity (number of items it contains, never negative and never changes), limit (index of first element that should not be read or written), position (index of next item to be read/written).

A byte buffer can be direct or non direct. Given a direct byte buffer, the JVM will do its best to do native I/O operations on it. This will avoid copying buffer's content to an intermediate buffer before invoking the O/S underlying I/O operations. There is a factory method allocateDirect(int capacity) that allocates a new direct byte buffer. If the capacity is negative we throw IllegalArgumentException.

Messaging Concepts (JMS etc)

JMS.

JMS is a layer on top an underlying messaging engine. JBoss, for example, has a messaging engine that implements the JMS specification, allowing transparent access to the messaging system.

Tibco.

Tibrv class has static open and close methods to create and destroy a Tibco "environment". getVersion returns the current version of Tibco for Java as a String. defaultQueue accesses the default queue (every process has exactly one default queue which you must not destroy).

Your RV client application can connect to a network service via a TibrvRvdTransport object that connects to an rvd (RV daemon process) or a TibrvRvaTransport which connects to an rva (RV agent process) that in turn connects to an RVD. The critical question -How does an RVD work? Basically, a program sends a message and RVD transmits it across the network. It sends messages with matching subject names to the appropriate listeners. (RVAs are used as intermediaries between RVDs and applications - precluding the need for Tibco (JNI) shared libraries to be installed on the client machine). They are used as a communication mechanism for Java applets to talk to Tibco machinery. RVAs add an extra level of indirection and are thus not as efficient as talking directly to a RVD. The RVD is basically a software router.

(note: If a transport can't connect to an rvd transport process it will automatically start one.)

Property Management in Java (-D option)

System.getProperty is a static method on the System class that takes a (String) key and returns a (String) value. Standard system properties include java.home (where Java is installed) and java.class.path (the Java classpath) and os.name and os.version.

System properties are set on startup using the -D<key>=<value> option. These properties may need to be set in a launch script to a Java program.

Properties can also be maintained in an external file. Then you call System.getProperties to get a reference to the Properties object (that inherits from Hashtable). The Hashtable methods put and putall can be called on the object but their use is strongly discouraged in favour of the method setProperty which allows you to set String keys and values (i.e. it enforces a type-constraint on the keys of the Properties object).

Properties can be maintained in an external file and then setProperty can be used to add them to the application's properties table. Some people like to write singleton managers for properties so access can be centralised and controlled (and type-constraints enforced) for System properties.

The Idiosyncracies of the Java VM Shutdown Sequence

JVM shutdown is in two parts.

  1. All registered shutdown hooks
  2. All uninvoked finalizers are run

Runtime.getRuntime().addShutdownHook(Thread) registers a new VM shutdown hook.

A shutdown hook is simply an "initialized but unstarted thread". When VM shuts down, it starts all shutdown hooks and runs them concurrently. Then it will run all uninvoked finalizers(assuming finalization-on-exit has been enabled).

Once shutdown has been initiated it can only be stopped by invoking the halt method, to forcibly terminate the JVM. halt has one argument which is status code. Any non-zero value indicates abnormal termination.

Read more in the Runtime (singleton) class documentation below:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html

Wednesday 26 November 2008

Java and Eclipse Shortcuts

Eclipse

Cntrl-Shift-T find Type

Cntrl-L Goto line
Cntrl-B Build

F3 jump to definition

Alt-Enter - File properties (file path, size, file encoding)

Set up different workspaces for "test projects" versus "real projects".

Configuring Eclipse to reference Existing Code

1. Set up a new workspace (different from the source location)
2. use the existing location for the code
3. create projects for specific parts of the code you want to change

Select "autoconfigure JRE and project layout based on source".

Switching Tabs in Eclipse

Control-Tab in Windows applications is replaced by ALT-LEFT and ALT-RIGHT, and Control-F6. Since control-tab is used so frequently it is easy to forget this Eclipse peculiarity.

To override this shortcut, do Windows->Preferences->General->Keys. Then replace "Change Next Editor" from Control-F6 to Control-tab. Sanity at last.

Cntrl-E is another useful way to bring up "Editor List" that allows explicit window switching.

Java

javap -c Disassemble a class file

Friday 21 November 2008

Power of JFlex and CUP in Combination: LALR PArsers for Java

As a Java open source junkie you will have almost certainly have heard of JFlex and CUP but not really delved deeply into it, right? Here's a rapid sommaire of the two technologies.
  • JFlex (a rewrite of JLex) is known as the "fast scanner generator for Java".
  • CUP (no, not Cambridge University Press, but "Construction of Useful Parsers") is an LALR parser generator for Java. Rusty on ye olde Dragon Book basics? To remind you, LALR is lookahead LR parsing. LR parsing means the parser reads the input Left to right, and produces a Right-most derivation (LR is a general form of shift-reduce parsing, starting from the leaves of a tree and working up to the root - in computer science trees grow downwards from the root!). When referring to LALR(k) the k is the lookahead value. LALR is quite popular since it results in smaller parsing tables than canonical LR tables (Dragon Book Ch4, Syntax Analysis).
CUP user's manual states it is very similar to YACC, only it is written in Java, uses specifications with embedded Java and produces Java-implemented parsers. Think of CUP as YACC with a Java jacket and Java insoles. YACC was created by Stephen Johnson of AT&T, an article by the inventor can be found on the dinosaur.compilertools.net webpage. Parser generators like YACC are described in 4.9 of Dragon Book. Worth reading just as background to using CUP. YACC stands for "Yet Another Compiler Compiler" reflecting the parser generator mania of the 1970s.

Classgen is a related technology from the Technical University of Munich. It generates classes in the Visitor style. This page gives a good idea of what specifications look like in Classgen and what it can do. The whole area of creating computer programs based on computer specifications is extremely fascinating. Let the computers do all the hard programming and let humans customize the color schemes of the resulting software.

Speed Reading Java Open Source Code

Requires more than a passing knowledge of Java knowledge, APIs and standard open source components (par exemple, gnu.getopt).

Monday 17 November 2008

Navigating Ganymede using Awesome Child-Oriented Fonts like Comic Sans - Makes Reading Java More Fun!

Making your way around Eclipse ain't easy when you're used to MSVC-style GUIs, so here's a primer on how to get things configured double-speedy in the old Ganymede release of Eclipse.

Change Font Size. This should not be difficult but for first-timers it's a super-sized sudoku puzzle.

Windows->Preferences->General->Appearance->Colors and Fonts. Click on the right category e.g. Java editor text font. Set a good font and size. Changing your font regularly keeps code-reading interesting.
  • Jokeman is a fun font to liven up boring code.
  • Kartika9 is very clear and well-spaced.
  • Vivaldi is beautiful and elegant but suffers from lack of readability.
  • Mistral makes Java look beautiful.
  • ComicSansMS is a nice, friendly readable font, classified as "casual, non-connecting script", a "child-oriented font" in the words of its inventor, Vincent Connare, who designed it for children's software. To quote an esteemed computer philosophe, "The real art of discovery consists not in finding new programming languages, but in seeing with new fonts".
  • Gill Sans MT is another "soft font" described as a "humanist" font, created by Eric Gill in 1927 and very popular in logos for media companies (e.g. BBC). Eric Gill received the accolade "Royal Designer for Industry" (Gill9 looks fine).

Show Line Numbers: W->P->G->Editors->Text Editors->Show line numbers