Context management

As you saw Nomin's expressions, hooks and conversions have access to the configured context. What is a context in terms of Nomin? Just a service supplying named resources, shared components/services across the mappings. And any of Groovy closures defined in Nomin's mappings has access to the context service.

If you plan to use Nomin in a concurrent environment notice that all the components used in Nomin's mappings should be thread-safe or externally synchronized, Nomin doesn't manage components' concurrency by itself.

At the moment there are several available context adapters such as MapContext, SpringContext and JndiContext. The first one just adapts a Map instance to be a context. They can be found at the org.nomin.context package. It's quite easy to implement an adapter to the required container.

Well, let's look at the examples.

  // Configuration.java
  class Configuration {
    public Configuration() {
      Map<String, Object> ctx = new HashMap();
      ctx.put("component1", component1);
      ctx.put("component2", component2);
      // and so on
      nomin = new Nomin(new MapContext(ctx), "theSnipet.groovy");
    }
  }

The next mapping snippet shows how to use the components.

  a.calculatedPropertyA = { component1.calculatePropertyA(b) }
  b.calculatedPropertyB = { component2.calculatePropertyB(a) }

So, resources/components identified by its keys in the context are used like as they are defined locally.

Now I'll introduce the features that are available for using only in Groovy code.

  // Configuration.groovy
  class Configuration {
    def ctx = [component1: component1, component2: component2,
               calcA: { b -> component1.calculatePropertyA(b) },
               calcB: { a-> component2.calculatePropertyA(a) }]

    def nomin = new Nomin(new MapContext(ctx), "theSnipet.groovy")
  }

The next mapping snippet shows how to use components and closures defined in the context.

  a.calculatedPropertyA = { component1.calculatePropertyA(b) }
  b.calculatedPropertyB = { component2.calculatePropertyA(a) }
  // for using named closures just call these like methods
  a.calculatedPropertyA = { calcA(b) }
  b.calculatedPropertyB = { calcB(a) }