~ubuntu-branches/ubuntu/trusty/ivy/trusty

« back to all changes in this revision

Viewing changes to src/example/multi-project/projects/console/src/console/Main.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 *  contributor license agreements.  See the NOTICE file distributed with
 
4
 *  this work for additional information regarding copyright ownership.
 
5
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 *  (the "License"); you may not use this file except in compliance with
 
7
 *  the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *  Unless required by applicable law or agreed to in writing, software
 
12
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing permissions and
 
15
 *  limitations under the License.
 
16
 *
 
17
 */
 
18
package console;
 
19
 
 
20
import java.io.BufferedReader;
 
21
import java.io.InputStreamReader;
 
22
import java.util.Collection;
 
23
import java.util.Arrays;
 
24
import java.lang.reflect.Method;
 
25
 
 
26
 
 
27
public final class Main {
 
28
    private static final Collection QUIT_COMMANDS = 
 
29
        Arrays.asList(new String[] {"quit", "q", "exit"});
 
30
    private static final Collection HELP_COMMANDS = 
 
31
        Arrays.asList(new String[] {"help", "h", "?"});
 
32
 
 
33
    public static void main(String[] a) throws Exception {
 
34
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 
35
      while (true) {
 
36
        System.out.print("> ");
 
37
        String command = in.readLine().trim();
 
38
        if (QUIT_COMMANDS.contains(command)) {
 
39
          return;
 
40
        }
 
41
        if (HELP_COMMANDS.contains(command)) {
 
42
          help();
 
43
          continue;
 
44
        }
 
45
        String[] split = command.split(" ");
 
46
        if (split.length == 0) {
 
47
          error(command);
 
48
          continue;
 
49
        }
 
50
        
 
51
        try {
 
52
          String[] args = new String[split.length - 1];
 
53
          System.arraycopy(split, 1, args, 0, args.length);
 
54
          Class cl = Class.forName(split[0] + ".Main");
 
55
          Method m = cl.getMethod("main", new Class[] {String[].class});
 
56
          m.invoke(null, new Object[] {args});
 
57
        } catch (Exception ex) {
 
58
          error(command);
 
59
          continue;
 
60
        }
 
61
      }
 
62
    }
 
63
    
 
64
    private static void help() {
 
65
      System.out.println("available commands:");
 
66
      System.out.println("\tquit: quit the console");
 
67
      System.out.println("\thelp: displays this message");
 
68
      System.out.println("\tlist -dir <dir>: list files in given directory");
 
69
      System.out.println("\tfind -dir <dir> -name <name>: list files with given name in given dir");
 
70
      System.out.println("\tsizewhere -dir <dir> -name <name>: "
 
71
           + "compute total size of files with given name in given dir");
 
72
      System.out.println("\thelp: displays this message");
 
73
    }
 
74
            
 
75
    private static void error(String command) {
 
76
      System.out.println("unknown command " + command);
 
77
      System.out.println("type ? for help");
 
78
    }
 
79
 
 
80
    private Main() {
 
81
    }
 
82
}