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