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.Freenet7Interface;
25 import de.todesbaum.jsite.application.InsertListener;
26 import de.todesbaum.jsite.application.Node;
27 import de.todesbaum.jsite.application.Project;
28 import de.todesbaum.jsite.application.ProjectInserter;
31 * Command-line interface for jSite.
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35 public class CLI implements InsertListener {
37 /** Object used for synchronization. */
38 private Object lockObject = new Object();
40 /** Writer for the console. */
41 private PrintWriter outputWriter = new PrintWriter(System.out, true);
43 /** The freenet interface. */
44 private Freenet7Interface freenetInterface;
46 /** The project inserter. */
47 private ProjectInserter projectInserter = new ProjectInserter();
49 /** The list of nodes. */
53 private Project[] projects;
55 /** Whether the insert has finished. */
56 private boolean finished = false;
58 /** Whether the insert finished successfully. */
59 private boolean success;
62 * Creates a new command-line interface.
65 * The command-line arguments
67 private CLI(String[] args) {
69 if ((args.length == 0) || args[0].equals("-h") || args[0].equals("--help")) {
70 outputWriter.println("\nParameters:\n");
71 outputWriter.println(" --node=<node name>");
72 outputWriter.println(" --project=<project name>");
73 outputWriter.println(" --local-directory=<local directory>");
74 outputWriter.println(" --path=<path>");
75 outputWriter.println(" --edition=<edition>");
76 outputWriter.println("\nA project gets inserted when a new project is loaded on the command line,");
77 outputWriter.println("or when the command line is finished. --local-directory, --path, and --edition");
78 outputWriter.println("override the parameters in the project.");
82 Configuration configuration = new Configuration();
83 if (!configuration.createLockFile()) {
84 outputWriter.println("Lock file found!");
88 projectInserter.addInsertListener(this);
89 projects = configuration.getProjects();
90 Node node = configuration.getSelectedNode();
91 nodes = configuration.getNodes();
93 freenetInterface = new Freenet7Interface();
94 freenetInterface.setNode(node);
96 projectInserter.setFreenetInterface(freenetInterface);
98 Project currentProject = null;
99 for (String argument : args) {
100 String value = argument.substring(argument.indexOf('=') + 1).trim();
101 if (argument.startsWith("--node=")) {
102 Node newNode = getNode(value);
103 if (newNode == null) {
104 outputWriter.println("Node \"" + value + "\" not found.");
108 freenetInterface.setNode(node);
109 } else if (argument.startsWith("--project=")) {
110 if (currentProject != null) {
111 if (insertProject(currentProject)) {
112 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
114 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
116 currentProject = null;
118 currentProject = getProject(value);
119 if (currentProject == null) {
120 outputWriter.println("Project \"" + value + "\" not found.");
122 } else if (argument.startsWith("--local-directory")) {
123 if (currentProject == null) {
124 outputWriter.println("You can't specifiy --local-directory before --project.");
127 currentProject.setLocalPath(value);
128 } else if (argument.startsWith("--path=")) {
129 if (currentProject == null) {
130 outputWriter.println("You can't specify --path before --project.");
133 currentProject.setPath(value);
134 } else if (argument.startsWith("--edition=")) {
135 if (currentProject == null) {
136 outputWriter.println("You can't specify --edition before --project.");
139 currentProject.setEdition(Integer.parseInt(value));
141 outputWriter.println("Unknown parameter: " + argument);
146 if (currentProject != null) {
147 if (insertProject(currentProject)) {
148 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
150 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
154 configuration.setProjects(projects);
155 configuration.save();
159 * Returns the project with the given name.
162 * The name of the project
163 * @return The project, or <code>null</code> if no project could be found
165 private Project getProject(String name) {
166 for (Project project : projects) {
167 if (project.getName().equals(name)) {
175 * Returns the node with the given name.
178 * The name of the node
179 * @return The node, or <code>null</code> if no node could be found
181 private Node getNode(String name) {
182 for (Node node : nodes) {
183 if (node.getName().equals(name)) {
191 * Inserts the given project.
193 * @param currentProject
194 * The project to insert
195 * @return <code>true</code> if the insert finished successfully,
196 * <code>false</code> otherwise
198 private boolean insertProject(Project currentProject) {
199 if (!freenetInterface.hasNode()) {
200 outputWriter.println("Node is not running!");
203 projectInserter.setProject(currentProject);
204 projectInserter.start();
205 synchronized (lockObject) {
209 } catch (InterruptedException e) {
210 /* ignore, we're in a loop. */
218 // INTERFACE InsertListener
224 public void projectInsertStarted(Project project) {
225 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
231 public void projectURIGenerated(Project project, String uri) {
232 outputWriter.println("URI: " + uri);
238 public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
239 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
245 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
246 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
248 this.success = success;
249 synchronized (lockObject) {
259 * Creates a new command-line interface with the given arguments.
262 * The command-line arguments
264 public static void main(String[] args) {