java-forum.org - Java programmieren aus Leidenschaft
Java 6 Einstieg und professioneller Einsatz
Alter Preis: 34,90 EUR
Jetzt: 0,00 EUR

zzgl. Versandkosten

Zurück   java-forum.org - Java programmieren aus Leidenschaft > Java - Programmierung > Plattformprogrammierung

Plattformprogrammierung OSGi, Eclipse RCP und Co.

Thema geschlossen    
Themen-Optionen Thema durchsuchen Ansicht
Alt 30.07.2009, 15:58   #1 (permalink)
Neuer Benutzer
Bit
 
Registriert seit: 30.07.2009
Fachbeiträge: 1
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
Standard OSGi Config Admin persistiert nicht

Hallo zusammen,

ich habe ein Problem mit dem Config Admin. Undzwar habe ich vom OSGi-Book das Beispiel 13-1 gestartet und ausgeführt. Für diejenigen, die sich interessieren, aber den Code nicht zur Hand haben, stelle ich den hier zur Verfügung.

Activator.java
Java Code: Quelltext in neuem Fenster öffnen
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
package org.osgibook.console.cm;
 
import java.util.Properties;
 
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.util.tracker.ServiceTracker;
 
public class Activator implements BundleActivator, CommandProvider {
 
    /** ServiceTracker zur Verfolgung des Config Admin Service */
    private ServiceTracker configurationAdminTracker;
 
    public void start(BundleContext context) throws Exception {
 
        // Tracker anlegen und oeffnen
        configurationAdminTracker = new ServiceTracker(context,
            ConfigurationAdmin.class.getName(), null);
        configurationAdminTracker.open();
 
        // Diese Activator-Instanz als CommandProvider registrieren
        context.registerService(CommandProvider.class.getName(), this, null);
    }
 
    public void stop(BundleContext context) throws Exception {
        configurationAdminTracker.close();
    }
 
    public void _configure(CommandInterpreter commandInterpreter) {
 
        ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) configurationAdminTracker
            .getService();
 
        if (configurationAdmin == null) {
            commandInterpreter.println("Service ConfigurationAdmin not available!");
            return;
        }
 
        // Kommandozeile parsen
        String pid = commandInterpreter.nextArgument();
 
        if (pid == null) {
            commandInterpreter.print("Usage: " + getHelp());
            return;
        }
 
        // Key-Value-Paare von der Kommandozeile lesen
        Properties properties = new Properties();
        String property = commandInterpreter.nextArgument();
        while (property != null) {
            String[] keyValue = property.split("=");
            properties.put(keyValue[0], keyValue[1]);
            property = commandInterpreter.nextArgument();
        }
 
