Bonjour à tous,
j'aimerais faire fonctionner deux composants dynamiques simples avec OSGi, dans mon avec la plateforme Apache Felix, afin de comprendre le fonctionnement de base. Je veux arriver à récupérer un objet (service) de type String (le plus simple je crois) dynamiquement.
J'ai travaillé à partir de deux tutoriels, un de EclipseZone et l'autre de l'e-book "OSGi in practice" mais je n'arrives pas.
Ci-dessous, mes premiers pas de bébé OSGi.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /** * Text Provider */ import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { //Création du service fourni String texte = "Texte envoyé par le premier composant"; //Enregistrement du service /** * @param String.class.getName() Nom de l'interface * @param img L'objet du service * @param null Propriétés du service */ context.registerService(String.class.getName(), texte, null); } public void stop(BundleContext context) throws Exception { System.out.println("Arrêt du composant imgprovider_1"); //Désenregistrer le service } }Je vous remercie par avance.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 /** * Text Consumer */ import org.osgi.framework.*; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer; public class Activator implements BundleActivator { private BundleContext context; private ServiceTracker tracker=null; //Tracker pour récupéré l'objet private ServiceTracker imgTracker; private String texte; public void start(BundleContext context) throws Exception { this.context=context; tracker = new ServiceTracker(context, String.class.getName(), new SCustomizer(context)); tracker.open(); texte = tracker.getService().toString(); if(null!=texte) System.out.println(texte); else System.out.println("bbb"); texte = (String) ServiceFactory.getService(this , tracker.getService()); } public void stop(BundleContext context) throws Exception { tracker.close(); } private class SCustomizer extends ServiceTracker{ private int finderCount=0; private ServiceRegistration registration=null; private boolean registering = false; public SCustomizer(BundleContext context){ super(context, String.class.getName(), null); } public Object addingService(ServiceReference sref){ String texte = (String) context.getService(sref); synchronized(this) { finderCount ++; if (registering) return texte; registering = (finderCount == 1); if (!registering) return texte; } ServiceRegistration reg = context.registerService(String.class.getName(), texte, null); synchronized(this) { registering = false; registration = reg; } return registration; } public void modifiedService(ServiceReference sref, Object service){} public void removedService(ServiceReference sref, Object service){ ServiceRegistration registration = (ServiceRegistration) service; registration.unregister(); context.ungetService(sref); } } }
Willy
Partager