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