]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrPreferenceStore.java
+ Add Property.NAME support (use getString() ) to the default node label provider
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / 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.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.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;
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 Session session;
49 private BundleContext bundleContext;
50
51 /** Retrieves the preference node */
52 protected Node getPreferenceNode() {
53 try {
54 if (session.hasPendingChanges())
55 session.save();
56 Node userHome = UserJcrUtils.getUserHome(session);
57 if (userHome == null)
58 throw new ArgeoException("No user home for "
59 + session.getUserID());
60 Node preferences;
61 if (!userHome.hasNode(ARGEO_PREFERENCES)) {
62 preferences = userHome.addNode(ARGEO_PREFERENCES);
63 preferences.addMixin(ArgeoTypes.ARGEO_PREFERENCE_NODE);
64 session.save();
65 } else
66 preferences = userHome.getNode(ARGEO_PREFERENCES);
67
68 String pluginPreferencesName = bundleContext.getBundle()
69 .getSymbolicName();
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);
76 session.save();
77 vm.checkin(preferences.getPath());
78 } else
79 pluginPreferences = preferences.getNode(pluginPreferencesName);
80 return pluginPreferences;
81 } catch (RepositoryException e) {
82 e.printStackTrace();
83 JcrUtils.discardQuietly(session);
84 throw new ArgeoException("Cannot retrieve preferences", e);
85 }
86
87 }
88
89 @Override
90 public void load() throws IOException {
91 ByteArrayOutputStream out = null;
92 ByteArrayInputStream in = null;
93 try {
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());
100 }
101 }
102 out = new ByteArrayOutputStream();
103 props.store(out, "");
104 in = new ByteArrayInputStream(out.toByteArray());
105 load(in);
106 } catch (Exception e) {
107 e.printStackTrace();
108 throw new ArgeoException("Cannot load preferences", e);
109 } finally {
110 IOUtils.closeQuietly(in);
111 IOUtils.closeQuietly(out);
112 }
113 }
114
115 @Override
116 public void save() throws IOException {
117 ByteArrayOutputStream out = null;
118 ByteArrayInputStream in = null;
119 Node pluginPreferences = null;
120 try {
121 out = new ByteArrayOutputStream();
122 save(out, "");
123 in = new ByteArrayInputStream(out.toByteArray());
124 Properties props = new Properties();
125 props.load(in);
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);
134 }
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);
141 } finally {
142 IOUtils.closeQuietly(in);
143 IOUtils.closeQuietly(out);
144 }
145 }
146
147 public void init() {
148 try {
149 load();
150 } catch (IOException e) {
151 throw new ArgeoException("Cannot initialize preference store", e);
152 }
153 }
154
155 public void setSession(Session session) {
156 this.session = session;
157 }
158
159 public void setBundleContext(BundleContext bundleContext) {
160 this.bundleContext = bundleContext;
161 }
162
163 }