]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JackrabbitContainer.java
f6ae9357e201457a6eb8f439088b807f078f2b1b
[lgpl/argeo-commons.git] / JackrabbitContainer.java
1 package org.argeo.server.jackrabbit;
2
3 import java.io.File;
4 import java.io.InputStream;
5
6 import javax.jcr.Credentials;
7 import javax.jcr.LoginException;
8 import javax.jcr.NoSuchWorkspaceException;
9 import javax.jcr.Repository;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12
13 import org.apache.commons.io.FileUtils;
14 import org.apache.commons.io.IOUtils;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.jackrabbit.core.RepositoryImpl;
18 import org.apache.jackrabbit.core.TransientRepository;
19 import org.apache.jackrabbit.core.config.RepositoryConfig;
20 import org.springframework.beans.factory.DisposableBean;
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.core.io.Resource;
23
24 public class JackrabbitContainer implements InitializingBean, DisposableBean,
25 Repository {
26 private Log log = LogFactory.getLog(JackrabbitContainer.class);
27
28 private Resource configuration;
29 private File homeDirectory;
30
31 private Boolean inMemory = false;
32
33 private Repository repository;
34
35 public void afterPropertiesSet() throws Exception {
36 if (inMemory && homeDirectory.exists()) {
37 FileUtils.deleteDirectory(homeDirectory);
38 log.warn("Deleted Jackrabbit home directory " + homeDirectory);
39 }
40
41 RepositoryConfig config;
42 InputStream in = configuration.getInputStream();
43 try {
44 config = RepositoryConfig.create(in, homeDirectory
45 .getCanonicalPath());
46 } catch (Exception e) {
47 throw new RuntimeException("Cannot read configuration", e);
48 } finally {
49 IOUtils.closeQuietly(in);
50 }
51
52 if (inMemory)
53 repository = new TransientRepository(config);
54 else
55 repository = RepositoryImpl.create(config);
56
57 log.info("Initialized Jackrabbit repository " + repository + " in "
58 + homeDirectory + " with config " + configuration);
59 }
60
61 public void destroy() throws Exception {
62 if (repository != null) {
63 if (repository instanceof RepositoryImpl)
64 ((RepositoryImpl) repository).shutdown();
65 else if (repository instanceof TransientRepository)
66 ((TransientRepository) repository).shutdown();
67 }
68
69 if (inMemory)
70 if (homeDirectory.exists()) {
71 FileUtils.deleteDirectory(homeDirectory);
72 if (log.isDebugEnabled())
73 log.debug("Deleted Jackrabbit home directory "
74 + homeDirectory);
75 }
76 log.info("Destroyed Jackrabbit repository " + repository + " in "
77 + homeDirectory + " with config " + configuration);
78 }
79
80 // JCR REPOSITORY (delegated)
81 public String getDescriptor(String key) {
82 return repository.getDescriptor(key);
83 }
84
85 public String[] getDescriptorKeys() {
86 return repository.getDescriptorKeys();
87 }
88
89 public Session login() throws LoginException, RepositoryException {
90 return repository.login();
91 }
92
93 public Session login(Credentials credentials, String workspaceName)
94 throws LoginException, NoSuchWorkspaceException,
95 RepositoryException {
96 return repository.login(credentials, workspaceName);
97 }
98
99 public Session login(Credentials credentials) throws LoginException,
100 RepositoryException {
101 return repository.login(credentials);
102 }
103
104 public Session login(String workspaceName) throws LoginException,
105 NoSuchWorkspaceException, RepositoryException {
106 return repository.login(workspaceName);
107 }
108
109 // BEANS METHODS
110 public void setHomeDirectory(File homeDirectory) {
111 this.homeDirectory = homeDirectory;
112 }
113
114 public void setConfiguration(Resource configuration) {
115 this.configuration = configuration;
116 }
117
118 public void setInMemory(Boolean inMemory) {
119 this.inMemory = inMemory;
120 }
121
122 }