1 9 package org.ozoneDB.core.admin; 10 11 import java.io.*; 12 import java.util.zip.GZIPInputStream ; 13 import java.util.zip.GZIPOutputStream ; 14 import javax.xml.parsers.SAXParser ; 15 import javax.xml.parsers.SAXParserFactory ; 16 17 import org.apache.xml.serialize.OutputFormat; 18 import org.apache.xml.serialize.XMLSerializer; 19 import org.ozoneDB.DxLib.DxCollection; 20 import org.ozoneDB.DxLib.DxIterator; 21 import org.ozoneDB.ExternalDatabase; 22 import org.ozoneDB.ExternalTransaction; 23 import org.ozoneDB.core.*; 24 import org.ozoneDB.xml.util.*; 25 import org.xml.sax.InputSource ; 26 import org.xml.sax.helpers.ParserAdapter ; 27 28 29 34 public class AdminClient implements SAXChunkProducerDelegate { 35 36 final static String COMMAND_NU = "newuser"; 37 final static String COMMAND_RU = "removeuser"; 38 final static String COMMAND_AU = "allusers"; 39 final static String COMMAND_NG = "newgroup"; 40 final static String COMMAND_RG = "removegroup"; 41 final static String COMMAND_AG = "allgroups"; 42 final static String COMMAND_U2G = "user2group"; 43 final static String COMMAND_SD = "shutdown"; 44 final static String COMMAND_TXS = "txs"; 45 final static String COMMAND_BACKUP = "backup"; 46 final static String COMMAND_RESTORE = "restore"; 47 final static String COMMAND_START_GARBAGECOLLECTION = "startGC"; 48 49 final static String DB_URL = "ozonedb:remote://localhost:3333"; 50 51 String dbURL = DB_URL; 52 53 PrintWriter out; 54 55 PrintWriter verboseOut; 56 57 ExternalDatabase db; 58 59 Admin admin; 60 61 62 public static void main(String [] args) throws Exception { 63 if (args.length == 0 || args[0].equals("-h") || args[0].equals("-help")) { 64 printUsage(); 65 System.exit(1); 66 } 67 String command = args[0]; 68 69 AdminClient client = new AdminClient(args); 70 71 try { 72 client.open(); 73 74 if (command.equals(COMMAND_NU)) { 75 client.newUser(args); 76 } else if (command.equals(COMMAND_AU)) { 77 client.allUsers(args); 78 } else if (command.equals(COMMAND_RU)) { 79 client.removeUser(args); 80 } else if (command.equals(COMMAND_NG)) { 81 client.newGroup(args); 82 } else if (command.equals(COMMAND_AG)) { 83 client.allGroups(args); 84 } else if (command.equals(COMMAND_RG)) { 85 client.removeGroup(args); 86 } else if (command.equals(COMMAND_U2G)) { 87 client.addUser2Group(args); 88 } else if (command.equals(COMMAND_TXS)) { 89 client.numberOfTxs(); 90 } else if (command.equals(COMMAND_SD)) { 91 client.shutdown(); 92 } else if (command.equals(COMMAND_BACKUP)) { 93 client.backup(args); 94 } else if (command.equals(COMMAND_RESTORE)) { 95 client.restore(args); 96 } else if (command.equals(COMMAND_START_GARBAGECOLLECTION)) { 97 client.startGarbageCollection(); 98 } else { 99 System.out.println("Unknown command: " + command); 100 printUsage(); 101 } 102 } catch (Exception e) { 103 client.out.println("Error: " + e.getMessage()); 104 e.printStackTrace(client.verboseOut); 105 } 106 client.close(); 107 } 108 109 110 public static void printUsage() { 115 System.out.println("usage: ozoneAdmin <command> [options]"); 116 System.out.println(""); 117 System.out.println("commands: " + COMMAND_NU + " " + COMMAND_RU + " " + COMMAND_AU + " " + 118 COMMAND_NG + " " + COMMAND_RG + " " + COMMAND_AG + " " + 119 COMMAND_TXS + " " + COMMAND_BACKUP + " " + COMMAND_RESTORE + " " + COMMAND_START_GARBAGECOLLECTION); 120 System.out.println(""); 121 System.out.println("note: all -name=value must be within double quotes for Windows systems!"); 122 System.out.println(""); 123 System.out.println("overall options:"); 124 System.out.println(" \"-dburl=<URL>\" The URL of the database. (default: " + DB_URL + ")"); 125 System.out.println(" \"-verbose\" Be verbose."); 126 System.out.println(""); 127 System.out.println(COMMAND_NU + " options:"); 128 System.out.println(" \"-name=<username>\""); 129 System.out.println(" \"-id=<userid>\""); 130 System.out.println(""); 131 System.out.println(COMMAND_RU + " options:"); 132 System.out.println(" \"-name=<username>\""); 133 System.out.println(""); 134 System.out.println(COMMAND_AU + " options:"); 135 System.out.println(" -"); 136 System.out.println(""); 137 System.out.println(COMMAND_NG + " options:"); 138 System.out.println(" \"-name=<groupname>\""); 139 System.out.println(" \"-id=<groupid>\""); 140 System.out.println(""); 141 System.out.println(COMMAND_RG + " options:"); 142 System.out.println(" \"-name=<groupname>\""); 143 System.out.println(""); 144 System.out.println(COMMAND_AG + " options:"); 145 System.out.println(" -"); 146 System.out.println(""); 147 System.out.println(COMMAND_U2G + " options:"); 148 System.out.println(" \"-username=<username>\""); 149 System.out.println(" \"-groupname=<groupname>\""); 150 System.out.println(""); 151 System.out.println(COMMAND_TXS + " options:"); 152 System.out.println(" -"); 153 System.out.println(""); 154 System.out.println(COMMAND_SD + " options:"); 155 System.out.println(" -"); 156 System.out.println(""); 157 System.out.println(COMMAND_BACKUP + " options:"); 158 System.out.println(" \"-outfile=<filename>\" The name of the output file. (default: System.out)"); 159 System.out.println(" \"-compress\" gzip the output."); 160 System.out.println(" \"-indent\" Indent the XML output."); 161 System.out.println(""); 163 System.out.println(COMMAND_RESTORE + " options:"); 164 System.out.println(" \"-infile=<filename>\" The name of the input file. (default: System.in)"); 165 System.out.println(" \"-compress\" Input is gzipped."); 166 System.out.println(""); 167 System.out.println(COMMAND_START_GARBAGECOLLECTION + ": initiate persistent object garbage collection."); 168 } 169 170 171 public AdminClient(String [] _args) throws Exception { 172 out = new PrintWriter(System.out, true); 174 verboseOut = new PrintWriter(new CharArrayWriter()); 175 176 for (int i = 1; i < _args.length; i++) { 178 String arg = _args[i]; 179 180 if (arg.startsWith("-dburl=")) { 181 dbURL = arg.substring(7); 182 verboseOut.println("dbURL: " + dbURL); 183 } else if (arg.equals("-v") || arg.equals("-verbose")) { 184 verboseOut = new PrintWriter(System.out, true); 185 verboseOut.println("verbose=true"); 186 } 187 } 188 } 189 190 191 public synchronized void open() throws Exception { 192 db = ExternalDatabase.openDatabase(dbURL); 193 db.reloadClasses(); 194 195 admin = db.admin(); 196 } 197 198 199 protected synchronized void close() throws Exception { 200 if (db != null) { 201 db.close(); 202 db = null; 203 } 204 } 205 206 207 protected void newUser(String [] _args) throws Exception { 208 String name = null; 209 int id = -1; 210 211 for (int i = 1; i < _args.length; i++) { 213 String arg = _args[i]; 214 215 if (arg.startsWith("-name=")) { 216 name = arg.substring(6); 217 verboseOut.println("name=" + name); 218 } else if (arg.startsWith("-id=")) { 219 id = Integer.parseInt(arg.substring(4)); 220 verboseOut.println("id=" + id); 221 } else { 222 out.println("Unknow argument: " + arg); 223 } 224 } 225 try { 226 admin.newUser(name, id); 227 } catch (UserManagerException e) { 228 out.println("Unable to create new user: " + e.getMessage()); 229 } 230 } 231 232 233 protected void removeUser(String [] _args) throws Exception { 234 String name = null; 235 236 for (int i = 1; i < _args.length; i++) { 238 String arg = _args[i]; 239 240 if (arg.startsWith("-name=")) { 241 name = arg.substring(6); 242 verboseOut.println("name=" + name); 243 } else { 244 out.println("Unknow argument: " + arg); 245 } 246 } 247 try { 248 admin.removeUser(name); 249 } catch (UserManagerException e) { 250 out.println("Unable to remove user: " + e.getMessage()); 251 } 252 } 253 254 255 protected void allUsers(String [] _args) throws Exception { 256 DxCollection users = null; 257 try { 258 users = admin.allUsers(); 259 } catch (UserManagerException e) { 260 out.println("Unable to retrieve all users: " + e.getMessage()); 261 } 262 if (users.isEmpty()) { 263 out.println("No users found."); 264 } else { 265 for (DxIterator it = users.iterator(); it.next() != null;) { 266 User user = (User) it.object(); 267 268 out.println("user name: " + user.name()); 269 out.println(" id: " + user.id()); 270 } 271 } 272 273 } 274 275 276 protected void newGroup(String [] _args) throws Exception { 277 String name = null; 278 int id = -1; 279 280 for (int i = 1; i < _args.length; i++) { 282 String arg = _args[i]; 283 284 if (arg.startsWith("-name=")) { 285 name = arg.substring(6); 286 verboseOut.println("name=" + name); 287 } else if (arg.startsWith("-id=")) { 288 id = Integer.parseInt(arg.substring(4)); 289 verboseOut.println("id=" + id); 290 } else { 291 out.println("Unknow argument: " + arg); 292 } 293 } 294 try { 295 admin.newGroup(name, id); 296 } catch (UserManagerException e) { 297 out.println("Unable to create new group: " + e.getMessage()); 298 } 299 } 300 301 302 protected void removeGroup(String [] _args) throws Exception { 303 String name = null; 304 305 for (int i = 1; i < _args.length; i++) { 307 String arg = _args[i]; 308 309 if (arg.startsWith("-name=")) { 310 name = arg.substring(6); 311 verboseOut.println("name=" + name); 312 } else { 313 out.println("Unknow argument: " + arg); 314 } 315 } 316 try { 317 admin.removeGroup(name); 318 } catch (UserManagerException e) { 319 out.println("Unable to remove group: " + e.getMessage()); 320 } 321 } 322 323 324 protected void allGroups(String [] _args) throws Exception { 325 DxCollection groups = null; 326 try { 327 groups = admin.allGroups(); 328 } catch (UserManagerException e) { 329 out.println("Unable to retrieve all groups: " + e.getMessage()); 330 return; 331 } 332 if (groups.isEmpty()) { 333 out.println("No groups found."); 334 } else { 335 for (DxIterator it = groups.iterator(); it.next() != null;) { 336 Group group = (Group) it.object(); 337 338 out.println("group name: " + group.name()); 339 out.println(" id: " + group.id()); 340 out.println(" user ID count: " + group.usersCount()); 341 342 DxIterator it2 = group.userIDs().iterator(); 343 int c = 1; 344 String line = " user IDs: "; 345 while (it2.next() != null) { 346 line += (c > 1 ? ", " : ""); 347 line += it2.object().toString(); 348 if (c++ % 10 == 0) { 349 out.println(line); 350 line = " "; 351 } 352 } 353 out.println(line); 354 } 355 } 356 } 357 358 359 protected void addUser2Group(String [] _args) throws Exception { 360 String username = null; 361 String groupname = null; 362 363 for (int i = 1; i < _args.length; i++) { 365 String arg = _args[i]; 366 367 if (arg.startsWith("-username=")) { 368 username = arg.substring(10); 369 verboseOut.println("user name=" + username); 370 } else if (arg.startsWith("-groupname=")) { 371 groupname = arg.substring(11); 372 verboseOut.println("group name=" + groupname); 373 } else { 374 out.println("Unknow argument: " + arg); 375 } 376 } 377 try { 378 admin.addUser2Group(username, groupname); 379 } catch (UserManagerException e) { 380 out.println("Unable to add user to group: " + e.getMessage()); 381 } 382 } 383 384 385 protected void numberOfTxs() throws Exception { 386 out.println(admin.numberOfTxs()); 387 } 388 389 390 protected void shutdown() throws Exception { 391 admin.shutdown(); 392 } 393 394 protected void startGarbageCollection() { 395 admin.startGarbageCollection(); 396 } 397 398 399 protected void restore(String [] _args) throws Exception { 400 String filename = "-"; 401 boolean compress = false; 402 403 for (int i = 1; i < _args.length; i++) { 405 String arg = _args[i]; 406 407 if (arg.equals("-compress")) { 408 compress = true; 409 verboseOut.println("compress=" + compress); 410 } else if (arg.startsWith("-infile=")) { 411 filename = arg.substring(8); 412 verboseOut.println("filename=" + filename); 413 } else { 414 out.println("Unknow argument: " + arg); 415 } 416 } 417 418 try { 419 admin.beginRestore(); 420 421 InputStream in = System.in; 422 if (!filename.equals("-")) { 423 in = new FileInputStream(filename); 424 } 425 if (compress) { 426 in = new GZIPInputStream (in, 4096); 427 } else { 428 in = new BufferedInputStream(in, 4096); 429 } 430 InputSource xmlSource = new InputSource (in); 431 432 SAXChunkProducer producer = new SAXChunkProducer(this); 433 434 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 435 SAXParser parser = parserFactory.newSAXParser(); 436 ParserAdapter adapter = new ParserAdapter (parser.getParser()); 437 adapter.setContentHandler(producer); 438 adapter.parse(xmlSource); 439 } catch (Exception e) { 440 out.println("\nAn error occured while restoring the database."); 441 out.println("The database is not entirely restored and is probably not usable!"); 442 out.println("Please completely install and restore again."); 443 out.println(""); 444 e.printStackTrace(out); 445 } finally { 446 admin.processRestoreChunk(null); 448 } 449 } 450 451 452 456 public void processChunk(SAXChunkProducer _producer) throws Exception { 457 admin.processRestoreChunk(_producer.chunkStream().toByteArray()); 458 } 459 460 461 protected void backup(String [] _args) throws Exception { 462 String filename = "-"; 463 boolean indent = false; 464 boolean raw = false; 465 boolean compress = false; 466 467 for (int i = 1; i < _args.length; i++) { 469 String arg = _args[i]; 470 471 if (arg.equals("-compress")) { 472 compress = true; 473 verboseOut.println("compress=" + compress); 474 } else if (arg.equals("-indent")) { 475 indent = true; 476 verboseOut.println("indent=" + indent); 477 } else if (arg.equals("-raw")) { 478 raw = true; 479 verboseOut.println("raw=" + raw); 480 } else if (arg.startsWith("-outfile=")) { 481 filename = arg.substring(9); 482 verboseOut.println("filename=" + filename); 483 } else { 484 out.println("Unknow argument: " + arg); 485 } 486 } 487 488 ExternalTransaction tx = db.newTransaction(); 489 tx.begin(); 490 try { 491 admin.beginBackup(); 492 493 OutputStream out = System.out; 494 if (!filename.equals("-")) { 495 out = new FileOutputStream(filename); 496 } 497 if (compress) { 498 out = new GZIPOutputStream (out, 4096); 499 } else { 500 out = new BufferedOutputStream(out, 4096); 501 } 502 503 XMLSerializer serializer = new XMLSerializer(out, new OutputFormat("xml", "UTF-8", indent)); 504 SAXChunkConsumer consumer = new SAXChunkConsumer(serializer.asContentHandler()); 505 506 byte[] bytes = null; 507 while ((bytes = admin.nextBackupChunk()) != null) { 508 consumer.processChunk(bytes); 509 } 510 if (out instanceof GZIPOutputStream ) { 511 ((GZIPOutputStream ) out).finish(); 512 } 513 out.close(); 514 } finally { 515 tx.rollback(); 516 } 517 } 518 519 } 520 | Popular Tags |