3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.main;
22 import java.io.PrintWriter;
24 import de.todesbaum.jsite.application.EditionProject;
25 import de.todesbaum.jsite.application.Freenet7Interface;
26 import de.todesbaum.jsite.application.InsertListener;
27 import de.todesbaum.jsite.application.Node;
28 import de.todesbaum.jsite.application.Project;
29 import de.todesbaum.jsite.application.ProjectInserter;
32 * @author David Roden <droden@gmail.com>
33 * @version $Id: CLI.java 418 2006-03-29 17:49:16Z bombe $
35 public class CLI implements InsertListener {
37 private Object lockObject = new Object();
38 private PrintWriter outputWriter = new PrintWriter(System.out, true);
39 private Freenet7Interface freenetInterface;
40 private ProjectInserter projectInserter = new ProjectInserter();
42 private Project[] projects;
43 private boolean finished = false;
44 private boolean success;
46 private CLI(String[] args) {
48 if ((args.length == 0) || args[0].equals("-h") || args[0].equals("--help")) {
49 outputWriter.println("\nParameters:\n");
50 outputWriter.println(" --node=<node name>");
51 outputWriter.println(" --project=<project name>");
52 outputWriter.println(" --local-directory=<local directory>");
53 outputWriter.println(" --path=<path>");
54 outputWriter.println(" --edition=<edition>");
55 outputWriter.println("\nA project gets inserted when a new project is loaded on the command line,");
56 outputWriter.println("or when the command line is finished. --local-directory, --path, and --edition");
57 outputWriter.println("override the parameters in the project.");
61 Configuration configuration = new Configuration();
62 if (!configuration.createLockFile()) {
63 outputWriter.println("Lock file found!");
67 projectInserter.addInsertListener(this);
68 projects = configuration.getProjects();
69 Node node = configuration.getSelectedNode();
70 nodes = configuration.getNodes();
72 freenetInterface = new Freenet7Interface();
73 freenetInterface.setNode(node);
75 projectInserter.setFreenetInterface(freenetInterface);
77 Project currentProject = null;
78 for (int argumentIndex = 0, argumentSize = args.length; argumentIndex < argumentSize; argumentIndex++) {
79 String argument = args[argumentIndex];
80 String value = argument.substring(argument.indexOf('=') + 1).trim();
81 if (argument.startsWith("--node=")) {
82 Node newNode = getNode(value);
83 if (newNode == null) {
84 outputWriter.println("Node \"" + value + "\" not found.");
88 freenetInterface.setNode(node);
89 } else if (argument.startsWith("--project=")) {
90 if (currentProject != null) {
91 if (insertProject(currentProject)) {
92 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
94 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
96 currentProject = null;
98 currentProject = getProject(value);
99 if (currentProject == null) {
100 outputWriter.println("Project \"" + value + "\" not found.");
102 } else if (argument.startsWith("--local-directory")) {
103 if (currentProject == null) {
104 outputWriter.println("You can't specifiy --local-directory before --project.");
107 currentProject.setLocalPath(value);
108 } else if (argument.startsWith("--path=")) {
109 if (currentProject == null) {
110 outputWriter.println("You can't specify --path before --project.");
113 currentProject.setPath(value);
114 } else if (argument.startsWith("--edition=")) {
115 if (currentProject == null) {
116 outputWriter.println("You can't specify --edition before --project.");
119 if (currentProject instanceof EditionProject) {
120 ((EditionProject) currentProject).setEdition(Integer.parseInt(value));
122 outputWriter.println("Project \"" + currentProject.getName() + "\" is not an edition-based project.");
126 outputWriter.println("Unknown parameter: " + argument);
131 if (currentProject != null) {
132 if (insertProject(currentProject)) {
133 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
135 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
139 configuration.setProjects(projects);
140 configuration.save();
143 private Project getProject(String name) {
144 for (Project project: projects) {
145 if (project.getName().equals(name)) {
152 private Node getNode(String name) {
153 for (Node node: nodes) {
154 if (node.getName().equals(name)) {
161 private boolean insertProject(Project currentProject) {
162 if (!freenetInterface.hasNode()) {
163 outputWriter.println("Node is not running!");
166 projectInserter.setProject(currentProject);
167 projectInserter.start();
168 synchronized (lockObject) {
172 } catch (InterruptedException e) {
180 // INTERFACE InsertListener
186 public void projectInsertStarted(Project project) {
187 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
193 public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
194 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
200 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
201 outputWriter.println("Request URI: " + project.getFinalURI(0));
204 if (project instanceof EditionProject) {
205 ((EditionProject) project).setEdition(((EditionProject) project).getEdition() + 1);
208 this.success = success;
209 synchronized (lockObject) {
218 public static void main(String[] args) {