2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.jcr
;
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
;
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
;
38 * Wrapper around a JCR repository which allows to simplify configuration and
39 * intercept some actions. It exposes itself as a {@link Repository}.
41 public abstract class JcrRepositoryWrapper
implements Repository
{
42 // private final static Log log = LogFactory
43 // .getLog(JcrRepositoryWrapper.class);
46 private Repository repository
;
48 private Map
<String
, String
> additionalDescriptors
= new HashMap
<>();
50 private Boolean autocreateWorkspaces
= false;
52 public JcrRepositoryWrapper(Repository repository
) {
53 setRepository(repository
);
59 public JcrRepositoryWrapper() {
63 // public void init() {
66 // /** Shutdown the repository */
67 // public void destroy() throws Exception {
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");
74 additionalDescriptors
.remove(key
);
76 additionalDescriptors
.put(key
, value
);
80 * DELEGATED JCR REPOSITORY METHODS
83 public String
getDescriptor(String key
) {
84 if (additionalDescriptors
.containsKey(key
))
85 return additionalDescriptors
.get(key
);
86 return getRepository().getDescriptor(key
);
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()]);
97 /** Central login method */
98 public Session
login(Credentials credentials
, String workspaceName
)
99 throws LoginException
, NoSuchWorkspaceException
, RepositoryException
{
102 session
= getRepository().login(credentials
, workspaceName
);
103 } catch (NoSuchWorkspaceException e
) {
104 if (autocreateWorkspaces
&& workspaceName
!= null)
105 session
= createWorkspaceAndLogsIn(credentials
, workspaceName
);
109 processNewSession(session
);
113 public Session
login() throws LoginException
, RepositoryException
{
114 return login(null, null);
117 public Session
login(Credentials credentials
) throws LoginException
, RepositoryException
{
118 return login(credentials
, null);
121 public Session
login(String workspaceName
) throws LoginException
, NoSuchWorkspaceException
, RepositoryException
{
122 return login(null, workspaceName
);
125 /** Called after a session has been created, does nothing by default. */
126 protected void processNewSession(Session session
) {
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.");
141 * Logs in to the default workspace, creates the required workspace, logs
142 * out, logs in to the required workspace.
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
);
151 return getRepository().login(credentials
, workspaceName
);
154 public boolean isStandardDescriptor(String key
) {
155 return getRepository().isStandardDescriptor(key
);
158 public boolean isSingleValueDescriptor(String key
) {
159 if (additionalDescriptors
.containsKey(key
))
161 return getRepository().isSingleValueDescriptor(key
);
164 public Value
getDescriptorValue(String key
) {
165 if (additionalDescriptors
.containsKey(key
))
166 return new StrValue(additionalDescriptors
.get(key
));
167 return getRepository().getDescriptorValue(key
);
170 public Value
[] getDescriptorValues(String key
) {
171 return getRepository().getDescriptorValues(key
);
174 public synchronized void setRepository(Repository repository
) {
175 this.repository
= repository
;
178 public void setAutocreateWorkspaces(Boolean autocreateWorkspaces
) {
179 this.autocreateWorkspaces
= autocreateWorkspaces
;
182 protected static class StrValue
implements Value
{
183 private final String str
;
185 public StrValue(String str
) {
190 public String
getString() throws ValueFormatException
, IllegalStateException
, RepositoryException
{
195 public InputStream
getStream() throws RepositoryException
{
196 throw new UnsupportedOperationException();
200 public Binary
getBinary() throws RepositoryException
{
201 throw new UnsupportedOperationException();
205 public long getLong() throws ValueFormatException
, RepositoryException
{
207 return Long
.parseLong(str
);
208 } catch (NumberFormatException e
) {
209 throw new ValueFormatException("Cannot convert", e
);
214 public double getDouble() throws ValueFormatException
, RepositoryException
{
216 return Double
.parseDouble(str
);
217 } catch (NumberFormatException e
) {
218 throw new ValueFormatException("Cannot convert", e
);
223 public BigDecimal
getDecimal() throws ValueFormatException
, RepositoryException
{
225 return new BigDecimal(str
);
226 } catch (NumberFormatException e
) {
227 throw new ValueFormatException("Cannot convert", e
);
232 public Calendar
getDate() throws ValueFormatException
, RepositoryException
{
233 throw new UnsupportedOperationException();
237 public boolean getBoolean() throws ValueFormatException
, RepositoryException
{
239 return Boolean
.parseBoolean(str
);
240 } catch (NumberFormatException e
) {
241 throw new ValueFormatException("Cannot convert", e
);
246 public int getType() {
247 return PropertyType
.STRING
;