IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OGSi Java Discussion :

[OSGi] Service dynamique simple


Sujet :

OGSi Java

  1. #21
    Invité
    Invité(e)
    Par défaut
    Oui certes ceci marche mais en fait tu n''utilises pas le tracker là.
    J'ai fait des tests et si tu ne donnes pas de Customizer tracker.getService() retourne bien le service voulu. Si au contraire j'ai un Customizer je me payais un ClassCastException parce que tracker.getService() retournait un ServiceRegistrationImpl...
    Je vais creuser car ca m'intrigue tout ca

  2. #22
    Membre habitué
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    151
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 151
    Points : 133
    Points
    133
    Par défaut
    J'avais la même erreur "ClassCastException" et ça m'intrigue aussi. Je l'ai esquivé en étendant la classe ServiceTracker plutôt qu'en implémentant l'interface ServiceTrackerCustomizer.
    Tout vient à point qui sait programmer.

  3. #23
    Invité
    Invité(e)
    Par défaut
    ok merci OSGi in practice
    • When we call getService, the tracker will return whatever object we
      returned from addingService.
    • When the underlying service is modified or unregistered, the tracker will
      call modifiedService or removedService respectively, passing both the
      service reference and the object that we returned from addingService.

    Because of the first one of these promises, it’s perhaps more conventional to
    return the actual service object from addingService
    Donc tu pourrais changer ton code ainsi pour utiliser le tracker :
    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
     
    /**
     * Text Consumer
     */
     
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceReference;
    import org.osgi.framework.ServiceRegistration;
    import org.osgi.util.tracker.ServiceTracker;
     
    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 = (String) tracker.getService();
     
            if (null != texte)
                System.out.println(texte);
            else
                System.out.println("bbb");
     
        }
     
        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 texte;
            }
     
            public void modifiedService(ServiceReference sref, Object service) {
            }
     
            public void removedService(ServiceReference sref, Object service) {
                ServiceRegistration registration = (ServiceRegistration) service;
     
                registration.unregister();
                context.ungetService(sref);
            }
        }
    }
    et
    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
     
    /**
     * Text Provider
     */
     
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
     
    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
        }
    }
    Et là tadadam ca marche niiiickel comme tu voulais avec le tracker

  4. #24
    Membre habitué
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    151
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 151
    Points : 133
    Points
    133
    Par défaut
    Oh My God. Faut que je teste ça tout de suite.

    EDIT1 : ça maaaaaarche !!! C'est encore mieux là. Le tracker est utilisé et je suis sur que ça va fonctionner maintenant. Thank you, merci, milesker,danke, je ne sais pas en quelle langue te le dire.


    EDIT2 : il subsiste un bug, le désenregistrement du bundle fournissant le service me génère un erreur à la fermeture. T'as une idée ? La méthode ungetService(ServiceReference reference) semble être la bonne mais je ne vois pas de ServiceReference à proposer.

    Nota : l'erreur est "ERROR during dispatch .. cannot cast String to ServiceRegistration..."
    Tout vient à point qui sait programmer.

  5. #25
    Invité
    Invité(e)
    Par défaut
    Simplement
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    context.ungetService(sref);
    ne serait pas suffisant ? Car ton objet maitnenant est de type String (mon ode du dessus a omis ce changement...)

  6. #26
    Membre habitué
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    151
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 151
    Points : 133
    Points
    133
    Par défaut
    Il n'y a plus aucune erreur !! Je crois que je vais venir en Allemagne pour te remercier.
    Tu m'as aidé à terminé un projet de synthèse de DUT Informatique. Mes enseignants voulait deux bundles représentatifs pour valider leur thèse de reconfiguration dynamique d'applications multimédias intégrant la qualité de service. Pour cela j'ai codé avec un collègue une application qui permet, d'une part, de créer un graphe représentant l'application à base de composants lié entre eux par des flux de données, et d'autre part, de générer l'application, c'est à dire, installer puis démarrer les bundles dans un ordre précis.

    Je ne vais pas te demander d'autres services pour ce soir George. Je te remercie encore et on va certainement se revoir d'ici peu. Je vais au lit avec une bonne nouvelle.

    Wil
    Tout vient à point qui sait programmer.

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Implémentation d'un service REST simple en Java
    Par 84mickael dans le forum REST
    Réponses: 5
    Dernier message: 31/10/2010, 10h40
  2. Création Tableau dynamique simple
    Par iori11 dans le forum Langage
    Réponses: 1
    Dernier message: 18/09/2008, 21h11
  3. Exemple pl/sql dynamique simple
    Par nunurs83 dans le forum PL/SQL
    Réponses: 7
    Dernier message: 25/08/2008, 17h54
  4. Réponses: 7
    Dernier message: 07/11/2007, 10h20
  5. Carte dynamique simple : choix language
    Par forest82 dans le forum Général Conception Web
    Réponses: 6
    Dernier message: 19/09/2005, 12h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo