View Javadoc

1   /*
2    * GUMS2MapUser.java
3    *
4    * Created on June 9, 2004, 1:44 PM
5    */
6   package gov.bnl.gums.admin;
7   
8   import org.apache.commons.cli.*;
9   
10  import java.io.FileNotFoundException;
11  import java.io.FileOutputStream;
12  import java.io.PrintStream;
13  
14  /**
15   * @author Gabriele Carcassi, Jay Packard
16   */
17  public abstract class GenerateMap extends RemoteCommand {
18      protected org.apache.commons.cli.Options buildOptions() {
19          Options options = new Options();
20  
21          Option mapfile = new Option("g", "gumsUrl", true,
22      		"Fully Qualified GUMS URL to override gums.location within the gums-client.properties file");
23          options.addOption(mapfile);
24          
25          mapfile = new Option("f", "file", true,
26               "saves in the specified file; prints to the console by default");
27          options.addOption(mapfile);
28  
29          return options;
30      }
31  
32      protected void execute(org.apache.commons.cli.CommandLine cmd)
33          throws Exception {
34          String hostname = null;
35          
36          String gumsUrl = cmd.getOptionValue("g", null);
37          
38          if (cmd.getArgs().length == 0) {
39              if (isUsingProxy()) {
40                  failForWrongParameters("Service DN is missing");
41              }
42                  
43              try {
44                  hostname = getClientDN();
45              } catch (Exception e) {
46                  System.err.print("Couldn't retrieve the DN of the service/host");
47                  System.exit(-1);
48              }
49          } else if (cmd.getArgs().length == 1) {
50              hostname = cmd.getArgs()[0];
51          } else {
52              failForWrongParameters("Too many arguments...");
53          }
54  
55          String file = cmd.getOptionValue("f");
56  
57          try {
58              if (file == null) {
59                  // print to std out if no filename entered
60                  System.out.println(generateMap(hostname, gumsUrl));
61              } else {
62                  PrintStream filename = new PrintStream(new FileOutputStream(
63                              file));
64  
65                  filename.print(generateMap(hostname, gumsUrl));
66              }
67          } catch (FileNotFoundException e) {
68              System.err.println("Couldn't open file " + file + ": " +
69                  e.getMessage());
70              System.exit(-1);
71          }
72      }
73  
74      protected abstract String generateMap(String hostname, String gumsUrl)
75          throws Exception;
76  }