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