5601329319d6cc7796d9b048f6838307a3c8cd14
[jSite.git] / src / de / todesbaum / jsite / main / Main.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
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.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.File;
25 import java.io.IOException;
26 import java.text.MessageFormat;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.Map.Entry;
33
34 import javax.swing.AbstractAction;
35 import javax.swing.Action;
36 import javax.swing.ButtonGroup;
37 import javax.swing.Icon;
38 import javax.swing.JList;
39 import javax.swing.JMenu;
40 import javax.swing.JMenuBar;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPanel;
43 import javax.swing.JRadioButtonMenuItem;
44 import javax.swing.event.ListSelectionEvent;
45 import javax.swing.event.ListSelectionListener;
46
47 import de.todesbaum.jsite.application.FileOption;
48 import de.todesbaum.jsite.application.Freenet7Interface;
49 import de.todesbaum.jsite.application.Node;
50 import de.todesbaum.jsite.application.Project;
51 import de.todesbaum.jsite.gui.NodeManagerListener;
52 import de.todesbaum.jsite.gui.NodeManagerPage;
53 import de.todesbaum.jsite.gui.ProjectFilesPage;
54 import de.todesbaum.jsite.gui.ProjectInsertPage;
55 import de.todesbaum.jsite.gui.ProjectPage;
56 import de.todesbaum.jsite.gui.UpdateChecker;
57 import de.todesbaum.jsite.gui.UpdateListener;
58 import de.todesbaum.jsite.i18n.I18n;
59 import de.todesbaum.jsite.i18n.I18nContainer;
60 import de.todesbaum.util.image.IconLoader;
61 import de.todesbaum.util.swing.TWizard;
62 import de.todesbaum.util.swing.TWizardPage;
63 import de.todesbaum.util.swing.WizardListener;
64
65 /**
66  * The main class that ties together everything.
67  *
68  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
69  */
70 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
71
72         /** Whether the debug mode is activated. */
73         private static boolean debug = false;
74
75         /** The version. */
76         private static final Version VERSION = new Version(0, 6, 2);
77
78         /** The configuration. */
79         private Configuration configuration;
80
81         /** The freenet interface. */
82         private Freenet7Interface freenetInterface = new Freenet7Interface();
83
84         /** The update checker. */
85         private final UpdateChecker updateChecker;
86
87         /** The jSite icon. */
88         private Icon jSiteIcon;
89
90         /**
91          * Enumeration for all possible pages.
92          *
93          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
94          */
95         private static enum PageType {
96
97                 /** The node manager page. */
98                 PAGE_NODE_MANAGER,
99
100                 /** The project page. */
101                 PAGE_PROJECTS,
102
103                 /** The project files page. */
104                 PAGE_PROJECT_FILES,
105
106                 /** The project insert page. */
107                 PAGE_INSERT_PROJECT
108
109         }
110
111         /** The supported locales. */
112         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
113
114         /** The actions that switch the language. */
115         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
116
117         /** The “manage nodes” action. */
118         private Action manageNodeAction;
119
120         /** The “check for updates” action. */
121         private Action checkForUpdatesAction;
122
123         /** The “about jSite” action. */
124         private Action aboutAction;
125
126         /** The wizard. */
127         private TWizard wizard;
128
129         /** The node menu. */
130         private JMenu nodeMenu;
131
132         /** The currently selected node. */
133         private Node selectedNode;
134
135         /** Mapping from page type to page. */
136         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
137
138         /**
139          * Creates a new core with the default configuration file.
140          */
141         private Main() {
142                 this(null);
143         }
144
145         /**
146          * Creates a new core with the given configuration from the given file.
147          *
148          * @param configFilename
149          *            The name of the configuration file
150          */
151         private Main(String configFilename) {
152                 if (configFilename != null) {
153                         configuration = new Configuration(configFilename);
154                 } else {
155                         configuration = new Configuration();
156                 }
157                 Locale.setDefault(configuration.getLocale());
158                 I18n.setLocale(configuration.getLocale());
159                 if (!configuration.createLockFile()) {
160                         int option = JOptionPane.showOptionDialog(null, I18n.getMessage("jsite.main.already-running"), "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { I18n.getMessage("jsite.main.already-running.override"), I18n.getMessage("jsite.wizard.quit") }, I18n.getMessage("jsite.wizard.quit"));
161                         if (option != 0) {
162                                 throw new IllegalStateException("Lockfile override not active, refusing start.");
163                         }
164                         configuration.removeLockfileOnExit();
165                 }
166                 wizard = new TWizard();
167                 createActions();
168                 wizard.setJMenuBar(createMenuBar());
169                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
170                 wizard.setPreviousEnabled(false);
171                 wizard.setNextEnabled(true);
172                 wizard.addWizardListener(this);
173                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
174                 wizard.setIcon(jSiteIcon);
175
176                 updateChecker = new UpdateChecker(wizard, freenetInterface);
177                 updateChecker.addUpdateListener(this);
178                 updateChecker.start();
179
180                 initPages();
181                 showPage(PageType.PAGE_PROJECTS);
182         }
183
184         /**
185          * Creates all actions.
186          */
187         private void createActions() {
188                 for (final Locale locale : SUPPORTED_LOCALES) {
189                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
190
191                                 @SuppressWarnings("synthetic-access")
192                                 public void actionPerformed(ActionEvent actionEvent) {
193                                         switchLanguage(locale);
194                                 }
195                         });
196                 }
197                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
198
199                         @SuppressWarnings("synthetic-access")
200                         public void actionPerformed(ActionEvent actionEvent) {
201                                 showPage(PageType.PAGE_NODE_MANAGER);
202                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
203                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
204                         }
205                 };
206                 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
207
208                         /**
209                          * {@inheritDoc}
210                          */
211                         public void actionPerformed(ActionEvent actionEvent) {
212                                 /* TODO */
213                         }
214                 };
215                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
216
217                         @SuppressWarnings("synthetic-access")
218                         public void actionPerformed(ActionEvent e) {
219                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
220                         }
221                 };
222
223                 I18nContainer.getInstance().registerRunnable(new Runnable() {
224
225                         @SuppressWarnings("synthetic-access")
226                         public void run() {
227                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
228                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
229                         }
230                 });
231         }
232
233         /**
234          * Creates the menu bar.
235          *
236          * @return The menu bar
237          */
238         private JMenuBar createMenuBar() {
239                 JMenuBar menuBar = new JMenuBar();
240                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
241                 menuBar.add(languageMenu);
242                 ButtonGroup languageButtonGroup = new ButtonGroup();
243                 for (Locale locale : SUPPORTED_LOCALES) {
244                         Action languageAction = languageActions.get(locale);
245                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
246                         if (locale.equals(Locale.getDefault())) {
247                                 menuItem.setSelected(true);
248                         }
249                         languageAction.putValue("menuItem", menuItem);
250                         languageButtonGroup.add(menuItem);
251                         languageMenu.add(menuItem);
252                 }
253                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
254                 menuBar.add(nodeMenu);
255                 selectedNode = configuration.getSelectedNode();
256                 nodesUpdated(configuration.getNodes());
257
258                 /* evil hack to right-align the help menu */
259                 JPanel panel = new JPanel();
260                 panel.setOpaque(false);
261                 menuBar.add(panel);
262
263                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
264                 menuBar.add(helpMenu);
265                 helpMenu.add(checkForUpdatesAction);
266                 helpMenu.add(aboutAction);
267
268                 I18nContainer.getInstance().registerRunnable(new Runnable() {
269
270                         @SuppressWarnings("synthetic-access")
271                         public void run() {
272                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
273                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
274                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
275                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
276                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
277                                 }
278                         }
279                 });
280
281                 return menuBar;
282         }
283
284         /**
285          * Initializes all pages.
286          */
287         private void initPages() {
288                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
289                 nodeManagerPage.setName("page.node-manager");
290                 nodeManagerPage.addNodeManagerListener(this);
291                 nodeManagerPage.setNodes(configuration.getNodes());
292                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
293
294                 ProjectPage projectPage = new ProjectPage(wizard);
295                 projectPage.setName("page.project");
296                 projectPage.setProjects(configuration.getProjects());
297                 projectPage.setFreenetInterface(freenetInterface);
298                 projectPage.addListSelectionListener(this);
299                 pages.put(PageType.PAGE_PROJECTS, projectPage);
300
301                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
302                 projectFilesPage.setName("page.project.files");
303                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
304
305                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
306                 projectInsertPage.setDebug(debug);
307                 projectInsertPage.setName("page.project.insert");
308                 projectInsertPage.setFreenetInterface(freenetInterface);
309                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
310         }
311
312         /**
313          * Shows the page with the given type.
314          *
315          * @param pageType
316          *            The page type to show
317          */
318         private void showPage(PageType pageType) {
319                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
320                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
321                 wizard.setPage(pages.get(pageType));
322                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
323         }
324
325         /**
326          * Saves the configuration.
327          *
328          * @return <code>true</code> if the configuration could be saved,
329          *         <code>false</code> otherwise
330          */
331         private boolean saveConfiguration() {
332                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
333                 configuration.setNodes(nodeManagerPage.getNodes());
334                 if (selectedNode != null) {
335                         configuration.setSelectedNode(selectedNode);
336                 }
337
338                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
339                 configuration.setProjects(projectPage.getProjects());
340
341                 return configuration.save();
342         }
343
344         /**
345          * Finds a supported locale for the given locale.
346          *
347          * @param forLocale
348          *            The locale to find a supported locale for
349          * @return The supported locale that was found, or the default locale if no
350          *         supported locale could be found
351          */
352         private Locale findSupportedLocale(Locale forLocale) {
353                 for (Locale locale : SUPPORTED_LOCALES) {
354                         if (locale.equals(forLocale)) {
355                                 return locale;
356                         }
357                 }
358                 for (Locale locale : SUPPORTED_LOCALES) {
359                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
360                                 return locale;
361                         }
362                 }
363                 for (Locale locale : SUPPORTED_LOCALES) {
364                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
365                                 return locale;
366                         }
367                 }
368                 return SUPPORTED_LOCALES[0];
369         }
370
371         /**
372          * Returns the version.
373          *
374          * @return The version
375          */
376         public static final Version getVersion() {
377                 return VERSION;
378         }
379
380         //
381         // ACTIONS
382         //
383
384         /**
385          * Switches the language of the interface to the given locale.
386          *
387          * @param locale
388          *            The locale to switch to
389          */
390         private void switchLanguage(Locale locale) {
391                 Locale supportedLocale = findSupportedLocale(locale);
392                 Action languageAction = languageActions.get(supportedLocale);
393                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
394                 menuItem.setSelected(true);
395                 I18n.setLocale(supportedLocale);
396                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
397                         try {
398                                 i18nRunnable.run();
399                         } catch (Throwable t) {
400                                 /* we probably shouldn't swallow this. */
401                         }
402                 }
403                 wizard.setPage(wizard.getPage());
404                 configuration.setLocale(supportedLocale);
405         }
406
407         //
408         // INTERFACE ListSelectionListener
409         //
410
411         /**
412          * {@inheritDoc}
413          */
414         public void valueChanged(ListSelectionEvent e) {
415                 JList list = (JList) e.getSource();
416                 int selectedRow = list.getSelectedIndex();
417                 wizard.setNextEnabled(selectedRow > -1);
418         }
419
420         //
421         // INTERFACE WizardListener
422         //
423
424         /**
425          * {@inheritDoc}
426          */
427         public void wizardNextPressed(TWizard wizard) {
428                 String pageName = wizard.getPage().getName();
429                 if ("page.node-manager".equals(pageName)) {
430                         showPage(PageType.PAGE_PROJECTS);
431                 } else if ("page.project".equals(pageName)) {
432                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
433                         Project project = projectPage.getSelectedProject();
434                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
435                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
436                                 return;
437                         }
438                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
439                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
440                                 return;
441                         }
442                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
443                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
444                         showPage(PageType.PAGE_PROJECT_FILES);
445                 } else if ("page.project.files".equals(pageName)) {
446                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
447                         Project project = projectPage.getSelectedProject();
448                         if (selectedNode == null) {
449                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
450                                 return;
451                         }
452                         if (project.getIndexFile() == null) {
453                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
454                                         return;
455                                 }
456                         } else {
457                                 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
458                                 if (!indexFile.exists()) {
459                                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
460                                         return;
461                                 }
462                         }
463                         String indexFile = project.getIndexFile();
464                         boolean hasIndexFile = (indexFile != null);
465                         if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
466                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
467                                         return;
468                                 }
469                         }
470                         if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
471                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
472                                         return;
473                                 }
474                         }
475                         Map<String, FileOption> fileOptions = project.getFileOptions();
476                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
477                         for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
478                                 FileOption fileOption = fileOptionEntry.getValue();
479                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
480                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
481                                         return;
482                                 }
483                         }
484                         boolean nodeRunning = false;
485                         try {
486                                 nodeRunning = freenetInterface.isNodePresent();
487                         } catch (IOException e) {
488                                 /* ignore. */
489                         }
490                         if (!nodeRunning) {
491                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
492                                 return;
493                         }
494                         configuration.save();
495                         showPage(PageType.PAGE_INSERT_PROJECT);
496                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
497                         nodeMenu.setEnabled(false);
498                 } else if ("page.project.insert".equals(pageName)) {
499                         showPage(PageType.PAGE_PROJECTS);
500                         nodeMenu.setEnabled(true);
501                 }
502         }
503
504         /**
505          * {@inheritDoc}
506          */
507         public void wizardPreviousPressed(TWizard wizard) {
508                 String pageName = wizard.getPage().getName();
509                 if ("page.project".equals(pageName)) {
510                         showPage(PageType.PAGE_NODE_MANAGER);
511                 } else if ("page.project.files".equals(pageName)) {
512                         showPage(PageType.PAGE_PROJECTS);
513                 } else if ("page.project.insert".equals(pageName)) {
514                         showPage(PageType.PAGE_PROJECT_FILES);
515                 }
516         }
517
518         /**
519          * {@inheritDoc}
520          */
521         public void wizardQuitPressed(TWizard wizard) {
522                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
523                         if (saveConfiguration()) {
524                                 System.exit(0);
525                         }
526                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
527                                 System.exit(0);
528                         }
529                 }
530         }
531
532         //
533         // INTERFACE NodeManagerListener
534         //
535
536         /**
537          * {@inheritDoc}
538          */
539         public void nodesUpdated(Node[] nodes) {
540                 nodeMenu.removeAll();
541                 ButtonGroup nodeButtonGroup = new ButtonGroup();
542                 Node newSelectedNode = null;
543                 for (Node node : nodes) {
544                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
545                         nodeMenuItem.putClientProperty("Node", node);
546                         nodeMenuItem.addActionListener(this);
547                         nodeButtonGroup.add(nodeMenuItem);
548                         if (node.equals(selectedNode)) {
549                                 newSelectedNode = node;
550                                 nodeMenuItem.setSelected(true);
551                         }
552                         nodeMenu.add(nodeMenuItem);
553                 }
554                 nodeMenu.addSeparator();
555                 nodeMenu.add(manageNodeAction);
556                 selectedNode = newSelectedNode;
557                 freenetInterface.setNode(selectedNode);
558         }
559
560         /**
561          * {@inheritDoc}
562          */
563         public void actionPerformed(ActionEvent e) {
564                 Object source = e.getSource();
565                 if (source instanceof JRadioButtonMenuItem) {
566                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
567                         Node node = (Node) menuItem.getClientProperty("Node");
568                         selectedNode = node;
569                         freenetInterface.setNode(selectedNode);
570                 }
571         }
572
573         //
574         // INTERFACE UpdateListener
575         //
576
577         /**
578          * {@inheritDoc}
579          */
580         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
581                 if (foundVersion.compareTo(VERSION) > 0) {
582                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.found-version.message"), foundVersion.toString(), new Date(versionTimestamp)), I18n.getMessage("jsite.update-checker.found-version.title"), JOptionPane.INFORMATION_MESSAGE);
583                 }
584         }
585
586         //
587         // MAIN METHOD
588         //
589
590         /**
591          * Main method that is called by the VM.
592          *
593          * @param args
594          *            The command-line arguments
595          */
596         public static void main(String[] args) {
597                 String configFilename = null;
598                 boolean nextIsConfigFilename = false;
599                 for (String argument : args) {
600                         if (nextIsConfigFilename) {
601                                 configFilename = argument;
602                                 nextIsConfigFilename = false;
603                         }
604                         if ("--help".equals(argument)) {
605                                 printHelp();
606                                 return;
607                         } else if ("--debug".equals(argument)) {
608                                 debug = true;
609                         } else if ("--config-file".equals(argument)) {
610                                 nextIsConfigFilename = true;
611                         }
612                 }
613                 if (nextIsConfigFilename) {
614                         System.out.println("--config-file needs parameter!");
615                         return;
616                 }
617                 new Main(configFilename);
618         }
619
620         /**
621          * Prints a small syntax help.
622          */
623         private static void printHelp() {
624                 System.out.println("--help\tshows this cruft");
625                 System.out.println("--debug\tenables some debug output");
626                 System.out.println("--config-file <file>\tuse specified configuration file");
627         }
628
629 }