1 package org
.argeo
.eclipse
.ui
.jcr
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.util
.Properties
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.PropertyIterator
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.version
.VersionManager
;
15 import org
.apache
.commons
.io
.IOUtils
;
16 import org
.argeo
.ArgeoException
;
17 import org
.argeo
.jcr
.ArgeoNames
;
18 import org
.argeo
.jcr
.ArgeoTypes
;
19 import org
.argeo
.jcr
.JcrUtils
;
20 import org
.eclipse
.jface
.preference
.PreferenceStore
;
21 import org
.eclipse
.ui
.preferences
.ScopedPreferenceStore
;
22 import org
.osgi
.framework
.BundleContext
;
25 * Persist preferences as key/value pairs under ~/argeo:preferences.<br>
26 * TODO: better integrate JCR and Eclipse:<br>
28 * - use eclipse preferences<br>
29 * - better integrate with {@link ScopedPreferenceStore} provided by RAP
31 public class JcrPreferenceStore
extends PreferenceStore
implements ArgeoNames
{
32 private Session session
;
33 private BundleContext bundleContext
;
35 /** Retrieves the preference node */
36 protected Node
getPreferenceNode() {
38 if (session
.hasPendingChanges())
40 Node userHome
= JcrUtils
.getUserHome(session
);
42 throw new ArgeoException("No user home for "
43 + session
.getUserID());
45 if (!userHome
.hasNode(ARGEO_PREFERENCES
)) {
46 preferences
= userHome
.addNode(ARGEO_PREFERENCES
);
47 preferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
50 preferences
= userHome
.getNode(ARGEO_PREFERENCES
);
52 String pluginPreferencesName
= bundleContext
.getBundle()
54 Node pluginPreferences
;
55 if (!preferences
.hasNode(pluginPreferencesName
)) {
56 VersionManager vm
= session
.getWorkspace().getVersionManager();
57 vm
.checkout(preferences
.getPath());
58 pluginPreferences
= preferences
.addNode(pluginPreferencesName
);
59 pluginPreferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
61 vm
.checkin(preferences
.getPath());
63 pluginPreferences
= preferences
.getNode(pluginPreferencesName
);
64 return pluginPreferences
;
65 } catch (RepositoryException e
) {
67 JcrUtils
.discardQuietly(session
);
68 throw new ArgeoException("Cannot retrieve preferences", e
);
74 public void load() throws IOException
{
75 ByteArrayOutputStream out
= null;
76 ByteArrayInputStream in
= null;
78 Properties props
= new Properties();
79 PropertyIterator it
= getPreferenceNode().getProperties();
80 while (it
.hasNext()) {
81 Property p
= it
.nextProperty();
82 if (!p
.isMultiple() && !p
.getDefinition().isProtected()) {
83 props
.setProperty(p
.getName(), p
.getValue().getString());
86 out
= new ByteArrayOutputStream();
88 in
= new ByteArrayInputStream(out
.toByteArray());
90 } catch (Exception e
) {
92 throw new ArgeoException("Cannot load preferences", e
);
94 IOUtils
.closeQuietly(in
);
95 IOUtils
.closeQuietly(out
);
100 public void save() throws IOException
{
101 ByteArrayOutputStream out
= null;
102 ByteArrayInputStream in
= null;
103 Node pluginPreferences
= null;
105 out
= new ByteArrayOutputStream();
107 in
= new ByteArrayInputStream(out
.toByteArray());
108 Properties props
= new Properties();
110 pluginPreferences
= getPreferenceNode();
111 VersionManager vm
= pluginPreferences
.getSession().getWorkspace()
112 .getVersionManager();
113 vm
.checkout(pluginPreferences
.getPath());
114 for (Object key
: props
.keySet()) {
115 String name
= key
.toString();
116 String value
= props
.getProperty(name
);
117 pluginPreferences
.setProperty(name
, value
);
119 JcrUtils
.updateLastModified(pluginPreferences
);
120 pluginPreferences
.getSession().save();
121 vm
.checkin(pluginPreferences
.getPath());
122 } catch (Exception e
) {
123 JcrUtils
.discardUnderlyingSessionQuietly(pluginPreferences
);
124 throw new ArgeoException("Cannot save preferences", e
);
126 IOUtils
.closeQuietly(in
);
127 IOUtils
.closeQuietly(out
);
134 } catch (IOException e
) {
135 throw new ArgeoException("Cannot initialize preference store", e
);
139 public void setSession(Session session
) {
140 this.session
= session
;
143 public void setBundleContext(BundleContext bundleContext
) {
144 this.bundleContext
= bundleContext
;