View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Managed Avalon wrapper for the {@link JCRMailRepository} class.
34   */
35  public class AvalonJCRMailRepository extends JCRMailRepository
36          implements Configurable, Initializable {
37  
38      //--------------------------------------------------------< Configurable >
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      //-------------------------------------------------------< Initializable >
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 }