View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import fr.in2p3.jsaga.EngineProperties;
4   import fr.in2p3.jsaga.engine.session.SessionConfiguration;
5   import fr.in2p3.jsaga.engine.descriptors.*;
6   import fr.in2p3.jsaga.engine.schema.config.Execution;
7   import fr.in2p3.jsaga.engine.schema.config.Protocol;
8   import fr.in2p3.jsaga.helpers.ASCIITableFormatter;
9   import fr.in2p3.jsaga.impl.context.ContextImpl;
10  import org.apache.commons.cli.*;
11  import org.ogf.saga.context.Context;
12  import org.ogf.saga.error.DoesNotExistException;
13  import org.ogf.saga.session.Session;
14  import org.ogf.saga.session.SessionFactory;
15  
16  import java.net.URL;
17  import java.util.*;
18  
19  /* ***************************************************
20   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
21   * ***             http://cc.in2p3.fr/             ***
22   * ***************************************************
23   * File:   Help
24   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
25   * Date:   6 avr. 2007
26   * ***************************************************
27   * Description:                                      */
28  /**
29   *
30   */
31  public class Help extends AbstractCommand {
32      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
33      private static final String OPT_VERSION = "v", LONGOPT_VERSION = "version";
34      private static final String OPT_SECURITY = "s", LONGOPT_SECURITY = "security",
35              ARG_SECURITY_USAGE = "usage", ARG_SECURITY_DEFAULT = "default", ARG_SECURITY_MISSING = "missing",
36              USAGE_OPT_SECURITY = "<mode> = "+ARG_SECURITY_USAGE+" | "+ ARG_SECURITY_DEFAULT +" | "+ARG_SECURITY_MISSING;
37      private static final String OPT_DATA = "d", LONGOPT_DATA = "data";
38      private static final String OPT_JOB = "j", LONGOPT_JOB = "job";
39      private static final String LONGOPT_DUMP_ADAPTORS = "adaptors";
40      private static final String LONGOPT_DUMP_SESSION = "session";
41      private static final String LONGOPT_DUMP_CONFIG = "config";
42  
43      public Help() {
44          super("jsaga-help");
45      }
46  
47      public static void main(String[] args) throws Exception {
48          Help command = new Help();
49          CommandLine line = command.parse(args);
50          if (line.hasOption(OPT_HELP))
51          {
52              command.printHelpAndExit(null);
53          }
54          else if (line.hasOption(OPT_VERSION))
55          {
56              Package p = Help.class.getPackage();
57              System.out.println("JSAGA Engine");
58              System.out.println(p.getImplementationVendor());
59              System.out.println();
60              System.out.println("SAGA  specification  version: "+p.getSpecificationVersion());
61              System.out.println("JSAGA implementation version: "+p.getImplementationVersion());
62  
63          }
64          else if (line.hasOption(OPT_SECURITY))
65          {
66              String arg = line.getOptionValue(OPT_SECURITY);
67  
68              Session session = SessionFactory.createSession(true);
69              String LEGENDE = "\nwhere:\n"+
70                      "\t_Attribute_\tcan not be entered from the prompt\n"+
71                      "\t*Attribute*\tis a hidden attribute\n"+
72                      "\t<Attribute>\tis a path to an existing file or directory\n"+
73                      "\t[Attribute]\tis an optional attribute\n";
74              if (arg.equals(ARG_SECURITY_USAGE)) {
75                  ASCIITableFormatter formatter = new ASCIITableFormatter(new String[] {
76                          "Type", "Attributes usage"});
77                  for (Context context : session.listContexts()) {
78                      String type = context.getAttribute(Context.TYPE);
79                      String usage = ((ContextImpl) context).getUsage();
80                      formatter.append(new String[] {type, usage});
81                  }
82                  formatter.dump(System.out);
83                  System.out.print(LEGENDE);
84              } else if (arg.equals(ARG_SECURITY_DEFAULT)) {
85                  ASCIITableFormatter formatter = new ASCIITableFormatter(new String[] {
86                          "Type", "Default attributes"});
87                  Set<String> ignored = new HashSet<String>();
88                  ignored.addAll(Arrays.asList(Context.TYPE, ContextImpl.URL_PREFIX,
89                          ContextImpl.BASE_URL_INCLUDES, ContextImpl.BASE_URL_EXCLUDES,
90                          ContextImpl.JOB_SERVICE_ATTRIBUTES, ContextImpl.DATA_SERVICE_ATTRIBUTES));
91                  for (Context context : session.listContexts()) {
92                      String type = context.getAttribute(Context.TYPE);
93                      boolean first = true;
94                      for (String key : context.listAttributes()) {
95                          if (! ignored.contains(key)) {
96                              formatter.append(new String[] {
97                                      (first ? type : null),
98                                      key+"="+((ContextImpl)context).getDefault(key)});
99                              first = false;
100                         }
101                     }
102                 }
103                 formatter.dump(System.out);
104             } else if (arg.equals(ARG_SECURITY_MISSING)) {
105                 ASCIITableFormatter formatter = new ASCIITableFormatter(new String[] {
106                         "Type", "Missing attributes"});
107                 for (Context context : session.listContexts()) {
108                     String type = context.getAttribute(Context.TYPE);
109                     String missing = ((ContextImpl) context).getMissings();
110                     formatter.append(new String[] {
111                             type,
112                             (missing!=null ? missing.toString() : null)});
113                 }
114                 formatter.dump(System.out);
115                 System.out.print(LEGENDE);
116             } else {
117                 command.printHelpAndExit("Missing required argument: "+ USAGE_OPT_SECURITY);
118             }
119         }
120         else if (line.hasOption(OPT_DATA))
121         {
122             DataAdaptorDescriptor descriptor = AdaptorDescriptors.getInstance().getDataDesc();
123             ASCIITableFormatter formatter = new ASCIITableFormatter(new String[] {
124                     "Scheme", "Supported contexts"});
125             for (Protocol protocol : descriptor.getXML()) {
126                 formatter.append(new String[] {
127                         protocol.getType(),
128                         Arrays.toString(protocol.getSupportedContextType())});
129             }
130             formatter.dump(System.out);
131         }
132         else if (line.hasOption(OPT_JOB))
133         {
134             JobAdaptorDescriptor descriptor = AdaptorDescriptors.getInstance().getJobDesc();
135             ASCIITableFormatter formatter = new ASCIITableFormatter(new String[] {
136                     "Scheme", "Supported contexts"});
137             for (Execution execution : descriptor.getXML()) {
138                 formatter.append(new String[] {
139                         execution.getType(),
140                         Arrays.toString(execution.getSupportedContextType())});
141             }
142             formatter.dump(System.out);
143         }
144         else if (line.hasOption(LONGOPT_DUMP_ADAPTORS))
145         {
146             System.out.println(new String(AdaptorDescriptors.getInstance().toByteArray()));
147         }
148         else if (line.hasOption(LONGOPT_DUMP_SESSION))
149         {
150             Session session = SessionFactory.createSession(true);
151             for (org.ogf.saga.context.Context context : session.listContexts()) {
152                 System.out.println("-------------------------");
153                 for (String key : context.listAttributes()) {
154                     try {
155                         if (context.isVectorAttribute(key)) {
156                             System.out.println(key+"="+Arrays.toString(context.getVectorAttribute(key)));
157                         } else {
158                             System.out.println(key+"="+context.getAttribute(key));
159                         }
160                     } catch (DoesNotExistException e) {
161                         System.out.println(key+"=[NOT INITIALIZED]");
162                     }
163                 }
164             }
165         }
166         else if (line.hasOption(LONGOPT_DUMP_CONFIG))
167         {
168             // WARNING: this code is JSAGA specific
169             URL url = EngineProperties.getURL(EngineProperties.JSAGA_DEFAULT_CONTEXTS);
170             SessionConfiguration cfg = new SessionConfiguration(url);
171             System.out.println(cfg.toXML());
172         }
173     }
174 
175     protected Options createOptions() {
176         Options opt = new Options();
177 
178         // command group
179         OptionGroup group = new OptionGroup();
180         group.setRequired(true);
181         {
182             group.addOption(OptionBuilder.withDescription("Display this help and exit")
183                     .withLongOpt(LONGOPT_HELP)
184                     .create(OPT_HELP));
185             group.addOption(OptionBuilder.withDescription("Output version information and exit")
186                     .withLongOpt(LONGOPT_VERSION)
187                     .create(OPT_VERSION));
188             group.addOption(OptionBuilder.withDescription("Information about security context instances.\n"+ USAGE_OPT_SECURITY)
189                     .withLongOpt(LONGOPT_SECURITY)
190                     .withArgName("mode")
191                     .hasArg()
192                     .create(OPT_SECURITY));
193             group.addOption(OptionBuilder.withDescription("Information about data protocols.")
194                     .withLongOpt(LONGOPT_DATA)
195                     .create(OPT_DATA));
196             group.addOption(OptionBuilder.withDescription("Information about job services.")
197                     .withLongOpt(LONGOPT_JOB)
198                     .create(OPT_JOB));
199             group.addOption(OptionBuilder.withDescription("Dump information about adaptors as XML")
200                     .withLongOpt(LONGOPT_DUMP_ADAPTORS)
201                     .create());
202             group.addOption(OptionBuilder.withDescription("Dump the default session")
203                     .withLongOpt(LONGOPT_DUMP_SESSION)
204                     .create());
205             group.addOption(OptionBuilder.withDescription("Dump the effective XML configuration")
206                     .withLongOpt(LONGOPT_DUMP_CONFIG)
207                     .create());
208         }
209         opt.addOptionGroup(group);
210 
211         // system properties
212         opt.addOption(OptionBuilder.withDescription("Set context instance attribute (e.g. -DVOMS[0].UserVO=dteam)")
213                 .withArgName("ctxId>.<attr>=<value")
214                 .hasArg()
215                 .withValueSeparator()
216                 .create("D"));
217         return opt;
218     }
219 }