1 package fr.in2p3.jsaga.command;
2
3 import fr.in2p3.jsaga.impl.logicalfile.LogicalDirectoryImpl;
4 import org.apache.commons.cli.*;
5 import org.ogf.saga.attributes.Attributes;
6 import org.ogf.saga.error.BadParameterException;
7 import org.ogf.saga.logicalfile.LogicalFile;
8 import org.ogf.saga.logicalfile.LogicalFileFactory;
9 import org.ogf.saga.namespace.Flags;
10 import org.ogf.saga.namespace.NSEntry;
11 import org.ogf.saga.session.Session;
12 import org.ogf.saga.session.SessionFactory;
13 import org.ogf.saga.url.URL;
14 import org.ogf.saga.url.URLFactory;
15
16 import java.util.HashMap;
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class NSLogicalMetaData extends AbstractCommand {
31 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
32 private static final String OPT_GET = "g", LONGOPT_GET = "get";
33 private static final String OPT_SET = "s", LONGOPT_SET = "set";
34 private static final String OPT_REMOVE = "r", LONGOPT_REMOVE = "remove";
35 private static final String OPT_LIST = "l", LONGOPT_LIST = "list";
36 private static final String OPT_LIST_ALL_KEYS = "k", LONGOPT_LIST_ALL_KEYS = "list-all-keys";
37 private static final String OPT_LIST_ALL_VALUES = "v", LONGOPT_LIST_ALL_VALUES = "list-all-values";
38
39 public NSLogicalMetaData() {
40 super("jsaga-logical-metadata", new String[]{"URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
41 }
42
43 public static void main(String[] args) throws Exception {
44 NSLogicalMetaData command = new NSLogicalMetaData();
45 CommandLine line = command.parse(args);
46 if (line.hasOption(OPT_HELP))
47 {
48 command.printHelpAndExit(null);
49 }
50 else
51 {
52
53 String arg = command.m_nonOptionValues[0];
54 URL url = URLFactory.createURL(arg);
55
56
57 Session session = SessionFactory.createSession(true);
58 Attributes entry;
59 if (url.getPath().endsWith("/")) {
60 entry = LogicalFileFactory.createLogicalDirectory(session, url, Flags.NONE.getValue());
61 } else {
62 entry = LogicalFileFactory.createLogicalFile(session, url, Flags.NONE.getValue());
63 }
64
65
66 if (line.hasOption(OPT_GET)) {
67 String key = line.getOptionValue(OPT_GET);
68 if (entry.isVectorAttribute(key)) {
69 String[] values = entry.getVectorAttribute(key);
70 for (int v=0; v<values.length; v++) {
71 System.out.println(values[v]);
72 }
73 } else {
74 System.out.println(entry.getAttribute(key));
75 }
76 } else if (line.hasOption(OPT_SET)) {
77 String[] array = line.getOptionValues(OPT_SET);
78 String key = array[0];
79 String[] values = array[1].split(",");
80 switch (values.length) {
81 case 0:
82 throw new Exception("Option "+OPT_SET+" requires at least 2 arguments: <key> <value>*");
83 case 1:
84 entry.setAttribute(key, values[0]);
85 break;
86 default:
87 entry.setVectorAttribute(key, values);
88 break;
89 }
90 } else if (line.hasOption(OPT_REMOVE)) {
91 String key = line.getOptionValue(OPT_REMOVE);
92 entry.removeAttribute(key);
93 } else if (line.hasOption(OPT_LIST)) {
94 String[] keys = entry.listAttributes();
95 for (int i=0; i<keys.length; i++) {
96 System.out.print(keys[i]+" = ");
97 if (entry.isVectorAttribute(keys[i])) {
98 String[] values = entry.getVectorAttribute(keys[i]);
99 for (int v=0; v<values.length; v++) {
100 System.out.println(indent(v==0 ? 0 : keys[i].length()+3) + values[v]);
101 }
102 } else {
103 System.out.println(entry.getAttribute(keys[i]));
104 }
105 }
106 } else if (line.hasOption(OPT_LIST_ALL_KEYS)) {
107 if (entry instanceof LogicalFile) {
108 throw new BadParameterException("Option -"+OPT_LIST_ALL_KEYS+" requires path to end with a '/'");
109 }
110 String[] keys = ((LogicalDirectoryImpl)entry).listAttributesRecursive(new HashMap<String,String>());
111 for (int i=0; i<keys.length; i++) {
112 System.out.println(keys[i]);
113 }
114 } else if (line.hasOption(OPT_LIST_ALL_VALUES)) {
115 String key = line.getOptionValue(OPT_LIST_ALL_VALUES);
116 if (entry instanceof LogicalFile) {
117 throw new BadParameterException("Option -"+OPT_LIST_ALL_VALUES+" requires path to end with a '/'");
118 }
119 String[] values = ((LogicalDirectoryImpl)entry).listAttributeValuesRecursive(key, new HashMap<String,String>());
120 for (int i=0; i<values.length; i++) {
121 System.out.println(values[i]);
122 }
123 }
124
125
126 ((NSEntry)entry).close();
127 System.exit(0);
128 }
129 }
130
131 private static String indent(int indent) {
132 StringBuffer buffer = new StringBuffer();
133 for (int i=0; i<indent; i++) {
134 buffer.append(' ');
135 }
136 return buffer.toString();
137 }
138
139 protected Options createOptions() {
140 Options opt = new Options();
141
142
143 OptionGroup group = new OptionGroup();
144 group.setRequired(true);
145 {
146 group.addOption(OptionBuilder.withDescription("Display this help and exit")
147 .withLongOpt(LONGOPT_HELP)
148 .create(OPT_HELP));
149 group.addOption(OptionBuilder.withDescription("Get meta-data <key>")
150 .hasArg()
151 .withArgName("key")
152 .withLongOpt(LONGOPT_GET)
153 .create(OPT_GET));
154 group.addOption(OptionBuilder.withDescription("Set meta-data <key> with comma-separated values (spaces not allowed)")
155 .hasArgs(2)
156 .withArgName("key values")
157 .withLongOpt(LONGOPT_SET)
158 .create(OPT_SET));
159 group.addOption(OptionBuilder.withDescription("Remove meta-data <key>")
160 .hasArg()
161 .withArgName("key")
162 .withLongOpt(LONGOPT_REMOVE)
163 .create(OPT_REMOVE));
164 group.addOption(OptionBuilder.withDescription("List meta-data <key>-<value> pairs")
165 .withLongOpt(LONGOPT_LIST)
166 .create(OPT_LIST));
167 group.addOption(OptionBuilder.withDescription("List all meta-data keys")
168 .withLongOpt(LONGOPT_LIST_ALL_KEYS)
169 .create(OPT_LIST_ALL_KEYS));
170 group.addOption(OptionBuilder.withDescription("List all meta-data values for <key>")
171 .hasArg()
172 .withArgName("key")
173 .withLongOpt(LONGOPT_LIST_ALL_VALUES)
174 .create(OPT_LIST_ALL_VALUES));
175 }
176 opt.addOptionGroup(group);
177
178 return opt;
179 }
180 }