]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/JcrRepositoryWrapper.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / JcrRepositoryWrapper.java
1 package org.argeo.jcr;
2
3 import java.io.InputStream;
4 import java.math.BigDecimal;
5 import java.util.Arrays;
6 import java.util.Calendar;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.jcr.Binary;
12 import javax.jcr.Credentials;
13 import javax.jcr.LoginException;
14 import javax.jcr.NoSuchWorkspaceException;
15 import javax.jcr.PropertyType;
16 import javax.jcr.Repository;
17 import javax.jcr.RepositoryException;
18 import javax.jcr.Session;
19 import javax.jcr.Value;
20 import javax.jcr.ValueFormatException;
21
22 /**
23 * Wrapper around a JCR repository which allows to simplify configuration and
24 * intercept some actions. It exposes itself as a {@link Repository}.
25 */
26 public abstract class JcrRepositoryWrapper implements Repository {
27 // private final static Log log = LogFactory
28 // .getLog(JcrRepositoryWrapper.class);
29
30 // wrapped repository
31 private Repository repository;
32
33 private Map<String, String> additionalDescriptors = new HashMap<>();
34
35 private Boolean autocreateWorkspaces = false;
36
37 public JcrRepositoryWrapper(Repository repository) {
38 setRepository(repository);
39 }
40
41 /**
42 * Empty constructor
43 */
44 public JcrRepositoryWrapper() {
45 }
46
47 // /** Initializes */
48 // public void init() {
49 // }
50 //
51 // /** Shutdown the repository */
52 // public void destroy() throws Exception {
53 // }
54
55 protected void putDescriptor(String key, String value) {
56 if (Arrays.asList(getRepository().getDescriptorKeys()).contains(key))
57 throw new IllegalArgumentException("Descriptor key " + key + " is already defined in wrapped repository");
58 if (value == null)
59 additionalDescriptors.remove(key);
60 else
61 additionalDescriptors.put(key, value);
62 }
63
64 /*
65 * DELEGATED JCR REPOSITORY METHODS
66 */
67
68 public String getDescriptor(String key) {
69 if (additionalDescriptors.containsKey(key))
70 return additionalDescriptors.get(key);
71 return getRepository().getDescriptor(key);
72 }
73
74 public String[] getDescriptorKeys() {
75 if (additionalDescriptors.size() == 0)
76 return getRepository().getDescriptorKeys();
77 List<String> keys = Arrays.asList(getRepository().getDescriptorKeys());
78 keys.addAll(additionalDescriptors.keySet());
79 return keys.toArray(new String[keys.size()]);
80 }
81
82 /** Central login method */
83 public Session login(Credentials credentials, String workspaceName)
84 throws LoginException, NoSuchWorkspaceException, RepositoryException {
85 Session session;
86 try {
87 session = getRepository(workspaceName).login(credentials, workspaceName);
88 } catch (NoSuchWorkspaceException e) {
89 if (autocreateWorkspaces && workspaceName != null)
90 session = createWorkspaceAndLogsIn(credentials, workspaceName);
91 else
92 throw e;
93 }
94 processNewSession(session, workspaceName);
95 return session;
96 }
97
98 public Session login() throws LoginException, RepositoryException {
99 return login(null, null);
100 }
101
102 public Session login(Credentials credentials) throws LoginException, RepositoryException {
103 return login(credentials, null);
104 }
105
106 public Session login(String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException {
107 return login(null, workspaceName);
108 }
109
110 /** Called after a session has been created, does nothing by default. */
111 protected void processNewSession(Session session, String workspaceName) {
112 }
113
114 /**
115 * Wraps access to the repository, making sure it is available.
116 *
117 * @deprecated Use {@link #getDefaultRepository()} instead.
118 */
119 @Deprecated
120 protected synchronized Repository getRepository() {
121 return getDefaultRepository();
122 }
123
124 protected synchronized Repository getDefaultRepository() {
125 return repository;
126 }
127
128 protected synchronized Repository getRepository(String workspaceName) {
129 return getDefaultRepository();
130 }
131
132 /**
133 * Logs in to the default workspace, creates the required workspace, logs out,
134 * logs in to the required workspace.
135 */
136 protected Session createWorkspaceAndLogsIn(Credentials credentials, String workspaceName)
137 throws RepositoryException {
138 if (workspaceName == null)
139 throw new IllegalArgumentException("No workspace specified.");
140 Session session = getRepository(workspaceName).login(credentials);
141 session.getWorkspace().createWorkspace(workspaceName);
142 session.logout();
143 return getRepository(workspaceName).login(credentials, workspaceName);
144 }
145
146 public boolean isStandardDescriptor(String key) {
147 return getRepository().isStandardDescriptor(key);
148 }
149
150 public boolean isSingleValueDescriptor(String key) {
151 if (additionalDescriptors.containsKey(key))
152 return true;
153 return getRepository().isSingleValueDescriptor(key);
154 }
155
156 public Value getDescriptorValue(String key) {
157 if (additionalDescriptors.containsKey(key))
158 return new StrValue(additionalDescriptors.get(key));
159 return getRepository().getDescriptorValue(key);
160 }
161
162 public Value[] getDescriptorValues(String key) {
163 return getRepository().getDescriptorValues(key);
164 }
165
166 public synchronized void setRepository(Repository repository) {
167 this.repository = repository;
168 }
169
170 public void setAutocreateWorkspaces(Boolean autocreateWorkspaces) {
171 this.autocreateWorkspaces = autocreateWorkspaces;
172 }
173
174 protected static class StrValue implements Value {
175 private final String str;
176
177 public StrValue(String str) {
178 this.str = str;
179 }
180
181 @Override
182 public String getString() throws ValueFormatException, IllegalStateException, RepositoryException {
183 return str;
184 }
185
186 @Override
187 public InputStream getStream() throws RepositoryException {
188 throw new UnsupportedOperationException();
189 }
190
191 @Override
192 public Binary getBinary() throws RepositoryException {
193 throw new UnsupportedOperationException();
194 }
195
196 @Override
197 public long getLong() throws ValueFormatException, RepositoryException {
198 try {
199 return Long.parseLong(str);
200 } catch (NumberFormatException e) {
201 throw new ValueFormatException("Cannot convert", e);
202 }
203 }
204
205 @Override
206 public double getDouble() throws ValueFormatException, RepositoryException {
207 try {
208 return Double.parseDouble(str);
209 } catch (NumberFormatException e) {
210 throw new ValueFormatException("Cannot convert", e);
211 }
212 }
213
214 @Override
215 public BigDecimal getDecimal() throws ValueFormatException, RepositoryException {
216 try {
217 return new BigDecimal(str);
218 } catch (NumberFormatException e) {
219 throw new ValueFormatException("Cannot convert", e);
220 }
221 }
222
223 @Override
224 public Calendar getDate() throws ValueFormatException, RepositoryException {
225 throw new UnsupportedOperationException();
226 }
227
228 @Override
229 public boolean getBoolean() throws ValueFormatException, RepositoryException {
230 try {
231 return Boolean.parseBoolean(str);
232 } catch (NumberFormatException e) {
233 throw new ValueFormatException("Cannot convert", e);
234 }
235 }
236
237 @Override
238 public int getType() {
239 return PropertyType.STRING;
240 }
241
242 }
243
244 }