Language Design – things not to do 2

Don’t make the behaviour of your strings depend on the system time! (Java String hashCodes I’m looking at you).

It seems like this is new in Java7, and is for security reasons (mostly to stop DOS attacks on hashmaps). So perhaps it could have been abstracted? Instead of putting the code into String.class, call a native method for the string hash seed, and let the JVM control the String hashCode policy? It is yet another source of non-detrminism in the core language.

Adding nodes to a MongoDB replica set using the Java driver

MongoDB console has a method for adding a replica to a replica set called rs.add. The Java driver doesn’t support this directly, but you can use the code from the mongo shell to implement add directly by running commands against the admin database.

rs.add = function (hostport, arb) {
    var cfg = hostport;
 
    var local = db.getSisterDB("local");
    assert(local.system.replset.count() <= 1, "error: local.system.replset has unexpected contents");
    var c = local.system.replset.findOne();
    assert(c, "no config object retrievable from local.system.replset");
 
    c.version++;
 
    var max = 0;
    for (var i in c.members)
        if (c.members[i]._id > max) max = c.members[i]._id;
    if (isString(hostport)) {
        cfg = { _id: max + 1, host: hostport };
        if (arb)
            cfg.arbiterOnly = true;
    }
    c.members.push(cfg);
    return this._runCmd({ replSetReconfig: c });
}

Testing using FEST Swing in Jenkins

You can use the same trick as with cruise control (which actually originated from the hudson website)

java -DJENKINS_HOME=C:\Jenkins -jar C:\Jenkins\jenkins.war
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon=1
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName=testuser
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword=secret

Convert URL to URI using lambdaj

import ch.lambdaj.function.convert.Converter;
 
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
 
public class ConvertUrlToUri implements Converter<URL, URI>
{
   public static ConvertUrlToUri convertUrlToUri()
   {
      return new ConvertUrlToUri();
   }
 
   @Override
   public URI convert(final URL from)
   {
      try
      {
         return from.toURI();
      }
      catch (final URISyntaxException e)
      {
         throw new RuntimeException("unable to convert " + from + " to URI", e);
      }
   }
}