2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
.jcr
;
18 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.ByteArrayOutputStream
;
20 import java
.io
.IOException
;
21 import java
.util
.Properties
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.Property
;
25 import javax
.jcr
.PropertyIterator
;
26 import javax
.jcr
.RepositoryException
;
27 import javax
.jcr
.Session
;
28 import javax
.jcr
.version
.VersionManager
;
30 import org
.apache
.commons
.io
.IOUtils
;
31 import org
.argeo
.ArgeoException
;
32 import org
.argeo
.jcr
.ArgeoNames
;
33 import org
.argeo
.jcr
.ArgeoTypes
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.argeo
.jcr
.UserJcrUtils
;
36 import org
.eclipse
.jface
.preference
.PreferenceStore
;
37 import org
.eclipse
.ui
.preferences
.ScopedPreferenceStore
;
38 import org
.osgi
.framework
.BundleContext
;
41 * Persist preferences as key/value pairs under ~/argeo:preferences.<br>
42 * TODO: better integrate JCR and Eclipse:<br>
44 * - use eclipse preferences<br>
45 * - better integrate with {@link ScopedPreferenceStore} provided by RAP
47 public class JcrPreferenceStore
extends PreferenceStore
implements ArgeoNames
{
48 private Session session
;
49 private BundleContext bundleContext
;
51 /** Retrieves the preference node */
52 protected Node
getPreferenceNode() {
54 if (session
.hasPendingChanges())
56 Node userHome
= UserJcrUtils
.getUserHome(session
);
58 throw new ArgeoException("No user home for "
59 + session
.getUserID());
61 if (!userHome
.hasNode(ARGEO_PREFERENCES
)) {
62 preferences
= userHome
.addNode(ARGEO_PREFERENCES
);
63 preferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
66 preferences
= userHome
.getNode(ARGEO_PREFERENCES
);
68 String pluginPreferencesName
= bundleContext
.getBundle()
70 Node pluginPreferences
;
71 if (!preferences
.hasNode(pluginPreferencesName
)) {
72 VersionManager vm
= session
.getWorkspace().getVersionManager();
73 vm
.checkout(preferences
.getPath());
74 pluginPreferences
= preferences
.addNode(pluginPreferencesName
);
75 pluginPreferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
77 vm
.checkin(preferences
.getPath());
79 pluginPreferences
= preferences
.getNode(pluginPreferencesName
);
80 return pluginPreferences
;
81 } catch (RepositoryException e
) {
83 JcrUtils
.discardQuietly(session
);
84 throw new ArgeoException("Cannot retrieve preferences", e
);
90 public void load() throws IOException
{
91 ByteArrayOutputStream out
= null;
92 ByteArrayInputStream in
= null;
94 Properties props
= new Properties();
95 PropertyIterator it
= getPreferenceNode().getProperties();
96 while (it
.hasNext()) {
97 Property p
= it
.nextProperty();
98 if (!p
.isMultiple() && !p
.getDefinition().isProtected()) {
99 props
.setProperty(p
.getName(), p
.getValue().getString());
102 out
= new ByteArrayOutputStream();
103 props
.store(out
, "");
104 in
= new ByteArrayInputStream(out
.toByteArray());
106 } catch (Exception e
) {
108 throw new ArgeoException("Cannot load preferences", e
);
110 IOUtils
.closeQuietly(in
);
111 IOUtils
.closeQuietly(out
);
116 public void save() throws IOException
{
117 ByteArrayOutputStream out
= null;
118 ByteArrayInputStream in
= null;
119 Node pluginPreferences
= null;
121 out
= new ByteArrayOutputStream();
123 in
= new ByteArrayInputStream(out
.toByteArray());
124 Properties props
= new Properties();
126 pluginPreferences
= getPreferenceNode();
127 VersionManager vm
= pluginPreferences
.getSession().getWorkspace()
128 .getVersionManager();
129 vm
.checkout(pluginPreferences
.getPath());
130 for (Object key
: props
.keySet()) {
131 String name
= key
.toString();
132 String value
= props
.getProperty(name
);
133 pluginPreferences
.setProperty(name
, value
);
135 JcrUtils
.updateLastModified(pluginPreferences
);
136 pluginPreferences
.getSession().save();
137 vm
.checkin(pluginPreferences
.getPath());
138 } catch (Exception e
) {
139 JcrUtils
.discardUnderlyingSessionQuietly(pluginPreferences
);
140 throw new ArgeoException("Cannot save preferences", e
);
142 IOUtils
.closeQuietly(in
);
143 IOUtils
.closeQuietly(out
);
150 } catch (IOException e
) {
151 throw new ArgeoException("Cannot initialize preference store", e
);
155 public void setSession(Session session
) {
156 this.session
= session
;
159 public void setBundleContext(BundleContext bundleContext
) {
160 this.bundleContext
= bundleContext
;