package commons.cmd;
import basics.application.Binder;
import basics.unexpected.Problem;
import basics.utl.Empty;
import basics.utl.SUtl;
import commons.application.GLOBAL;
import commons.application.core.Command;
public class bind extends Command
{

private static final String ARGS = "ARGS: [<name>] <classname>, where <name> is a key, and <classname> is a fully qualified Classname. ";

@SuppressWarnings("unchecked")
public void process () throws Problem
{
   String args = ctx.consume(GLOBAL.CTX_ENVKEY_ARG0, "");
   if (Empty.is(args)) throw new Problem("Missing arg. " + description());
   String v[] = SUtl.split(args, ",", true);
   String className = v.length == 2 ? v[1] : v[0];
   Class c = null;
   try
   {
      c = Class.forName(className);
   }
   catch (Exception e)
   {
      throw new Problem(description() + "Can't find class: " + className, e);
   }
   if (c == null) throw new Problem(description() + "Can't bind class: " + className);
   if (v.length == 2) Binder.register(v[0], c);
   else Binder.register(c);
}

public void signContract ()
{}

public String description ()
{
   return implName() + " binds a class (for depencency injection), either to name or for selection by interface. " + ARGS;
}
}
