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