Add “--config-file” parameter to CLI. Fixes #65.
[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("  --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.");
81                         return;
82                 }
83
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=")) {
88                                 configFile = value;
89                         }
90                 }
91
92                 Configuration configuration = new Configuration(configFile);
93                 if (!configuration.createLockFile()) {
94                         outputWriter.println("Lock file found!");
95                         return;
96                 }
97
98                 projectInserter.addInsertListener(this);
99                 projects = configuration.getProjects();
100                 Node node = configuration.getSelectedNode();
101                 nodes = configuration.getNodes();
102
103                 freenetInterface = new Freenet7Interface();
104                 freenetInterface.setNode(node);
105
106                 projectInserter.setFreenetInterface(freenetInterface);
107
108                 Project currentProject = null;
109                 for (String argument : args) {
110                         String value = argument.substring(argument.indexOf('=') + 1).trim();
111                         if (argument.startsWith("--node=")) {
112                                 Node newNode = getNode(value);
113                                 if (newNode == null) {
114                                         outputWriter.println("Node \"" + value + "\" not found.");
115                                         return;
116                                 }
117                                 node = newNode;
118                                 freenetInterface.setNode(node);
119                         } else if (argument.startsWith("--project=")) {
120                                 if (currentProject != null) {
121                                         if (insertProject(currentProject)) {
122                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
123                                         } else {
124                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
125                                         }
126                                         currentProject = null;
127                                 }
128                                 currentProject = getProject(value);
129                                 if (currentProject == null) {
130                                         outputWriter.println("Project \"" + value + "\" not found.");
131                                 }
132                         } else if (argument.startsWith("--local-directory")) {
133                                 if (currentProject == null) {
134                                         outputWriter.println("You can't specifiy --local-directory before --project.");
135                                         return;
136                                 }
137                                 currentProject.setLocalPath(value);
138                         } else if (argument.startsWith("--path=")) {
139                                 if (currentProject == null) {
140                                         outputWriter.println("You can't specify --path before --project.");
141                                         return;
142                                 }
143                                 currentProject.setPath(value);
144                         } else if (argument.startsWith("--edition=")) {
145                                 if (currentProject == null) {
146                                         outputWriter.println("You can't specify --edition before --project.");
147                                         return;
148                                 }
149                                 currentProject.setEdition(Integer.parseInt(value));
150                         } else {
151                                 outputWriter.println("Unknown parameter: " + argument);
152                                 return;
153                         }
154                 }
155
156                 if (currentProject != null) {
157                         if (insertProject(currentProject)) {
158                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
159                         } else {
160                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
161                         }
162                 }
163
164                 configuration.setProjects(projects);
165                 configuration.save();
166         }
167
168         /**
169          * Returns the project with the given name.
170          *
171          * @param name
172          *            The name of the project
173          * @return The project, or <code>null</code> if no project could be found
174          */
175         private Project getProject(String name) {
176                 for (Project project : projects) {
177                         if (project.getName().equals(name)) {
178                                 return project;
179                         }
180                 }
181                 return null;
182         }
183
184         /**
185          * Returns the node with the given name.
186          *
187          * @param name
188          *            The name of the node
189          * @return The node, or <code>null</code> if no node could be found
190          */
191         private Node getNode(String name) {
192                 for (Node node : nodes) {
193                         if (node.getName().equals(name)) {
194                                 return node;
195                         }
196                 }
197                 return null;
198         }
199
200         /**
201          * Inserts the given project.
202          *
203          * @param currentProject
204          *            The project to insert
205          * @return <code>true</code> if the insert finished successfully,
206          *         <code>false</code> otherwise
207          */
208         private boolean insertProject(Project currentProject) {
209                 if (!freenetInterface.hasNode()) {
210                         outputWriter.println("Node is not running!");
211                         return false;
212                 }
213                 projectInserter.setProject(currentProject);
214                 projectInserter.start(new ProgressListener() {
215
216                         public void onProgress(long copied, long length) {
217                                 System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
218                         }
219                 });
220                 synchronized (lockObject) {
221                         while (!finished) {
222                                 try {
223                                         lockObject.wait();
224                                 } catch (InterruptedException e) {
225                                         /* ignore, we're in a loop. */
226                                 }
227                         }
228                 }
229                 return success;
230         }
231
232         //
233         // INTERFACE InsertListener
234         //
235
236         /**
237          * {@inheritDoc}
238          */
239         public void projectInsertStarted(Project project) {
240                 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
241         }
242
243         /**
244          * {@inheritDoc}
245          */
246         public void projectUploadFinished(Project project) {
247                 outputWriter.println("Project \"" + project.getName() + "\" has ben uploaded, starting insert...");
248         }
249
250         /**
251          * {@inheritDoc}
252          */
253         public void projectURIGenerated(Project project, String uri) {
254                 outputWriter.println("URI: " + uri);
255         }
256
257         /**
258          * {@inheritDoc}
259          */
260         public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
261                 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
262         }
263
264         /**
265          * {@inheritDoc}
266          */
267         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
268                 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
269                 finished = true;
270                 this.success = success;
271                 synchronized (lockObject) {
272                         lockObject.notify();
273                 }
274         }
275
276         //
277         // MAIN
278         //
279
280         /**
281          * Creates a new command-line interface with the given arguments.
282          *
283          * @param args
284          *            The command-line arguments
285          */
286         public static void main(String[] args) {
287                 new CLI(args);
288         }
289
290 }