Package org.jboss.fresh.shell.commands.cms

Source Code of org.jboss.fresh.shell.commands.cms.CtxDelegateExe

package org.jboss.fresh.shell.commands.cms;

import org.jboss.fresh.ctx.Context;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.Shell;
import org.jboss.fresh.registry.RegistryContext;

import java.io.PrintWriter;

// EX OK

/**
* @author unknown
* @modified $Author: strukelj $
* @version $Revision: 2780 $
*/
public class CtxDelegateExe extends AbstractExecutable {

  protected static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(CtxDelegateExe.class);
  public static final String HELP = "--help";

  /**
   Retrieves a Context object from the InitialContext by the name specified with a parameter and registeres it with the shell context as a delegate.
   */


  public void process(String exepath, String[] params) throws Exception {
    log.debug("CtxDelegateExe entered.");

    if ((params.length == 0) || helpRequested()) {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      out.print("Usage: ctxdelegate [--help] [-ex] CONTEXT_NAMES\n");
      out.print("    --help : this help\n");
      out.close();
      log.debug("CtxDelegateExe done.");
      return;
    }

    Shell shell = getShell();

    PrintWriter err = new PrintWriter(new BufferWriter(getStdOut()));

    for (int i = 0; i < params.length; i++) {

      String tmp = params[i];

      // take parameter 1
      // look up the name through InitialContext
      RegistryContext ctx = new RegistryContext();
      Object obj = null;
      try {
        obj = (Context) ctx.lookup(tmp);
      } catch (Exception ex) {
        if (canThrowEx()) {
          throw new Exception("No object found in JNDI for name: " + tmp);
        } else {
          err.println("No object found in JNDI for name: " + tmp);
          log.debug("CtxDelegateExe done.");
          return;
        }
      }

      // See if what you got is instance of Context

      if (!(obj instanceof Context)) {
        if (canThrowEx()) {
          throw new Exception("Object found in JNDI for name: " + tmp + " is not org.jboss.fresh.ctx.Context type.");
        } else {
          err.println("Object found in JNDI for name: " + tmp + " is not org.jboss.fresh.ctx.Context type.");
          log.debug("CtxDelegateExe done.");
          return;
        }
      }

      Context ctx0 = (Context) obj;
      // now shell.getContext()
      if(log.isDebugEnabled()) {
        log.debug("shell: " + shell + ", ctx: "  + shell.getContext());
      }
      shell.getContext().registerDelegate(ctx0);
      shell.getContext().put("AppContext", ctx0);

      Context gctx = null;
      try {
        tmp = "java:/FRESH/GlobalContext";
        gctx = (Context) ctx.lookup(tmp);
      } catch (Exception ex) {
        if (canThrowEx()) {
          throw new Exception("No object found in JNDI for name: " + tmp);
        } else {
          err.println("No object found in JNDI for name: " + tmp);
          log.debug("CtxDelegateExe done.");
          return;
        }
      }

      shell.getContext().put("GlobalContext", gctx);

      // on context call registerDelegate()
      // done

    }

    log.debug("CtxDelegateExe done.");
  }
}
TOP

Related Classes of org.jboss.fresh.shell.commands.cms.CtxDelegateExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.