-
Notifications
You must be signed in to change notification settings - Fork 660
/
create-tracker.script
63 lines (60 loc) · 2.49 KB
/
create-tracker.script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
create-tracker = {
trackingContext = $1
trackedClass = $2
serviceAdded = $3
serviceRemoved = $4
shellClassLoader = (($.session class) getClassLoader)
arrayClass = ($shellClassLoader loadClass java.lang.reflect.Array)
handler = {
proxy = $1
method = $2
arguments = $3
name = ($method name)
# echo "Invoking $name"
if { $name equals "addingService" } \
then {
ref = ($arrayClass get $arguments 0)
svc = ($.context getService $ref)
$serviceAdded execute $.commandSession [ $svc ]
$svc
} elif { $name equals "removedService" } \
then {
ref = ($arrayClass get $arguments 0)
svc = ($arrayClass get $arguments 1)
$serviceRemoved execute $.commandSession [ $svc ]
$.context ungetService $ref
}
}
# create handler
customizerClass = ($shellClassLoader loadClass org.osgi.util.tracker.ServiceTrackerCustomizer)
array = ([ $customizerClass ] toArray ($arrayClass newInstance "java.lang.Class" 1))
proxyClass = ($shellClassLoader loadClass java.lang.reflect.Proxy)
customizer = ($proxyClass newProxyInstance $shellClassLoader $array $handler)
# create tracker
trackerClass = ($shellClassLoader loadClass org.osgi.util.tracker.ServiceTracker)
constructor = ($trackerClass constructor org.osgi.framework.BundleContext java.lang.String org.osgi.util.tracker.ServiceTrackerCustomizer)
if { "java.lang.Class" equals ($trackedClass class) } \
then {
trackedClass = ($trackedClass name)
}
tracker = ($constructor newInstance $trackingContext $trackedClass $customizer)
$tracker
}
tracker = (create-tracker $.context "org.osgi.service.cm.ManagedService" { s = (($1 class) toString); echo "service-added $s" } { s = (($1 class) toString); echo "service-removed $s" })
$tracker open true