blob: 1a73a00500b2ca57e7664c6b20dce0e731f9aade [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 */
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000019// DWB8: throw IllegatlStateException if session used after closed (as per rfc132)
20// DWB9: there is no API to list all variables: https://www.osgi.org/bugzilla/show_bug.cgi?id=49
21// DWB10: add SCOPE support: https://www.osgi.org/bugzilla/show_bug.cgi?id=51
Richard S. Hallaf656a02009-06-11 16:07:20 +000022package aQute.shell.runtime;
23
24import java.io.*;
25import java.lang.reflect.*;
26import java.util.*;
27
28import org.osgi.service.command.*;
29
30public class CommandSessionImpl implements CommandSession, Converter {
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000031 String COLUMN = "%-20s %s\n";
32 InputStream in;
33 PrintStream out;
34 PrintStream err;
35 CommandShellImpl service;
36 Map<Object, Object> variables = new HashMap<Object, Object>();
37 private boolean closed; // derek
Richard S. Hallaf656a02009-06-11 16:07:20 +000038
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000039 CommandSessionImpl(CommandShellImpl service, InputStream in, PrintStream out, PrintStream err) {
40 this.service = service;
41 this.in = in;
42 this.out = out;
43 this.err = err;
44 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000045
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000046 public void close() {
47 this.closed = true; // derek
48 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000049
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000050 public Object execute(CharSequence commandline) throws Exception {
51 assert service != null;
52 assert service.threadIO != null;
53
54 if (closed)
55 throw new IllegalStateException("session is closed"); // derek
Richard S. Hallaf656a02009-06-11 16:07:20 +000056
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000057 Closure impl = new Closure(this, null, commandline);
58 Object result = impl.execute(this, null);
59 return result;
60 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000061
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000062 public InputStream getKeyboard() {
63 return in;
64 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000065
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000066 public Object get(String name) {
67 // XXX: derek.baum@paremus.com
68 // there is no API to list all variables, so overload name == null
69 if (name == null)
70 return variables.keySet();
Richard S. Hallaf656a02009-06-11 16:07:20 +000071
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000072 if (variables != null && variables.containsKey(name))
73 return variables.get(name);
Richard S. Hallaf656a02009-06-11 16:07:20 +000074
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000075 // XXX: derek: add SCOPE support
76 if (name.startsWith("*:")) {
77 String path = variables.containsKey("SCOPE") ? variables.get("SCOPE").toString()
78 : "osgi:*";
79 String func = name.substring(2);
80 for (String scope : path.split(":")) {
81 Object result = service.get(scope + ":" + func);
82 if (result != null)
83 return result;
84 }
85 return null;
86 }
87 return service.get(name);
88 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000089
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000090 public void put(String name, Object value) {
91 variables.put(name, value);
92 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000093
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000094 public PrintStream getConsole() {
95 return out;
96 }
Richard S. Hallaf656a02009-06-11 16:07:20 +000097
Guillaume Nodetb4e055d2009-06-22 15:21:30 +000098 @SuppressWarnings("unchecked")
99 public CharSequence format(Object target, int level, Converter escape) throws Exception {
100 if (target == null)
101 return "null";
Richard S. Hallaf656a02009-06-11 16:07:20 +0000102
Guillaume Nodetb4e055d2009-06-22 15:21:30 +0000103 if (target instanceof CharSequence)
104 return (CharSequence) target;
Richard S. Hallaf656a02009-06-11 16:07:20 +0000105
Guillaume Nodetb4e055d2009-06-22 15:21:30 +0000106 for (Converter c : service.converters) {
107 CharSequence s = c.format(target, level, this);
108 if (s != null)
109 return s;
110 }
Richard S. Hallaf656a02009-06-11 16:07:20 +0000111
Guillaume Nodetb4e055d2009-06-22 15:21:30 +0000112 if (target.getClass().isArray()) {
113 if (target.getClass().getComponentType().isPrimitive()) {
114 if (target.getClass().getComponentType() == boolean.class)
115 return Arrays.toString((boolean[]) target);
116 else if (target.getClass().getComponentType() == byte.class)
117 return Arrays.toString((byte[]) target);
118 else if (target.getClass().getComponentType() == short.class)
119 return Arrays.toString((short[]) target);
120 else if (target.getClass().getComponentType() == int.class)
121 return Arrays.toString((int[]) target);
122 else if (target.getClass().getComponentType() == long.class)
123 return Arrays.toString((long[]) target);
124 else if (target.getClass().getComponentType() == float.class)
125 return Arrays.toString((float[]) target);
126 else if (target.getClass().getComponentType() == double.class)
127 return Arrays.toString((double[]) target);
128 else if (target.getClass().getComponentType() == char.class)
129 return Arrays.toString((char[]) target);
130 }
131 target = Arrays.asList((Object[]) target);
132 }
133 if (target instanceof Collection) {
134 if (level == Converter.INSPECT) {
135 StringBuilder sb = new StringBuilder();
136 Collection<?> c = (Collection<?>) target;
137 for (Object o : c) {
138 sb.append(format(o, level + 1, this));
139 sb.append("\n");
140 }
141 return sb;
142 }
143 else if (level == Converter.LINE) {
144 StringBuilder sb = new StringBuilder();
145 String del = "[";
146 Collection<?> c = (Collection<?>) target;
147 for (Object o : c) {
148 sb.append(del);
149 sb.append(format(o, level + 1, this));
150 del = ", ";
151 }
152 sb.append("]");
153 return sb;
154 }
155 }
156 if (target instanceof Dictionary) {
157 Map<Object, Object> result = new HashMap<Object, Object>();
158 for (Enumeration e = ((Dictionary) target).keys(); e.hasMoreElements();) {
159 Object key = e.nextElement();
160 result.put(key, ((Dictionary) target).get(key));
161 }
162 target = result;
163 }
164 if (target instanceof Map) {
165 if (level == Converter.INSPECT) {
166 StringBuilder sb = new StringBuilder();
167 Map<?, ?> c = (Map<?, ?>) target;
168 for (Map.Entry<?, ?> entry : c.entrySet()) {
169 CharSequence key = format(entry.getKey(), level + 1, this);
170 sb.append(key);
171 for (int i = key.length(); i < 20; i++)
172 sb.append(' ');
173 sb.append(format(entry.getValue(), level + 1, this));
174 sb.append("\n");
175 }
176 return sb;
177 }
178 else if (level == Converter.LINE) {
179 StringBuilder sb = new StringBuilder();
180 String del = "[";
181 Map<?, ?> c = (Map<?, ?>) target;
182 for (Map.Entry<?, ?> entry : c.entrySet()) {
183 sb.append(del);
184 sb.append(format(entry, level + 1, this));
185 del = ", ";
186 }
187 sb.append("]");
188 return sb;
189 }
190 }
191 if (level == Converter.INSPECT)
192 return inspect(target);
193 else
194 return target.toString();
195 }
196
197 CharSequence inspect(Object b) {
198 boolean found = false;
199 Formatter f = new Formatter();
200 Method methods[] = b.getClass().getMethods();
201 for (Method m : methods) {
202 try {
203 String name = m.getName();
204 if (m.getName().startsWith("get") && !m.getName().equals("getClass")
205 && m.getParameterTypes().length == 0 && Modifier.isPublic(m.getModifiers())) {
206 found = true;
207 name = name.substring(3);
208 m.setAccessible(true);
209 Object value = m.invoke(b, (Object[]) null);
210 f.format(COLUMN, name, format(value, Converter.LINE, this));
211 }
212 } catch (IllegalAccessException e) {
213 // Ignore
214 } catch (Exception e) {
215 e.printStackTrace();
216 }
217 }
218 if (found)
219 return (StringBuilder) f.out();
220 else
221 return b.toString();
222 }
223
224 public Object convert(Class<?> desiredType, Object in) {
225 return service.convert(desiredType, in);
226 }
227
228 public CharSequence format(Object result, int inspect) {
229 try {
230 return format(result, inspect, this);
231 } catch (Exception e) {
232 return "<can not format " + result + ":" + e;
233 }
234 }
Richard S. Hallaf656a02009-06-11 16:07:20 +0000235
236}