2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.server
.jackrabbit
;
20 import java
.io
.InputStream
;
21 import java
.util
.HashMap
;
24 import javax
.jcr
.Credentials
;
25 import javax
.jcr
.LoginException
;
26 import javax
.jcr
.NoSuchWorkspaceException
;
27 import javax
.jcr
.Repository
;
28 import javax
.jcr
.RepositoryException
;
29 import javax
.jcr
.Session
;
30 import javax
.jcr
.Value
;
32 import org
.apache
.commons
.io
.FileUtils
;
33 import org
.apache
.commons
.io
.IOUtils
;
34 import org
.apache
.commons
.logging
.Log
;
35 import org
.apache
.commons
.logging
.LogFactory
;
36 import org
.apache
.jackrabbit
.commons
.NamespaceHelper
;
37 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
38 import org
.apache
.jackrabbit
.core
.TransientRepository
;
39 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
40 import org
.argeo
.ArgeoException
;
41 import org
.springframework
.beans
.factory
.DisposableBean
;
42 import org
.springframework
.beans
.factory
.InitializingBean
;
43 import org
.springframework
.core
.io
.Resource
;
46 * Wrapper around a Jackrabbit repository which allows to configure it in Spring
47 * and expose it as a {@link Repository}.
49 public class JackrabbitContainer
implements InitializingBean
, DisposableBean
,
51 private Log log
= LogFactory
.getLog(JackrabbitContainer
.class);
53 private Resource configuration
;
54 private File homeDirectory
;
56 private Boolean inMemory
= false;
58 private Repository repository
;
60 /** Namespaces to register: key is prefix, value namespace */
61 private Map
<String
, String
> namespaces
= new HashMap
<String
, String
>();
63 public void afterPropertiesSet() throws Exception
{
64 if (inMemory
&& homeDirectory
.exists()) {
65 FileUtils
.deleteDirectory(homeDirectory
);
66 log
.warn("Deleted Jackrabbit home directory " + homeDirectory
);
69 RepositoryConfig config
;
70 InputStream in
= configuration
.getInputStream();
72 config
= RepositoryConfig
.create(in
,
73 homeDirectory
.getCanonicalPath());
74 } catch (Exception e
) {
75 throw new RuntimeException("Cannot read configuration", e
);
77 IOUtils
.closeQuietly(in
);
81 repository
= new TransientRepository(config
);
83 repository
= RepositoryImpl
.create(config
);
85 log
.info("Initialized Jackrabbit repository " + repository
+ " in "
86 + homeDirectory
+ " with config " + configuration
);
89 public void destroy() throws Exception
{
90 if (repository
!= null) {
91 if (repository
instanceof RepositoryImpl
)
92 ((RepositoryImpl
) repository
).shutdown();
93 else if (repository
instanceof TransientRepository
)
94 ((TransientRepository
) repository
).shutdown();
98 if (homeDirectory
.exists()) {
99 FileUtils
.deleteDirectory(homeDirectory
);
100 if (log
.isDebugEnabled())
101 log
.debug("Deleted Jackrabbit home directory "
104 log
.info("Destroyed Jackrabbit repository " + repository
+ " in "
105 + homeDirectory
+ " with config " + configuration
);
108 // JCR REPOSITORY (delegated)
109 public String
getDescriptor(String key
) {
110 return repository
.getDescriptor(key
);
113 public String
[] getDescriptorKeys() {
114 return repository
.getDescriptorKeys();
117 public Session
login() throws LoginException
, RepositoryException
{
118 Session session
= repository
.login();
119 processNewSession(session
);
123 public Session
login(Credentials credentials
, String workspaceName
)
124 throws LoginException
, NoSuchWorkspaceException
,
125 RepositoryException
{
126 Session session
= repository
.login(credentials
, workspaceName
);
127 processNewSession(session
);
131 public Session
login(Credentials credentials
) throws LoginException
,
132 RepositoryException
{
133 Session session
= repository
.login(credentials
);
134 processNewSession(session
);
138 public Session
login(String workspaceName
) throws LoginException
,
139 NoSuchWorkspaceException
, RepositoryException
{
140 Session session
= repository
.login(workspaceName
);
141 processNewSession(session
);
145 protected synchronized void processNewSession(Session session
) {
147 NamespaceHelper namespaceHelper
= new NamespaceHelper(session
);
148 namespaceHelper
.registerNamespaces(namespaces
);
149 } catch (RepositoryException e
) {
150 throw new ArgeoException("Cannot process new session", e
);
154 public boolean isStandardDescriptor(String key
) {
155 return repository
.isStandardDescriptor(key
);
158 public boolean isSingleValueDescriptor(String key
) {
159 return repository
.isSingleValueDescriptor(key
);
162 public Value
getDescriptorValue(String key
) {
163 return repository
.getDescriptorValue(key
);
166 public Value
[] getDescriptorValues(String key
) {
167 return repository
.getDescriptorValues(key
);
171 public void setHomeDirectory(File homeDirectory
) {
172 this.homeDirectory
= homeDirectory
;
175 public void setConfiguration(Resource configuration
) {
176 this.configuration
= configuration
;
179 public void setInMemory(Boolean inMemory
) {
180 this.inMemory
= inMemory
;
183 public void setNamespaces(Map
<String
, String
> namespaces
) {
184 this.namespaces
= namespaces
;