blob: 3b354961c7901a98dfed88d1d1a8c99618d00774 [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 */
19package aQute.shell.osgi;
20
21import org.osgi.framework.*;
22import org.osgi.service.command.*;
23import org.osgi.service.component.*;
24import org.osgi.service.packageadmin.*;
25import org.osgi.service.permissionadmin.*;
26import org.osgi.service.startlevel.*;
27import org.osgi.service.threadio.*;
28
29import aQute.shell.runtime.*;
30
31public 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) {
48 addCommand("osgi", commands.service(StartLevel.class.getName(),
49 null), StartLevel.class);
50 addCommand("osgi", commands.service(PackageAdmin.class.getName(),
51 null), PackageAdmin.class);
52 addCommand("osgi", commands.service(
53 PermissionAdmin.class.getName(), null),
54 PermissionAdmin.class);
55 addCommand("osgi", commands.getContext(), BundleContext.class);
56 }
57 }
58
59 protected void deactivate(ComponentContext context) {
60 System.out.println("Deactivating");
61 }
62
63 public Object get(String name) {
64 if (bundle.getBundleContext() != null) {
65 BundleContext context = bundle.getBundleContext();
66 try {
67 Object cmd = super.get(name);
68 if (cmd != null)
69 return cmd;
70
71 int n = name.indexOf(':');
72 if (n < 0)
73 return null;
74
75 String service = name.substring(0, n);
76 String function = name.substring(n + 1);
77
78 String filter = String.format(
79 "(&(osgi.command.scope=%s)(osgi.command.function=%s))",
80 service, function);
81 ServiceReference refs[] = context.getServiceReferences(null,
82 filter);
83 if (refs == null || refs.length == 0)
84 return null;
85
86 if (refs.length > 1)
87 throw new IllegalArgumentException(
88 "Command name is not unambiguous: " + name
89 + ", found multiple impls");
90
91 return new ServiceCommand(this, refs[0], function);
92 } catch (InvalidSyntaxException ise) {
93 ise.printStackTrace();
94 }
95 }
96 return super.get(name);
97 }
98
99 public void setThreadio(Object t) {
100 super.setThreadio((ThreadIO) t);
101 }
102
103 public void setBundle(Bundle bundle) {
104 this.bundle = bundle;
105 }
106
107 public void setConverter(Converter c) {
108 super.setConverter(c);
109 }
110
111}