This project has retired. For details please refer to its
Attic page.
Commander xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.commander;
20
21 import java.io.FileInputStream;
22 import java.io.PrintWriter;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import org.apache.chemistry.opencmis.client.bindings.CmisBindingFactory;
30 import org.apache.chemistry.opencmis.commons.SessionParameter;
31 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
32 import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
33
34
35
36
37
38
39
40 public class Commander {
41
42 private static final Map<String, Command> COMMAND_MAP = new LinkedHashMap<String, Command>();
43 static {
44 addCommand(new InfosCommand());
45 addCommand(new ListCommand());
46 addCommand(new DeleteCommand());
47 }
48
49 private final PrintWriter fPW;
50
51
52
53
54 public Commander(String[] args) {
55 fPW = new PrintWriter(System.out);
56
57 if (args.length < 2) {
58 printUsage(fPW);
59 return;
60 }
61
62 try {
63
64 Command command = COMMAND_MAP.get(args[1].toLowerCase());
65 if (command == null) {
66 printUsage(fPW);
67 return;
68 }
69
70
71 CmisBinding binding = createBinding(args[0]);
72
73
74 String[] commandArgs = new String[args.length - 2];
75 System.arraycopy(args, 2, commandArgs, 0, commandArgs.length);
76
77
78 command.execute(binding, commandArgs, fPW);
79 } catch (Exception e) {
80 fPW.println("Exception:");
81
82 if (e instanceof CmisBaseException) {
83 fPW.println(e);
84 } else {
85 e.printStackTrace(fPW);
86 }
87 } finally {
88 fPW.flush();
89 }
90 }
91
92
93
94
95 private static void printUsage(PrintWriter output) {
96 output.println("CMIS Commander\n");
97 output.println("Usage: Commander <config file> <command>\n");
98 output.println("Available commands:");
99 for (Command command : COMMAND_MAP.values()) {
100 output.println(" " + command.getUsage());
101 }
102
103 output.flush();
104 }
105
106
107
108
109 private static CmisBinding createBinding(String configFile) throws Exception {
110 Properties properties = new Properties();
111 properties.load(new FileInputStream(configFile));
112
113 Map<String, String> sessionParameters = new HashMap<String, String>();
114
115 for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) {
116 String key = (String) e.nextElement();
117 String value = properties.getProperty(key);
118 sessionParameters.put(key, value);
119 }
120
121 CmisBindingFactory factory = CmisBindingFactory.newInstance();
122
123 CmisBinding result = null;
124 if (sessionParameters.containsKey(SessionParameter.ATOMPUB_URL)) {
125 result = factory.createCmisAtomPubBinding(sessionParameters);
126 } else if (sessionParameters.containsKey(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE)) {
127 result = factory.createCmisWebServicesBinding(sessionParameters);
128 } else {
129 throw new IllegalArgumentException("Cannot find CMIS binding information in config file!");
130 }
131
132 return result;
133 }
134
135
136
137
138 private static final void addCommand(Command command) {
139 if ((command == null) || (command.getCommandName() == null)) {
140 return;
141 }
142
143 COMMAND_MAP.put(command.getCommandName().toLowerCase(), command);
144 }
145
146
147
148
149 public static void main(String[] args) {
150 new Commander(args);
151 }
152 }