2 * jSite - CLI.java - Copyright © 2006–2011 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.main;
21 import java.io.PrintWriter;
23 import de.todesbaum.jsite.application.Freenet7Interface;
24 import de.todesbaum.jsite.application.InsertListener;
25 import de.todesbaum.jsite.application.Node;
26 import de.todesbaum.jsite.application.Project;
27 import de.todesbaum.jsite.application.ProjectInserter;
28 import de.todesbaum.util.io.StreamCopier.ProgressListener;
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(" --config-file=<configuration file>");
72 outputWriter.println(" --node=<node name>");
73 outputWriter.println(" --project=<project name>");
74 outputWriter.println(" --local-directory=<local directory>");
75 outputWriter.println(" --path=<path>");
76 outputWriter.println(" --edition=<edition>");
77 outputWriter.println("\nA project gets inserted when a new project is loaded on the command line,");
78 outputWriter.println("or when the command line is finished. --local-directory, --path, and --edition");
79 outputWriter.println("override the parameters in the project.");
83 String configFile = System.getProperty("user.home") + "/.jSite/config7";
84 for (String argument : args) {
85 String value = argument.substring(argument.indexOf('=') + 1).trim();
86 if (argument.startsWith("--config-file=")) {
91 ConfigurationLocator configurationLocator = new ConfigurationLocator();
92 if (configFile != null) {
93 configurationLocator.setCustomLocation(configFile);
95 Configuration configuration = new Configuration(configurationLocator, configurationLocator.findPreferredLocation());
97 projectInserter.addInsertListener(this);
98 projects = configuration.getProjects();
99 Node node = configuration.getSelectedNode();
100 nodes = configuration.getNodes();
102 freenetInterface = new Freenet7Interface();
103 freenetInterface.setNode(node);
105 projectInserter.setFreenetInterface(freenetInterface);
107 Project currentProject = null;
108 for (String argument : args) {
109 if (argument.startsWith("--config-file=")) {
110 /* we already parsed this one. */
113 String value = argument.substring(argument.indexOf('=') + 1).trim();
114 if (argument.startsWith("--node=")) {
115 Node newNode = getNode(value);
116 if (newNode == null) {
117 outputWriter.println("Node \"" + value + "\" not found.");
121 freenetInterface.setNode(node);
122 } else if (argument.startsWith("--project=")) {
123 if (currentProject != null) {
124 if (insertProject(currentProject)) {
125 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
127 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
129 currentProject = null;
131 currentProject = getProject(value);
132 if (currentProject == null) {
133 outputWriter.println("Project \"" + value + "\" not found.");
135 } else if (argument.startsWith("--local-directory")) {
136 if (currentProject == null) {
137 outputWriter.println("You can't specifiy --local-directory before --project.");
140 currentProject.setLocalPath(value);
141 } else if (argument.startsWith("--path=")) {
142 if (currentProject == null) {
143 outputWriter.println("You can't specify --path before --project.");
146 currentProject.setPath(value);
147 } else if (argument.startsWith("--edition=")) {
148 if (currentProject == null) {
149 outputWriter.println("You can't specify --edition before --project.");
152 currentProject.setEdition(Integer.parseInt(value));
154 outputWriter.println("Unknown parameter: " + argument);
160 if (currentProject != null) {
161 if (insertProject(currentProject)) {
162 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
165 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
169 configuration.setProjects(projects);
170 configuration.save();
172 System.exit(errorCode);
176 * Returns the project with the given name.
179 * The name of the project
180 * @return The project, or <code>null</code> if no project could be found
182 private Project getProject(String name) {
183 for (Project project : projects) {
184 if (project.getName().equals(name)) {
192 * Returns the node with the given name.
195 * The name of the node
196 * @return The node, or <code>null</code> if no node could be found
198 private Node getNode(String name) {
199 for (Node node : nodes) {
200 if (node.getName().equals(name)) {
208 * Inserts the given project.
210 * @param currentProject
211 * The project to insert
212 * @return <code>true</code> if the insert finished successfully,
213 * <code>false</code> otherwise
215 private boolean insertProject(Project currentProject) {
216 if (!freenetInterface.hasNode()) {
217 outputWriter.println("Node is not running!");
220 projectInserter.setProject(currentProject);
221 projectInserter.start(new ProgressListener() {
223 public void onProgress(long copied, long length) {
224 System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
227 synchronized (lockObject) {
231 } catch (InterruptedException e) {
232 /* ignore, we're in a loop. */
240 // INTERFACE InsertListener
246 public void projectInsertStarted(Project project) {
247 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
253 public void projectUploadFinished(Project project) {
254 outputWriter.println("Project \"" + project.getName() + "\" has been uploaded, starting insert...");
260 public void projectURIGenerated(Project project, String uri) {
261 outputWriter.println("URI: " + uri);
267 public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
268 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
274 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
275 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
277 this.success = success;
278 synchronized (lockObject) {
288 * Creates a new command-line interface with the given arguments.
291 * The command-line arguments
293 public static void main(String[] args) {