installing in rhel: update version number
[openafs-wiki.git] / JavaAdminAPI / AdminTest.java
1 import org.openafs.jafs.ErrorTable;
2 import java.io.*;
3 import java.util.*;
4
5 /**
6  * A common error testing class for the native methods
7  */
8
9 public class AdminTest
10 {
11   Properties properties;
12   String cellName = null;
13   String username = null;
14   String password = null;
15
16   org.openafs.jafs.Cell   cellHandle;
17   org.openafs.jafs.Token  token;
18
19   /*
20     static
21     {
22     try {
23     System.loadLibrary("pwd");
24     } catch (Exception e) {
25     // Ignore
26     }
27     }
28   */
29
30   AdminTest() throws Exception
31   {
32     InputStream in = new FileInputStream("adminTest.properties");
33     if (in == null) throw new FileNotFoundException("adminTest.properties");
34     properties = new Properties();
35     properties.load(in);
36     cellName = getProperty("cellName", cellName);
37     //username = getProperty("username", username);
38     //password = getProperty("password", password);
39   }
40   private String getProperty(String name, String dflt)
41   {
42     String p = properties.getProperty(name);
43     return (p == null) ? dflt : p;
44   }
45   private int getProperty(String name, int dflt)
46   {
47     String p = properties.getProperty(name);
48     if (p == null) {
49       return dflt;
50     } else {
51       try {
52         return Integer.parseInt(p);
53       } catch (Exception e) {
54         return dflt;
55       }
56     }
57   }
58
59   private void authenticate() throws IOException
60   {
61     try {
62       System.out.println("  [user: " + username + "@" + cellName + "]");
63       token = new org.openafs.jafs.Token( username, password, cellName );
64       cellHandle = new org.openafs.jafs.Cell( token );
65     } catch ( org.openafs.jafs.AFSException e ) {
66       System.out.println( "Unexpected Exception\n  Error: " + ErrorTable.getMessage(e.getErrorCode()) + 
67                           "\n  Cell: " + cellName + "\n  Username: " + username);
68       return;
69     }
70   }
71
72   private void listCell() throws Exception
73   {
74     try {
75       System.out.println("======================================");
76       System.out.println("LISTING CELL INFO");
77       System.out.println("Total Number of Users: " + cellHandle.getUserCount());
78       System.out.println("Total Number of Groups: " + cellHandle.getGroupCount());
79       System.out.println("Total Number of Servers: " + cellHandle.getServerCount());
80       System.out.println("======================================");
81     } catch (Exception e) {
82       e.printStackTrace();
83     }
84   }
85
86   private void listUser(String name) throws Exception
87   {
88     if (name == null) {
89       System.out.println("Name is null!");
90       return;
91     }
92     org.openafs.jafs.User[] users = cellHandle.getUsers();
93     try {
94       System.out.println("Searching for user: " + name + "...");
95       for (int i = 0; i < users.length; i++) {
96         if (name.equals(users[i].getName())) {
97           System.out.println("User found!\n");
98           //System.out.println(users[i].getInfo());
99           System.out.println(users[i].toString());
100           System.out.println("Membership: " + users[i].getGroupMembershipCount());
101           System.out.println("Groups Owned: " + users[i].getGroupsOwnedCount());
102           return;
103         }
104       }
105       System.out.println("User '" + name + "' could not be found!");
106     } catch (Exception e) {
107       e.printStackTrace();
108     }
109   }
110
111   private void listUsers() throws Exception
112   {
113     org.openafs.jafs.User[] users = cellHandle.getUsers();
114     try {
115       System.out.println("======================================");
116       System.out.println("LISTING USERS");
117       for (int i = 0; i < users.length; i++) {
118         System.out.println("User " + i + " = " + users[i].getName());
119       }
120       System.out.println("======================================");
121       System.out.println("Total Number of Users: " + users.length);
122     } catch (Exception e) {
123       e.printStackTrace();
124     }
125   }
126
127   private void listGroup(String name) throws Exception
128   {
129     if (name == null) {
130       System.out.println("Name is null!");
131       return;
132     }
133     org.openafs.jafs.Group[] groups = cellHandle.getGroups();
134     try {
135       System.out.println("Searching for group: " + name + "...");
136       for (int i = 0; i < groups.length; i++) {
137         if (name.equals(groups[i].getName())) {
138           System.out.println("Group found!\n");
139           groups[i].refresh();
140           //System.out.println(groups[i].getInfo());
141           System.out.println(groups[i].toString());
142           System.out.println("Membership: " + groups[i].getMembershipCount());
143           return;
144         }
145       }
146       System.out.println("Group '" + name + "' could not be found!");
147     } catch (Exception e) {
148       e.printStackTrace();
149     }
150   }
151
152   private void listGroups() throws Exception
153   {
154     org.openafs.jafs.Group[] groups = cellHandle.getGroups();
155     try {
156       System.out.println("======================================");
157       System.out.println("LISTING GROUPS");
158       for (int i = 0; i < groups.length; i++) {
159         System.out.println("Group " + i + " = " + groups[i].getName());
160       }
161       System.out.println("======================================");
162       System.out.println("Total Number of Groups: " + groups.length);
163     } catch (Exception e) {
164       e.printStackTrace();
165     }
166   }
167
168   private void listGroupsX() throws Exception
169   {
170     org.openafs.jafs.Group[] groups = cellHandle.getGroups();
171     try {
172       System.out.println("======================================");
173       System.out.println("LISTING GROUPS");
174       for (int i = 0; i < groups.length; i++) {
175         System.out.println("Group " + i + " = " + groups[i].getName());
176       }
177       System.out.println("======================================");
178       System.out.println("Total Number of Groups: " + groups.length);
179     } catch (Exception e) {
180       e.printStackTrace();
181     }
182   }
183
184   private void listServer(String name) throws Exception
185   {
186     if (name == null) {
187       System.out.println("Name is null!");
188       return;
189     }
190     org.openafs.jafs.Server[] servers = cellHandle.getServers();
191     try {
192       System.out.println("Searching for server: " + name + "...");
193       for (int i = 0; i < servers.length; i++) {
194         if (name.equals(servers[i].getName())) {
195           System.out.println("Server found!\n");
196           //System.out.println(servers[i].getInfo());
197           System.out.println(servers[i].toString());
198           System.out.println("# of Partition: " + servers[i].getPartitionCount());
199           System.out.println("# of Admins: " + servers[i].getAdminCount());
200           System.out.println("# of Keys: " + servers[i].getKeyCount());
201           System.out.println("# of Processes: " + servers[i].getProcessCount());
202           System.out.println("# of IP Addresses: " + servers[i].getIPAddresses().length);
203           for (int x = 0; x < servers[i].getIPAddresses().length; x++) {
204             System.out.println(" -> IP " + x + ": " + servers[i].getIPAddresses()[x]);
205           }
206           return;
207         }
208       }
209       System.out.println("Server '" + name + "' could not be found!");
210     } catch (org.openafs.jafs.AFSException e) {
211       System.out.println("ERROR: " + ErrorTable.getMessage(e.getErrorCode()));
212     } catch (Exception e) {
213       e.printStackTrace();
214     }
215   }
216
217   private void listServers() throws Exception
218   {
219     org.openafs.jafs.Server[] servers = cellHandle.getServers();
220     try {
221       System.out.println("======================================");
222       System.out.println("LISTING SERVERS");
223       for (int i = 0; i < servers.length; i++) {
224         System.out.println("Server " + i + " = " + servers[i].toString());
225       }
226       System.out.println("======================================");
227       System.out.println("Total Number of Servers: " + servers.length);
228     } catch (Exception e) {
229       e.printStackTrace();
230     }
231   }
232
233
234   private void listPartition(String name) throws Exception
235   {
236     if (name == null) {
237       System.out.println("Name is null!");
238       return;
239     }
240     org.openafs.jafs.Server[] servers = cellHandle.getServers();
241     try {
242       System.out.println("Searching for partition: " + name + "...");
243       for (int i = 0; i < servers.length; i++) {
244         System.out.println("  Server " + i + " = " + servers[i].getName());
245         if (!servers[i].isFileServer() || servers[i].isBadFileServer()) {
246           System.out.println("  (not a file server)");
247         } else {
248           org.openafs.jafs.Partition[] partitions = servers[i].getPartitions();
249           for (int p = 0; p < partitions.length; p++) {
250             if (name.equals(partitions[p].getName())) {
251               System.out.println("Partition found!\n");
252               //System.out.println(partitions[p].getInfo());
253               System.out.println(partitions[p].toString());
254               System.out.println("Volume count: " + partitions[i].getVolumeCount());
255             }
256           }
257         }
258       }
259       System.out.println("Done");
260     } catch (Exception e) {
261       e.printStackTrace();
262     }
263   }
264
265   private void listPartitions() throws Exception
266   {
267     int n = 0;
268     org.openafs.jafs.Server[] servers = cellHandle.getServers();
269     try {
270       System.out.println("======================================");
271       System.out.println("LISTING PARTITIONS");
272       for (int i = 0; i < servers.length; i++) {
273         System.out.println("Server " + i + " = " + servers[i].getName());
274         if (!servers[i].isFileServer() || servers[i].isBadFileServer()) {
275           System.out.println("  (not a file server)");
276         } else {
277           org.openafs.jafs.Partition[] partitions = servers[i].getPartitions();
278           n += partitions.length;
279           for (int p = 0; p < partitions.length; p++) {
280             System.out.println("  Partition " + p + " = " + partitions[p].toString());
281           }
282         }
283       }
284       System.out.println("======================================");
285       System.out.println("Total Number of Servers: " + servers.length);
286       System.out.println("Total Number of Partitions: " + n);
287     } catch (Exception e) {
288       e.printStackTrace();
289     }
290   }
291
292   /*
293     private void enumeratePartitions(String serverName, int start, int end) throws Exception
294     {
295     int i = 1;
296     org.openafs.jafs.Server server = cellHandle.getServer(serverName);
297     try {
298     System.out.println("======================================");
299     System.out.println("ENUMERATING PARTITIONS FROM " + start + " to " + end);
300     Enumeration e = server.getPartitionEnumeration();
301
302     i = start;
303     while (e.hasMoreElements()) {
304     System.out.println(" " + i + " -> " + e.nextElement());
305     i++;
306     //if (i >= end) break;
307     }
308
309     System.out.println("======================================");
310     } catch (Exception e) {
311     e.printStackTrace();
312     }
313     }
314   */
315
316   private void listVolume(String name) throws Exception
317   {
318     if (name == null) {
319       System.out.println("Name is null!");
320       return;
321     }
322     org.openafs.jafs.Server[] servers = cellHandle.getServers();
323     try {
324       System.out.println("Searching for volume: " + name + "...");
325       for (int i = 0; i < servers.length; i++) {
326         System.out.println("  Server " + i + " = " + servers[i].getName());
327         if (!servers[i].isFileServer() || servers[i].isBadFileServer()) {
328           System.out.println("  (not a file server)");
329         } else {
330           org.openafs.jafs.Partition[] partitions = servers[i].getPartitions();
331           for (int p = 0; p < partitions.length; p++) {
332             System.out.println("    Partition " + p + " = " + partitions[p].getName());
333             org.openafs.jafs.Volume[] volumes = partitions[p].getVolumes();
334             for (int v = 0; v < volumes.length; v++) {
335               if (name.equals(volumes[v].getName())) {
336                 System.out.println("        Volume found!\n");
337                 //System.out.println(volumes[v].getInfo());
338                 System.out.println(volumes[v].toString());
339                 System.out.println("Quota: " + volumes[v].getQuota());
340               }
341             }
342           }
343         }
344       }
345       System.out.println("Done");
346     } catch (Exception e) {
347       e.printStackTrace();
348     }
349   }
350
351   private void listVolumes() throws Exception
352   {
353     int nP = 0;
354     int nV = 0;
355     org.openafs.jafs.Server[] servers = cellHandle.getServers();
356     try {
357       System.out.println("======================================");
358       System.out.println("LISTING PARTITIONS");
359       for (int i = 0; i < servers.length; i++) {
360         System.out.println("Server " + i + " = " + servers[i].getName());
361         if (!servers[i].isFileServer() || servers[i].isBadFileServer()) {
362           System.out.println("  (not a file server)");
363         } else {
364           org.openafs.jafs.Partition[] partitions = servers[i].getPartitions();
365           nP += partitions.length;
366           for (int p = 0; p < partitions.length; p++) {
367             System.out.println("  Partition " + p + " = " + partitions[p].getName());
368             org.openafs.jafs.Volume[] volumes = partitions[p].getVolumes();
369             nV += volumes.length;
370             for (int v = 0; v < volumes.length; v++) {
371               System.out.println("    Volume " + v + " = " + volumes[v].getName());
372             }
373           }
374         }
375       }
376       System.out.println("======================================");
377       System.out.println("Total Number of Servers: " + servers.length);
378       System.out.println("Total Number of Partitions: " + nP);
379       System.out.println("Total Number of Volumes: " + nV);
380     } catch (Exception e) {
381       e.printStackTrace();
382     }
383   }
384
385   /*
386     private void enumerateVolumes(String serverName, String partitionName, int start, int length) throws Exception
387     {
388     int i = 1;
389     int n = 0;
390     org.openafs.jafs.Server server = cellHandle.getServer(serverName);
391     org.openafs.jafs.Partition partition = server.getPartition(partitionName);
392     try {
393     System.out.println("======================================");
394     System.out.println("ENUMERATING PARTITIONS FROM " + start + " to " + (start + length));
395     Enumeration e = partition.getVolumeEnumeration(start);
396
397     i = start;
398     while (e.hasMoreElements()) {
399     System.out.println(" " + i + " -> " + e.nextElement());
400     i++;
401     n++;
402     if (n >= length) break;
403     }
404
405     System.out.println("======================================");
406     } catch (Exception e) {
407     e.printStackTrace();
408     }
409     }
410   */
411
412   private void listProcess(String name) throws Exception
413   {
414     if (name == null) {
415       System.out.println("Name is null!");
416       return;
417     }
418     org.openafs.jafs.Server[] servers = cellHandle.getServers();
419     try {
420       System.out.println("Searching for process: " + name + "...");
421       for (int i = 0; i < servers.length; i++) {
422         System.out.println("  Server " + i + " = " + servers[i].getName());
423         org.openafs.jafs.Process[] processes = servers[i].getProcesses();
424         for (int p = 0; p < processes.length; p++) {
425           if (name.equals(processes[p].getName())) {
426             System.out.println("Process found!\n");
427             //System.out.println(processes[p].getInfo());
428             System.out.println(processes[p].toString());
429           }
430         }
431       }
432       System.out.println("Done");
433     } catch (Exception e) {
434       e.printStackTrace();
435     }
436   }
437
438   private void listProcesses() throws Exception
439   {
440     int n = 0;
441     org.openafs.jafs.Server[] servers = cellHandle.getServers();
442     try {
443       System.out.println("======================================");
444       System.out.println("LISTING PROCESSES");
445       for (int i = 0; i < servers.length; i++) {
446         System.out.println("Server " + i + " = " + servers[i].getName());
447         org.openafs.jafs.Process[] processes = servers[i].getProcesses();
448         n += processes.length;
449         for (int p = 0; p < processes.length; p++) {
450           System.out.println("  Process " + p + " = " + processes[p].getName());
451         }
452       }
453       System.out.println("======================================");
454       System.out.println("Total Number of Servers: " + servers.length);
455       System.out.println("Total Number of Processes: " + n);
456     } catch (Exception e) {
457       e.printStackTrace();
458     }
459   }
460
461   private void listACL(String path) throws Exception
462   {
463     try {
464       System.out.println("======================================");
465       System.out.println("LISTING ACL for: \n\t" + path);
466
467       token.klog();
468       org.openafs.jafs.ACL acl = new org.openafs.jafs.ACL(path);
469       System.out.println(acl.toString());
470
471       System.out.println("======================================");
472     } catch (org.openafs.jafs.AFSException e) {
473       System.out.println("AFS Error: " + e.getMessage());
474     } catch (Exception e) {
475       e.printStackTrace();
476     }
477   }
478
479   private void setACL(String path) throws Exception
480   {
481     try {
482       System.out.println("======================================");
483       System.out.println("SETTING ACL for: \n\t" + path);
484
485       token.klog();
486
487       org.openafs.jafs.ACL acl = new org.openafs.jafs.ACL(path);
488       System.out.println(acl.toString());
489
490       org.openafs.jafs.ACL.Entry entry = new org.openafs.jafs.ACL.Entry( username, 29 );
491       acl.addPositiveEntry(entry);
492
493       System.out.println("Adding ACL entry: " + entry + "\n");
494       System.out.println("ACL after modifications:");
495       System.out.println(acl.toString());
496
497       System.out.println("======================================");
498     } catch (org.openafs.jafs.AFSException e) {
499       System.out.println("AFS Error: " + e.getMessage());
500     } catch (Exception e) {
501       e.printStackTrace();
502     }
503   }
504
505   private void listDir(String path) throws Exception
506   {
507     try {
508       System.out.println("======================================");
509       System.out.println("LISTING Directory for: \n\t" + path);
510
511       token.klog();
512
513       org.openafs.jafs.File file = new org.openafs.jafs.File(path);
514       String[] e = file.list();
515       if ( e == null ) {
516         System.out.println( file.getErrorMessage() );
517       } else {
518         for (int i = 0; i < e.length; i++) {
519           System.out.println(e[i]);
520         }
521       }
522
523       System.out.println("======================================");
524     } catch (org.openafs.jafs.AFSSecurityException e) {
525       System.out.println("AFS Security Error: " + e.getMessage());
526     } catch (org.openafs.jafs.AFSException e) {
527       System.out.println("AFS Error: " + e.getMessage());
528     } catch (Exception e) {
529       e.printStackTrace();
530     }
531   }
532
533   private void setUserGroupQuota() throws Exception
534   {
535     org.openafs.jafs.User user = cellHandle.getUser( username );
536     try {
537       System.out.println("======================================");
538       System.out.println("SETTING GROUP QUOTA FOR " + user.getName());
539       user.setGroupCreationQuota(-1);
540       user.setGroupCreationQuota(20);
541       user.setGroupCreationQuota(0);
542       System.out.println("======================================");
543     } catch (Exception e) {
544       e.printStackTrace();
545     }
546   }
547
548   private void salvage(String name, String serverName, String partitionName) throws Exception
549   {
550     org.openafs.jafs.Server server = cellHandle.getServer(serverName);
551     org.openafs.jafs.Partition partition = server.getPartition(partitionName);
552     org.openafs.jafs.Volume volume = partition.getVolume(name);
553     try {
554       System.out.println("======================================");
555       System.out.println("SALVAGING VOLUME " + volume.getName());
556       volume.salvage();
557       System.out.println("======================================");
558     } catch (Exception e) {
559       e.printStackTrace();
560     }
561   }
562
563   private void release(String name, String serverName, String partitionName) throws Exception
564   {
565     org.openafs.jafs.Server server = cellHandle.getServer(serverName);
566     org.openafs.jafs.Partition partition = server.getPartition(partitionName);
567     org.openafs.jafs.Volume volume = partition.getVolume(name);
568     try {
569       System.out.println("======================================");
570       System.out.println("RELEASING VOLUME " + volume.getName());
571       volume.release();
572       System.out.println("======================================");
573     } catch (Exception e) {
574       e.printStackTrace();
575     }
576   }
577
578   private void createReadOnly(String name, String serverName, String partitionName) throws Exception
579   {
580     org.openafs.jafs.Server server = cellHandle.getServer(serverName);
581     org.openafs.jafs.Partition partition = server.getPartition(partitionName);
582     org.openafs.jafs.Volume volume = partition.getVolume(name);
583     try {
584       System.out.println("======================================");
585       System.out.println("CREATING READ-ONLY REPLICA OF VOLUME " + volume.getName());
586       //volume.createReadOnly(cellHandle.getServer(server), cellHandle.getPartition(server, partition));
587       //volume.create( cellHandle, vosHandle, -1, newVolumeName, 0 );
588       volume.createReadOnly(partition);
589       System.out.println("======================================");
590     } catch (Exception e) {
591       e.printStackTrace();
592     }
593   }
594
595   private void createBackup(String name, String serverName, String partitionName) throws Exception
596   {
597     org.openafs.jafs.Server server = cellHandle.getServer(serverName);
598     org.openafs.jafs.Partition partition = server.getPartition(partitionName);
599     org.openafs.jafs.Volume volume = partition.getVolume(name);
600     try {
601       System.out.println("======================================");
602       System.out.println("CREATING BACKUP REPLICA OF VOLUME " + volume.getName());
603       volume.createBackup();
604       System.out.println("======================================");
605     } catch (Exception e) {
606       e.printStackTrace();
607     }
608   }
609
610   /**
611    * execute:
612    * takes in String representing a system command, executes it,
613    * and waits for it to finish.
614    *
615    * throws IOException
616    */
617   private void execute( String command ) throws IOException
618   {
619     //System.out.println( "Executing: " + command );
620     java.lang.Process proc = Runtime.getRuntime().exec( command );
621     //      System.out.println( command );
622     try {
623       proc.waitFor();
624     } catch ( InterruptedException e ) {
625       throw new IOException( "Interrupted system command: " + command );
626     }
627   }
628
629   private void printVersionInfo() throws Exception
630   {
631     System.out.println("JAFS native library build information:");
632     System.out.println("\tDescription:    " + org.openafs.jafs.VersionInfo.getDescription());
633     System.out.println("\tBuild Platform: " + org.openafs.jafs.VersionInfo.getBuildPlatform());
634     System.out.println("\tBuild Time:     " + org.openafs.jafs.VersionInfo.getBuildDate());
635     System.out.println("\tBuild Number:   " + org.openafs.jafs.VersionInfo.getBuildNumber());
636     System.out.println("\tBuild Version:  " + org.openafs.jafs.VersionInfo.getVersion());
637     System.out.println("\tFull Version:   " + org.openafs.jafs.VersionInfo.getFullVersion());
638   }
639
640   /**
641    * Get password from native library 
642    */
643   //private static native String getNativePassword();
644
645   private static String getPassword() throws IOException
646   {
647     String pwd = "";
648     MaskingThread mt = new MaskingThread( "Please enter your AFS password: " );
649     Thread t = new Thread( mt );
650     t.start();
651
652     while (true) {
653       char c = (char)System.in.read();
654       mt.stopMasking();
655       if ( c == '\r' ) {
656         c = (char) System.in.read();
657         if ( c == '\n' ) {
658           break;
659         } else {
660           continue;
661         }
662       } else if ( c == '\n' ) {
663         break;
664       } else {
665         pwd += c;
666       }
667     }
668     return pwd;
669   }
670
671   private static String getUsername()
672   {
673     System.out.print("Please enter your AFS username: ");
674     BufferedReader r = new BufferedReader( new InputStreamReader(System.in) );
675     try {
676       return r.readLine();
677     } catch (Exception e) {
678       System.out.println("Error trying to read your username.");
679       System.exit(1);
680     }
681     return null;
682   }
683
684   private static void printUsage()
685   {
686     System.out.println("Usage:");
687     System.out.println("  AdminTest [auth] [cell] [user] [group] [server]");
688     System.out.println("            [partition] [volume] [key] [process]");
689     System.out.println("");
690     System.out.println("  AdminTest [list users]      [list user <username>]");
691     System.out.println("            [list groups]     [list group <group name>]");
692     System.out.println("            [list servers]    [list server <server name>]");
693     System.out.println("            [list partitions] [list partition <partition name>]");
694     System.out.println("            [list volumes]    [list volume <volume name>]");
695     System.out.println("            [list processes]  [list process <process name>]");
696     System.out.println("            [list cell]");
697     System.out.println("            [list dir <path>]");
698     System.out.println("            [list acl <path>]");
699     System.out.println("            [set  acl <path>]");
700     System.out.println("            [enum partitions <server> <start> <length>]");
701     System.out.println("            [enum volumes <server> <partition> <start> <length>]");
702     System.out.println("            [vol salvage <volume name> <server> <partition>]");
703     System.out.println("            [vol backup <volume name> <server> <partition>]");
704     System.out.println("            [vol release <volume name> <server> <partition>]");
705     System.out.println("            [vol replicate <volume name> <server> <partition>]");
706     System.out.println("");
707     System.out.println("            -v or -version     Display version of JAFS library");
708     System.out.println("");
709     System.out.println("            -u <username>");
710     System.out.println("            -p <password>");
711     System.out.println("");
712     System.out.println("Example:");
713     System.out.println("  AdminTest list users -u admin -p testpass");
714     System.out.println("\nie: AdminTest cell\nThe above example will run the AdminTest program,\ntesting 'cell' functionality.");
715   }
716   public static void main(String[] args) throws Exception
717   {
718     AdminTest at = new AdminTest();
719     try {
720       for (int i = 0; i < args.length; i++) {
721         if (args[i].startsWith("-h") || args[i].startsWith("h")) {
722           printUsage();
723           System.exit(0);
724         } else if (args[i].equals("-u")) {
725           i++;
726           at.username = args[i];
727         } else if (args[i].equals("-p")) {
728           i++;
729           at.password = args[i];
730         } else if (args[i].equals("-g") || args[i].equals("--graphical")) {
731           i++;
732           PasswordPrompt p = new PasswordPrompt("AFS Admin Account");
733             
734           String[] info = new String[2];
735           while ( p.isShowing() ) {
736             Thread.sleep( 500 );
737           }
738           try {
739             p.setUserInfo( info );
740             at.username = info[0];
741             at.password = info[1];
742           } catch (Exception e2) {
743             // Ignore
744           }
745         } else if (args[i].equals("-v") || args[i].equals("-version")) {
746           at.authenticate();
747           at.printVersionInfo();
748           return;
749         }
750       }
751
752       if ( at.username == null ) at.username = getUsername();
753       if ( at.password == null ) at.password = getPassword();
754
755       at.authenticate();
756
757       if ( args.length > 0 ) run( at, args );
758
759       while( true )
760         {
761           System.out.print("Command: ");
762           BufferedReader r = new BufferedReader( new InputStreamReader(System.in) );
763           try {
764             run( at, split( r.readLine() ) );
765           } catch (Exception e) {
766             System.out.println("Error trying to read command.");
767           }
768         }
769        
770     } catch (ArrayIndexOutOfBoundsException e) {
771       printUsage();
772     }
773   }
774   private static void run( AdminTest at, String[] args ) throws Exception
775   {
776     try {
777       for (int i = 0; i < args.length; i++) {
778         if (args[i].equals("auth")) {
779           System.out.println("not available");
780           /*
781             } else if (args[i].equals("enum")) {
782             i++;
783             if (args[i].equals("partitions")) {
784             String serverName = args[i++];
785             int start = Integer.parseInt(args[i++]);
786             int end = Integer.parseInt(args[i++]);
787             at.enumeratePartitions(serverName, start, end);
788             } else if (args[i].equals("volumes")) {
789             String serverName = args[++i];
790             String partitionName = args[++i];
791             int start = Integer.parseInt(args[++i]);
792             int end = Integer.parseInt(args[++i]);
793             at.enumerateVolumes(serverName, partitionName, start, end);
794             }
795           */
796         } else if (args[i].equals("list")) {
797           i++;
798           if (args[i].equals("users")) {
799             at.listUsers();
800           } else if (args[i].equals("user")) {
801             i++;
802             at.listUser(args[i]);
803           } else if (args[i].equals("groups")) {
804             at.listGroups();
805           } else if (args[i].equals("groupsx")) {
806             at.listGroupsX();
807           } else if (args[i].equals("group")) {
808             i++;
809             at.listGroup(args[i]);
810           } else if (args[i].equals("servers")) {
811             at.listServers();
812           } else if (args[i].equals("server")) {
813             i++;
814             at.listServer(args[i]);
815           } else if (args[i].equals("partitions")) {
816             at.listPartitions();
817           } else if (args[i].equals("partition")) {
818             i++;
819             at.listPartition(args[i]);
820           } else if (args[i].equals("volumes")) {
821             at.listVolumes();
822           } else if (args[i].equals("volume")) {
823             i++;
824             at.listVolume(args[i]);
825           } else if (args[i].equals("processes")) {
826             at.listProcesses();
827           } else if (args[i].equals("process")) {
828             i++;
829             at.listProcess(args[i]);
830           } else if (args[i].equals("cell")) {
831             at.listCell();
832           } else if (args[i].equals("acl")) {
833             i++;
834             at.listACL(args[i]);
835           } else if (args[i].equals("dir")) {
836             i++;
837             at.listDir(args[i]);
838           }
839         } else if (args[i].equals("set")) {
840           i++;
841           if (args[i].equals("user")) {
842             i++;
843             if (args[i].equals("group-quota")) {
844               at.setUserGroupQuota();
845             }
846           } else if (args[i].equals("group")) {
847           } else if (args[i].equals("acl")) {
848             i++;
849             at.setACL(args[i]);
850           }
851         } else if (args[i].equals("vol")) {
852           i++;
853           if (args[i].equals("salvage")) {
854             i++;
855             String name = args[i];
856             i++;
857             String server = args[i];
858             i++;
859             String partition = args[i];
860             at.salvage(name, server, partition);
861           } else if (args[i].equals("backup")) {
862             i++;
863             String name = args[i];
864             i++;
865             String server = args[i];
866             i++;
867             String partition = args[i];
868             at.createBackup(name, server, partition);
869           } else if (args[i].equals("release")) {
870             i++;
871             String name = args[i];
872             i++;
873             String server = args[i];
874             i++;
875             String partition = args[i];
876             at.release(name, server, partition);
877           } else if (args[i].equals("replicate")) {
878             i++;
879             String name = args[i];
880             i++;
881             String server = args[i];
882             i++;
883             String partition = args[i];
884             at.createReadOnly(name, server, partition);
885           }
886         } else if (args[i].equals("cell")) {
887           //at.testCell();
888           System.out.println("Not Available");
889         } else if (args[i].equals("user")) {
890           //at.testUser();
891           System.out.println("Not Available");
892         } else if (args[i].equals("group")) {
893           //at.testGroup();
894           System.out.println("Not Available");
895         } else if (args[i].equals("server")) {
896           //at.testServer();
897           System.out.println("Not Available");
898         } else if (args[i].equals("partition") || args[i].equals("part")) {
899           //at.testPartition();
900           System.out.println("Not Available");
901         } else if (args[i].equals("volume")) {
902           //at.testVolume();
903           System.out.println("Not Available");
904         } else if (args[i].equals("key")) {
905           //at.testKey();
906           System.out.println("Not Available");
907         } else if (args[i].equals("process") || args[i].equals("proc")) {
908           //at.testProcess();
909           System.out.println("Not Available");
910         } else if (args[i].equals("h") || args[i].equals("help")) {
911           printUsage();
912         } else if (args[i].equals("q") || args[i].equals("quit")) {
913           System.exit(0);
914         } else if (args[i].equals("-u") || args[i].equals("-p")) {
915           i++;
916         } else if (args[i].equals("-g") || args[i].equals("--graphical")) {
917           i++;
918         } else {
919           System.out.println("Invalid Argument: " + args[i]);
920         }
921       }
922     } catch (ArrayIndexOutOfBoundsException e) {
923       printUsage();
924     }
925   }
926
927   private static String[] split( String str )
928   {
929     int i = 0;
930     StringTokenizer st = new StringTokenizer(str);
931     String[] tokens = new String[st.countTokens()];
932     while (st.hasMoreTokens())  {
933       tokens[i++] = st.nextToken().trim();
934     }
935     return tokens;
936   }
937   
938   private static class MaskingThread extends Thread
939   {
940     private boolean stop = false;
941     private int index;
942     private String prompt;
943
944     public MaskingThread( String prompt )
945     {
946       this.prompt = prompt;
947     }
948     public void run()
949     {
950       while ( !stop )
951         {
952           try {
953             MaskingThread.sleep(1);
954           } catch (InterruptedException e ) {
955             e.printStackTrace();
956           }
957           if ( !stop ) {
958             System.out.print("\r" + prompt );
959           }
960           System.out.flush();
961         }    
962     }
963     public void stopMasking()
964     {
965       this.stop = true;
966     }
967   }
968
969 }