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
;
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
;
23 * Wrapper around a JCR repository which allows to simplify configuration and
24 * intercept some actions. It exposes itself as a {@link Repository}.
26 public abstract class JcrRepositoryWrapper
implements Repository
{
27 // private final static Log log = LogFactory
28 // .getLog(JcrRepositoryWrapper.class);
31 private Repository repository
;
33 private Map
<String
, String
> additionalDescriptors
= new HashMap
<>();
35 private Boolean autocreateWorkspaces
= false;
37 public JcrRepositoryWrapper(Repository repository
) {
38 setRepository(repository
);
44 public JcrRepositoryWrapper() {
48 // public void init() {
51 // /** Shutdown the repository */
52 // public void destroy() throws Exception {
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");
59 additionalDescriptors
.remove(key
);
61 additionalDescriptors
.put(key
, value
);
65 * DELEGATED JCR REPOSITORY METHODS
68 public String
getDescriptor(String key
) {
69 if (additionalDescriptors
.containsKey(key
))
70 return additionalDescriptors
.get(key
);
71 return getRepository().getDescriptor(key
);
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()]);
82 /** Central login method */
83 public Session
login(Credentials credentials
, String workspaceName
)
84 throws LoginException
, NoSuchWorkspaceException
, RepositoryException
{
87 session
= getRepository(workspaceName
).login(credentials
, workspaceName
);
88 } catch (NoSuchWorkspaceException e
) {
89 if (autocreateWorkspaces
&& workspaceName
!= null)
90 session
= createWorkspaceAndLogsIn(credentials
, workspaceName
);
94 processNewSession(session
, workspaceName
);
98 public Session
login() throws LoginException
, RepositoryException
{
99 return login(null, null);
102 public Session
login(Credentials credentials
) throws LoginException
, RepositoryException
{
103 return login(credentials
, null);
106 public Session
login(String workspaceName
) throws LoginException
, NoSuchWorkspaceException
, RepositoryException
{
107 return login(null, workspaceName
);
110 /** Called after a session has been created, does nothing by default. */
111 protected void processNewSession(Session session
, String workspaceName
) {
115 * Wraps access to the repository, making sure it is available.
117 * @deprecated Use {@link #getDefaultRepository()} instead.
120 protected synchronized Repository
getRepository() {
121 return getDefaultRepository();
124 protected synchronized Repository
getDefaultRepository() {
128 protected synchronized Repository
getRepository(String workspaceName
) {
129 return getDefaultRepository();
133 * Logs in to the default workspace, creates the required workspace, logs out,
134 * logs in to the required workspace.
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
);
143 return getRepository(workspaceName
).login(credentials
, workspaceName
);
146 public boolean isStandardDescriptor(String key
) {
147 return getRepository().isStandardDescriptor(key
);
150 public boolean isSingleValueDescriptor(String key
) {
151 if (additionalDescriptors
.containsKey(key
))
153 return getRepository().isSingleValueDescriptor(key
);
156 public Value
getDescriptorValue(String key
) {
157 if (additionalDescriptors
.containsKey(key
))
158 return new StrValue(additionalDescriptors
.get(key
));
159 return getRepository().getDescriptorValue(key
);
162 public Value
[] getDescriptorValues(String key
) {
163 return getRepository().getDescriptorValues(key
);
166 public synchronized void setRepository(Repository repository
) {
167 this.repository
= repository
;
170 public void setAutocreateWorkspaces(Boolean autocreateWorkspaces
) {
171 this.autocreateWorkspaces
= autocreateWorkspaces
;
174 protected static class StrValue
implements Value
{
175 private final String str
;
177 public StrValue(String str
) {
182 public String
getString() throws ValueFormatException
, IllegalStateException
, RepositoryException
{
187 public InputStream
getStream() throws RepositoryException
{
188 throw new UnsupportedOperationException();
192 public Binary
getBinary() throws RepositoryException
{
193 throw new UnsupportedOperationException();
197 public long getLong() throws ValueFormatException
, RepositoryException
{
199 return Long
.parseLong(str
);
200 } catch (NumberFormatException e
) {
201 throw new ValueFormatException("Cannot convert", e
);
206 public double getDouble() throws ValueFormatException
, RepositoryException
{
208 return Double
.parseDouble(str
);
209 } catch (NumberFormatException e
) {
210 throw new ValueFormatException("Cannot convert", e
);
215 public BigDecimal
getDecimal() throws ValueFormatException
, RepositoryException
{
217 return new BigDecimal(str
);
218 } catch (NumberFormatException e
) {
219 throw new ValueFormatException("Cannot convert", e
);
224 public Calendar
getDate() throws ValueFormatException
, RepositoryException
{
225 throw new UnsupportedOperationException();
229 public boolean getBoolean() throws ValueFormatException
, RepositoryException
{
231 return Boolean
.parseBoolean(str
);
232 } catch (NumberFormatException e
) {
233 throw new ValueFormatException("Cannot convert", e
);
238 public int getType() {
239 return PropertyType
.STRING
;