Richard S. Hall | af656a0 | 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 | */ |
| 19 | package aQute.shell.osgi; |
| 20 | |
| 21 | import java.io.*; |
| 22 | import java.util.*; |
| 23 | import java.util.regex.*; |
| 24 | |
| 25 | import org.osgi.framework.*; |
| 26 | import org.osgi.service.command.*; |
| 27 | |
| 28 | public class OSGiCommands implements Converter { |
| 29 | Bundle bundle; |
| 30 | String COLUMN = "%40s %s\n"; |
| 31 | |
| 32 | protected OSGiCommands(Bundle bundle) { |
| 33 | this.bundle = bundle; |
| 34 | } |
| 35 | |
| 36 | // Bundle[] getBundles() { |
| 37 | // return getContext().getBundles(); |
| 38 | // } |
| 39 | |
| 40 | public BundleContext getContext() { |
| 41 | if ( bundle.getState() != Bundle.ACTIVE ) |
| 42 | throw new IllegalStateException("Framework is not started yet"); |
| 43 | return bundle.getBundleContext(); |
| 44 | } |
| 45 | |
| 46 | CharSequence print(Bundle bundle) { |
| 47 | String version = (String) bundle.getHeaders().get("Bundle-Version"); |
| 48 | if ( version == null ) |
| 49 | version = "0.0.0"; |
| 50 | return String.format("%06d %s %s-%s", bundle.getBundleId(), |
| 51 | getState(bundle), bundle.getSymbolicName(), version); |
| 52 | } |
| 53 | |
| 54 | CharSequence print(ServiceReference ref) { |
| 55 | StringBuilder sb = new StringBuilder(); |
| 56 | Formatter f = new Formatter(sb); |
| 57 | |
| 58 | String spid = ""; |
| 59 | Object pid = ref.getProperty("service.pid"); |
| 60 | if (pid != null) { |
| 61 | spid = pid.toString(); |
| 62 | } |
| 63 | |
| 64 | f.format("%06d %3s %-40s %s", ref.getProperty("service.id"), ref |
| 65 | .getBundle().getBundleId(), getShortNames((String[]) ref |
| 66 | .getProperty("objectclass")), spid); |
| 67 | return sb; |
| 68 | } |
| 69 | |
| 70 | CharSequence getShortNames(String[] list) { |
| 71 | StringBuilder sb = new StringBuilder(); |
| 72 | String del = ""; |
| 73 | for (String s : list) { |
| 74 | sb.append(del + getShortName(s)); |
| 75 | del = " | "; |
| 76 | } |
| 77 | return sb; |
| 78 | } |
| 79 | |
| 80 | CharSequence getShortName(String name) { |
| 81 | int n = name.lastIndexOf('.'); |
| 82 | if (n < 0) |
| 83 | n = 0; |
| 84 | else |
| 85 | n++; |
| 86 | return name.subSequence(n, name.length()); |
| 87 | } |
| 88 | |
| 89 | private String getState(Bundle bundle) { |
| 90 | switch (bundle.getState()) { |
| 91 | case Bundle.ACTIVE: |
| 92 | return "ACT"; |
| 93 | |
| 94 | case Bundle.INSTALLED: |
| 95 | return "INS"; |
| 96 | |
| 97 | case Bundle.RESOLVED: |
| 98 | return "RES"; |
| 99 | |
| 100 | case Bundle.STARTING: |
| 101 | return "STA"; |
| 102 | |
| 103 | case Bundle.STOPPING: |
| 104 | return "STO"; |
| 105 | |
| 106 | case Bundle.UNINSTALLED: |
| 107 | return "UNI "; |
| 108 | } |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | public void grep(String match) throws IOException { |
| 113 | Pattern p = Pattern.compile(match); |
| 114 | BufferedReader rdr = new BufferedReader( |
| 115 | new InputStreamReader(System.in)); |
| 116 | String s = rdr.readLine(); |
| 117 | while (s != null) { |
| 118 | if (p.matcher(s).find()) { |
| 119 | System.out.println(s); |
| 120 | } |
| 121 | s = rdr.readLine(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public String tac() throws IOException { |
| 126 | StringWriter sw = new StringWriter(); |
| 127 | BufferedReader rdr = new BufferedReader( |
| 128 | new InputStreamReader(System.in)); |
| 129 | String s = rdr.readLine(); |
| 130 | while (s != null) { |
| 131 | sw.write(s); |
| 132 | s = rdr.readLine(); |
| 133 | } |
| 134 | return sw.toString(); |
| 135 | } |
| 136 | |
| 137 | public CharSequence echo(CommandSession session, Object args[]) { |
| 138 | StringBuilder sb = new StringBuilder(); |
| 139 | String del = ""; |
| 140 | for (Object arg : args) { |
| 141 | sb.append(del); |
| 142 | if (arg != null) { |
| 143 | sb.append(arg); |
| 144 | del = " "; |
| 145 | } |
| 146 | } |
| 147 | return sb; |
| 148 | } |
| 149 | |
| 150 | public void each(CommandSession session, Collection<Object> list, Function closure) throws Exception { |
| 151 | List<Object> args = new ArrayList<Object>(); |
| 152 | args.add(null); |
| 153 | for (Object x : list) { |
| 154 | args.set(0, x); |
| 155 | Object result = closure.execute(session, args); |
| 156 | System.out.println(session.format(result,Converter.INSPECT)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public Bundle bundle(Bundle i) { |
| 161 | return i; |
| 162 | } |
| 163 | |
| 164 | public String[] ls(CommandSession session, File f) throws Exception{ |
| 165 | File cwd = (File) session.get("_cwd"); |
| 166 | if (cwd == null) |
| 167 | cwd = new File("").getAbsoluteFile(); |
| 168 | |
| 169 | if ( f == null ) |
| 170 | f = cwd; |
| 171 | else if (!f.isAbsolute()) |
| 172 | f = new File(cwd, f.getPath()); |
| 173 | |
| 174 | if ( f.isDirectory() ) |
| 175 | return f.list(); |
| 176 | |
| 177 | if ( f.isFile() ) { |
| 178 | cat(session,f); |
| 179 | } |
| 180 | |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | public Object cat(CommandSession session, File f ) throws Exception { |
| 185 | File cwd = (File) session.get("_cwd"); |
| 186 | if (cwd == null) |
| 187 | cwd = new File("").getAbsoluteFile(); |
| 188 | |
| 189 | if ( !f.isAbsolute() ) |
| 190 | f = new File(cwd,f.getPath()); |
| 191 | |
| 192 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 193 | FileInputStream in = new FileInputStream(f); |
| 194 | byte [] buffer = new byte[ (int) (f.length() % 100000) ]; |
| 195 | int size = in.read(buffer); |
| 196 | while ( size > 0 ) { |
| 197 | bout.write(buffer,0,size); |
| 198 | size = in.read(buffer); |
| 199 | } |
| 200 | return new String(bout.toByteArray()); |
| 201 | } |
| 202 | public Object convert(Class<?> desiredType, Object in) throws Exception { |
| 203 | if (desiredType == Bundle.class) |
| 204 | return convertBundle(in); |
| 205 | else if (desiredType == ServiceReference.class) |
| 206 | return convertServiceReference(in); |
| 207 | else if (desiredType == Class.class) |
| 208 | return Class.forName(in.toString()); |
| 209 | else if (desiredType.isAssignableFrom(String.class) && in instanceof InputStream) |
| 210 | return read(((InputStream) in)); |
| 211 | |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | private Object convertServiceReference(Object in) |
| 216 | throws InvalidSyntaxException { |
| 217 | String s = in.toString(); |
| 218 | if (s.startsWith("(") && s.endsWith(")")) { |
| 219 | ServiceReference refs[] = getContext().getServiceReferences(null, String |
| 220 | .format("(|(service.id=%s)(service.pid=%s))", in, in)); |
| 221 | if (refs != null && refs.length > 0) |
| 222 | return refs[0]; |
| 223 | } |
| 224 | |
| 225 | ServiceReference refs[] = getContext().getServiceReferences(null, String |
| 226 | .format("(|(service.id=%s)(service.pid=%s))", in, in)); |
| 227 | if (refs != null && refs.length > 0) |
| 228 | return refs[0]; |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | private Object convertBundle(Object in) { |
| 233 | String s = in.toString(); |
| 234 | try { |
| 235 | long id = Long.parseLong(s); |
| 236 | return getContext().getBundle(id); |
| 237 | } catch (NumberFormatException nfe) { |
| 238 | // Ignore |
| 239 | } |
| 240 | |
| 241 | Bundle bundles[] = getContext().getBundles(); |
| 242 | for (Bundle b : bundles) { |
| 243 | if (b.getLocation().equals(s)) |
| 244 | return b; |
| 245 | |
| 246 | if (b.getSymbolicName().equals(s)) |
| 247 | return b; |
| 248 | } |
| 249 | |
| 250 | return null; |
| 251 | } |
| 252 | |
| 253 | public CharSequence format(Object target, int level, Converter converter ) throws IOException { |
| 254 | if ( level == INSPECT && target instanceof InputStream ) { |
| 255 | return read(((InputStream)target)); |
| 256 | } |
| 257 | if (level == LINE && target instanceof Bundle) |
| 258 | return print((Bundle) target); |
| 259 | if (level == LINE && target instanceof ServiceReference) |
| 260 | return print((ServiceReference) target); |
| 261 | if (level == PART && target instanceof Bundle) |
| 262 | return ((Bundle) target).getSymbolicName(); |
| 263 | if (level == PART && target instanceof ServiceReference) |
| 264 | return getShortNames((String[]) ((ServiceReference) target) |
| 265 | .getProperty("objectclass")); |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | public CharSequence read(InputStream in) throws IOException { |
| 270 | int c; |
| 271 | StringBuffer sb = new StringBuffer(); |
| 272 | while ( (c=in.read())> 0 ) { |
| 273 | if ( c >=32 && c <= 0x7F || c=='\n' || c=='\r') { |
| 274 | sb.append( (char) c); |
| 275 | } else { |
| 276 | String s = Integer.toHexString(c).toUpperCase(); |
| 277 | sb.append("\\"); |
| 278 | if ( s.length() < 1) |
| 279 | sb.append(0); |
| 280 | sb.append(s); |
| 281 | } |
| 282 | } |
| 283 | return sb; |
| 284 | } |
| 285 | |
| 286 | public void start(Bundle b) throws BundleException { |
| 287 | b.start(); |
| 288 | } |
| 289 | |
| 290 | public void stop(Bundle b) throws BundleException { |
| 291 | b.stop(); |
| 292 | } |
| 293 | |
| 294 | public Object service(String clazz, String filter) throws InvalidSyntaxException { |
| 295 | ServiceReference ref[] = getContext().getServiceReferences(clazz,filter); |
| 296 | if (ref == null ) |
| 297 | return null; |
| 298 | |
| 299 | return getContext().getService(ref[0]); |
| 300 | } |
| 301 | |
| 302 | } |