0

を使用していくつかの簡単な設定をロードして保存しようとしていますorg.osgi.service.prefs.Preferences。最初の保存は機能しましたが、その後の実行で行った変更はファイルの変更に失敗します。APIVogellaの記事を見ると、私は正しい手順を実行していると思います。デバッグモードで実行すると、を呼び出した後clear()も、同じ子のキーと値のペアが表示されます。さらに、設定をフラッシュした後、ディスク上のファイルは変更されません。これを機能させるために電話flush()する必要がありますか?(メモリ内の何かを変更するためにディスクにフラッシュする必要があるのはばかげているようです-そしてそれは役に立ちません)。

私は何が間違っているのですか?

記述子を保存するためのコードは次のとおりです(これは、McCaffer、Lemieux、およびAniszczykによって「EclipseRich Client Platform」から恥知らずにコピーされ、Eclipse 3.8.1のAPIを更新するためにいくつかの小さな変更が加えられています)。

Preferences preferences = ConfigurationScope.INSTANCE.getNode(Application.PLUGIN_ID);
preferences.put(LAST_USER, connectionDetails.getUserId());
Preferences connections = preferences.node(SAVED);
try {
    connections.clear();
    //preferences.flush();
} catch (BackingStoreException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
preferences = ConfigurationScope.INSTANCE.getNode(Application.PLUGIN_ID);
connections = preferences.node(SAVED);
for (Iterator<String> it = savedDetails.keySet().iterator(); it.hasNext();) {
    String name = it.next();
    ConnectionDetails d = (ConnectionDetails) savedDetails.get(name);
    Preferences connection = connections.node(name);
    connection.put(SERVER, d.getServer());
    connection.put(PASSWORD, d.getPassword());
}
try {
    preferences.flush();
} catch (BackingStoreException e) {
    e.printStackTrace();
}       
4

1 に答える 1

2

変更を適切に適用するには、設定をフラッシュする必要があります。つまり、を呼び出す必要がありますflush()。一部の操作は自動フラッシュされる場合がありますが、これは信頼できない実装の詳細です。また、clear()選択したノードのキーのみを削除します。ノードとそのすべての子を削除するには、をremoveNode()呼び出す必要があります。

// get node "config/plugin.id"
// note: "config" identifies the configuration scope used here
final Preferences preferences = ConfigurationScope.INSTANCE.getNode("plugin.id");

// set key "a" on node "config/plugin.id"
preferences.put("a", "value");

// get node "config/plugin.id/node1"
final Preferences connections = preferences.node("node1");

// remove all keys from node "config/plugin.id/node1"
// note: this really on removed keys on the selected node
connections.clear();

// these calls are bogous and not necessary
// they get the same nodes as above
//preferences = ConfigurationScope.INSTANCE.getNode("plugin.id");
//connections = preferences.node("node1");

// store some values to separate child nodes of "config/plugin.id/node1"
for (Entry<String, ConnectionDetails> e : valuesToSave.entrySet()) {
    String name = e.getKey();
    ConnectionDetails d = e.getValue();
    // get node "config/plugin.id/node1/<name>"
    Preferences connection = connections.node(name);
    // set keys "b" and "c"
    connection.put("b", d.getServer());
    connection.put("c", d.getPassword());
}

// flush changes to disk (if not already happend)
// note: this is required to make sure modifications are persisted
// flush always needs to be called after making modifications
preferences.flush();
于 2013-02-06T08:40:02.493 に答える