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
.workbench
.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 static final long serialVersionUID
= 1854011367784598758L;
50 private Session session
;
51 private BundleContext bundleContext
;
53 /** Retrieves the preference node */
54 protected Node
getPreferenceNode() {
56 if (session
.hasPendingChanges())
58 Node userHome
= UserJcrUtils
.getUserHome(session
);
60 throw new ArgeoException("No user home for "
61 + session
.getUserID());
63 if (!userHome
.hasNode(ARGEO_PREFERENCES
)) {
64 preferences
= userHome
.addNode(ARGEO_PREFERENCES
);
65 preferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
68 preferences
= userHome
.getNode(ARGEO_PREFERENCES
);
70 String pluginPreferencesName
= bundleContext
.getBundle()
72 Node pluginPreferences
;
73 if (!preferences
.hasNode(pluginPreferencesName
)) {
74 VersionManager vm
= session
.getWorkspace().getVersionManager();
75 vm
.checkout(preferences
.getPath());
76 pluginPreferences
= preferences
.addNode(pluginPreferencesName
);
77 pluginPreferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
79 vm
.checkin(preferences
.getPath());
81 pluginPreferences
= preferences
.getNode(pluginPreferencesName
);
82 return pluginPreferences
;
83 } catch (RepositoryException e
) {
85 JcrUtils
.discardQuietly(session
);
86 throw new ArgeoException("Cannot retrieve preferences", e
);
92 public void load() throws IOException
{
93 ByteArrayOutputStream out
= null;
94 ByteArrayInputStream in
= null;
96 Properties props
= new Properties();
97 PropertyIterator it
= getPreferenceNode().getProperties();
98 while (it
.hasNext()) {
99 Property p
= it
.nextProperty();
100 if (!p
.isMultiple() && !p
.getDefinition().isProtected()) {
101 props
.setProperty(p
.getName(), p
.getValue().getString());
104 out
= new ByteArrayOutputStream();
105 props
.store(out
, "");
106 in
= new ByteArrayInputStream(out
.toByteArray());
108 } catch (Exception e
) {
110 throw new ArgeoException("Cannot load preferences", e
);
112 IOUtils
.closeQuietly(in
);
113 IOUtils
.closeQuietly(out
);
118 public void save() throws IOException
{
119 ByteArrayOutputStream out
= null;
120 ByteArrayInputStream in
= null;
121 Node pluginPreferences
= null;
123 out
= new ByteArrayOutputStream();
125 in
= new ByteArrayInputStream(out
.toByteArray());
126 Properties props
= new Properties();
128 pluginPreferences
= getPreferenceNode();
129 VersionManager vm
= pluginPreferences
.getSession().getWorkspace()
130 .getVersionManager();
131 vm
.checkout(pluginPreferences
.getPath());
132 for (Object key
: props
.keySet()) {
133 String name
= key
.toString();
134 String value
= props
.getProperty(name
);
135 pluginPreferences
.setProperty(name
, value
);
137 JcrUtils
.updateLastModified(pluginPreferences
);
138 pluginPreferences
.getSession().save();
139 vm
.checkin(pluginPreferences
.getPath());
140 } catch (Exception e
) {
141 JcrUtils
.discardUnderlyingSessionQuietly(pluginPreferences
);
142 throw new ArgeoException("Cannot save preferences", e
);
144 IOUtils
.closeQuietly(in
);
145 IOUtils
.closeQuietly(out
);
152 } catch (IOException e
) {
153 throw new ArgeoException("Cannot initialize preference store", e
);
157 public void setSession(Session session
) {
158 this.session
= session
;
161 public void setBundleContext(BundleContext bundleContext
) {
162 this.bundleContext
= bundleContext
;