2 * ******************************************************************************
\r
3 * Copyright (C) 2007, International Business Machines Corporation and others.
\r
4 * All Rights Reserved.
\r
5 * ******************************************************************************
\r
7 package com.ibm.icu.dev.tool.tzu;
\r
10 import java.io.FileNotFoundException;
\r
11 import java.io.IOException;
\r
14 * Loads the ICUTZU tool, command-line version.
\r
16 public class CLILoader {
\r
18 * The filename of the log file in patch mode.
\r
20 public static final String LOG_FILENAME_PATCH = "icutzu_patch.log";
\r
23 * The filename of the log file in discovery only mode.
\r
25 public static final String LOG_FILENAME_DISCOVERYONLY = "icutzu_discovery.log";
\r
28 * The backup directory to use in patch mode, or null if there should be no backups.
\r
30 private File backupDir = null;
\r
33 * The current directory.
\r
35 private File curDir = null;
\r
38 * A logger that manages both output to the screen and to the log file.
\r
40 private Logger logger;
\r
43 * The file that stores the path list.
\r
45 private File pathFile = null;
\r
48 * A glorified list of IncludePath objects representing the list of directories used in
\r
51 private PathModel pathModel;
\r
54 * The file that stores the result list.
\r
56 private File resultFile = null;
\r
59 * A glorified list of ICUFile objects representing the ICU4J jars found in discovery mode and
\r
60 * used in patch mode.
\r
62 private ResultModel resultModel;
\r
65 * A glorified map of Strings to URLs populated by parsing the directory structure in the ICU
\r
66 * Time Zone repository online and used in patch mode.
\r
68 private SourceModel sourceModel;
\r
71 * The local timezone resource file.
\r
73 private File tzFile = null;
\r
76 * Entry point for the command-line version of the tool.
\r
79 * The base directory of the tool.
\r
81 * The location to store backups.
\r
83 * The file to load paths from.
\r
85 * The file to load/save results to/from.
\r
87 * The local timezone resource file.
\r
89 public CLILoader(File curDir, File backupDir, File pathFile, File resultFile, File tzFile) {
\r
90 // determine whether we are running in discover only mode or patch mode
\r
91 boolean discoverOnly = "true".equalsIgnoreCase(System.getProperty("discoveronly"));
\r
92 boolean silentPatch = "true".equalsIgnoreCase(System.getProperty("silentpatch"));
\r
93 File logFile = new File(curDir.getPath(), (discoverOnly ? "icutzu_discover.log"
\r
94 : "icutzu_patch.log"));
\r
96 // create the logger based on the silentpatch option
\r
98 this.logger = Logger.getInstance(logFile, silentPatch ? Logger.QUIET : Logger.NORMAL);
\r
99 } catch (FileNotFoundException ex) {
\r
100 System.out.println("Could not open " + logFile.getPath() + " for writing.");
\r
104 this.pathFile = pathFile;
\r
105 this.resultFile = resultFile;
\r
106 this.tzFile = tzFile;
\r
107 this.backupDir = backupDir;
\r
108 this.curDir = curDir;
\r
110 // if discoveryonly is enabled, call the search method
\r
111 // otherwise, call the update method
\r
117 } catch (IllegalArgumentException ex) {
\r
118 logger.errorln(ex.getMessage());
\r
119 } catch (IOException ex) {
\r
120 logger.errorln(ex.getMessage());
\r
121 } catch (InterruptedException ex) {
\r
122 logger.errorln(ex.getMessage());
\r
127 * Discover Only Mode. Load the path list from the path file and save the path of each updatable
\r
128 * ICU jar files it finds to the result list
\r
130 * @throws IOException
\r
131 * @throws IllegalArgumentException
\r
132 * @throws InterruptedException
\r
134 private void search() throws IOException, IllegalArgumentException, InterruptedException {
\r
135 logger.printlnToScreen("");
\r
136 logger.printlnToScreen("*********** Command-Line \'Discover Only\'"
\r
137 + " Mode Started ***********");
\r
138 logger.printlnToScreen("");
\r
139 logger.printlnToScreen("\'Discover Only\' Mode:");
\r
140 logger.printlnToScreen("In this mode, " + "the tool will search for ICU4J jars"
\r
141 + " in the directories specified in DirectorySearch.txt"
\r
142 + " and print the ICU4J jars detected and their respective"
\r
143 + " time zone version to the file ICUList.txt");
\r
144 logger.printlnToScreen("");
\r
146 // initialize the result model and the path model
\r
147 resultModel = new ResultModel(logger, resultFile);
\r
148 pathModel = new PathModel(logger, pathFile);
\r
150 // load paths stored in PathModel.PATHLIST_FILENAME
\r
151 pathModel.loadPaths();
\r
153 // perform the search, putting the the results in the result model,
\r
154 // searching all subdirectories of the included path, using the backup
\r
155 // directory specified, and without using a status bar (since this is
\r
157 logger.printlnToScreen("");
\r
158 logger.printlnToScreen("Search started.");
\r
159 pathModel.searchAll(resultModel, true, curDir, backupDir);
\r
160 logger.printlnToScreen("Search ended.");
\r
161 logger.printlnToScreen("");
\r
163 // save the results in PathModel.RESULTLIST_FILENAME
\r
164 resultModel.saveResults();
\r
166 logger.printlnToScreen("");
\r
167 logger.printlnToScreen("Please run with DISCOVERYONLY=false"
\r
168 + " in order to update ICU4J jars listed in ICUList.txt");
\r
169 logger.printlnToScreen("*********** Command-Line \'Discover Only\'"
\r
170 + " Mode Ended ***********");
\r
171 logger.printlnToScreen("");
\r
175 * Patch Mode. Load all the results from resultFile, populate the tz version list via the web,
\r
176 * and update all the results with the best available Time Zone data.
\r
178 * @throws IOException
\r
179 * @throws IllegalArgumentException
\r
180 * @throws InterruptedException
\r
182 private void update() throws IOException, IllegalArgumentException, InterruptedException {
\r
183 logger.printlnToScreen("");
\r
184 logger.printlnToScreen("*********** Command-Line \'Patch\'" + " Mode Started ***********");
\r
185 logger.printlnToScreen("");
\r
186 logger.printlnToScreen("\'Patch\' Mode:");
\r
187 logger.printlnToScreen("In this mode, the tool patches each of the"
\r
188 + " ICU4J jars listed in ICUList.txt with the new time zone" + " information.");
\r
189 logger.printlnToScreen("");
\r
191 // initialize the result model and the source model
\r
192 resultModel = new ResultModel(logger, resultFile);
\r
193 sourceModel = new SourceModel(logger, tzFile);
\r
195 // load the results from the result list file
\r
196 resultModel.loadResults();
\r
198 // if the offline is not set to true, populate the list of available
\r
199 // timezone resource versions
\r
200 if (!"true".equalsIgnoreCase(System.getProperty("offline")))
\r
201 sourceModel.findSources();
\r
203 // perform the updates using the best tz version available
\r
204 resultModel.updateAll(sourceModel.getURL(sourceModel.getSelectedItem()), backupDir);
\r
206 logger.printlnToScreen("");
\r
207 logger.printlnToScreen("*********** Command-Line \'Patch\'" + " Mode Ended ***********");
\r
208 logger.printlnToScreen("");
\r