2 * jSite - CLI.java - Copyright © 2006–2012 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;
22 import java.util.List;
24 import net.pterodactylus.util.io.StreamCopier.ProgressListener;
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 * Command-line interface for jSite.
34 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class CLI implements InsertListener {
38 /** Object used for synchronization. */
39 private Object lockObject = new Object();
41 /** Writer for the console. */
42 private PrintWriter outputWriter = new PrintWriter(System.out, true);
44 /** The freenet interface. */
45 private Freenet7Interface freenetInterface;
47 /** The project inserter. */
48 private ProjectInserter projectInserter = new ProjectInserter();
50 /** The list of nodes. */
54 private List<Project> projects;
56 /** Whether the insert has finished. */
57 private boolean finished = false;
59 /** Whether the insert finished successfully. */
60 private boolean success;
63 * Creates a new command-line interface.
66 * The command-line arguments
68 private CLI(String[] args) {
70 if ((args.length == 0) || args[0].equals("-h") || args[0].equals("--help")) {
71 outputWriter.println("\nParameters:\n");
72 outputWriter.println(" --config-file=<configuration file>");
73 outputWriter.println(" --node=<node name>");
74 outputWriter.println(" --project=<project name>");
75 outputWriter.println(" --local-directory=<local directory>");
76 outputWriter.println(" --path=<path>");
77 outputWriter.println(" --edition=<edition>");
78 outputWriter.println("\nA project gets inserted when a new project is loaded on the command line,");
79 outputWriter.println("or when the command line is finished. --local-directory, --path, and --edition");
80 outputWriter.println("override the parameters in the project.");
84 String configFile = System.getProperty("user.home") + "/.jSite/config7";
85 for (String argument : args) {
86 String value = argument.substring(argument.indexOf('=') + 1).trim();
87 if (argument.startsWith("--config-file=")) {
92 ConfigurationLocator configurationLocator = new ConfigurationLocator();
93 if (configFile != null) {
94 configurationLocator.setCustomLocation(configFile);
96 Configuration configuration = new Configuration(configurationLocator, configurationLocator.findPreferredLocation());
98 projectInserter.addInsertListener(this);
99 projects = configuration.getProjects();
100 Node node = configuration.getSelectedNode();
101 nodes = configuration.getNodes();
103 freenetInterface = new Freenet7Interface();
104 freenetInterface.setNode(node);
106 projectInserter.setFreenetInterface(freenetInterface);
108 Project currentProject = null;
109 for (String argument : args) {
110 if (argument.startsWith("--config-file=")) {
111 /* we already parsed this one. */
114 String value = argument.substring(argument.indexOf('=') + 1).trim();
115 if (argument.startsWith("--node=")) {
116 Node newNode = getNode(value);
117 if (newNode == null) {
118 outputWriter.println("Node \"" + value + "\" not found.");
122 freenetInterface.setNode(node);
123 } else if (argument.startsWith("--project=")) {
124 if (currentProject != null) {
125 if (insertProject(currentProject)) {
126 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
128 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
130 currentProject = null;
132 currentProject = getProject(value);
133 if (currentProject == null) {
134 outputWriter.println("Project \"" + value + "\" not found.");
136 } else if (argument.startsWith("--local-directory")) {
137 if (currentProject == null) {
138 outputWriter.println("You can't specifiy --local-directory before --project.");
141 currentProject.setLocalPath(value);
142 } else if (argument.startsWith("--path=")) {
143 if (currentProject == null) {
144 outputWriter.println("You can't specify --path before --project.");
147 currentProject.setPath(value);
148 } else if (argument.startsWith("--edition=")) {
149 if (currentProject == null) {
150 outputWriter.println("You can't specify --edition before --project.");
153 currentProject.setEdition(Integer.parseInt(value));
155 outputWriter.println("Unknown parameter: " + argument);
161 if (currentProject != null) {
162 if (insertProject(currentProject)) {
163 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
166 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
170 configuration.setProjects(projects);
171 configuration.save();
173 System.exit(errorCode);
177 * Returns the project with the given name.
180 * The name of the project
181 * @return The project, or <code>null</code> if no project could be found
183 private Project getProject(String name) {
184 for (Project project : projects) {
185 if (project.getName().equals(name)) {
193 * Returns the node with the given name.
196 * The name of the node
197 * @return The node, or <code>null</code> if no node could be found
199 private Node getNode(String name) {
200 for (Node node : nodes) {
201 if (node.getName().equals(name)) {
209 * Inserts the given project.
211 * @param currentProject
212 * The project to insert
213 * @return <code>true</code> if the insert finished successfully,
214 * <code>false</code> otherwise
216 private boolean insertProject(Project currentProject) {
217 if (!freenetInterface.hasNode()) {
218 outputWriter.println("Node is not running!");
221 projectInserter.setProject(currentProject);
222 projectInserter.start(new ProgressListener() {
225 public void onProgress(long copied, long length) {
226 System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
229 synchronized (lockObject) {
233 } catch (InterruptedException e) {
234 /* ignore, we're in a loop. */
242 // INTERFACE InsertListener
249 public void projectInsertStarted(Project project) {
250 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
257 public void projectUploadFinished(Project project) {
258 outputWriter.println("Project \"" + project.getName() + "\" has been uploaded, starting insert...");
265 public void projectURIGenerated(Project project, String uri) {
266 outputWriter.println("URI: " + uri);
273 public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
274 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
281 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
282 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
284 this.success = success;
285 synchronized (lockObject) {
295 * Creates a new command-line interface with the given arguments.
298 * The command-line arguments
300 public static void main(String[] args) {