]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/JcrPreferenceStore.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / JcrPreferenceStore.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.cms.ui.workbench.jcr;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.util.Properties;
22
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;
29
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;
38
39 /**
40 * Persist preferences as key/value pairs under ~/argeo:preferences.<br>
41 * TODO: better integrate JCR and Eclipse:<br>
42 * - typing<br>
43 * - use eclipse preferences<br>
44 * - better integrate with <code>ScopedPreferenceStore</code> provided by RAP
45 */
46 public class JcrPreferenceStore extends PreferenceStore implements ArgeoNames {
47 private static final long serialVersionUID = 1854011367784598758L;
48
49 private Session session;
50 private BundleContext bundleContext;
51
52 /** Retrieves the preference node */
53 protected Node getPreferenceNode() {
54 try {
55 if (session.hasPendingChanges())
56 session.save();
57 Node userHome = NodeUtils.getUserHome(session);
58 if (userHome == null)
59 throw new EclipseUiException("No user home for "
60 + session.getUserID());
61 Node preferences;
62 if (!userHome.hasNode(ARGEO_PREFERENCES)) {
63 preferences = userHome.addNode(ARGEO_PREFERENCES);
64 preferences.addMixin(ArgeoTypes.ARGEO_PREFERENCE_NODE);
65 session.save();
66 } else
67 preferences = userHome.getNode(ARGEO_PREFERENCES);
68
69 String pluginPreferencesName = bundleContext.getBundle()
70 .getSymbolicName();
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);
77 session.save();
78 vm.checkin(preferences.getPath());
79 } else
80 pluginPreferences = preferences.getNode(pluginPreferencesName);
81 return pluginPreferences;
82 } catch (RepositoryException e) {
83 e.printStackTrace();
84 JcrUtils.discardQuietly(session);
85 throw new EclipseUiException("Cannot retrieve preferences", e);
86 }
87
88 }
89
90 @Override
91 public void load() throws IOException {
92 ByteArrayOutputStream out = null;
93 ByteArrayInputStream in = null;
94 try {
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());
101 }
102 }
103 out = new ByteArrayOutputStream();
104 props.store(out, "");
105 in = new ByteArrayInputStream(out.toByteArray());
106 load(in);
107 } catch (Exception e) {
108 e.printStackTrace();
109 throw new EclipseUiException("Cannot load preferences", e);
110 } finally {
111 IOUtils.closeQuietly(in);
112 IOUtils.closeQuietly(out);
113 }
114 }
115
116 @Override
117 public void save() throws IOException {
118 ByteArrayOutputStream out = null;
119 ByteArrayInputStream in = null;
120 Node pluginPreferences = null;
121 try {
122 out = new ByteArrayOutputStream();
123 save(out, "");
124 in = new ByteArrayInputStream(out.toByteArray());
125 Properties props = new Properties();
126 props.load(in);
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);
135 }
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);
142 } finally {
143 IOUtils.closeQuietly(in);
144 IOUtils.closeQuietly(out);
145 }
146 }
147
148 public void init() {
149 try {
150 load();
151 } catch (IOException e) {
152 throw new EclipseUiException("Cannot initialize preference store", e);
153 }
154 }
155
156 public void setSession(Session session) {
157 this.session = session;
158 }
159
160 public void setBundleContext(BundleContext bundleContext) {
161 this.bundleContext = bundleContext;
162 }
163
164 }