1 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 ; 33 import java.io.BufferedReader ; 34 import java.io.ByteArrayOutputStream ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FilenameFilter ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.InputStreamReader ; 41 import java.text.NumberFormat ; 42 import java.util.ArrayList ; 43 import java.util.Arrays ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Properties ; 47 import java.util.TreeSet ; 48 import java.util.Vector ; 49 import java.util.prefs.Preferences ; 50 51 public class AdminClient { 52 53 public static void main(String args[]) { 54 printCopyright(); 55 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 56 57 List 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 try { 73 BufferedReader copyrightReader = new BufferedReader (new InputStreamReader (AdminClient.class.getResourceAsStream("/conf/copyright.txt"))); 74 String line = null; 75 while ((line = copyrightReader.readLine()) != null) { 76 System.err.println(line); 77 } 78 } catch (Exception e) { 79 } 81 } 82 83 private static void execute(List commands, Preferences serverPrefs) { 84 if ("repair".equals(commands.get(0))) { 85 File 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 ((String ) commands.get(3)); 93 case 3: 94 out = new File ((String ) commands.get(2)); 95 case 2: 96 in = new File ((String ) 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 [] localefiles = new File ("src/apps/default/WEB-INF/classes/i18n").listFiles(new FilenameFilter () { 111 public boolean accept(File dir, String name) { 112 return name.endsWith("_en.properties"); 113 } 114 }); 115 String locale = (String ) commands.get(1); 116 checkLocaleFiles(localefiles, locale, show); 117 localefiles = new File ("src/apps/installer/WEB-INF/classes/i18n").listFiles(new FilenameFilter () { 118 public boolean accept(File dir, String 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 appConfig = new Properties (); 129 try { 130 appConfig.load(new FileInputStream ((String ) commands.get(1))); 131 JDBCDatabaseExport.export(appConfig, 132 (String ) commands.get(2), 133 new File ((String ) commands.get(1)).getParent()); 134 } catch (IOException 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 method = (String ) commands.get(0); 150 Vector args = new Vector (); 151 for (int i = 1; i < commands.size(); i++) { 152 args.addElement(commands.get(i)); 153 } 154 Object result = client.execute(method, args); 155 System.err.println("Operation '" + method + "' okay:"); 156 if (result instanceof Object []) { 157 System.out.println("" + Arrays.asList((Object []) result)); 158 } else if (result instanceof byte[]) { 159 System.out.println(new String ((byte[]) result, "UTF-8")); 160 } else { 161 System.out.println(result); 162 } 163 } catch (Exception e) { 164 System.err.println("AdminClient: error executing command: " + e.getMessage()); 165 } 166 } 167 } 168 169 private static void checkLocaleFiles(File [] localefiles, String locale, boolean show) { 170 for (int i = 0; i < localefiles.length; i++) { 171 Properties defaultBundle = new Properties (); 172 Properties compareBundle = new Properties (); 173 try { 174 defaultBundle.load(new FileInputStream (localefiles[i])); 175 String bundleName = localefiles[i].getName(); 176 bundleName = bundleName.substring(0, bundleName.length() - "_en.properties".length()); 177 File bundleFile = new File (localefiles[i].getParentFile(), bundleName + "_" + locale + ".properties"); 178 compareBundle.load(new FileInputStream (bundleFile)); 179 System.out.println("== Checking bundle " + bundleFile.getName()); 180 Properties 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 it = new TreeSet (problems[0].keySet()).iterator(); 185 while (it.hasNext()) { 186 String key = (String ) 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 it = new TreeSet (problems[1].keySet()).iterator(); 195 while (it.hasNext()) { 196 String key = (String ) it.next(); 197 System.out.println(key + "\t=\t" + problems[1].getProperty(key)); 198 } 199 } 200 } 201 } catch (IOException e) { 202 System.err.println("can't find bundle: " + e.getMessage()); 203 } 204 } 205 } 206 207 208 private static List parseOptions(String args[], Preferences serverPrefs) { 209 int argNo; 210 List commands = new ArrayList (); 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 fileName = args[argNo].substring("file:".length()); 224 System.err.print("Reading file '" + fileName + "'"); 225 try { 226 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 227 File file = new File (fileName); 228 InputStream in = new BufferedInputStream (new FileInputStream (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 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 fileName = args[argNo].substring("properties:".length()); 247 System.err.println("Reading properties from file '" + fileName + "'"); 248 try { 249 Properties props = new Properties (); 250 props.load(new FileInputStream (fileName)); 251 commands.add(props); 252 } catch (IOException 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 |