Example usage for org.apache.commons.chain Context get

List of usage examples for org.apache.commons.chain Context get

Introduction

In this page you can find the example usage for org.apache.commons.chain Context get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:org.apache.jackrabbit.standalone.cli.CommandHelper.java

/**
 * Gets the current working <code>Repository</code>
 * @param ctx// ww w  . ja v  a 2 s . c  o  m
 *        the <code>Context</code>
 * @return the current working <code>Repository</code>
 * @throws CommandException
 *         if the current working <code>Repository</code> is unset.
 */
public static Repository getRepository(Context ctx) throws CommandException {
    Repository r = (Repository) ctx.get(REPOSITORY_KEY);
    if (r == null) {
        throw new CommandException("exception.no.current.repository");
    }
    return r;
}

From source file:org.apache.jackrabbit.standalone.cli.CommandHelper.java

public static String getRepositoryAddress(Context ctx) throws CommandException {
    String a = (String) ctx.get(REPO_ADDRESS_KEY);
    if (a == null) {
        throw new CommandException("exception.no.current.repository");
    }//w w  w . ja  v  a  2 s.  c  o m
    return a;
}

From source file:org.apache.jackrabbit.standalone.cli.CommandHelper.java

/**
 * Gets the current working <code>Session</code>
 * @param ctx/*from  w w  w.  j  av  a  2s . c  om*/
 *        the <code>Context</code>
 * @return the current working <code>Session</code>
 * @throws CommandException
 *         if the current working <code>Session</code> is unset.
 */
public static Session getSession(Context ctx) throws CommandException {
    Session s = (Session) ctx.get(SESSION_KEY);
    if (s == null) {
        throw new CommandException("exception.no.current.session");
    }
    return s;
}

From source file:org.apache.jackrabbit.standalone.cli.CommandHelper.java

/**
 * Checks <code>Node</code> existence.
 * @param ctx//www.j av a2s  .  c o  m
 *        the <code>Context</code>
 * @param path
 *        the path to the <code>Node</code>
 * @return true if the <code>Node</code> exists at the given path
 * @throws CommandException
 *         if the current working <code>Session</code> is unset.
 * @throws RepositoryException
 *         if the underlying repository throws a
 *         <code>RepositoryException</code>
 */
public static boolean hasNode(Context ctx, String path) throws CommandException, RepositoryException {
    Session s = getSession(ctx);
    if (path.equals("/")) {
        return true;
    } else if (path.startsWith("/")) {
        return s.getRootNode().hasNode(path.substring(1));
    } else {
        Node current = (Node) ctx.get(CURRENT_NODE_KEY);
        return current.hasNode(path);
    }
}

From source file:org.apache.jackrabbit.standalone.cli.core.AddNode.java

/**
 * {@inheritDoc}/*from   w ww .  ja  va2s .com*/
 */
public boolean execute(Context ctx) throws Exception {
    Node node = CommandHelper.getCurrentNode(ctx);
    String nodeType = (String) ctx.get(this.typeKey);
    String name = (String) ctx.get(this.relPathKey);

    if (log.isDebugEnabled()) {
        log.debug("adding node at " + node.getPath() + "/" + name);
    }

    // If the new node name starts with / add it to the root node
    if (name.startsWith("/")) {
        node = CommandHelper.getSession(ctx).getRootNode();
        name = name.substring(1);
    }

    if (nodeType == null) {
        node.addNode(name);
    } else {
        node.addNode(name, nodeType);
    }
    return false;
}

From source file:org.apache.jackrabbit.standalone.cli.core.Clone.java

/**
 * {@inheritDoc}/*from w  w  w  .  j  a va2 s. c o m*/
 */
public boolean execute(Context ctx) throws Exception {
    String srcWorkspace = (String) ctx.get(this.srcWorkspaceKey);
    String srcAbsPath = (String) ctx.get(this.srcAbsPathKey);
    String destAbsPath = (String) ctx.get(this.destAbsPathKey);
    Boolean removeExisting = Boolean.valueOf((String) ctx.get(this.removeExistingKey));

    Workspace w = CommandHelper.getSession(ctx).getWorkspace();

    if (log.isDebugEnabled()) {
        log.debug("cloning node. from [" + srcWorkspace + ":" + srcAbsPath + "] to [" + w.getName() + ":"
                + destAbsPath + "]");
    }

    w.clone(srcWorkspace, srcAbsPath, destAbsPath, removeExisting.booleanValue());

    return false;
}

