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