X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fjcr%2FJcrPreferenceStore.java;h=7090055973f4cf7d916fc8918ddb6aac23325830;hb=1d5afdce3e91054f07ddd3c98309c363b4cf1d46;hp=f8dc23f47c2b6bab131ade2c4e37c6ddb9919d29;hpb=ee198517ee566006f73ae7ec5a47d011cfa12d9c;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrPreferenceStore.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrPreferenceStore.java index f8dc23f47..709005597 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrPreferenceStore.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrPreferenceStore.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.eclipse.ui.jcr; import java.io.ByteArrayInputStream; @@ -10,30 +25,60 @@ import javax.jcr.Property; import javax.jcr.PropertyIterator; import javax.jcr.RepositoryException; import javax.jcr.Session; +import javax.jcr.version.VersionManager; import org.apache.commons.io.IOUtils; import org.argeo.ArgeoException; import org.argeo.jcr.ArgeoNames; +import org.argeo.jcr.ArgeoTypes; import org.argeo.jcr.JcrUtils; import org.eclipse.jface.preference.PreferenceStore; +import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.osgi.framework.BundleContext; -/** Persist preferences as key/value pairs under ~/argeo:preferences */ -public class JcrPreferenceStore extends PreferenceStore { +/** + * Persist preferences as key/value pairs under ~/argeo:preferences.
+ * TODO: better integrate JCR and Eclipse:
+ * - typing
+ * - use eclipse preferences
+ * - better integrate with {@link ScopedPreferenceStore} provided by RAP + */ +public class JcrPreferenceStore extends PreferenceStore implements ArgeoNames { private Session session; + private BundleContext bundleContext; /** Retrieves the preference node */ protected Node getPreferenceNode() { try { + if (session.hasPendingChanges()) + session.save(); Node userHome = JcrUtils.getUserHome(session); + if (userHome == null) + throw new ArgeoException("No user home for " + + session.getUserID()); Node preferences; - if (!userHome.hasNode(ArgeoNames.ARGEO_PREFERENCES)) { - preferences = userHome.addNode(ArgeoNames.ARGEO_PREFERENCES); + if (!userHome.hasNode(ARGEO_PREFERENCES)) { + preferences = userHome.addNode(ARGEO_PREFERENCES); + preferences.addMixin(ArgeoTypes.ARGEO_PREFERENCE_NODE); session.save(); - } else { - preferences = userHome.getNode(ArgeoNames.ARGEO_PREFERENCES); - } - return preferences; + } else + preferences = userHome.getNode(ARGEO_PREFERENCES); + + String pluginPreferencesName = bundleContext.getBundle() + .getSymbolicName(); + Node pluginPreferences; + if (!preferences.hasNode(pluginPreferencesName)) { + VersionManager vm = session.getWorkspace().getVersionManager(); + vm.checkout(preferences.getPath()); + pluginPreferences = preferences.addNode(pluginPreferencesName); + pluginPreferences.addMixin(ArgeoTypes.ARGEO_PREFERENCE_NODE); + session.save(); + vm.checkin(preferences.getPath()); + } else + pluginPreferences = preferences.getNode(pluginPreferencesName); + return pluginPreferences; } catch (RepositoryException e) { + e.printStackTrace(); JcrUtils.discardQuietly(session); throw new ArgeoException("Cannot retrieve preferences", e); } @@ -49,7 +94,7 @@ public class JcrPreferenceStore extends PreferenceStore { PropertyIterator it = getPreferenceNode().getProperties(); while (it.hasNext()) { Property p = it.nextProperty(); - if (!p.isMultiple()) { + if (!p.isMultiple() && !p.getDefinition().isProtected()) { props.setProperty(p.getName(), p.getValue().getString()); } } @@ -58,6 +103,7 @@ public class JcrPreferenceStore extends PreferenceStore { in = new ByteArrayInputStream(out.toByteArray()); load(in); } catch (Exception e) { + e.printStackTrace(); throw new ArgeoException("Cannot load preferences", e); } finally { IOUtils.closeQuietly(in); @@ -69,31 +115,48 @@ public class JcrPreferenceStore extends PreferenceStore { public void save() throws IOException { ByteArrayOutputStream out = null; ByteArrayInputStream in = null; - Node preferences = null; + Node pluginPreferences = null; try { out = new ByteArrayOutputStream(); save(out, ""); in = new ByteArrayInputStream(out.toByteArray()); Properties props = new Properties(); props.load(in); - preferences = getPreferenceNode(); + pluginPreferences = getPreferenceNode(); + VersionManager vm = pluginPreferences.getSession().getWorkspace() + .getVersionManager(); + vm.checkout(pluginPreferences.getPath()); for (Object key : props.keySet()) { String name = key.toString(); String value = props.getProperty(name); - preferences.setProperty(name, value); + pluginPreferences.setProperty(name, value); } - preferences.getSession().save(); + JcrUtils.updateLastModified(pluginPreferences); + pluginPreferences.getSession().save(); + vm.checkin(pluginPreferences.getPath()); } catch (Exception e) { - JcrUtils.discardUnderlyingSessionQuietly(preferences); - throw new ArgeoException("Cannot load preferences", e); + JcrUtils.discardUnderlyingSessionQuietly(pluginPreferences); + throw new ArgeoException("Cannot save preferences", e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } + public void init() { + try { + load(); + } catch (IOException e) { + throw new ArgeoException("Cannot initialize preference store", e); + } + } + public void setSession(Session session) { this.session = session; } + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + }