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