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