RSS

Category Archives: java

Random Ideas for XINS and Logdoc

>Here’s a random list of possible enhancements to XINS.

First, a couple of changes that would make it much easier to configure an IDE (such as Eclipse or NetBeans) to work with XINS:

  • Move all Java source code to src/java/; currently it is spread out over src/java-common, src/java-client-framework and src/java-server-framework. Still, the JAR files can remain the same.
  • Put the generated Java source files also under src/java/, such as the Log and TranslationBundle classes.
  • Put all generated class files under build/classes/.
  • Make the Library classes detect the XINS version at runtime, instead of using a text replacement technique to modify the source code before compiling it.
Then some ideas on Logdoc:
  • Split out Logdoc from XINS. It’s not needed inside XINS, all it needs is a JAR file and some Ant tasks for generating some stuff (like the Java source files and the documentation).
  • Make it easy to plug in a different logging library. Currently, Logdoc generates Log4J code, but it should be fairly simple to make it generate code for other logging libraries. It doesn’t mean Logdoc should actually implement this, but it would at least facilitate it.
To be continued.
 
1 Comment

Posted by on 30 December 2009 in java, logdoc, xins, xins3

 

Command Line Input in Java

>

Today, someone introduced me to Spring Roo, which has quite some similarities with XINS at first glance. I really like the command line shell interface Roo has, featuring unbuffered text input, completion and fancy colors. I know it’s not even close to a rich IDE like NetBeans or Eclipse, but I love command line tools for their productivity and simpicity.

Looking at some documentation, the text-based shell interface is implemented using these technologies:

  • JLine – Library for handling console from Java, unbuffered.
  • JAnsi – Library for using ANSI escape sequences to format console output.
  • JNA – Windows-specific library for displaying colored output on Windows-systems.

Both libraries work on Windows, Mac OS X and Linux/UNIX-systems. Definitely worth considering in case you want to implement a console application in Java.

PS. Upon further inspection, Roo is very different from XINS, though. Roo is an aspect-oriented build-time code generator/manager that generates JSPs, Spring code, Java plumbing code (such as toString() methods), while XINS is an RPC-oriented component technology framework that also has (optional) runtime components (both client- and server-side, if you wish).

 
Leave a comment

Posted by on 28 December 2009 in jansi, java, jline, jna, roo, spring

 

Weird Error While Executing LessCSS

>This is really odd: I got the following output while running lessc on some .less files from an Apache Ant task:

-lesscss:
[003:221] MagicCam 0: Current process: java, Flash is loaded: no
[003:221] Error(magiccammac.cc:276): MagicCam 0: MagicCamOpen: Not an allowed process!
[000:002] MagicCam 0: Current process: java, Flash is loaded: no
[000:002] Error(magiccammac.cc:276): MagicCam 0: MagicCamOpen: Not an allowed process!
[000:001] MagicCam 1: Current process: java, Flash is loaded: no
[000:001] Error(magiccammac.cc:276): MagicCam 1: MagicCamOpen: Not an allowed process!
[000:002] MagicCam 1: Current process: java, Flash is loaded: no
[000:002] Error(magiccammac.cc:276): MagicCam 1: MagicCamOpen: Not an allowed process!
[lesscss] 0 file(s) transformed in 4 ms; 7 unmodified file(s) skipped.

Now I had it at a different point in the build process:

do-generate-headings:
Warning: Reference saxon.classpath has not been set at runtime, but was found during
build file parsing, attempting to resolve. Future versions of Ant may support
referencing ids defined in non-executed targets.
[xslt] Processing /Users/ernst/albizia/build/contentdb/pfizer/content/PreprocessedSite.xml to /Users/ernst/albizia/build/contentdb/pfizer/content/PageHeadings.ErigeronStrings.xml
[xslt] Loading stylesheet /Users/ernst/albizia/src/build-xslt/GeneratePageHeadings.xslt
[xmlvalidate2] 1 file(s) have been successfully validated.
[000:224] MagicCam 0: Current process: java, Flash is loaded: no
[000:224] Error(magiccammac.cc:276): MagicCam 0: MagicCamOpen: Not an allowed process!
[000:003] MagicCam 0: Current process: java, Flash is loaded: no
[000:003] Error(magiccammac.cc:276): MagicCam 0: MagicCamOpen: Not an allowed process!
[000:000] MagicCam 1: Current process: java, Flash is loaded: no
[000:001] Error(magiccammac.cc:276): MagicCam 1: MagicCamOpen: Not an allowed process!
[000:002] MagicCam 1: Current process: java, Flash is loaded: no
[000:002] Error(magiccammac.cc:276): MagicCam 1: MagicCamOpen: Not an allowed process!

