Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (81 sloc) 3.202 kb
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
1 h1. Groovy OSGi modules
3dde1da @jetztgradnet added groovyx-osgi project
authored
2
3 h2. About
4
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
5 The groovyx.osgi module contains helper classes to make OSGi programming easier. This package is inspired by "ScalaModules":http://github.com/weiglewilczek/scalamodules/wiki.
3dde1da @jetztgradnet added groovyx-osgi project
authored
6
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
7 Features:
3dde1da @jetztgradnet added groovyx-osgi project
authored
8
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
9 * simplifies working with OSGi service registry
10 * safe access to OSGi services, without fear for OSGi dynamics
11 * Service finder DSL
1066d32 @jetztgradnet added link to filter4osgi project and Hamlet D'Arcy's blog
authored
12 * OSGi service filter DSL. The filter support is derived from the "filter4osgi":http://code.google.com/p/filter4osgi/ project and was contributed by "Hamlet D'Arcy":http://hamletdarcy.blogspot.com/.
13
14
3dde1da @jetztgradnet added groovyx-osgi project
authored
15
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
16 h2. Usage
3dde1da @jetztgradnet added groovyx-osgi project
authored
17
10e9688 @jetztgradnet added extended version and Java equivalent of introductory example
authored
18 (Almost) all of *groovyx.osgi* is implemented using the classes *groovyx.osgi.ServcieFinder* and *groovyx.osgi.ServiceWrapper*. *groovyx.osgi.OsgiCategory* provides some glue code to allow easy access of the functionality provided by *ServiceFinder* and *ServiceWrapper* from *BundleContext*, *ServiceReference*, and *ServiceReference[]*.
19
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
20 h3. Simple example
21
22 In this example, all services of type MyService are looked up in the OSGi service registry.
23
24 <pre><code>
25 import grooxy.osgi.OsgiCategory
26
27 List results
28 use(OsgiCategory) {
6bd0c9d @jetztgradnet simplified examples
authored
29 results = bundleContext.findServices(MyService).withEachService() { srv ->
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
30 srv.doSomething()
31 }
32 }
33
34 results.each { result -> println result }
35 </code></pre>
36
10e9688 @jetztgradnet added extended version and Java equivalent of introductory example
authored
37 The above sample is equivalent to:
38
39 <pre><code>
40 import grooxy.osgi.ServiceFinder
41 import grooxy.osgi.ServiceWrapper
42
43 ServiceWrapper services = new ServiceFinder(bundleContext)
44 .setClassName(MyService)
45 .find()
46 List results = services.withEachService() { srv -> srv.doSomething() }
47 results.each { result -> println result }
48 </code></pre>
49
50 The equivalent Java code looks like this:
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
51
10e9688 @jetztgradnet added extended version and Java equivalent of introductory example
authored
52 <pre><code>
53 import java.util.ArrayList;
54 import java.util.List
55 import org.osgi.framework.BundleActivator;
56 import org.osgi.framework.BundleContext;
57 import org.osgi.framework.ServiceReference;
58
59 List<String> results = new ArrayList<String>();
60 ServiceReference[] references = context.getServiceReferences(MyService.class.getName(), null);
61 for (int r = 0; r < references.length; r++) {
62 ServiceReference reference = references[r];
63 if (reference == null) {
64 continue;
65 }
66 try {
67 MyService service = (MyService) context.getService(reference);
68 if (service != null) {
69 String result = service.doSomething();
70 results.add(result);
71 }
72 }
73 finally {
74 context.ungetService(reference);
75 }
76 }
77 </code></pre>
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
78
79
10e9688 @jetztgradnet added extended version and Java equivalent of introductory example
authored
80 h2. Reference
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
81
82 All examples can be run in a fashion similar to the simple example above. For brevity,
83 only the configuration closure is included in the following snippets.
84
85 h3. Find services by class name
86
87 <pre><code>
88 import grooxy.osgi.OsgiCategory
89 import grooxy.osgi.ServiceWrapper
90
91 use(OsgiCategory) {
6bd0c9d @jetztgradnet simplified examples
authored
92 ServiceWrapper services = bundleContext.findServices(MyService)
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
93 }
94 </code></pre>
95
96 <pre><code>
97 import grooxy.osgi.ServiceFinder
98 import grooxy.osgi.ServiceWrapper
99
100 ServiceWrapper services = new ServiceFinder(bundleContext)
101 .single()
102 .setClassName(MyService.class.getName())
103 .find()
104 </code></pre>
105
106
107
108 h2. Roadmap
109
110 * simplified service registration MyObject.registerService (Map, Closure)
3dcfdca @jetztgradnet fixed Roadmap entry
authored
111 * simplified use of ServiceTracker, ManagedService, and ManagedServiceFactory
56a8a87 @jetztgradnet first cut of specific docs for each sub-module
authored
112 * improve docs
Something went wrong with that request. Please try again.