Sunday, 26 June 2005

Compiling for older JDKs

Most developers seem to assume that they can simply set the javac setting to output 1.2 or 1.3 compatible bytecodes, and they can then compile using JDK1.4 or 1.5. But, this doesn't work.

The problem is that the Java class libraries have changed between JDK1,3 and 1.4 (and so on). Each change provides the potential to break the build.

For example, in JDK1.4 a new method was added to StringBuffer - append(StringBuffer). In itself, this was not newsworthy. However, this method overloads (not overrides!) other methods, notably append(Object).

If you compile using JDK1.4, and you are appending a StringBuffer to a StringBUffer, javac will choose the nice JDK1.4 append(StringBuffer) method. When you use this JAR on JDK1.3 however, you will get a NoSuchMethodError (not Exception!). This is because overloaded methods are bound into the bytecode at compile time.

Oh, and that 'use 1.3 bytecodes' flag in javac? Well that makes no difference. It just avoids writing bad bytecodes, and doesn't check for valid methods.

I'm writing this because it has come up twice recently on commons-dev. Firstly, during the release of Jelly 1.0, where it took quite some effort to convince people of the problem. Secondly, during ongoing work on Email 1.0, where exactly this issue occurred.

The only safe solution is to:

  • compile the JAR file using the lowest JDK supported by your open source library
  • make sure that is the JAR file released to ibiblio's maven repository
  • include that jar file in the source distribution as well as the binary
  • make sure that the manifest file correctly references the lowest JDK version
And to achieve this I use ant and not maven.

Its not a maven issue per se, but maven does require a higher JDK to run itself than most of the libraries I release. Now, I am told that maven can build using a different JDK version to that which it runs on (maven.compile.executable). Well, great if that works. But, I'm not sure that I trust it to actually do all the necessary steps.

Hence my appoach - all compilation steps are done with ant. All website steps are done with maven. Just using the right tool for the right problem in my book.

Friday, 4 March 2005

So where next with Joda-Time? A JSR?

Releasing a v1.0 can be a strange experience. Once its done, the stress can just disappear, and you take a bit of a rest (assuming there was no great heap of new bugs discovered, which there wasn't).

And its a bit like that with Joda-Time. v1.0 is done and out in the wild. There was quite a bit of positive feedback, but I've no idea how popular it really was because Sourceforge statistics are up the spout.

The main question then becomes well what next? v1.1 would be the obvious answer, but I need to gather some energy to add more code. And until and unless I get feedback on Joda-Time in use its difficult to know what people are missing.

The other possible direction is a JSR. Various people have suggested this, but I am a little skeptical. JDOM, Jade (scientific units) and Groovy haven't exactly been good indicators of what JSRs do to open source projects.

So, what do people reading this think. Are you not using Joda-Time because its not in the JDK? Is a JSR a good direction to go? Have you got your own ideas for what java.time should look like?

Tuesday, 22 February 2005

Joda-Time 1.0 released

Yes, its finally here! Joda-Time, the replacement for JDK Date and Calendar (and much more) has reached 1.0!

Full details can be found on the website. However, let me draw out a few key features:

- Easy to Use - Calendar makes accessing 'normal' dates difficult, due to the lack of simple methods. Joda-Time has straightforward field accessors. And the index of January is 1!

- Extensible - Joda-Time supports multiple calendar systems via a plugin mechanism, which is much more extensible than the subclasses of Calendar. Currently supported calendars are ISO8601, GregorianJulian, Buddhist (Thai) and Coptic.

- Comprehensive - Joda-Time includes classes for datetimes, dates without times, times without dates, intervals and time periods.

- Advanced formatting - An advanced and extensible formatting mechanism is included, allowing input and output to string in a thread-safe manner.

- Well documented. There is documentation in the form of a quick and full user guide, plus reference pages, FAQs and javadoc.

- Tested. There is good test coverage, see the website, providing an assurance of quality.

- Built in security. Advanced operations that could prove a security risk (like changing the current time) are protected by standard JDK security.

And if you learn best by an example:

public boolean isRentalOverdue(DateTime datetimeRented) {
  Period rentalPeriod = Period.days(2);
  return datetimeRented.plus(rentalPeriod).isBeforeNow()
}
public boolean isJoinedInLastThreeMonths(DateTime datetimeJoined) {
  Interval last3Months = new Interval(Period.months(3), new DateTime());
  return last3Months.contains(datetimeJoined);
}
public boolean isBirthdayInLeapYear(YearMonthDay dateOfBirth) {
  return dateOfBirth.year().isLeap();
}

So, now its released, what next? Well I hope that some of you reading this will want to help add some other calendar systems, such as Hebrew, Islamic, Chinese, etc. Any takers?

Wednesday, 16 February 2005

Joda-Time 0.99 done, 1.0 on its way...

Yes, after lots of late nights, version 0.99 of Joda-Time (the replacement for JDK Date and Calendar, and much much more) is out in the wild.

The plan is to get a few days feedback on 0.99, remove the deprecated methods, then release as 1.0 next week (and definitely before the end of February).

By the way, if you used version 0.98, you'll need to read the release notes, as there were a few last minute changes between 0.98 and 0.99. But I'm determined to let nothing stop 1.0 ;-)

