package commons.cmd;
import basics.unexpected.Failure;
import basics.unexpected.Problem;
import basics.utl.Empty;
import basics.utl.SUtl;
import commons.application.GLOBAL;
import commons.application.core.Command;
public class newbean 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, " ");
   if (v.length != 2) throw new Problem("Missing arg(s). " + description());
   String keyName = v[0];
   String className = v[1];
   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);
   Object object = null;
   try
   {
      object = c.newInstance();
   }
   catch (InstantiationException e)
   {
      e.printStackTrace();
      throw new Failure(e);
   }
   catch (IllegalAccessException e)
   {
      e.printStackTrace();
      throw new Failure(e);
   }
   ctx.put(keyName, object);
}

public void signContract ()
{}

public String description ()
{
   return implName() + " creates a new Object and stores it in the context. " + ARGS;
}
}