From source file:org.apache.jackrabbit.standalone.cli.core.Copy.java

/**
 * {@inheritDoc}/*from  w ww . j  a  v a2  s. c o m*/
 */
public boolean execute(Context ctx) throws Exception {
    String srcWorkspace = (String) ctx.get(this.srcWorkspaceKey);
    String srcAbsPath = (String) ctx.get(this.srcAbsPathKey);
    String destAbsPath = (String) ctx.get(this.destAbsPathKey);

    Workspace w = CommandHelper.getSession(ctx).getWorkspace();

    if (srcWorkspace == null) {
        srcWorkspace = w.getName();
    }

    if (log.isDebugEnabled()) {
        log.debug("copying node from [" + srcWorkspace + ":" + srcAbsPath + "] to [" + w.getName() + ":"
                + destAbsPath + "]");
    }

    if (destAbsPath.endsWith("/")) {
        Item source = CommandHelper.getSession(ctx).getItem(srcAbsPath);
        destAbsPath = destAbsPath + source.getName();
    }

    w.copy(srcWorkspace, srcAbsPath, destAbsPath);

    return false;
}

From source file:org.apache.jackrabbit.standalone.cli.core.CurrentNode.java

/**
 * {@inheritDoc}//from  ww  w.ja va 2s .  co  m
 */
public boolean execute(Context ctx) throws Exception {
    String dest = (String) ctx.get(this.pathKey);
    Node n = CommandHelper.getNode(ctx, dest);
    if (log.isDebugEnabled()) {
        log.debug("set current working node to " + n.getPath());
    }
    CommandHelper.setCurrentNode(ctx, n);
    return false;
}

From source file:org.apache.jackrabbit.standalone.cli.core.Login.java

/**
 * {@inheritDoc}/*from www .j  a v  a  2 s.c  o  m*/
 */
public boolean execute(Context ctx) throws Exception {
    String anon = "anonymous";

    String user = (String) ctx.get(this.userKey);
    String password = (String) ctx.get(this.passwordKey);
    String workspace = (String) ctx.get(this.workspaceKey);

    if (user == null) {
        user = anon;
    }

    if (password == null || (password.equals(anon) && !user.equals(anon))) {
        ConsoleReader reader = new ConsoleReader();
        password = reader.readLine("Password: ", (char) 0);
    }

    if (log.isDebugEnabled()) {
        log.debug("logging in as " + user);
    }

    Session session = null;
    Repository repo = CommandHelper.getRepository(ctx);

    Credentials credentials = new SimpleCredentials(user, password.toCharArray());

    if (workspace == null) {
        session = repo.login(credentials);
    } else {
        session = repo.login(credentials, workspace);
    }
    CommandHelper.setSession(ctx, session);
    CommandHelper.setCurrentNode(ctx, session.getRootNode());
    return false;
}

From source file:org.apache.jackrabbit.standalone.cli.core.Move.java

/**
 * {@inheritDoc}/*w  ww. jav a2  s.c om*/
 */
public boolean execute(Context ctx) throws Exception {
    String srcAbsPath = (String) ctx.get(this.srcAbsPathKey);
    String destAbsPath = (String) ctx.get(this.destAbsPathKey);

    boolean persistent = Boolean.valueOf((String) ctx.get(this.persistentKey)).booleanValue();

    if (!srcAbsPath.startsWith("/") || !destAbsPath.startsWith("/")) {
        throw new IllegalArgumentException(bundle.getString("exception.illegalargument") + ". "
                + bundle.getString("exception.only.absolute.path") + ".");
    }

    Session s = CommandHelper.getSession(ctx);

    if (log.isDebugEnabled()) {
        log.debug("moving node from " + srcAbsPath + " to " + destAbsPath);
    }

    if (destAbsPath.endsWith("/")) {
        Item source = s.getItem(srcAbsPath);
        destAbsPath = destAbsPath + source.getName();
    }

    if (persistent) {
        s.getWorkspace().move(srcAbsPath, destAbsPath);
    } else {
        s.move(srcAbsPath, destAbsPath);
    }

    return false;
}