]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/JcrRepositoryWrapper.java
Adapt to package names changes in Spring Security
[lgpl/argeo-commons.git] / org.argeo.server.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 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;
25
26 import org.argeo.ArgeoException;
27
28 /**
29 * Wrapper around a JCR repository which allows to simplify configuration and
30 * intercept some actions. It exposes itself as a {@link Repository}.
31 */
32 public abstract class JcrRepositoryWrapper implements Repository {
33 // private final static Log log = LogFactory
34 // .getLog(JcrRepositoryWrapper.class);
35
36 // wrapped repository
37 private Repository repository;
38
39 private Boolean autocreateWorkspaces = false;
40
41 /**
42 * Empty constructor, {@link #init()} should be called after properties have
43 * been set
44 */
45 public JcrRepositoryWrapper() {
46 }
47
48 /** Initializes */
49 public void init() {
50 }
51
52 /** Shutdown the repository */
53 public void destroy() throws Exception {
54 }
55
56 /*
57 * DELEGATED JCR REPOSITORY METHODS
58 */
59
60 public String getDescriptor(String key) {
61 return getRepository().getDescriptor(key);
62 }
63
64 public String[] getDescriptorKeys() {
65 return getRepository().getDescriptorKeys();
66 }
67
68 /** Central login method */
69 public Session login(Credentials credentials, String workspaceName)
70 throws LoginException, NoSuchWorkspaceException,
71 RepositoryException {
72 Session session;
73 try {
74 session = getRepository().login(credentials, workspaceName);
75 } catch (NoSuchWorkspaceException e) {
76 if (autocreateWorkspaces && workspaceName != null)
77 session = createWorkspaceAndLogsIn(credentials, workspaceName);
78 else
79 throw e;
80 }
81 processNewSession(session);
82 return session;
83 }
84
85 public Session login() throws LoginException, RepositoryException {
86 return login(null, null);
87 }
88
89 public Session login(Credentials credentials) throws LoginException,
90 RepositoryException {
91 return login(credentials, null);
92 }
93
94 public Session login(String workspaceName) throws LoginException,
95 NoSuchWorkspaceException, RepositoryException {
96 return login(null, workspaceName);
97 }
98
99 /** Called after a session has been created, does nothing by default. */
100 protected void processNewSession(Session session) {
101 }
102
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.");
110 // }
111 return repository;
112 }
113
114 /**
115 * Logs in to the default workspace, creates the required workspace, logs
116 * out, logs in to the required workspace.
117 */
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);
124 session.logout();
125 return getRepository().login(credentials, workspaceName);
126 }
127
128 public boolean isStandardDescriptor(String key) {
129 return getRepository().isStandardDescriptor(key);
130 }
131
132 public boolean isSingleValueDescriptor(String key) {
133 return getRepository().isSingleValueDescriptor(key);
134 }
135
136 public Value getDescriptorValue(String key) {
137 return getRepository().getDescriptorValue(key);
138 }
139
140 public Value[] getDescriptorValues(String key) {
141 return getRepository().getDescriptorValues(key);
142 }
143
144 public synchronized void setRepository(Repository repository) {
145 this.repository = repository;
146 }
147
148 public void setAutocreateWorkspaces(Boolean autocreateWorkspaces) {
149 this.autocreateWorkspaces = autocreateWorkspaces;
150 }
151
152 }