]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
Improve JCR mapper
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / server / jackrabbit / 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.jackrabbit.core.RepositoryImpl;
16 import org.apache.jackrabbit.core.TransientRepository;
17 import org.apache.jackrabbit.core.config.RepositoryConfig;
18 import org.springframework.beans.factory.DisposableBean;
19 import org.springframework.beans.factory.InitializingBean;
20 import org.springframework.core.io.Resource;
21
22 public class JackrabbitContainer implements InitializingBean, DisposableBean,
23 Repository {
24 private Resource configuration;
25 private File homeDirectory;
26
27 private Boolean inMemory = false;
28
29 private Repository repository;
30
31 public void afterPropertiesSet() throws Exception {
32 RepositoryConfig config;
33 InputStream in = configuration.getInputStream();
34 try {
35 config = RepositoryConfig.create(in, homeDirectory
36 .getCanonicalPath());
37 } catch (Exception e) {
38 throw new RuntimeException("Cannot read configuration", e);
39 } finally {
40 IOUtils.closeQuietly(in);
41 }
42
43 if (inMemory)
44 repository = new TransientRepository(config);
45 else
46 repository = RepositoryImpl.create(config);
47 }
48
49 public void destroy() throws Exception {
50 if (repository != null) {
51 if (repository instanceof RepositoryImpl)
52 ((RepositoryImpl) repository).shutdown();
53 }
54
55 if (inMemory)
56 if (homeDirectory.exists())
57 FileUtils.deleteDirectory(homeDirectory);
58 }
59
60 // JCR REPOSITORY (delegated)
61 public String getDescriptor(String key) {
62 return repository.getDescriptor(key);
63 }
64
65 public String[] getDescriptorKeys() {
66 return repository.getDescriptorKeys();
67 }
68
69 public Session login() throws LoginException, RepositoryException {
70 return repository.login();
71 }
72
73 public Session login(Credentials credentials, String workspaceName)
74 throws LoginException, NoSuchWorkspaceException,
75 RepositoryException {
76 return repository.login(credentials, workspaceName);
77 }
78
79 public Session login(Credentials credentials) throws LoginException,
80 RepositoryException {
81 return repository.login(credentials);
82 }
83
84 public Session login(String workspaceName) throws LoginException,
85 NoSuchWorkspaceException, RepositoryException {
86 return repository.login(workspaceName);
87 }
88
89 // BEANS METHODS
90 public void setHomeDirectory(File homeDirectory) {
91 this.homeDirectory = homeDirectory;
92 }
93
94 public void setConfiguration(Resource configuration) {
95 this.configuration = configuration;
96 }
97
98 }