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
;
27 * Wrapper around a JCR repository which allows to simplify configuration and
28 * intercept some actions. It exposes itself as a {@link Repository}.
30 public abstract class JcrRepositoryWrapper
implements Repository
{
31 // private final static Log log = LogFactory
32 // .getLog(JcrRepositoryWrapper.class);
35 private Repository repository
;
37 private Boolean autocreateWorkspaces
= false;
40 * Empty constructor, {@link #init()} should be called after properties have
43 public JcrRepositoryWrapper() {
50 /** Shutdown the repository */
51 public void destroy() throws Exception
{
55 * DELEGATED JCR REPOSITORY METHODS
58 public String
getDescriptor(String key
) {
59 return getRepository().getDescriptor(key
);
62 public String
[] getDescriptorKeys() {
63 return getRepository().getDescriptorKeys();
66 /** Central login method */
67 public Session
login(Credentials credentials
, String workspaceName
)
68 throws LoginException
, NoSuchWorkspaceException
,
72 session
= getRepository().login(credentials
, workspaceName
);
73 } catch (NoSuchWorkspaceException e
) {
74 if (autocreateWorkspaces
&& workspaceName
!= null)
75 session
= createWorkspaceAndLogsIn(credentials
, workspaceName
);
79 processNewSession(session
);
83 public Session
login() throws LoginException
, RepositoryException
{
84 return login(null, null);
87 public Session
login(Credentials credentials
) throws LoginException
,
89 return login(credentials
, null);
92 public Session
login(String workspaceName
) throws LoginException
,
93 NoSuchWorkspaceException
, RepositoryException
{
94 return login(null, workspaceName
);
97 /** Called after a session has been created, does nothing by default. */
98 protected void processNewSession(Session session
) {
101 /** Wraps access to the repository, making sure it is available. */
102 protected synchronized Repository
getRepository() {
103 // if (repository == null) {
104 // throw new ArgeoJcrException("No repository initialized."
105 // + " Was the init() method called?"
106 // + " The destroy() method should also"
107 // + " be called on shutdown.");
113 * Logs in to the default workspace, creates the required workspace, logs
114 * out, logs in to the required workspace.
116 protected Session
createWorkspaceAndLogsIn(Credentials credentials
,
117 String workspaceName
) throws RepositoryException
{
118 if (workspaceName
== null)
119 throw new ArgeoJcrException("No workspace specified.");
120 Session session
= getRepository().login(credentials
);
121 session
.getWorkspace().createWorkspace(workspaceName
);
123 return getRepository().login(credentials
, workspaceName
);
126 public boolean isStandardDescriptor(String key
) {
127 return getRepository().isStandardDescriptor(key
);
130 public boolean isSingleValueDescriptor(String key
) {
131 return getRepository().isSingleValueDescriptor(key
);
134 public Value
getDescriptorValue(String key
) {
135 return getRepository().getDescriptorValue(key
);
138 public Value
[] getDescriptorValues(String key
) {
139 return getRepository().getDescriptorValues(key
);
142 public synchronized void setRepository(Repository repository
) {
143 this.repository
= repository
;
146 public void setAutocreateWorkspaces(Boolean autocreateWorkspaces
) {
147 this.autocreateWorkspaces
= autocreateWorkspaces
;