Good Introductory Documents on Structural Operational Semantics

Some useful documents that give an introduction to structural (small-step) operational semantics:

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 });
}

Use new version of Jackson JSON with Jersey

Guice Module:

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.google.inject.Singleton;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
 
public final class JerseyModule extends JerseyServletModule
{
   @Override
   protected void configureServlets()
   {
      bind(JacksonJaxbJsonProvider.class).in(Singleton.class);
      filter("/*").through(GuiceContainer.class);
   }
}

And the Maven pom:

<dependency>
   <groupId>com.fasterxml.jackson.jaxrs</groupId>
   <artifactId>jackson-jaxrs-json-provider</artifactId>
   <version>2.0.5</version>
</dependency>

Jersey + Guice ExceptionMapper

import com.google.inject.Singleton;
 
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
 
@Provider
@Singleton
public class MyExceptionMapper implements ExceptionMapper<MyException>
{
   @Override
   public Response toResponse(final MyException exception)
   {
      return Response.status(Status.NOT_FOUND).entity(exception.getMessage()).build();
   }
}
import com.google.inject.AbstractModule;
 
public class MyModule extends AbstractModule
{
   @Override
   protected void configure()
   {
      bind(MyExceptionMapper.class);
   }
}

Implement toString method on DOMSource when marshalling with JAXB

When serialising with JAXB to a DOMSource the object that results does not have a useful implementation of toString and alot of boiler plate is required to set up the marshalling. Here is some code that returns a DOMSource from an arbitrary JAXB annotated object with the toString method of the object return the XML representation:

public class SerialisationHelper
{
   private final String marshalledBean;
   private final Document document;
 
   private SerialisationHelper(final Object bean) throws JAXBException, ParserConfigurationException
   {
      document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
 
      final Marshaller marshaller = JAXBContext.newInstance(bean.getClass()).createMarshaller();
      marshaller.marshal(bean, document);
      final StringWriter writer = new StringWriter();
      marshaller.marshal(bean, writer);
      marshalledBean = writer.toString();
   }
 
   @Override
   public String toString()
   {
      return marshalledBean;
   }
 
   private Document toDocument()
   {
      return document;
   }
 
   public static DOMSource marshalled(final Object entity)
   {
      try
      {
         final SerialisationHelper serialisationHelper = new SerialisationHelper(entity);
         return new DOMSource(serialisationHelper.toDocument()) {
            @Override
            public String toString()
            {
               return serialisationHelper.toString();
            }};
      }
      catch (final JAXBException e)
      {
         throw new RuntimeException("unable to marshal " + entity, e);
      }
      catch (final ParserConfigurationException e)
      {
         throw new RuntimeException("unable to marshal " + entity, e);
      }
   }
}

Refactoring Ideas: Replace static method with same named static method

Want to produce the same kind of import in the affected classes (static, normal or fully qualified):

public static String repeat(final int count, final char character)
{
   return org.apache.commons.lang3.StringUtils.repeat(character, count);
}

Refactoring Ideas: inline method with flag parameter

Check all calls to see if the value of the flag can be analysed, if so inline everywhere (otherwise tell the developer where it is ambiguous):

   public static String pad(final String string, final int length, final char character, final boolean before)
   {
      if(before)
      {
         return leftPad(string, length, character);
      }
      else
      {
         return rightPad(string, length, character);
      }
   }