]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/tool/tzu/CLILoader.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / tool / tzu / CLILoader.java
1 /*\r
2  * ******************************************************************************\r
3  * Copyright (C) 2007, International Business Machines Corporation and others.\r
4  * All Rights Reserved.\r
5  * ******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.tool.tzu;\r
8 \r
9 import java.io.File;\r
10 import java.io.FileNotFoundException;\r
11 import java.io.IOException;\r
12 \r
13 /**\r
14  * Loads the ICUTZU tool, command-line version.\r
15  */\r
16 public class CLILoader {\r
17     /**\r
18      * The filename of the log file in patch mode.\r
19      */\r
20     public static final String LOG_FILENAME_PATCH = "icutzu_patch.log";\r
21 \r
22     /**\r
23      * The filename of the log file in discovery only mode.\r
24      */\r
25     public static final String LOG_FILENAME_DISCOVERYONLY = "icutzu_discovery.log";\r
26 \r
27     /**\r
28      * The backup directory to use in patch mode, or null if there should be no backups.\r
29      */\r
30     private File backupDir = null;\r
31 \r
32     /**\r
33      * The current directory.\r
34      */\r
35     private File curDir = null;\r
36 \r
37     /**\r
38      * A logger that manages both output to the screen and to the log file.\r
39      */\r
40     private Logger logger;\r
41 \r
42     /**\r
43      * The file that stores the path list.\r
44      */\r
45     private File pathFile = null;\r
46 \r
47     /**\r
48      * A glorified list of IncludePath objects representing the list of directories used in\r
49      * discovery mode.\r
50      */\r
51     private PathModel pathModel;\r
52 \r
53     /**\r
54      * The file that stores the result list.\r
55      */\r
56     private File resultFile = null;\r
57 \r
58     /**\r
59      * A glorified list of ICUFile objects representing the ICU4J jars found in discovery mode and\r
60      * used in patch mode.\r
61      */\r
62     private ResultModel resultModel;\r
63 \r
64     /**\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
67      */\r
68     private SourceModel sourceModel;\r
69 \r
70     /**\r
71      * The local timezone resource file.\r
72      */\r
73     private File tzFile = null;\r
74 \r
75     /**\r
76      * Entry point for the command-line version of the tool.\r
77      * \r
78      * @param curDir\r
79      *            The base directory of the tool.\r
80      * @param backupDir\r
81      *            The location to store backups.\r
82      * @param pathFile\r
83      *            The file to load paths from.\r
84      * @param resultFile\r
85      *            The file to load/save results to/from.\r
86      * @param tzFile\r
87      *            The local timezone resource file.\r
88      */\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
95 \r
96         // create the logger based on the silentpatch option\r
97         try {\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
101             System.exit(-1);\r
102         }\r
103 \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
109 \r
110         // if discoveryonly is enabled, call the search method\r
111         // otherwise, call the update method\r
112         try {\r
113             if (discoverOnly)\r
114                 search();\r
115             else\r
116                 update();\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
123         }\r
124     }\r
125 \r
126     /**\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
129      * \r
130      * @throws IOException\r
131      * @throws IllegalArgumentException\r
132      * @throws InterruptedException\r
133      */\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
145 \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
149 \r
150         // load paths stored in PathModel.PATHLIST_FILENAME\r
151         pathModel.loadPaths();\r
152 \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
156         // command-line)\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
162 \r
163         // save the results in PathModel.RESULTLIST_FILENAME\r
164         resultModel.saveResults();\r
165 \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
172     }\r
173 \r
174     /**\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
177      * \r
178      * @throws IOException\r
179      * @throws IllegalArgumentException\r
180      * @throws InterruptedException\r
181      */\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
190 \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
194 \r
195         // load the results from the result list file\r
196         resultModel.loadResults();\r
197 \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
202 \r
203         // perform the updates using the best tz version available\r
204         resultModel.updateAll(sourceModel.getURL(sourceModel.getSelectedItem()), backupDir);\r
205 \r
206         logger.printlnToScreen("");\r
207         logger.printlnToScreen("*********** Command-Line \'Patch\'" + " Mode Ended ***********");\r
208         logger.printlnToScreen("");\r
209     }\r
210 }\r