3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.main;
22 import java.io.PrintWriter;
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;
32 * @author David Roden <droden@gmail.com>
35 public class CLI implements InsertListener {
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();
42 private Project[] projects;
43 private boolean finished = false;
44 private boolean success;
46 private CLI(String[] args) {
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.");
61 Configuration configuration = new Configuration();
62 if (!configuration.createLockFile()) {
63 outputWriter.println("Lock file found!");
67 projectInserter.addInsertListener(this);
68 projects = configuration.getProjects();
69 Node node = configuration.getSelectedNode();
70 nodes = configuration.getNodes();
72 freenetInterface = new Freenet7Interface();
73 freenetInterface.setNode(node);
75 projectInserter.setFreenetInterface(freenetInterface);
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.");
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.");
93 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
95 currentProject = null;
97 currentProject = getProject(value);
98 if (currentProject == null) {
99 outputWriter.println("Project \"" + value + "\" not found.");
101 } else if (argument.startsWith("--local-directory")) {
102 if (currentProject == null) {
103 outputWriter.println("You can't specifiy --local-directory before --project.");
106 currentProject.setLocalPath(value);
107 } else if (argument.startsWith("--path=")) {
108 if (currentProject == null) {
109 outputWriter.println("You can't specify --path before --project.");
112 currentProject.setPath(value);
113 } else if (argument.startsWith("--edition=")) {
114 if (currentProject == null) {
115 outputWriter.println("You can't specify --edition before --project.");
118 if (currentProject instanceof EditionProject) {
119 ((EditionProject) currentProject).setEdition(Integer.parseInt(value));
121 outputWriter.println("Project \"" + currentProject.getName() + "\" is not an edition-based project.");
125 outputWriter.println("Unknown parameter: " + argument);
130 if (currentProject != null) {
131 if (insertProject(currentProject)) {
132 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
134 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
138 configuration.setProjects(projects);
139 configuration.save();
142 private Project getProject(String name) {
143 for (Project project: projects) {
144 if (project.getName().equals(name)) {
151 private Node getNode(String name) {
152 for (Node node: nodes) {
153 if (node.getName().equals(name)) {
160 private boolean insertProject(Project currentProject) {
161 if (!freenetInterface.hasNode()) {
162 outputWriter.println("Node is not running!");
165 projectInserter.setProject(currentProject);
166 projectInserter.start();
167 synchronized (lockObject) {
171 } catch (InterruptedException e) {
179 // INTERFACE InsertListener
185 public void projectInsertStarted(Project project) {
186 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
189 public void projectURIGenerated(Project project, String uri) {
190 outputWriter.println("URI: " + uri);
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) + "%");
203 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
204 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
207 if (project instanceof EditionProject) {
208 ((EditionProject) project).setEdition(((EditionProject) project).getEdition() + 1);
211 this.success = success;
212 synchronized (lockObject) {
221 public static void main(String[] args) {