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
.eclipse
.ui
.EclipseUiException
;
32 import org
.argeo
.jcr
.JcrUtils
;
33 import org
.argeo
.node
.ArgeoNames
;
34 import org
.argeo
.node
.ArgeoTypes
;
35 import org
.argeo
.node
.NodeUtils
;
36 import org
.eclipse
.jface
.preference
.PreferenceStore
;
37 import org
.osgi
.framework
.BundleContext
;
40 * Persist preferences as key/value pairs under ~/argeo:preferences.<br>
41 * TODO: better integrate JCR and Eclipse:<br>
43 * - use eclipse preferences<br>
44 * - better integrate with <code>ScopedPreferenceStore</code> provided by RAP
46 public class JcrPreferenceStore
extends PreferenceStore
implements ArgeoNames
{
47 private static final long serialVersionUID
= 1854011367784598758L;
49 private Session session
;
50 private BundleContext bundleContext
;
52 /** Retrieves the preference node */
53 protected Node
getPreferenceNode() {
55 if (session
.hasPendingChanges())
57 Node userHome
= NodeUtils
.getUserHome(session
);
59 throw new EclipseUiException("No user home for "
60 + session
.getUserID());
62 if (!userHome
.hasNode(ARGEO_PREFERENCES
)) {
63 preferences
= userHome
.addNode(ARGEO_PREFERENCES
);
64 preferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
67 preferences
= userHome
.getNode(ARGEO_PREFERENCES
);
69 String pluginPreferencesName
= bundleContext
.getBundle()
71 Node pluginPreferences
;
72 if (!preferences
.hasNode(pluginPreferencesName
)) {
73 VersionManager vm
= session
.getWorkspace().getVersionManager();
74 vm
.checkout(preferences
.getPath());
75 pluginPreferences
= preferences
.addNode(pluginPreferencesName
);
76 pluginPreferences
.addMixin(ArgeoTypes
.ARGEO_PREFERENCE_NODE
);
78 vm
.checkin(preferences
.getPath());
80 pluginPreferences
= preferences
.getNode(pluginPreferencesName
);
81 return pluginPreferences
;
82 } catch (RepositoryException e
) {
84 JcrUtils
.discardQuietly(session
);
85 throw new EclipseUiException("Cannot retrieve preferences", e
);
91 public void load() throws IOException
{
92 ByteArrayOutputStream out
= null;
93 ByteArrayInputStream in
= null;
95 Properties props
= new Properties();
96 PropertyIterator it
= getPreferenceNode().getProperties();
97 while (it
.hasNext()) {
98 Property p
= it
.nextProperty();
99 if (!p
.isMultiple() && !p
.getDefinition().isProtected()) {
100 props
.setProperty(p
.getName(), p
.getValue().getString());
103 out
= new ByteArrayOutputStream();
104 props
.store(out
, "");
105 in
= new ByteArrayInputStream(out
.toByteArray());
107 } catch (Exception e
) {
109 throw new EclipseUiException("Cannot load preferences", e
);
111 IOUtils
.closeQuietly(in
);
112 IOUtils
.closeQuietly(out
);
117 public void save() throws IOException
{
118 ByteArrayOutputStream out
= null;
119 ByteArrayInputStream in
= null;
120 Node pluginPreferences
= null;
122 out
= new ByteArrayOutputStream();
124 in
= new ByteArrayInputStream(out
.toByteArray());
125 Properties props
= new Properties();
127 pluginPreferences
= getPreferenceNode();
128 VersionManager vm
= pluginPreferences
.getSession().getWorkspace()
129 .getVersionManager();
130 vm
.checkout(pluginPreferences
.getPath());
131 for (Object key
: props
.keySet()) {
132 String name
= key
.toString();
133 String value
= props
.getProperty(name
);
134 pluginPreferences
.setProperty(name
, value
);
136 JcrUtils
.updateLastModified(pluginPreferences
);
137 pluginPreferences
.getSession().save();
138 vm
.checkin(pluginPreferences
.getPath());
139 } catch (Exception e
) {
140 JcrUtils
.discardUnderlyingSessionQuietly(pluginPreferences
);
141 throw new EclipseUiException("Cannot save preferences", e
);
143 IOUtils
.closeQuietly(in
);
144 IOUtils
.closeQuietly(out
);
151 } catch (IOException e
) {
152 throw new EclipseUiException("Cannot initialize preference store", e
);
156 public void setSession(Session session
) {
157 this.session
= session
;
160 public void setBundleContext(BundleContext bundleContext
) {
161 this.bundleContext
= bundleContext
;