        // Service mit Properties konfigurieren
        try {
            Configuration configuration = configurationAdmin.getConfiguration(pid,
                null);
            configuration.update(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * Gibt den Hilfetext fuer das configure-Kommando zurueck
     */
    public String getHelp() {
        return "configure <pid> (<key>=<value>)+";
    }
}

Activator.java
Java Code: Quelltext in neuem Fenster öffnen
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
package org.osgibook.helloworld;
 
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgibook.translation.TranslationService;
 
public class Activator implements BundleActivator, CommandProvider {
 
    /** Der BundleContext des helloworld-Bundles */
    private BundleContext    bundleContext;
 
    /** Der ServiceTracker zur Ueberwachung des TranslationService */
    private ServiceTracker  translationServiceTracker;
 
    public void start(BundleContext context) throws Exception {
        this.bundleContext = context;
 
        context.registerService(CommandProvider.class.getName(), this, null);
 
        translationServiceTracker = new ServiceTracker(context,
            TranslationService.class.getName(), null);
        translationServiceTracker.open();
    }
 
    public void stop(BundleContext context) throws Exception {
        translationServiceTracker.close();
    }
 
    public void _greet(CommandInterpreter commandInterpreter) {
 
        TranslationService translationService = (TranslationService) translationServiceTracker
            .getService();
 
        if (translationService == null) {
            commandInterpreter.println("TranslationService z.Zt. nicht verfuegbar.");
            return;
        }
 
        // Pruefen, ob der Anwender ein zusaetzliches Argument eingegeben hat
        String key = commandInterpreter.nextArgument();
        if (key == null) {
            // Wenn keine Nachricht eingegeben wurde, hello ausgeben
            key = "hello";
        }
 
        // Lokalisierte Nachricht zu uebergebenem Key suchen
        String translatedMsg = translationService.getTranslation(key);
 
        // Parameter ersetzen
        String message = String.format(translatedMsg, bundleContext.getBundle()
            .getSymbolicName());
 
        // Komplette Nachricht auf der Konsole ausgeben
        commandInterpreter.println(message);
    }
 
    /**
     * Die Methode getHelp() wird von Equinox aufgerufen, wenn der Anwender "help"
     * auf der Konsole eingegeben hat.
     * 
     * <p>
     * Die Methode muss einen String zurueckliefern, der dann von Equinox
     * angezeigt wird. Der String sollte Erlaeuterungen zu den Kommandos
     * enthalten, die dieser CommandProvider zur Verfuegung stellt.
     * 
     * @return Ein String mit dem Hilfe-Text
     */
    public String getHelp() {
        StringBuilder help = new StringBuilder();
        help.append("\n--- Hello World Commands ---");
        help.append("\n\tgreet [hello|goodbye] - ");
        help.append("display the \"Hello World !\" message");
        return help.toString();
    }
}

TranslationService.java
Java Code: Quelltext in neuem Fenster öffnen
1
2
3
4
6
7
package org.osgibook.translation;
 
public interface TranslationService {
 
    public String getTranslation(String key);
}

Activator.java
Java Code: Quelltext in neuem Fenster öffnen
1
2
3
4
5
6
7
8
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package org.osgibook.translation.impl;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedService;
import org.osgibook.translation.TranslationService;
 
public class Activator implements BundleActivator {
 
    public void start(BundleContext context) throws Exception {
 
        // Service erzeugen
        TranslationServiceImpl translationService = new TranslationServiceImpl();
 
        // Klassennamen
        String[] clazzes = new String[] { TranslationService.class.getName(),
            ManagedService.class.getName() };
 
        // Service anmelden
        ServiceRegistration registration = context.registerService(clazzes,
            translationService, translationService.getDefaultConfig());
 
        translationService.setServiceRegistration(registration);
    }
 
    public void stop(BundleContext context) throws Exception {
    }
}

TranslationServiceImpl.java
Java Code: Quelltext in neuem Fenster öffnen
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
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
87
88
89
90
91
92
93
94
95
96
package org.osgibook.translation.impl;
 
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
 
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgibook.translation.TranslationService;
 
public class TranslationServiceImpl implements TranslationService,
    ManagedService {
 
    /** der key fuer die Property translation.country */
    private static final String TRANSLATION_COUNTRY = "translation.country";
 
    /** der key fuer die Property translation.language */
    private static final String TRANSLATION_LANGUAGE = "translation.language";
 
    /**
     * Das ResourceBundle, das die lokalisierten Nachrichten enthaelt
     */
    private ResourceBundle translations;
 
    private ServiceRegistration registration;
 
    public TranslationServiceImpl() {
        translations = null;
    }
 
    public String getTranslation(String message) {
        try {
            return translations.getString(message);
        } catch (MissingResourceException e) {
            return "";
        }
    }
 
    @SuppressWarnings("unchecked")
    public void updated(Dictionary properties) throws ConfigurationException {
 
        // Ggf. Default-Werte setzen
        Dictionary config = properties == null ? getDefaultConfig() : properties;
 
        // Setzt die Properties fuer den TranslationService
        registration.setProperties(config);
 
        // Locale ermitteln
        Locale locale = getLocaleFromConfiguration(config);
 
        // Nachrichten einlesen
        translations = ResourceBundle.getBundle("translation", locale);
    }
 
    @SuppressWarnings("unchecked")
    Dictionary getDefaultConfig() {
        Dictionary defaultConfig = new Hashtable();
        defaultConfig.put(TRANSLATION_LANGUAGE, "de");
        defaultConfig.put(TRANSLATION_COUNTRY, "DE");
        defaultConfig
            .put(Constants.SERVICE_PID, TranslationService.class.getName());
        return defaultConfig;
    }
 
    void setServiceRegistration(ServiceRegistration registration) {
        this.registration = registration;
    }
 
    @SuppressWarnings("unchecked")
    private Locale getLocaleFromConfiguration(Dictionary properties)
        throws ConfigurationException {
 
        // Properties auslesen...
        String language = (String) properties.get(TRANSLATION_LANGUAGE);
        String country = (String) properties.get(TRANSLATION_COUNTRY);
 
        // ...ueberpruefen...
        if (language == null) {
            throw new ConfigurationException(TRANSLATION_LANGUAGE,
                "Es muss eine Sprache angegeben sein.");
        }
 
        if (country == null) {
            throw new ConfigurationException(TRANSLATION_COUNTRY,
                "Es muss ein Land angegeben sein.");
        }
 
        // ... Locale zurueckliefern
        return new Locale(language, country);
    }
}

translation_de_DE.properties
Java Code: Quelltext in neuem Fenster öffnen
1
2
hello = Hallo OSGi-Welt sagt Bundle %s!
goodbye = Tschüß OSGi-Welt sagt Bundle %s!

translation_en_US.properties
Java Code: Quelltext in neuem Fenster öffnen
1
2
hello = Hello OSGi World from bundle %s!
goodbye = Goodbye OSGi World from bundle %s!

Zum Ausführen muss eine Launch Config angelegt werden, die neben den Bundles aus dem Workspace noch die folgenden Bundles enthält: org.eclipse.equinox.cm, org.eclipse.orgi, org.exlipse.orgi.services.

In der Konsole kann jetzt mit dem folgenden Kommando die Ausgabe angefordert werden:

Code:
osgi> greet hello
Hallo OSGI-Welt sagt Bundle org.osgibook.helloworld!
Wir geben eine neue Konfiguration ein.

Code:
osgi> configure org.osgibook.translation.TranslationService translation.language=en translation.country=US
osgi> greet hello
Hello OSGI-World from Bundle org.osgibook.helloworld!
Wenn man jetzt das Framework neu startet und den Befehl "greet hello" eingibt, kommt die Default-Konfiguration mit der deutschen Ausgabe. Ich möchte jedoch, dass die Einstellung persistent genutzt wird.

Ich habe eigentlich gedacht, dass der Config Admin "von Haus aus" persistiert. Tut es aber offensichtlich nicht.

Kann mir dort jemand weiter helfen? Wäre für jeden sinnvollen Tipp sehr dankbar.

Chris
ChrisBenyamin ist offline  
Bei Google nach dem markiertem Wort suchen Bei Wikipedia nach dem markiertem Wort suchen Im Forum nach dem markiertem Wort suchen
Thema geschlossen    

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
PostgreSQL Datenbankstruktur exportieren und importieren Shaddow Datenbankprogrammierung 2 30.05.2009 18:03
Eingebettem Apache Fop 0.95 eine config Datei beibringen. Chris81T Allgemeine Java-Themen 0 21.11.2008 08:04
Neues Problem mit ApplikationClient! th.weigand Allgemeines EE 0 14.01.2008 17:00
JDT ohne "eclipse" und OSGi nutzen? Novanic Allgemeine Java-Themen 1 19.11.2007 09:56
Problem mit Permissions unter OSGi hackejam Plattformprogrammierung 0 05.09.2006 11:02


Lesezeichen

Forumregeln
Es ist Ihnen erlaubt, neue Themen zu verfassen.
Es ist Ihnen erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are aus
Pingbacks are aus
Refbacks are aus


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:19 Uhr.


Powered by vBulletin® Version 3.8.6 (Deutsch)
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Thanks for Smilies by smilies.4-user.de