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.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;
31 * @author David Roden <droden@gmail.com>
34 public class CLI implements InsertListener {
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();
41 private Project[] projects;
42 private boolean finished = false;
43 private boolean success;
45 private CLI(String[] args) {
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.");
60 Configuration configuration = new Configuration();
61 if (!configuration.createLockFile()) {
62 outputWriter.println("Lock file found!");
66 projectInserter.addInsertListener(this);
67 projects = configuration.getProjects();
68 Node node = configuration.getSelectedNode();
69 nodes = configuration.getNodes();
71 freenetInterface = new Freenet7Interface();
72 freenetInterface.setNode(node);
74 projectInserter.setFreenetInterface(freenetInterface);
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.");
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.");
92 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
94 currentProject = null;
96 currentProject = getProject(value);
97 if (currentProject == null) {
98 outputWriter.println("Project \"" + value + "\" not found.");
100 } else if (argument.startsWith("--local-directory")) {
101 if (currentProject == null) {
102 outputWriter.println("You can't specifiy --local-directory before --project.");
105 currentProject.setLocalPath(value);
106 } else if (argument.startsWith("--path=")) {
107 if (currentProject == null) {
108 outputWriter.println("You can't specify --path before --project.");
111 currentProject.setPath(value);
112 } else if (argument.startsWith("--edition=")) {
113 if (currentProject == null) {
114 outputWriter.println("You can't specify --edition before --project.");
117 currentProject.setEdition(Integer.parseInt(value));
119 outputWriter.println("Unknown parameter: " + argument);
124 if (currentProject != null) {
125 if (insertProject(currentProject)) {
126 outputWriter.println("Project \"" + currentProject.getName() + "\" successfully inserted.");
128 outputWriter.println("Project \"" + currentProject.getName() + "\" was not successfully inserted.");
132 configuration.setProjects(projects);
133 configuration.save();
136 private Project getProject(String name) {
137 for (Project project: projects) {
138 if (project.getName().equals(name)) {
145 private Node getNode(String name) {
146 for (Node node: nodes) {
147 if (node.getName().equals(name)) {
154 private boolean insertProject(Project currentProject) {
155 if (!freenetInterface.hasNode()) {
156 outputWriter.println("Node is not running!");
159 projectInserter.setProject(currentProject);
160 projectInserter.start();
161 synchronized (lockObject) {
165 } catch (InterruptedException e) {
173 // INTERFACE InsertListener
179 public void projectInsertStarted(Project project) {
180 outputWriter.println("Starting Insert of project \"" + project.getName() + "\".");
183 public void projectURIGenerated(Project project, String uri) {
184 outputWriter.println("URI: " + uri);
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) + "%");
197 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
198 outputWriter.println("Request URI: " + project.getFinalRequestURI(0));
200 this.success = success;
201 synchronized (lockObject) {
210 public static void main(String[] args) {