Search in sources :

Example 6 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class Info method execute.

public boolean execute(Context ctx) throws Exception {
    PrintWriter out = CommandHelper.getOutput(ctx);
    out.println();
    try {
        CommandHelper.getRepository(ctx);
    } catch (CommandException e) {
        out.println("No connection to a repository.");
        return false;
    }
    out.println("Repository: " + CommandHelper.getRepositoryAddress(ctx));
    Session session;
    String currentPath;
    try {
        session = CommandHelper.getSession(ctx);
        currentPath = CommandHelper.getCurrentNode(ctx).getPath();
    } catch (CommandException e) {
        out.println("Not logged in / no session.");
        return false;
    }
    out.println("User      : " + session.getUserID());
    out.println("Workspace : " + session.getWorkspace().getName());
    out.println("Node      : " + currentPath);
    out.println();
    if (session.isLive()) {
        out.println("Session is live.");
    } else {
        out.println("Session is not live.");
    }
    if (session.hasPendingChanges()) {
        out.println("Session has pending changes.");
    } else {
        out.println("Session has no changes.");
    }
    return false;
}
Also used : CommandException(org.apache.jackrabbit.standalone.cli.CommandException) PrintWriter(java.io.PrintWriter) Session(javax.jcr.Session)

Example 7 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class AbstractExportViewToFile method getOutputStream.

/**
     * @return the OutputStream for the given file
     * @throws CommandException
     * @throws IOException
     */
protected OutputStream getOutputStream(Context ctx) throws CommandException, IOException {
    String to = (String) ctx.get(this.desFsPathKey);
    boolean overwrite = Boolean.valueOf((String) ctx.get(this.overwriteKey)).booleanValue();
    File f = new File(to);
    if (f.exists() && !overwrite) {
        throw new CommandException("exception.file.exists", new String[] { to });
    }
    if (!f.exists()) {
        f.createNewFile();
    }
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
    return out;
}
Also used : FileOutputStream(java.io.FileOutputStream) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class ImportFileSystem method execute.

/**
     * {@inheritDoc}
     */
public boolean execute(Context ctx) throws Exception {
    String file = (String) ctx.get(this.srcFsPathKey);
    Node parent = CommandHelper.getCurrentNode(ctx);
    if (log.isDebugEnabled()) {
        log.debug("importing filesystem from " + file + " to node " + parent);
    }
    if (file == null) {
        throw new CommandException("exception.fspath.is.null");
    }
    File f = new File(file);
    if (!f.exists()) {
        throw new CommandException("exception.file.not.found", new String[] { file });
    }
    if (f.isFile()) {
        this.importFile(parent, f);
    } else {
        Node folder = parent.addNode(f.getName(), "nt:folder");
        this.importFolder(folder, f);
    }
    return false;
}
Also used : Node(javax.jcr.Node) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) File(java.io.File)

Example 9 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class JcrClient method getPrompt.

/**
     * Prompt message
     * @return prompt the prompt message
     * @throws RepositoryException
     *         if the current <code>Repository</code> throws a
     *         <code>RepositoryException</code>
     */
private String getPrompt() throws RepositoryException {
    try {
        CommandHelper.getRepository(ctx);
    } catch (CommandException e) {
        return bundle.getString("phrase.not.connected");
    }
    boolean unsaved = false;
    try {
        unsaved = CommandHelper.getSession(ctx).hasPendingChanges();
    } catch (CommandException e) {
        return bundle.getString("phrase.not.logged.in");
    }
    try {
        Node n = CommandHelper.getCurrentNode(ctx);
        // the current node might be Invalid
        String path;
        try {
            path = n.getPath();
        } catch (InvalidItemStateException e) {
            CommandHelper.setCurrentNode(ctx, CommandHelper.getSession(ctx).getRootNode());
            path = CommandHelper.getCurrentNode(ctx).getPath();
        }
        if (unsaved) {
            return path + "*";
        } else {
            return path;
        }
    } catch (CommandException e) {
        return bundle.getString("phrase.not.logged.in");
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node) CommandException(org.apache.jackrabbit.standalone.cli.CommandException)

Example 10 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class Main method run.

public void run() throws Exception {
    String defaultFile = "jackrabbit-standalone.jar";
    URL location = Main.class.getProtectionDomain().getCodeSource().getLocation();
    if (location != null && "file".equals(location.getProtocol())) {
        File file = new File(location.getPath());
        if (file.isFile()) {
            defaultFile = location.getPath();
        }
    }
    File file = new File(command.getOptionValue("file", defaultFile));
    if (command.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar " + file.getName(), options, true);
    } else if (command.hasOption("notice")) {
        copyToOutput("/META-INF/NOTICE.txt");
    } else if (command.hasOption("license")) {
        copyToOutput("/META-INF/LICENSE.txt");
    } else if (command.hasOption("cli")) {
        System.setProperty("logback.configurationFile", "logback-cli.xml");
        String uri = command.getOptionValue("cli");
        Repository repository = JcrUtils.getRepository(uri);
        Context context = new ContextBase();
        CommandHelper.setRepository(context, repository, uri);
        try {
            Session session = repository.login();
            CommandHelper.setSession(context, session);
            CommandHelper.setCurrentNode(context, session.getRootNode());
        } catch (RepositoryException ignore) {
        // anonymous login not possible
        }
        new JcrClient(context).runInteractive();
        try {
            CommandHelper.getSession(context).logout();
        } catch (CommandException ignore) {
        // already logged out
        }
    } else {
        message("Welcome to Apache Jackrabbit!");
        message("-------------------------------");
        File repository = new File(command.getOptionValue("repo", "jackrabbit"));
        message("Using repository directory " + repository);
        repository.mkdirs();
        File tmp = new File(repository, "tmp");
        tmp.mkdir();
        File log = new File(repository, "log");
        log.mkdir();
        message("Writing log messages to " + log);
        prepareServerLog(log);
        if (command.hasOption("backup")) {
            backup(repository);
        } else {
            message("Starting the server...");
            prepareWebapp(file, repository, tmp);
            accessLog.setHandler(webapp);
            prepareAccessLog(log);
            server.setHandler(accessLog);
            prepareConnector();
            server.addConnector(connector);
            prepareShutdown();
            try {
                server.start();
                String host = connector.getHost();
                if (host == null) {
                    host = "localhost";
                }
                message("Apache Jackrabbit is now running at " + "http://" + host + ":" + connector.getPort() + "/");
            } catch (Throwable t) {
                System.err.println("Unable to start the server: " + t.getMessage());
                System.exit(1);
            }
        }
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) Context(org.apache.commons.chain.Context) Repository(javax.jcr.Repository) RepositoryException(javax.jcr.RepositoryException) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) File(java.io.File) URL(java.net.URL) JcrClient(org.apache.jackrabbit.standalone.cli.JcrClient) ContextBase(org.apache.commons.chain.impl.ContextBase) Session(javax.jcr.Session)

Aggregations

CommandException (org.apache.jackrabbit.standalone.cli.CommandException)11 File (java.io.File)7 Node (javax.jcr.Node)4 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 Session (javax.jcr.Session)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 NodeIterator (javax.jcr.NodeIterator)1 Repository (javax.jcr.Repository)1