1 package fr.in2p3.jsaga.command;
2
3 import fr.in2p3.jsaga.impl.context.*;
4 import org.apache.commons.cli.*;
5 import org.ogf.saga.context.Context;
6 import org.ogf.saga.error.NotImplementedException;
7 import org.ogf.saga.error.SagaException;
8 import org.ogf.saga.session.Session;
9 import org.ogf.saga.session.SessionFactory;
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class ContextInfo extends AbstractCommand {
24 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
25 private static final String OPT_ATTRIBUTE = "a", LONGOPT_ATTRIBUTE = "attribute";
26
27 public ContextInfo() {
28 super("jsaga-context-info", new String[]{"contextId"}, null);
29 }
30
31 public static void main(String[] args) throws Exception {
32 ContextInfo command = new ContextInfo();
33 CommandLine line = command.parse(args);
34 if (line.hasOption(OPT_HELP))
35 {
36 command.printHelpAndExit(null);
37 }
38 else {
39
40 Session session = SessionFactory.createSession(false);
41 ConfiguredContext[] configContexts = ConfigurableContextFactory.listConfiguredContext();
42 for (int i=0; i<configContexts.length; i++) {
43 if (command.m_nonOptionValues.length==0
44 || command.m_nonOptionValues[0].equals(configContexts[i].getUrlPrefix())
45 || command.m_nonOptionValues[0].equals(configContexts[i].getType()))
46 {
47 Context context = ConfigurableContextFactory.createContext(configContexts[i]);
48
49 System.out.println("Security context: "+getLabel(context));
50
51 print(session, context, line);
52 }
53 }
54 session.close();
55 }
56 }
57
58 public static String getLabel(Context context) throws SagaException {
59 return context.getAttribute(ContextImpl.URL_PREFIX)+" ("+context.getAttribute(Context.TYPE)+")";
60 }
61 private static void print(Session session, Context context, CommandLine line) {
62 try {
63 if (line.hasOption(OPT_ATTRIBUTE)) {
64 try {
65 System.out.println(" "+context.getAttribute(line.getOptionValue(OPT_ATTRIBUTE)));
66 } catch(NotImplementedException e) {
67 System.out.println(" Attribute not supported ["+e.getMessage()+"]");
68 }
69 } else {
70 session.addContext(context);
71 System.out.print(context);
72 }
73 } catch(Exception e) {
74 System.out.println(" Context not initialized ["+e.getMessage()+"]");
75 }
76 System.out.println();
77 }
78
79 protected Options createOptions() {
80 Options opt = new Options();
81
82
83 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
84 .withLongOpt(LONGOPT_HELP)
85 .create(OPT_HELP));
86
87
88 opt.addOption(OptionBuilder.withDescription("Query context instance(s) for attribute <attr> only")
89 .withArgName("attr")
90 .hasArg()
91 .withLongOpt(LONGOPT_ATTRIBUTE)
92 .create(OPT_ATTRIBUTE));
93
94
95 opt.addOption(OptionBuilder.withDescription("Set context instance attribute (e.g. -DVOMS[0].UserVO=dteam)")
96 .withArgName("ctxId>.<attr>=<value")
97 .hasArg()
98 .withValueSeparator()
99 .create("D"));
100 return opt;
101 }
102 }