1533c11feab4b95c8df26a0460d1d4b3025b676d
[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.EditionProject;
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;
30
31 /**
32  * @author David Roden <droden@gmail.com>
33  * @version $Id$
34  */
35 public class CLI implements InsertListener {
36
37         private Object lockObject = new Object();
38         private PrintWriter outputWriter = new PrintWriter(System.out, true);
39         private Freenet7Interface freenetInterface;
40         private ProjectInserter projectInserter = new ProjectInserter();
41         private Node[] nodes;
42         private Project[] projects;
43         private boolean finished = false;
44         private boolean success;
45
46         private CLI(String[] args) {
47
48                 if ((args.length == 0) || args[0].equals("-h") || args[0].equals("--help")) {
49                         outputWriter.println("\nParameters:\n");
50                         outputWriter.println("  --node=<node name>");
51                         outputWriter.println("  --project=<project name>");
52                         outputWriter.println("  --local-directory=<local directory>");
53                         outputWriter.println("  --path=<path>");
54                         outputWriter.println("  --edition=<edition>");
55                         outputWriter.println("\nA project gets inserted when a new project is loaded on the command line,");
56                         outputWriter.println("or when the command line is finished. --local-directory, --path, and --edition");
57                         outputWriter.println("override the parameters in the project.");
58                         return;
59                 }
60
61                 Configuration configuration = new Configuration();
62                 if (!configuration.createLockFile()) {
63                         outputWriter.println("Lock file found!");
64                         return;
65                 }
66
67                 projectInserter.addInsertListener(this);
68                 projects = configuration.getProjects();
69                 Node node = configuration.getSelectedNode();
70                 nodes = configuration.getNodes();
71
72                 freenetInterface = new Freenet7Interface();
73                 freenetInterface.setNode(node);
74
75                 projectInserter.setFreenetInterface(freenetInterface);
76
77                 Project currentProject = null;
78                 for (String argument: args) {
79                         String value = argument.substring(argument.indexOf('=') + 1).trim();
80                         if (argument.startsWith("--node=")) {
81                                 Node newNode = getNode(value);
82                                 if (newNode == null) {
83                                         outputWriter.println("Node \"" + value + "\" not found.");
84                                         return;
85                                 }
86                                 node = newNode;
87                                 freenetInterface.setNode(node);
88                         } else if (argument.startsWith("--project=")) {
89                                 if (currentProject != null) {
90                                         if (insertProject(currentProject)) {
91                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
92                                         } else {
93                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
94                                         }
95                                         currentProject = null;
96                                 }
97                                 currentProject = getProject(value);
98                                 if (currentProject == null) {
99                                         outputWriter.println("Project \"" + value + "\" not found.");
100                                 }
101                         } else if (argument.startsWith("--local-directory")) {
102                                 if (currentProject == null) {
103                                         outputWriter.println("You can't specifiy --local-directory before --project.");
104                                         return;
105                                 }
106                                 currentProject.setLocalPath(value);
107                         } else if (argument.startsWith("--path=")) {
108                                 if (currentProject == null) {
109                                         outputWriter.println("You can't specify --path before --project.");
110                                         return;
111                                 }
112                                 currentProject.setPath(value);
113                         } else if (argument.startsWith("--edition=")) {
114                                 if (currentProject == null) {
115                                         outputWriter.println("You can't specify --edition before --project.");
116                                         return;
117                                 }
118                                 if (currentProject instanceof EditionProject) {
119                                         ((EditionProject) currentProject).setEdition(Integer.parseInt(value));
120                                 } else {
121                                         outputWriter.println("Project \"" + currentProject.getName() + "\" is not an edition-based project.");
122                                         return;
123                                 }
124                         } else {
125                                 outputWriter.println("Unknown parameter: " + argument);
126                                 return;
127                         }
128                 }
129
130                 if (currentProject != null) {
131                         if (insertProject(currentProject)) {
132                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
133                         } else {
134                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
135                         }
136                 }
137
138                 configuration.setProjects(projects);
139                 configuration.save();
140         }
141
142         private Project getProject(String name) {
143                 for (Project project: projects) {
144                         if (project.getName().equals(name)) {
145                                 return project;
146                         }
147                 }
148                 return null;
149         }
150
151         private Node getNode(String name) {
152                 for (Node node: nodes) {
153                         if (node.getName().equals(name)) {
154                                 return node;
155                         }
156                 }
157                 return null;
158         }
159
160         private boolean insertProject(Project currentProject) {
161                 if (!freenetInterface.hasNode()) {
162                         outputWriter.println("Node is not running!");
163                         return false;
164                 }
165                 projectInserter.setProject(currentProject);
166                 projectInserter.start();
167                 synchronized (lockObject) {
168                         while (!finished) {
169                                 try {
170                                         lockObject.wait();
171                                 } catch (InterruptedException e) {
172                                 }
173                         }
174                 }
175                 return success;
176         }
177
178         //
179         // INTERFACE InsertListener
180         //
181
182         /**
183          * {@inheritDoc}
184          */
185         public void projectInsertStarted(Project project) {
186                 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
187         }
188
189         public void projectURIGenerated(Project project, String uri) {
190                 outputWriter.println("URI: " + uri);
191         }
192
193         /**
194          * {@inheritDoc}
195          */
196         public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
197                 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
198         }
199
200         /**
201          * {@inheritDoc}
202          */
203         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
204                 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
205                 finished = true;
206                 if (success) {
207                         if (project instanceof EditionProject) {
208                                 ((EditionProject) project).setEdition(((EditionProject) project).getEdition() + 1);
209                         }
210                 }
211                 this.success = success;
212                 synchronized (lockObject) {
213                         lockObject.notify();
214                 }
215         }
216
217         //
218         // MAIN
219         //
220
221         public static void main(String[] args) {
222                 new CLI(args);
223         }
224
225 }