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