KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > server > AdminClient


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.server;
26
27 import org.snipsnap.config.ServerConfiguration;
28 import org.snipsnap.util.JDBCDatabaseExport;
29 import org.snipsnap.util.LocaleComparator;
30 import org.snipsnap.util.XMLSnipRepair;
31
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.BufferedReader JavaDoc;
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FilenameFilter JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41 import java.text.NumberFormat JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Arrays JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Properties JavaDoc;
47 import java.util.TreeSet JavaDoc;
48 import java.util.Vector JavaDoc;
49 import java.util.prefs.Preferences JavaDoc;
50
51 public class AdminClient {
52
53   public static void main(String JavaDoc args[]) {
54     printCopyright();
55     Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
56
57     List JavaDoc commands = parseOptions(args, serverPrefs);
58     if (commands.size() > 0) {
59       execute(commands, serverPrefs);
60       System.exit(0);
61     } else {
62       System.err.println("usage: AdminClient command arguments");
63       System.err.println(" command may be either an XML-RPC method, 'repair' or 'checklocale'");
64     }
65     System.exit(1);
66   }
67
68   private static void printCopyright() {
69     System.err.println("SnipSnap AdminClient ($Revision: 1731 $)");
70
71     // output version and copyright information
72
try {
73       BufferedReader JavaDoc copyrightReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(AdminClient.class.getResourceAsStream("/conf/copyright.txt")));
74       String JavaDoc line = null;
75       while ((line = copyrightReader.readLine()) != null) {
76         System.err.println(line);
77       }
78     } catch (Exception JavaDoc e) {
79       // ignore io exception here ...
80
}
81   }
82
83   private static void execute(List JavaDoc commands, Preferences JavaDoc serverPrefs) {
84     if ("repair".equals(commands.get(0))) {
85       File JavaDoc in = null, out = null, webapp = null;
86       switch (commands.size()) {
87         case 1:
88           System.err.println("repair needs arguments: <input> <output> <webapp directory>");
89           System.exit(0);
90           break;
91         case 4:
92           webapp = new File JavaDoc((String JavaDoc) commands.get(3));
93         case 3:
94           out = new File JavaDoc((String JavaDoc) commands.get(2));
95         case 2:
96           in = new File JavaDoc((String JavaDoc) commands.get(1));
97       }
98       XMLSnipRepair.repair(in, out, webapp);
99     } else if ("checklocale".equals(commands.get(0))) {
100       boolean show = false;
101       switch (commands.size()) {
102         case 1:
103           System.err.println("checklocale needs at least one argument: [-show] <locale to check>");
104           System.exit(0);
105           break;
106         case 3:
107           show = "-show".equals(commands.get(1));
108           commands.remove(1);
109         case 2:
110           File JavaDoc[] localefiles = new File JavaDoc("src/apps/default/WEB-INF/classes/i18n").listFiles(new FilenameFilter JavaDoc() {
111             public boolean accept(File JavaDoc dir, String JavaDoc name) {
112               return name.endsWith("_en.properties");
113             }
114           });
115           String JavaDoc locale = (String JavaDoc) commands.get(1);
116           checkLocaleFiles(localefiles, locale, show);
117           localefiles = new File JavaDoc("src/apps/installer/WEB-INF/classes/i18n").listFiles(new FilenameFilter JavaDoc() {
118             public boolean accept(File JavaDoc dir, String JavaDoc name) {
119               return name.endsWith("_en.properties");
120             }
121           });
122           checkLocaleFiles(localefiles, locale, show);
123           break;
124       }
125     } else if ("dump".equals(commands.get(0))) {
126       switch (commands.size()) {
127         case 3:
128           Properties JavaDoc appConfig = new Properties JavaDoc();
129           try {
130             appConfig.load(new FileInputStream JavaDoc((String JavaDoc) commands.get(1)));
131             JDBCDatabaseExport.export(appConfig,
132                                       (String JavaDoc) commands.get(2),
133                                       new File JavaDoc((String JavaDoc) commands.get(1)).getParent());
134           } catch (IOException JavaDoc e) {
135             System.err.println("Error exporting data base: " + e.getMessage());
136           }
137           break;
138         default:
139           System.err.println("dump needs at two arguments: <application.conf> <application oid>");
140           System.exit(0);
141           break;
142       }
143     } else {
144       try {
145         System.err.println("Contacting Remote Server ...");
146         AdminXmlRpcClient client = new AdminXmlRpcClient(serverPrefs.get(ServerConfiguration.ADMIN_URL, null),
147                                                          serverPrefs.get(ServerConfiguration.ADMIN_USER, null),
148                                                          serverPrefs.get(ServerConfiguration.ADMIN_PASS, null));
149         String JavaDoc method = (String JavaDoc) commands.get(0);
150         Vector JavaDoc args = new Vector JavaDoc();
151         for (int i = 1; i < commands.size(); i++) {
152           args.addElement(commands.get(i));
153         }
154         Object JavaDoc result = client.execute(method, args);
155         System.err.println("Operation '" + method + "' okay:");
156         if (result instanceof Object JavaDoc[]) {
157           System.out.println("" + Arrays.asList((Object JavaDoc[]) result));
158         } else if (result instanceof byte[]) {
159           System.out.println(new String JavaDoc((byte[]) result, "UTF-8"));
160         } else {
161           System.out.println(result);
162         }
163       } catch (Exception JavaDoc e) {
164         System.err.println("AdminClient: error executing command: " + e.getMessage());
165       }
166     }
167   }
168
169   private static void checkLocaleFiles(File JavaDoc[] localefiles, String JavaDoc locale, boolean show) {
170     for (int i = 0; i < localefiles.length; i++) {
171       Properties JavaDoc defaultBundle = new Properties JavaDoc();
172       Properties JavaDoc compareBundle = new Properties JavaDoc();
173       try {
174         defaultBundle.load(new FileInputStream JavaDoc(localefiles[i]));
175         String JavaDoc bundleName = localefiles[i].getName();
176         bundleName = bundleName.substring(0, bundleName.length() - "_en.properties".length());
177         File JavaDoc bundleFile = new File JavaDoc(localefiles[i].getParentFile(), bundleName + "_" + locale + ".properties");
178         compareBundle.load(new FileInputStream JavaDoc(bundleFile));
179         System.out.println("== Checking bundle " + bundleFile.getName());
180         Properties JavaDoc problems[] = LocaleComparator.compareBundles(compareBundle, defaultBundle);
181         if (!problems[0].isEmpty()) {
182           System.out.println("== " + bundleFile.getName() + ": " + problems[0].size() + " missing properties.");
183           if (show) {
184             Iterator JavaDoc it = new TreeSet JavaDoc(problems[0].keySet()).iterator();
185             while (it.hasNext()) {
186               String JavaDoc key = (String JavaDoc) it.next();
187               System.out.println(key + "\t=\t" + problems[0].getProperty(key));
188             }
189           }
190         }
191         if (!problems[1].isEmpty()) {
192           System.out.println("== " + bundleFile.getName() + ": " + problems[1].size() + " not translated.");
193           if (show) {
194             Iterator JavaDoc it = new TreeSet JavaDoc(problems[1].keySet()).iterator();
195             while (it.hasNext()) {
196               String JavaDoc key = (String JavaDoc) it.next();
197               System.out.println(key + "\t=\t" + problems[1].getProperty(key));
198             }
199           }
200         }
201       } catch (IOException JavaDoc e) {
202         System.err.println("can't find bundle: " + e.getMessage());
203       }
204     }
205   }
206
207
208   private static List JavaDoc parseOptions(String JavaDoc args[], Preferences JavaDoc serverPrefs) {
209     int argNo;
210     List JavaDoc commands = new ArrayList JavaDoc();
211     for (argNo = 0; argNo < args.length; argNo++) {
212       if ("-url".equals(args[argNo]) && args.length > argNo + 1) {
213         serverPrefs.put(ServerConfiguration.ADMIN_URL, args[argNo + 1]);
214         argNo++;
215       } else if ("-user".equals(args[argNo]) && args.length > argNo + 1) {
216         serverPrefs.put(ServerConfiguration.ADMIN_USER, args[argNo + 1]);
217         argNo++;
218       } else if ("-password".equals(args[argNo]) && args.length > argNo + 1) {
219         serverPrefs.put(ServerConfiguration.ADMIN_PASS, args[argNo + 1]);
220         argNo++;
221       } else {
222         if (args[argNo] != null && args[argNo].startsWith("file:")) {
223           String JavaDoc fileName = args[argNo].substring("file:".length());
224           System.err.print("Reading file '" + fileName + "'");
225           try {
226             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
227             File JavaDoc file = new File JavaDoc(fileName);
228             InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
229             long fileLength = file.length();
230             System.err.println(" " + (fileLength / 1024) + " kB");
231             System.err.print("0%");
232             byte[] buffer = new byte[4096];
233             int n = 0;
234             int current = 0;
235             while ((n = in.read(buffer)) != -1) {
236               current += n;
237               loadProgress(fileLength, current, 4096);
238               bos.write(buffer, 0, n);
239             }
240             commands.add(bos.toByteArray());
241           } catch (IOException JavaDoc e) {
242             System.err.println("AdminClient: unable to load file: " + args[argNo] + ": " + e);
243           }
244           System.err.println();
245         } else if (args[argNo] != null && args[argNo].startsWith("properties:")) {
246           String JavaDoc fileName = args[argNo].substring("properties:".length());
247           System.err.println("Reading properties from file '" + fileName + "'");
248           try {
249             Properties JavaDoc props = new Properties JavaDoc();
250             props.load(new FileInputStream JavaDoc(fileName));
251             commands.add(props);
252           } catch (IOException JavaDoc e) {
253             System.err.println("AdminClient: unable to load properties: " + args[argNo] + ": " + e);
254           }
255         } else {
256           commands.add(args[argNo]);
257         }
258       }
259     }
260     return commands;
261   }
262
263   private static void loadProgress(long length, long current, int blockSize) {
264     long percentage = current * 100 / length;
265     if (percentage % 5 != 0 && ((current - blockSize) * 100 / length) % 5 == 0) {
266       System.err.print(".");
267     } else if (percentage % 20 == 0 && ((current - blockSize) * 100 / length) % 20 != 0) {
268       System.err.print(NumberFormat.getIntegerInstance().format(percentage) + "%");
269     }
270   }
271
272
273 }
274
Popular Tags