org.apache.jackrabbit.standalone.cli.CommandHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackrabbit-standalone-components Show documentation
Show all versions of jackrabbit-standalone-components Show documentation
Jackrabbit components for Jackrabbit Standalone
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.standalone.cli;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.ResourceBundle;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.chain.Context;
import org.apache.commons.collections4.IteratorUtils;
/**
* Utility class for getting and setting context attributes.
*/
public final class CommandHelper {
/** bundle */
private static ResourceBundle bundle = ResourceBundle
.getBundle(CommandHelper.class.getPackage().getName() + ".resources");
/** Current node key */
public static final String CURRENT_NODE_KEY = "jcr.current";
/** repository key */
public static final String REPOSITORY_KEY = "jcr.repository";
/** session key */
public static final String SESSION_KEY = "jcr.session";
/** output key */
public static final String OUTPUT_KEY = "jcr.output";
/** address key */
public static final String REPO_ADDRESS_KEY = "jcr.repo.address";
/**
* should never get called
*/
private CommandHelper() {
super();
}
/**
* Sets the current PrintWriter
.
* @param ctx
* the Context
* @param out
* the PrintWriter
*/
public static void setOutput(Context ctx, PrintWriter out) {
if (out == null) {
ctx.remove(OUTPUT_KEY);
} else {
ctx.put(OUTPUT_KEY, out);
}
}
/**
* Sets the current working Node
.
* @param ctx
* the Context
* @param node
* the current working Node
.
*/
public static void setCurrentNode(Context ctx, Node node) {
if (node == null) {
ctx.remove(CURRENT_NODE_KEY);
} else {
ctx.put(CURRENT_NODE_KEY, node);
}
}
/**
* Sets the current working Repository
* @param ctx
* the Context
* @param repository
* the current working Repository
*/
public static void setRepository(Context ctx, Repository repository, String address) {
if (repository == null) {
ctx.remove(REPOSITORY_KEY);
ctx.remove(REPO_ADDRESS_KEY);
} else {
ctx.put(REPOSITORY_KEY, repository);
ctx.put(REPO_ADDRESS_KEY, address);
}
}
/**
* Sets the current working Session
* @param ctx
* the Context
* @param session
* the current working Session
* @throws CommandException if there's an open working Session
*/
public static void setSession(Context ctx, Session session) throws CommandException {
if (session == null) {
ctx.remove(SESSION_KEY);
} else {
if (ctx.get(SESSION_KEY) != null) {
throw new CommandException("exception.already.logged.in");
}
ctx.put(SESSION_KEY, session);
}
}
/**
* Gets the current PrintWriter
* @param ctx
* the Context
* @return the current PrintWriter
*/
public static PrintWriter getOutput(Context ctx) {
PrintWriter out = (PrintWriter) ctx.get(OUTPUT_KEY);
if (out == null) {
out = new PrintWriter(System.out, true);
}
return out;
}
/**
* Gets the current working Node
* @param ctx
* the Context
* @return the current working Node
* @throws CommandException
* if the current working Node
can't be found.
*/
public static Node getCurrentNode(Context ctx) throws CommandException {
Node n = (Node) ctx.get(CURRENT_NODE_KEY);
if (n == null) {
throw new CommandException("exception.no.current.node");
}
return n;
}
/**
* Gets the current working Repository
* @param ctx
* the Context
* @return the current working Repository
* @throws CommandException
* if the current working Repository
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;
}
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");
}
return a;
}
/**
* Gets the current working Session
* @param ctx
* the Context
* @return the current working Session
* @throws CommandException
* if the current working Session
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;
}
/**
* Gets the Node
at the given path.
* @param ctx
* the Context
* @param path
* the path to the Node
* @return the Node
at the given path
* @throws CommandException
* if the Node
isn't found.
* @throws RepositoryException
* if the underlying repository throws a
* RepositoryException
*/
public static Node getNode(Context ctx, String path)
throws CommandException, RepositoryException {
try {
Item i = getItem(ctx, path);
if (!i.isNode()) {
throw new CommandException("exception.no.node.at",
new String[] {
path
});
}
return (Node) i;
} catch (PathNotFoundException e) {
throw new CommandException("exception.no.node.at", new String[] {
path
});
}
}
/**
* Gets the Item
at the given path.
* If the path is null it returns the current working Node
.
* @param ctx
* the Context
* @param path
* the path to the Item
* @return the Item
at the given path
* @throws CommandException
* if a Command
internal error occurs.
* @throws PathNotFoundException
* if there's no Node
at the given path.
* @throws RepositoryException
* if the underlying repository throws a
* RepositoryException
*/
public static Item getItem(Context ctx, String path)
throws CommandException, PathNotFoundException, RepositoryException {
Node current = getCurrentNode(ctx);
Item i = null;
if (path == null) {
i = current;
} else if (path.equals("/")) {
i = current.getSession().getRootNode();
} else if (path.startsWith("/")) {
i = current.getSession().getItem(path);
} else {
String newPath = current.getPath();
// handle the root node
if (!newPath.endsWith("/")) {
newPath += "/";
}
newPath += path;
i = current.getSession().getItem(newPath);
}
return i;
}
/**
* Checks Node
existence.
* @param ctx
* the Context
* @param path
* the path to the Node
* @return true if the Node
exists at the given path
* @throws CommandException
* if the current working Session
is unset.
* @throws RepositoryException
* if the underlying repository throws a
* RepositoryException
*/
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);
}
}
/**
* Gets the Node
s under the given Node
that
* match the given pattern.
* @param ctx
* the Context
* @param node
* the parent Node
* @param pattern
* the pattern
* @return an Iterator
that contains the matching nodes
* @throws RepositoryException
* if the underlying repository throws a
* RepositoryException
*/
public static NodeIterator getNodes(Context ctx, Node node, String pattern)
throws RepositoryException {
if (pattern != null) {
return node.getNodes(pattern);
} else {
return node.getNodes();
}
}
/**
* Gets the Property
s under the current working node for the
* given pattern
* @param ctx
* the Context
* @param node
* the parent Node
* @param pattern
* the pattern
* @return a PropertyIterator
* @throws RepositoryException
* if the underlying repository throws a
* RepositoryException
*/
public static PropertyIterator getProperties(
Context ctx,
Node node,
String pattern) throws RepositoryException {
if (pattern != null) {
return node.getProperties(pattern);
} else {
return node.getProperties();
}
}
/**
* @return the default ResourceBundle
*/
public static ResourceBundle getBundle() {
return bundle;
}
/**
* Gets the Item
s under the given Node
that
* match the pattern
* @param ctx
* the Context
* @param node
* the parent Node
* @param pattern
* the pattern
* @return an Iterator
with the Item
s that
* match the given pattern.
* @throws RepositoryException
* if the underlying repository throws a
* RepositoryException
*/
@SuppressWarnings("unchecked")
public static Iterator- getItems(Context ctx, Node node, String pattern)
throws RepositoryException {
return IteratorUtils.chainedIterator(getNodes(ctx, node, pattern),
getProperties(ctx, node, pattern));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy