jSite: First commit : verion 4.0 (written by Bombe)
[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: CLI.java 418 2006-03-29 17:49:16Z bombe $
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 (int argumentIndex = 0, argumentSize = args.length; argumentIndex < argumentSize; argumentIndex++) {
79                         String argument = args[argumentIndex];
80                         String value = argument.substring(argument.indexOf('=') + 1).trim();
81                         if (argument.startsWith("--node=")) {
82                                 Node newNode = getNode(value);
83                                 if (newNode == null) {
84                                         outputWriter.println("Node \"" + value + "\" not found.");
85                                         return;
86                                 }
87                                 node = newNode;
88                                 freenetInterface.setNode(node);
89                         } else if (argument.startsWith("--project=")) {
90                                 if (currentProject != null) {
91                                         if (insertProject(currentProject)) {
92                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
93                                         } else {
94                                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
95                                         }
96                                         currentProject = null;
97                                 }
98                                 currentProject = getProject(value);
99                                 if (currentProject == null) {
100                                         outputWriter.println("Project \"" + value + "\" not found.");
101                                 }
102                         } else if (argument.startsWith("--local-directory")) {
103                                 if (currentProject == null) {
104                                         outputWriter.println("You can't specifiy --local-directory before --project.");
105                                         return;
106                                 }
107                                 currentProject.setLocalPath(value);
108                         } else if (argument.startsWith("--path=")) {
109                                 if (currentProject == null) {
110                                         outputWriter.println("You can't specify --path before --project.");
111                                         return;
112                                 }
113                                 currentProject.setPath(value);
114                         } else if (argument.startsWith("--edition=")) {
115                                 if (currentProject == null) {
116                                         outputWriter.println("You can't specify --edition before --project.");
117                                         return;
118                                 }
119                                 if (currentProject instanceof EditionProject) {
120                                         ((EditionProject) currentProject).setEdition(Integer.parseInt(value));
121                                 } else {
122                                         outputWriter.println("Project \"" + currentProject.getName() + "\" is not an edition-based project.");
123                                         return;
124                                 }
125                         } else {
126                                 outputWriter.println("Unknown parameter: " + argument);
127                                 return;
128                         }
129                 }
130
131                 if (currentProject != null) {
132                         if (insertProject(currentProject)) {
133                                 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
134                         } else {
135                                 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
136                         }
137                 }
138
139                 configuration.setProjects(projects);
140                 configuration.save();
141         }
142
143         private Project getProject(String name) {
144                 for (Project project: projects) {
145                         if (project.getName().equals(name)) {
146                                 return project;
147                         }
148                 }
149                 return null;
150         }
151
152         private Node getNode(String name) {
153                 for (Node node: nodes) {
154                         if (node.getName().equals(name)) {
155                                 return node;
156                         }
157                 }
158                 return null;
159         }
160
161         private boolean insertProject(Project currentProject) {
162                 if (!freenetInterface.hasNode()) {
163                         outputWriter.println("Node is not running!");
164                         return false;
165                 }
166                 projectInserter.setProject(currentProject);
167                 projectInserter.start();
168                 synchronized (lockObject) {
169                         while (!finished) {
170                                 try {
171                                         lockObject.wait();
172                                 } catch (InterruptedException e) {
173                                 }
174                         }
175                 }
176                 return success;
177         }
178
179         //
180         // INTERFACE InsertListener
181         //
182
183         /**
184          * {@inheritDoc}
185          */
186         public void projectInsertStarted(Project project) {
187                 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
188         }
189
190         /**
191          * {@inheritDoc}
192          */
193         public void projectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
194                 outputWriter.println("Progress: " + succeeded + " done, " + failed + " failed, " + fatal + " fatal, " + total + " total" + (finalized ? " (finalized)" : "") + ", " + ((succeeded + failed + fatal) * 100 / total) + "%");
195         }
196
197         /**
198          * {@inheritDoc}
199          */
200         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
201                 outputWriter.println("Request URI: " + project.getFinalURI(0));
202                 finished = true;
203                 if (success) {
204                         if (project instanceof EditionProject) {
205                                 ((EditionProject) project).setEdition(((EditionProject) project).getEdition() + 1);
206                         }
207                 }
208                 this.success = success;
209                 synchronized (lockObject) {
210                         lockObject.notify();
211                 }
212         }
213
214         //
215         // MAIN
216         //
217
218         public static void main(String[] args) {
219                 new CLI(args);
220         }
221
222 }