There is a tiny bit of information here:

 
1 Comment

Posted by on 22 September 2009 in ant, flash, google talk, googletalk, java, lesscss, magiccam, magiccamopen

 

Java Language: Referencing Current Class From Static Context

>Currently, in Java SE 6, to reference the current class from a static context the name of the class is needed. For example:

public class Foo {
public static final valueOf(String s) {
if (s == null) {
return null;
} else if (s.startsWith(Foo.class.getName() + ':')) {
...
} else {
...
}
}
}

However, if this code would be copied to another class, or if the class is renamed, then that code will cause issues, possibly even causing hard-to-find bugs.

Instead it would be nice if the enclosing class could be referenced without using the name. A possible syntax that does not introduce new language keywords could just be:

class

The previous example would then become:

public class Foo {
public static final valueOf(String s) {
if (s == null) {
return null;
} else if (s.startsWith(class.getName() + ':')) {
...
} else {
...
}
}
}

Of course, this construct should also be allowed in non-static code.

One open question is how to deal with inner classes and referencing a certain outer class. I would not mind keeping that out of scope, so the feature only allows referencing the direct enclosing class.

Another issue is subclassing. Would the class statement reference the current class (in case of a non-static context) or the one the method is defined in?

 
Leave a comment

Posted by on 3 September 2009 in class, extension, java, language, this

 

Ant Task for Invoking LessCSS (Updated)

>Update (August 26, 2009): I’ve polished the Ant task for LessCSS files quite a bit and implemented support for both lessc and plessc (the LessPHP alternative).

Features:

  • converts .less files to .css
  • supports both lessc and plessc (at least one is required)
  • supports time-outs
  • only processes changed files
  • supports separate source/target directories, with include/exclude patterns

It’s available under de BSD-license for download from github: http://github.com/znerd/lesscss-ant-task/.

Notes:

  • for lessc (tested lessc v1.1.13) to work to fail properly when lesscs fails, a minor change to one of the lessc files is required, this may be resolved in an upcoming version of the task and/or an upcoming version of lessc
  • plessc v0.1.6 works well with the task, but has a rather annoying bug related to negative background positions.

Below is the original blog post.


Here’s some sample code for implementating an Apache Ant task for invoking lesscss:
import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.MatchingTask;

public final class LesscssTask extends MatchingTask {
public LesscssTask() {
setIncludes("*.less");
}

private File _sourceDir;
private File _destDir;

public void setDir(File dir) {
_sourceDir = dir;
}

public void setToDir(File dir) {
_destDir = dir;
}

@Override
protected void execute() throws BuildException {
for (String inFileName : getDirectoryScanner(_sourceDir).getIncludedFiles()) {
File inFile = new File(_sourceDir, inFileName);
String outFileName = inFile.getName().replaceFirst("\\.less$", ".css");
String outFilePath = new File(_destDir, outFileName).getPath();
String inFilePath = inFile.getPath();

log("lesscss: From \"" + inFilePath + "\" to \"" + outFilePath + "\".");

Execute.runCommand(this, new String[] { "lessc", inFilePath, outFilePath });
}
}
}

After defining the lesscss task in your build file (using a taskdef) invoking it is as simple as:

<lesscss dir="src/htdocs" todir="build/htdocs" />
 
Leave a comment

Posted by on 20 August 2009 in ant, apache ant, java, lesscss, task

 

Advanced Testing for Equality in Java Unit Tests

Testing the equals method
When testing Java classes, a common requirement is that the equals(Object) method works properly. When using JUnit, this can be as simple as:

import static org.junit.Assert.*;
:
assertEquals(a, b);

However, you may want to test that not only a considers itself equal to b but also vice versa. With a single assertEquals call this won’t work, so you add:

