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