View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.jcr;
21  
22  import javax.jcr.Credentials;
23  import javax.jcr.LoginException;
24  import javax.jcr.NoSuchWorkspaceException;
25  import javax.jcr.NodeIterator;
26  import javax.jcr.Repository;
27  import javax.jcr.RepositoryException;
28  import javax.jcr.Session;
29  import javax.jcr.SimpleCredentials;
30  import javax.jcr.query.InvalidQueryException;
31  import javax.jcr.query.Query;
32  import javax.jcr.query.QueryManager;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.jackrabbit.util.ISO9075;
36  import org.apache.jackrabbit.util.Text;
37  
38  /**
39   * Framework for JCR repositories used by James.
40   */
41  class AbstractJCRRepository {
42      
43      protected Log logger;
44  
45      /**
46       * JCR content repository used as the mail repository.
47       * Must be set before the any mail operations are performed.
48       */
49      protected Repository repository;
50      
51      /**
52       * Login credentials for accessing the repository.
53       * Set to <code>null</code> (the default) to use default credentials.
54       */
55      protected Credentials credentials;
56      
57      /**
58       * Name of the workspace used as the mail repository.
59       * Set to <code>null</code> (the default) to use the default workspace.
60       */
61      protected String workspace;
62      
63      /**
64       * Path (relative to root) of the mail repository within the workspace.
65       */
66      protected String path = "james";
67  
68      /**
69       * For setter injection.
70       */
71      public AbstractJCRRepository(Log logger) {
72          super();
73          this.logger = logger;
74      }
75      
76      /**
77       * Minimal constructor for injection.
78       * @param repository not null
79       */
80      public AbstractJCRRepository(Repository repository, Log logger) {
81          super();
82          this.repository = repository;
83          credentials = new SimpleCredentials("userid", "".toCharArray());
84          this.logger = logger;
85      }
86  
87      /**
88       * Maximal constructor for injection.
89       * @param repository not null
90       * @param credentials login credentials for accessing the repository
91       * or null to use default credentials
92       * @param workspace name of the workspace used as the mail repository.
93       * or null to use default workspace
94       * @param path path (relative to root) of the user node within the workspace,
95       * or null to use default.
96       */
97      public AbstractJCRRepository(Repository repository, Credentials credentials, String workspace, String path, 
98              Log logger) {
99          super();
100         this.repository = repository;
101         this.credentials = credentials;
102         this.workspace = workspace;
103         this.path = path;
104         this.logger = logger;
105     }
106 
107     
108     /**
109      * Gets the current logger.
110      * @return the logger, not null
111      */
112     public final Log getLogger() {
113         return logger;
114     }
115 
116     /**
117      * Sets the current logger.
118      * @param logger the logger to set, not null
119      */
120     public final void setLogger(Log logger) {
121         this.logger = logger;
122     }
123 
124     /**
125      * Retuns the JCR content repository used as the mail repository.
126      *
127      * @return JCR content repository
128      */
129     public Repository getRepository() {
130         return repository;
131     }
132 
133     /**
134      * Sets the JCR content repository to be used as the mail repository.
135      *
136      * @param repository JCR content repository
137      */
138     public void setRepository(Repository repository) {
139         this.repository = repository;
140     }
141 
142     /**
143      * Returns the login credentials for accessing the repository.
144      *
145      * @return login credentials,
146      *         or <code>null</code> if using the default credentials
147      */
148     public Credentials getCredentials() {
149         return credentials;
150     }
151 
152     /**
153      * Sets the login credentials for accessing the repository.
154      *
155      * @param credentials login credentials,
156      *                    or <code>null</code> to use the default credentials
157      */
158     public void setCredentials(Credentials credentials) {
159         this.credentials = credentials;
160     }
161 
162     /**
163      * Returns the name of the workspace used as the mail repository.
164      *
165      * @return workspace name,
166      *         or <code>null</code> if using the default workspace
167      */
168     public String getWorkspace() {
169         return workspace;
170     }
171 
172     /**
173      * Sets the name of the workspace used as the mail repository.
174      *
175      * @param workspace workspace name,
176      *                  or <code>null</code> to use the default workspace
177      */
178     public void setWorkspace(String workspace) {
179         this.workspace = workspace;
180     }
181 
182     /**
183      * Returns the path of the mail repository within the workspace.
184      *
185      * @return repository path
186      */
187     public String getPath() {
188         return path;
189     }
190 
191     /**
192      * Sets the path of the mail repository within the workspace.
193      *
194      * @param path repository path
195      */
196     public void setPath(String path) {
197         this.path = path;
198     }
199 
200     /**
201      * Logs into a new session.
202      * @return new session, not null
203      * @throws LoginException when login fails
204      * @throws NoSuchWorkspaceException when workspace does not exist
205      * @throws RepositoryException when access fails
206      */
207     protected Session login() throws LoginException, NoSuchWorkspaceException, RepositoryException {
208         Session session = repository.login(credentials, workspace);
209         return session;
210     }
211 
212     protected String toSafeName(String key) {
213         String name = ISO9075.encode(Text.escapeIllegalJcrChars(key));
214         return name;
215     }
216 
217     protected NodeIterator query(Session session, final String xpath) throws RepositoryException, InvalidQueryException {
218         QueryManager manager = session.getWorkspace().getQueryManager();
219         Query query = manager.createQuery(xpath, Query.XPATH);
220         NodeIterator iterator = query.execute().getNodes();
221         return iterator;
222     }
223 
224 }