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 | // DWB3: dynamically load optional framework components to reduce dependencies |
| 20 | // DWB4: get() with trailing colon causes org.osgi.framework.InvalidSyntaxException |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 21 | package aQute.shell.osgi; |
| 22 | |
| 23 | import org.osgi.framework.*; |
| 24 | import org.osgi.service.command.*; |
| 25 | import org.osgi.service.component.*; |
| 26 | import org.osgi.service.packageadmin.*; |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 27 | import org.osgi.service.threadio.*; |
| 28 | |
| 29 | import aQute.shell.runtime.*; |
| 30 | |
| 31 | public class OSGiShell extends CommandShellImpl { |
| 32 | Bundle bundle; |
| 33 | OSGiCommands commands; |
| 34 | |
| 35 | protected void activate(ComponentContext context) throws Exception { |
| 36 | this.bundle = context.getBundleContext().getBundle(); |
| 37 | if (threadIO == null) |
| 38 | threadIO = (ThreadIO) context.locateService("x"); |
| 39 | start(); |
| 40 | } |
| 41 | |
| 42 | public void start() throws Exception { |
| 43 | commands = new OSGiCommands(bundle); |
| 44 | addCommand("osgi", this.bundle); |
| 45 | addCommand("osgi", commands); |
| 46 | setConverter(commands); |
| 47 | if (bundle.getState() == Bundle.ACTIVE) { |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 48 | addCommand("osgi", commands.service(PackageAdmin.class.getName(), |
| 49 | null), PackageAdmin.class); |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 50 | addCommand("osgi", commands.getContext(), BundleContext.class); |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 51 | |
| 52 | try { |
| 53 | // derek - dynamically load StartLevel to avoid import dependency |
| 54 | String sl = "org.osgi.service.startlevel.StartLevel"; |
| 55 | Class<?> slClass = bundle.loadClass(sl); |
| 56 | addCommand("osgi", commands.service(sl, null), slClass); |
| 57 | } catch (ClassNotFoundException e) { |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | // derek - dynamically load PermissionAdmin to avoid import dependency |
| 62 | String pa = "org.osgi.service.permissionadmin.PermissionAdmin"; |
| 63 | Class<?> paClass = bundle.loadClass(pa); |
| 64 | addCommand("osgi", commands.service(pa, null), paClass); |
| 65 | } catch (ClassNotFoundException e) { |
| 66 | } |
| 67 | } |
| 68 | else { |
| 69 | System.err.println("eek! bundle not active: " + bundle); |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | protected void deactivate(ComponentContext context) { |
| 74 | System.out.println("Deactivating"); |
| 75 | } |
| 76 | |
| 77 | public Object get(String name) { |
| 78 | if (bundle.getBundleContext() != null) { |
| 79 | BundleContext context = bundle.getBundleContext(); |
| 80 | try { |
| 81 | Object cmd = super.get(name); |
| 82 | if (cmd != null) |
| 83 | return cmd; |
| 84 | |
| 85 | int n = name.indexOf(':'); |
| 86 | if (n < 0) |
| 87 | return null; |
| 88 | |
| 89 | String service = name.substring(0, n); |
| 90 | String function = name.substring(n + 1); |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 91 | |
| 92 | // derek - fix org.osgi.framework.InvalidSyntaxException |
| 93 | if (service.length() == 0 || function.length() == 0) |
| 94 | return null; |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 95 | |
| 96 | String filter = String.format( |
| 97 | "(&(osgi.command.scope=%s)(osgi.command.function=%s))", |
| 98 | service, function); |
| 99 | ServiceReference refs[] = context.getServiceReferences(null, |
| 100 | filter); |
| 101 | if (refs == null || refs.length == 0) |
| 102 | return null; |
| 103 | |
| 104 | if (refs.length > 1) |
| 105 | throw new IllegalArgumentException( |
| 106 | "Command name is not unambiguous: " + name |
| 107 | + ", found multiple impls"); |
| 108 | |
| 109 | return new ServiceCommand(this, refs[0], function); |
| 110 | } catch (InvalidSyntaxException ise) { |
| 111 | ise.printStackTrace(); |
| 112 | } |
| 113 | } |
| 114 | return super.get(name); |
| 115 | } |
| 116 | |
| 117 | public void setThreadio(Object t) { |
| 118 | super.setThreadio((ThreadIO) t); |
| 119 | } |
| 120 | |
| 121 | public void setBundle(Bundle bundle) { |
| 122 | this.bundle = bundle; |
| 123 | } |
| 124 | |
| 125 | public void setConverter(Converter c) { |
| 126 | super.setConverter(c); |
| 127 | } |
| 128 | |
| 129 | } |