/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.boot;

import foam.core.*;
import foam.nanos.*;
import foam.nanos.pm.PM;
import foam.nanos.logger.Logger;

public class NSpecFactory
  implements XFactory
{
  NSpec   spec_;
  ProxyX  x_;
  boolean isCreating_ = false;

  public NSpecFactory(ProxyX x, NSpec spec) {
    x_    = x;
    spec_ = spec;
  }

  public Object create(X x) {
    // Avoid infinite recursions when creating services
    if ( isCreating_ ) {
      return null;
    }

    isCreating_ = true;

    Object ns     = null;
    PM     pm     = new PM(this.getClass(), spec_ == null ? "-" : spec_.getName());
    Logger logger = (Logger) x.get("logger");

    try {
      if ( logger != null ) logger.info("Creating Service", spec_.getName());
      ns = spec_.createService(x_.getX());
      if ( logger != null ) logger.info("Created Service", spec_.getName());

      if ( ns instanceof ContextAware ) ((ContextAware) ns).setX(x_.getX());
      if ( ns instanceof NanoService  ) ((NanoService)  ns).start();
    } catch (Throwable t) {
      if ( logger != null ) logger.error("Error Creating Service", spec_.getName(), t);
    } finally {
      pm.log(x_.getX());
      isCreating_ = false;
    }

    return ns;
  }
}

@@ -206,13 +206,21 @@
 nos.
-pm.PM
+logger.Logger
 ;%0Aim
@@ -235,29 +235,21 @@
 m.nanos.
-logger.Logger
+pm.PM
 ;%0A%0Apubli
@@ -547,24 +547,225 @@
 eating_ ) %7B%0A
+      if ( ! %22logger%22.equals(spec_.getName()) ) %7B%0A        Logger logger = (Logger) x.get(%22logger%22);%0A        if ( logger != null ) logger.warning(%22Recursive Service Factory%22, spec_.getName());%0A      %7D%0A%0A
       return

Add recursive service factory logging.