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 javax
.jcr
.Credentials
;
19 import javax
.jcr
.LoginException
;
20 import javax
.jcr
.NoSuchWorkspaceException
;
21 import javax
.jcr
.Repository
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.Session
;
24 import javax
.jcr
.Value
;
26 import org
.argeo
.ArgeoException
;
29 * Wrapper around a JCR repository which allows to simplify configuration and
30 * intercept some actions. It exposes itself as a {@link Repository}.
32 public abstract class JcrRepositoryWrapper
implements Repository
{
33 // private final static Log log = LogFactory
34 // .getLog(JcrRepositoryWrapper.class);
37 private Repository repository
;
39 private Boolean autocreateWorkspaces
= false;
42 * Empty constructor, {@link #init()} should be called after properties have
45 public JcrRepositoryWrapper() {
52 /** Shutdown the repository */
53 public void destroy() throws Exception
{
57 * DELEGATED JCR REPOSITORY METHODS
60 public String
getDescriptor(String key
) {
61 return getRepository().getDescriptor(key
);
64 public String
[] getDescriptorKeys() {
65 return getRepository().getDescriptorKeys();
68 /** Central login method */
69 public Session
login(Credentials credentials
, String workspaceName
)
70 throws LoginException
, NoSuchWorkspaceException
,
74 session
= getRepository().login(credentials
, workspaceName
);
75 } catch (NoSuchWorkspaceException e
) {
76 if (autocreateWorkspaces
&& workspaceName
!= null)
77 session
= createWorkspaceAndLogsIn(credentials
, workspaceName
);
81 processNewSession(session
);
85 public Session
login() throws LoginException
, RepositoryException
{
86 return login(null, null);
89 public Session
login(Credentials credentials
) throws LoginException
,
91 return login(credentials
, null);
94 public Session
login(String workspaceName
) throws LoginException
,
95 NoSuchWorkspaceException
, RepositoryException
{
96 return login(null, workspaceName
);
99 /** Called after a session has been created, does nothing by default. */
100 protected void processNewSession(Session session
) {
103 /** Wraps access to the repository, making sure it is available. */
104 protected synchronized Repository
getRepository() {
105 // if (repository == null) {
106 // throw new ArgeoException("No repository initialized."
107 // + " Was the init() method called?"
108 // + " The destroy() method should also"
109 // + " be called on shutdown.");
115 * Logs in to the default workspace, creates the required workspace, logs
116 * out, logs in to the required workspace.
118 protected Session
createWorkspaceAndLogsIn(Credentials credentials
,
119 String workspaceName
) throws RepositoryException
{
120 if (workspaceName
== null)
121 throw new ArgeoException("No workspace specified.");
122 Session session
= getRepository().login(credentials
);
123 session
.getWorkspace().createWorkspace(workspaceName
);
125 return getRepository().login(credentials
, workspaceName
);
128 public boolean isStandardDescriptor(String key
) {
129 return getRepository().isStandardDescriptor(key
);
132 public boolean isSingleValueDescriptor(String key
) {
133 return getRepository().isSingleValueDescriptor(key
);
136 public Value
getDescriptorValue(String key
) {
137 return getRepository().getDescriptorValue(key
);
140 public Value
[] getDescriptorValues(String key
) {
141 return getRepository().getDescriptorValues(key
);
144 public synchronized void setRepository(Repository repository
) {
145 this.repository
= repository
;
148 public void setAutocreateWorkspaces(Boolean autocreateWorkspaces
) {
149 this.autocreateWorkspaces
= autocreateWorkspaces
;