KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > admin > AdminClient


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: AdminClient.java,v 1.4 2002/09/12 14:43:31 per_nyfelt Exp $
8

9 package org.ozoneDB.core.admin;
10
11 import java.io.*;
12 import java.util.zip.GZIPInputStream JavaDoc;
13 import java.util.zip.GZIPOutputStream JavaDoc;
14 import javax.xml.parsers.SAXParser JavaDoc;
15 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
26 import org.xml.sax.helpers.ParserAdapter JavaDoc;
27
28
29 /**
30  * @version $Revision: 1.4 $ $Date: 2002/09/12 14:43:31 $
31  * @author <a HREF="http://www.smb-tec.com">SMB</a>
32  * @author <a HREF="http://www.medium.net/">Medium.net</a>
33  */

34 public class AdminClient implements SAXChunkProducerDelegate {
35
36     final static String JavaDoc COMMAND_NU = "newuser";
37     final static String JavaDoc COMMAND_RU = "removeuser";
38     final static String JavaDoc COMMAND_AU = "allusers";
39     final static String JavaDoc COMMAND_NG = "newgroup";
40     final static String JavaDoc COMMAND_RG = "removegroup";
41     final static String JavaDoc COMMAND_AG = "allgroups";
42     final static String JavaDoc COMMAND_U2G = "user2group";
43     final static String JavaDoc COMMAND_SD = "shutdown";
44     final static String JavaDoc COMMAND_TXS = "txs";
45     final static String JavaDoc COMMAND_BACKUP = "backup";
46     final static String JavaDoc COMMAND_RESTORE = "restore";
47     final static String JavaDoc COMMAND_START_GARBAGECOLLECTION = "startGC";
48
49     final static String JavaDoc DB_URL = "ozonedb:remote://localhost:3333";
50
51     String JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc {
63         if (args.length == 0 || args[0].equals("-h") || args[0].equals("-help")) {
64             printUsage();
65             System.exit(1);
66         }
67         String JavaDoc 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 JavaDoc e) {
103             client.out.println("Error: " + e.getMessage());
104             e.printStackTrace(client.verboseOut);
105         }
106         client.close();
107     }
108
109
110 // note that on windows systems the equal (=) gets removed and breaks
111
// the command line into too many arguments
112
// as a work around, the -name=value in double quotes like
113
// "-name=value" allows both Windows and UNIX to work correctly
114
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( " \"-raw\" Write compiled XML." );
162
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 JavaDoc[] _args) throws Exception JavaDoc {
172         // use System.out for normal output and skip verbose output
173
out = new PrintWriter(System.out, true);
174         verboseOut = new PrintWriter(new CharArrayWriter());
175
176         // check command line arguments
177
for (int i = 1; i < _args.length; i++) {
178             String JavaDoc 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 JavaDoc {
192         db = ExternalDatabase.openDatabase(dbURL);
193         db.reloadClasses();
194
195         admin = db.admin();
196     }
197
198
199     protected synchronized void close() throws Exception JavaDoc {
200         if (db != null) {
201             db.close();
202             db = null;
203         }
204     }
205
206
207     protected void newUser(String JavaDoc[] _args) throws Exception JavaDoc {
208         String JavaDoc name = null;
209         int id = -1;
210
211         // check command line arguments
212
for (int i = 1; i < _args.length; i++) {
213             String JavaDoc 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 JavaDoc[] _args) throws Exception JavaDoc {
234         String JavaDoc name = null;
235
236         // check command line arguments
237
for (int i = 1; i < _args.length; i++) {
238             String JavaDoc 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 JavaDoc[] _args) throws Exception JavaDoc {
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 JavaDoc[] _args) throws Exception JavaDoc {
277         String JavaDoc name = null;
278         int id = -1;
279
280         // check command line arguments
281
for (int i = 1; i < _args.length; i++) {
282             String JavaDoc 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 JavaDoc[] _args) throws Exception JavaDoc {
303         String JavaDoc name = null;
304
305         // check command line arguments
306
for (int i = 1; i < _args.length; i++) {
307             String JavaDoc 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 JavaDoc[] _args) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc[] _args) throws Exception JavaDoc {
360         String JavaDoc username = null;
361         String JavaDoc groupname = null;
362
363         // check command line arguments
364
for (int i = 1; i < _args.length; i++) {
365             String JavaDoc 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 JavaDoc {
386         out.println(admin.numberOfTxs());
387     }
388
389
390     protected void shutdown() throws Exception JavaDoc {
391         admin.shutdown();
392     }
393
394     protected void startGarbageCollection() {
395         admin.startGarbageCollection();
396     }
397
398
399     protected void restore(String JavaDoc[] _args) throws Exception JavaDoc {
400         String JavaDoc filename = "-";
401         boolean compress = false;
402
403         // check command line arguments
404
for (int i = 1; i < _args.length; i++) {
405             String JavaDoc 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 JavaDoc in = System.in;
422             if (!filename.equals("-")) {
423                 in = new FileInputStream(filename);
424             }
425             if (compress) {
426                 in = new GZIPInputStream JavaDoc(in, 4096);
427             } else {
428                 in = new BufferedInputStream(in, 4096);
429             }
430             InputSource JavaDoc xmlSource = new InputSource JavaDoc(in);
431
432             SAXChunkProducer producer = new SAXChunkProducer(this);
433
434             SAXParserFactory JavaDoc parserFactory = SAXParserFactory.newInstance();
435             SAXParser JavaDoc parser = parserFactory.newSAXParser();
436             ParserAdapter JavaDoc adapter = new ParserAdapter JavaDoc(parser.getParser());
437             adapter.setContentHandler(producer);
438             adapter.parse(xmlSource);
439         } catch (Exception JavaDoc 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             // signal end
447
admin.processRestoreChunk(null);
448         }
449     }
450
451
452     /**
453      * This method is inherited from SAXChunkProducerDelegate. It is called
454      * by the producer when a chunk is ready to be processed.
455      */

456     public void processChunk(SAXChunkProducer _producer) throws Exception JavaDoc {
457         admin.processRestoreChunk(_producer.chunkStream().toByteArray());
458     }
459
460
461     protected void backup(String JavaDoc[] _args) throws Exception JavaDoc {
462         String JavaDoc filename = "-";
463         boolean indent = false;
464         boolean raw = false;
465         boolean compress = false;
466
467         // check command line arguments
468
for (int i = 1; i < _args.length; i++) {
469             String JavaDoc 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 JavaDoc out = System.out;
494             if (!filename.equals("-")) {
495                 out = new FileOutputStream(filename);
496             }
497             if (compress) {
498                 out = new GZIPOutputStream JavaDoc(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 JavaDoc) {
511                 ((GZIPOutputStream JavaDoc) out).finish();
512             }
513             out.close();
514         } finally {
515             tx.rollback();
516         }
517     }
518
519 }
520
Popular Tags