40f16df00e660679bbed47a44207fc21eb8fe108
[jSite.git] / CLI.java
1 /*
2  * jSite - CLI.java - Copyright © 2006–2011 David Roden
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package de.todesbaum.jsite.main;
20
21 import java.io.PrintWriter;
22
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;
29
30 /**
31  * Command-line interface for jSite.
32  *
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  */
35 public class CLI implements InsertListener {
36
37         /** Object used for synchronization. */
38         private Object lockObject = new Object();
39
40         /** Writer for the console. */
41         private PrintWriter outputWriter = new PrintWriter(System.out, true);
42
43         /** The freenet interface. */
44         private Freenet7Interface freenetInterface;
45
46         /** The project inserter. */
47         private ProjectInserter projectInserter = new ProjectInserter();
48
49         /** The list of nodes. */
50         private Node[] nodes;
51
52         /** The projects. */
53         private Project[] projects;
54
55         /** Whether the insert has finished. */
56         private boolean finished = false;
57
58         /** Whether the insert finished successfully. */
59         private boolean success;
60
61         /**
62          * Creates a new command-line interface.
63          *
64          * @param args
65          *            The command-line arguments
66          */
67         private CLI(String[] args) {
68
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.");
80                         return;
81                 }
82
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=")) {
87                                 configFile = value;
88                         }
89                 }
90
91                 ConfigurationLocator configurationLocator = new ConfigurationLocator();
92                 if (configFile != null) {
93                         configurationLocator.setCustomLocation(configFile);
94                 }
95                 Configuration configuration = new Configuration(configurationLocator, configurationLocator.findPreferredLocation());
96
97                 projectInserter.addInsertListener(this);
98                 projects = configuration.getProjects();
99                 Node node = configuration.getSelectedNode();
100                 nodes = configuration.getNodes();
101
102                 freenetInterface = new Freenet7Interface();
103                 freenetInterface.setNode(node);
104
105                 projectInserter.setFreenetInterface(freenetInterface);
106
107                 Project currentProject = null;
108                 for (String argument : args) {
109                         if (argument.startsWith("--config-file=")) {
110                                 /* we already parsed this one. */
111                                 continue;
112                         }
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.");
118                                         return;
119                                 }
120                                 node = newNode;
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.");
126                                         } else {
127                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
128                                         }
129                                         currentProject = null;
130                                 }
131                                 currentProject = getProject(value);
132                                 if (currentProject == null) {
133                                         outputWriter.println("Project \"" + value + "\" not found.");
134                                 }
135                         } else if (argument.startsWith("--local-directory")) {
136                                 if (currentProject == null) {
137                                         outputWriter.println("You can't specifiy --local-directory before --project.");
138                                         return;
139                                 }
140                                 currentProject.setLocalPath(value);
141                         } else if (argument.startsWith("--path=")) {
142                                 if (currentProject == null) {
143                                         outputWriter.println("You can't specify --path before --project.");
144                                         return;
145                                 }
146                                 currentProject.setPath(value);
147                         } else if (argument.startsWith("--edition=")) {
148                                 if (currentProject == null) {
149                                         outputWriter.println("You can't specify --edition before --project.");
150                                         return;
151                                 }
152                                 currentProject.setEdition(Integer.parseInt(value));
153                         } else {
154                                 outputWriter.println("Unknown parameter: " + argument);
155                                 return;
156                         }
157                 }
158
159                 int errorCode = 1;
160                 if (currentProject != null) {
161                         if (insertProject(currentProject)) {
162                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
163                                 errorCode = 0;
164                         } else {
165                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
166                         }
167                 }
168
169                 configuration.setProjects(projects);
170                 configuration.save();
171
172                 System.exit(errorCode);
173         }
174
175         /**
176          * Returns the project with the given name.
177          *
178          * @param name
179          *            The name of the project
180          * @return The project, or <code>null</code> if no project could be found
181          */
182         private Project getProject(String name) {
183                 for (Project project : projects) {
184                         if (project.getName().equals(name)) {
185                                 return project;
186                         }
187                 }
188                 return null;
189         }
190
191         /**
192          * Returns the node with the given name.
193          *
194          * @param name
195          *            The name of the node
196          * @return The node, or <code>null</code> if no node could be found
197          */
198         private Node getNode(String name) {
199                 for (Node node : nodes) {
200                         if (node.getName().equals(name)) {
201                                 return node;
202                         }
203                 }
204                 return null;
205         }
206
207         /**
208          * Inserts the given project.
209          *
210          * @param currentProject
211          *            The project to insert
212          * @return <code>true</code> if the insert finished successfully,
213          *         <code>false</code> otherwise
214          */
215         private boolean insertProject(Project currentProject) {
216                 if (!freenetInterface.hasNode()) {
217                         outputWriter.println("Node is not running!");
218                         return false;
219                 }
220                 projectInserter.setProject(currentProject);
221                 projectInserter.start(new ProgressListener() {
222
223                         public void onProgress(long copied, long length) {
224                                 System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
225                         }
226                 });
227                 synchronized (lockObject) {
228                         while (!finished) {
229                                 try {
230                                         lockObject.wait();
231                                 } catch (InterruptedException e) {
232                                         /* ignore, we're in a loop. */
233                                 }
234                         }
235                 }
236                 return success;
237         }
238
239         //
240         // INTERFACE InsertListener
241         //
242
243         /**
244          * {@inheritDoc}
245          */
246         public void projectInsertStarted(Project project) {
247                 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
248         }
249
250         /**
251          * {@inheritDoc}
252          */
253         public void projectUploadFinished(Project project) {
254                 outputWriter.println("Project \"" + project.getName() + "\" has been uploaded, starting insert...");
255         }
256
257         /**
258          * {@inheritDoc}
259          */
260         public void projectURIGenerated(Project project, String uri) {
261                 outputWriter.println("URI: " + uri);
262         }
263
264         /**
265          * {@inheritDoc}
266          */
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) + "%");
269         }
270
271         /**
272          * {@inheritDoc}
273          */
274         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
275                 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
276                 finished = true;
277                 this.success = success;
278                 synchronized (lockObject) {
279                         lockObject.notify();
280                 }
281         }
282
283         //
284         // MAIN
285         //
286
287         /**
288          * Creates a new command-line interface with the given arguments.
289          *
290          * @param args
291          *            The command-line arguments
292          */
293         public static void main(String[] args) {
294                 new CLI(args);
295         }
296
297 }