blob: 53263439b7c8c58a1d21ebf1961158cbd529013e [file] [log] [blame]
Richard S. Hallaf656a02009-06-11 16:07:20 +00001/*
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
20package aQute.shell.runtime;
21
22import java.io.*;
23import java.lang.reflect.*;
24import java.util.*;
25
26import org.osgi.service.command.*;
27import org.osgi.service.threadio.*;
28
29public class CommandShellImpl implements CommandProcessor {
30 Set<Converter> converters = new HashSet<Converter>();
31 protected ThreadIO threadIO;
32 public final static Object NO_SUCH_COMMAND = new Object();
33 Map<String, Object> commands = new LinkedHashMap<String, Object>();
34
35 public CommandShellImpl() {
36 addCommand("shell", this, "addCommand" );
37 }
38
39 public CommandSession createSession(InputStream in, PrintStream out,
40 PrintStream err) {
41
42 return new CommandSessionImpl(this, in, out, err);
43 }
44
45 public void setThreadio(ThreadIO threadIO) {
46 this.threadIO = threadIO;
47 }
48
49 public void setConverter(Converter c) {
50 converters.add(c);
51 }
52
53 public void unsetConverter(Converter c) {
54 converters.remove(c);
55 }
56
57 public Object get(String name) {
58 name = name.toLowerCase();
59 int n = name.indexOf(':');
60 if (n < 0)
61 return null;
62
63 String function = name.substring(n);
64
65 Object cmd = null;
66
67 if (commands.containsKey(name)) {
68 cmd = commands.get(name);
69 } else {
70 String scope = name.substring(0, n);
71 if (scope.equals("*")) {
72 for (Map.Entry<String, Object> entry : commands.entrySet()) {
73 if (entry.getKey().endsWith(function)) {
74 cmd = entry.getValue();
75 break;
76 }
77 }
78 }
79 }
80 if (cmd == null)
81 return null;
82
83 if (cmd instanceof Function)
84 return cmd;
85 else
86 return new Command(cmd, function.substring(1));
87 }
88
89 public void addCommand(String scope, Object target) {
90 addCommand(scope,target,target.getClass());
91 }
92
93 public void addCommand(String scope, Object target, Class<?> functions) {
94 if (target == null)
95 return;
96
97 String[] names = getFunctions(functions);
98 for (String function : names) {
99 addCommand(scope, target, function);
100 }
101 }
102
103 public void addCommand(String scope, Object target, String function) {
104 commands.put((scope + ":" + function).toLowerCase(), target);
105 }
106
107 public String[] getFunctions(Class<?> target) {
108 String[] functions;
109 Set<String> list = new TreeSet<String>();
110 Method methods[] = target.getMethods();
111 for (Method m : methods) {
112 list.add(m.getName());
113 if (m.getName().startsWith("get")) {
114 String s = m.getName().substring(3);
115 if (s.length() > 0)
116 list.add(s.substring(0, 1).toLowerCase() + s.substring(1));
117 }
118 }
119 functions = list.toArray(new String[list.size()]);
120 return functions;
121 }
122
123 protected void put(String name, Object target) {
124 commands.put(name, target);
125 }
126
127 public Object convert(Class<?> desiredType, Object in) {
128 for ( Converter c : converters ) {
129 try {
130 Object converted = c.convert(desiredType, in);
131 if ( converted != null)
132 return converted;
133 } catch( Exception e ) {
134 e.printStackTrace();
135 }
136 }
137 return null;
138 }
139
140}