assertEquals(b, a);

Perfect, right?

Possibly. What if the test fails? How do you determine what the equals method actually considers a difference? The comparison algorithm can be quite complex, involving several instance fields, including collections, maps and XML snippets.

Differences for humans
It would help if the class would describe the differences in a human-readable manner. Here’s how: Declare an interface VerboseDiff (or whatever better name you can come up with) with just one method that describes the differences between this object and the specified argument. It returns null if there are no differences:

public interface VerboseDiff {
  String describeDifferences(Object obj);
}

Then change your implementation classes to implement this interface and implement the equals method as follows:

@Override
public boolean equals(Object obj) {
  return describeDifferences(obj) == null;
}

Your describeDifferences method could be implemented as follows:

public String describeDifferences(Object obj) {
  if (obj == null)) {
    return "obj == null";
  } else if (! (obj instanceof ThisClass)) {
    return "obj is not an instance of ThisClass, but an instance of class " + obj;
  }

  ThisClass that = (ThisClass) obj;

  if (that._baseValue != _baseValue) {
    return "baseValue of this object (" + _baseValue + ") is different from base value of argument (" + baseValue + ')';
  }

  return null;
}

Now your unit tests can look like this:

assertEquals(null, a.describeDifferences(b));
assertEquals(null, b.describeDifferences(a));

Further reading
A final tip: Implementing a good comparison algorithm that is in line with the contract of the equals method is typically not straight-forward, especially if you need to deal with subclasses. The article How to Write an Equality Method in Java (at artima.com) covers the topic excellently.

 
Leave a comment

Posted by on 1 July 2009 in diff, equals, java, junit, test, testing

 

>Mac OS X, fonts and headless Java

>If on Mac OS X your custom font is not available in headless Java, try installing it in /System/Library/Fonts/ instead of /Library/Fonts/.

Don’t ask me why, but this solved the issue I’d been struggling with for days. Below is the background.

Font installed
I purchased a font called “standard 07_55” from the miniml.com site and installed it via the font catalog program, which copied the .otf (OpenType) file to /Library/Fonts/:

  $ ls -lae /Library/Fonts/standard*otf
-rw-r--r--@ 1 ernst admin 11996 Oct 8 15:39 /Library/Fonts/standard 07_55.otf

It was accessible from the user account on the Mac, which is confirmed by running a text editor program.

Java program to list all available fonts
The following Java program lists all available fonts:

  public class ListFonts {
public static void main(String[] args) {
String[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(int i=0; i<fonts.length; i++) System.out.println(fonts[i]);
}
}

Running the program from the graphical environment
When I ran this program is from Aqua, it worked well and displayed all fonts, including the installed “standard 07_55” font:

  $ java -version
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05-237)
Java HotSpot(TM) Client VM (build 1.5.0_13-119, mixed mode, sharing)
$ javac ListFonts.java
$ java -cp . ListFonts | grep tand
KufiStandardGK
standard 07_55

Even when I added -Djava.awt.headless=true it still worked fine:

  $ java -Djava.awt.headless=true -cp . ListFonts | grep tand
KufiStandardGK
standard 07_55

Running the program via a remote connection
However, when that same program was run via a remote connection (SSH), then the program would not start up like this:

  $ java -cp . ListFonts | grep tand
Exception in thread "main" java.lang.InternalError: Can't connect to window server - not enough permissions.
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1822)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1723)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:993)
at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:50)
at java.security.AccessController.doPrivileged(Native Method)
at apple.awt.CGraphicsEnvironment.(CGraphicsEnvironment.java:23)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
at ListFonts.main(ListFonts.java:3)

This is understandable, since this is a remote connection, it cannot access the window server. So I started the Java program in headless AWT mode, and then it did run. However, now it does not show the font anymore:

  $ java -Djava.awt.headless=true -cp . ListFonts | grep tand
KufiStandardGK

Solving the issue
The fix? Simple: as root, move the font file to /System:

  # sudo mv /Library/Fonts/standard*otf /System/Library/Fonts/

I hope this helps someone else with a similar issue.

 
Leave a comment

Posted by on 10 October 2008 in aqua, font, fonts, headless, java, mac, macosx