Oh, and if you want to help out by developing another calendar system, Hebrew, Islamic, Chinese, Thai, Japanese,... just drop me a line !

Saturday, 4 December 2004

Javapolis BOF - any tips?

I'm off to Javapolis in Antwerp so I'm hoping to meet other OSSers there. Seeing as Joda-Time is still an obsure project, I thought I'd host a BOF session to stir up some interest. So, does anyone have any tips on the BOF format? Or questions they want answering?

Saturday, 13 November 2004

Open source loose ends

It seems that I have a lot of OSS loose ends to tie up. Or alternatively, its scary how many things are needing a release.

Tonight I played with Joda-Primitives. This was my own breakoff from Commons-Primitives when I realised that I preferred a different design to commons. (Joda's primitive collections subclass JDK Collections, Commons' classes don't.)

Also waiting to be sorted are Commons Collections 3.2, IO 1.1 and Lang 2.1. And not forgetting the big Joda-Time 1.0. Somehow I need to get all of these done and dusted, while still keeping the day job. Sometimes I marvel at the amount of effort put in by other OSS developers, but maybe they're lucky enough to be able to work on OSS during work hours?

Tuesday, 9 November 2004

Testing a security permission

Writing a JUnit test for a security permission object. That's got to be easy, right? Well it took me a while to figure out a solution so I thought I'd share.

Joda-Time has a security permission class, JodaTimePermission, that controls whether you have the rights to do things such as alter the current time. But writing a test was a pain, and what I have doesn't work on JDK1.3 or earlier.

The problem is to be able to change the security policy without manually stopping and starting the JVM and changing the policy file.

My solution was to create two classes that represented 'yes' and 'no':

  private static final Policy RESTRICT;
  private static final Policy ALLOW;
  static {
    // don't call Policy.getPolicy()
    RESTRICT = new Policy() {
      public PermissionCollection getPermissions(CodeSource codesource) {
        Permissions p = new Permissions();
        p.add(new AllPermission());  // enable everything
        return p;
      }
      public void refresh() {
      }
      public boolean implies(ProtectionDomain domain, Permission permission) {
        if (permission instanceof JodaTimePermission) {
           return false;
        }
        return true;
      }
    };
    ALLOW = new Policy() {
      public PermissionCollection getPermissions(CodeSource codesource) {
        Permissions p = new Permissions();
        p.add(new AllPermission());  // enable everything
        return p;
      }
      public void refresh() {
      }
    };
  }

These could then be used in the actual test method:

  public void testChangeCurrentTime() {
    try {
      Policy.setPolicy(RESTRICT);
      System.setSecurityManager(new SecurityManager());
      DateTimeUtils.setsetCurrentMillisFixed(0L); // disallowed by security
      fail();
    } catch (SecurityException ex) {
      // ok
    } finally {
      System.setSecurityManager(null);
      Policy.setPolicy(ALLOW);
    }
  }

I have to say that it all feels like a hack to me. So, has anyone else come up with a better solution for testing security permissions?