]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
First draft of DAO implementation for JCR.
[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 else if (repository instanceof TransientRepository)
54 ((TransientRepository) repository).shutdown();
55 }
56
57 if (inMemory)
58 if (homeDirectory.exists())
59 FileUtils.deleteDirectory(homeDirectory);
60 }
61
62 // JCR REPOSITORY (delegated)
63 public String getDescriptor(String key) {
64 return repository.getDescriptor(key);
65 }
66
67 public String[] getDescriptorKeys() {
68 return repository.getDescriptorKeys();
69 }
70
71 public Session login() throws LoginException, RepositoryException {
72 return repository.login();
73 }
74
75 public Session login(Credentials credentials, String workspaceName)
76 throws LoginException, NoSuchWorkspaceException,
77 RepositoryException {
78 return repository.login(credentials, workspaceName);
79 }
80
81 public Session login(Credentials credentials) throws LoginException,
82 RepositoryException {
83 return repository.login(credentials);
84 }
85
86 public Session login(String workspaceName) throws LoginException,
87 NoSuchWorkspaceException, RepositoryException {
88 return repository.login(workspaceName);
89 }
90
91 // BEANS METHODS
92 public void setHomeDirectory(File homeDirectory) {
93 this.homeDirectory = homeDirectory;
94 }
95
96 public void setConfiguration(Resource configuration) {
97 this.configuration = configuration;
98 }
99
100 }