Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 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 | */ |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 19 | // 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 22 | package aQute.shell.runtime; |
| 23 | |
| 24 | import java.io.*; |
| 25 | import java.lang.reflect.*; |
| 26 | import java.util.*; |
| 27 | |
| 28 | import org.osgi.service.command.*; |
| 29 | |
| 30 | public class CommandSessionImpl implements CommandSession, Converter { |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 31 | 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 38 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 39 | 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 45 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 46 | public void close() { |
| 47 | this.closed = true; // derek |
| 48 | } |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 49 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 50 | 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 56 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 57 | Closure impl = new Closure(this, null, commandline); |
| 58 | Object result = impl.execute(this, null); |
| 59 | return result; |
| 60 | } |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 61 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 62 | public InputStream getKeyboard() { |
| 63 | return in; |
| 64 | } |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 65 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 66 | 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 71 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 72 | if (variables != null && variables.containsKey(name)) |
| 73 | return variables.get(name); |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 74 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 75 | // 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 89 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 90 | public void put(String name, Object value) { |
| 91 | variables.put(name, value); |
| 92 | } |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 93 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 94 | public PrintStream getConsole() { |
| 95 | return out; |
| 96 | } |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 97 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 98 | @SuppressWarnings("unchecked") |
| 99 | public CharSequence format(Object target, int level, Converter escape) throws Exception { |
| 100 | if (target == null) |
| 101 | return "null"; |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 102 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 103 | if (target instanceof CharSequence) |
| 104 | return (CharSequence) target; |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 105 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 106 | for (Converter c : service.converters) { |
| 107 | CharSequence s = c.format(target, level, this); |
| 108 | if (s != null) |
| 109 | return s; |
| 110 | } |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 111 | |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame^] | 112 | 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 235 | |
| 236 | } |