This project has retired. For details please refer to its Attic page.
Commander xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Commander tool main.
36   *
37   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
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       * Constructor.
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              // get the command object
64              Command command = COMMAND_MAP.get(args[1].toLowerCase());
65              if (command == null) {
66                  printUsage(fPW);
67                  return;
68              }
69  
70              // get provider object
71              CmisBinding binding = createBinding(args[0]);
72  
73              // prepare args
74              String[] commandArgs = new String[args.length - 2];
75              System.arraycopy(args, 2, commandArgs, 0, commandArgs.length);
76  
77              // execute
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       * Prints usage.
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      * Creates the provider object
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      * Adds a command
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      * Main.
148      */
149     public static void main(String[] args) {
150         new Commander(args);
151     }
152 }