1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.james.phoenix.jcr;
18
19 import javax.jcr.Session;
20 import javax.jcr.SimpleCredentials;
21 import javax.jcr.nodetype.NodeTypeManager;
22
23 import org.apache.avalon.framework.activity.Initializable;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.jackrabbit.api.JackrabbitNodeTypeManager;
28 import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;
29 import org.apache.jackrabbit.rmi.jackrabbit.JackrabbitClientAdapterFactory;
30 import org.apache.james.jcr.JCRMailRepository;
31
32
33
34
35 public class AvalonJCRMailRepository extends JCRMailRepository
36 implements Configurable, Initializable {
37
38
39
40 public void configure(Configuration configuration)
41 throws ConfigurationException {
42 String repository = configuration.getChild("repository").getValue();
43 try {
44 ClientRepositoryFactory factory = new ClientRepositoryFactory(
45 new JackrabbitClientAdapterFactory());
46 setRepository(factory.getRepository(repository));
47 } catch (Exception e) {
48 throw new ConfigurationException(
49 "Invalid repository address: " + repository, e);
50 }
51
52 String username = configuration.getChild("username").getValue(null);
53 String password = configuration.getChild("password").getValue(null);
54 if (username != null && password != null) {
55 setCredentials(
56 new SimpleCredentials(username, password.toCharArray()));
57 }
58
59 String workspace = configuration.getChild("workspace").getValue(null);
60 if (workspace != null) {
61 setWorkspace(workspace);
62 }
63
64 String path =
65 configuration.getAttribute("destinationURL", "james:repository");
66 int i = path.indexOf("://");
67 if (i != -1) {
68 path = path.substring(i + "://".length());
69 }
70 while (path.startsWith("/")) {
71 path = path.substring(1);
72 }
73 while (path.endsWith("/")) {
74 path = path.substring(0, path.length() - 1);
75 }
76 setPath(path);
77 }
78
79
80
81 public void initialize() throws Exception {
82 Session session =
83 getRepository().login(getCredentials(), getWorkspace());
84 try {
85 NodeTypeManager manager =
86 session.getWorkspace().getNodeTypeManager();
87 if (manager instanceof JackrabbitNodeTypeManager) {
88 JackrabbitNodeTypeManager jackrabbit =
89 (JackrabbitNodeTypeManager) manager;
90 if (!jackrabbit.hasNodeType("james:mail")) {
91 Class clazz = AvalonJCRMailRepository.class;
92 jackrabbit.registerNodeTypes(
93 clazz.getResourceAsStream("james.cnd"),
94 JackrabbitNodeTypeManager.TEXT_X_JCR_CND);
95 }
96 }
97
98 if (!session.getRootNode().hasNode(getPath())) {
99 session.getRootNode().addNode(getPath(), "nt:folder");
100 session.save();
101 }
102 } finally {
103 session.logout();
104 }
105 }
106
107 }