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.mailrepository;
21  
22  import org.apache.avalon.cornerstone.services.store.Store;
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.avalon.framework.logger.AbstractLogEnabled;
28  import org.apache.avalon.framework.service.ServiceException;
29  import org.apache.avalon.framework.service.ServiceManager;
30  import org.apache.avalon.framework.service.Serviceable;
31  import org.apache.james.services.SpoolRepository;
32  import org.apache.mailet.Mail;
33  
34  import javax.mail.MessagingException;
35  
36  import java.util.Collection;
37  import java.util.Iterator;
38  
39  /***
40   * This is a wrapper for the various implementations of SpoolRepositories.
41   * This does select the real spool repository via the select method of the
42   * provided Store.
43   *
44   * <p>The select method requires a configuration object with the form:
45   *  <br>&lt;spoolrepository destinationURL="file://path-to-root-dir-for-repository"
46   *  <br>            type="SPOOL"&gt;
47   *  <br>&lt;/spoolrepository&gt;
48   *
49   * @version This is $Revision: 165416 $
50   */
51  public class MailStoreSpoolRepository
52      extends AbstractLogEnabled
53      implements Serviceable, Initializable, Configurable, SpoolRepository {
54  
55      /***
56       * The wrapped spoolRepository
57       */
58      private SpoolRepository spoolRep;
59      
60      /***
61       * The providing mailStore
62       */
63      private Store mailStore;
64  
65      /***
66       * The repository configuration
67       */
68      private Configuration configuration;
69      
70      /***
71       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
72       */
73      public void service(ServiceManager serviceManager) throws ServiceException {
74          mailStore = (Store) serviceManager.lookup(Store.ROLE);
75      }
76  
77  
78      /***
79       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
80       */
81      public void configure(Configuration conf) throws ConfigurationException {
82          this.configuration = conf;
83      }
84  
85      /***
86       * @see org.apache.avalon.framework.activity.Initializable#initialize()
87       */
88      public void initialize() throws Exception {
89          try {
90              spoolRep  = (SpoolRepository) mailStore.select(configuration);
91          } catch (Exception e) {
92              getLogger().error("Cannot open private SpoolRepository");
93              throw e;
94          }
95          if (getLogger().isInfoEnabled()) {
96              getLogger().info("SpoolRepository opened: "
97                        + spoolRep.hashCode());
98          }
99      }
100 
101     /***
102      * @see org.apache.james.services.SpoolRepository#accept()
103      */
104     public Mail accept() throws InterruptedException {
105         return spoolRep.accept();
106     }
107 
108     /***
109      * @see org.apache.james.services.SpoolRepository#accept(long)
110      */
111     public Mail accept(final long delay) throws InterruptedException {
112         return spoolRep.accept(delay); 
113     }
114 
115     /***
116      * @see org.apache.james.services.SpoolRepository#accept(org.apache.james.services.SpoolRepository.AcceptFilter)
117      */
118     public Mail accept(SpoolRepository.AcceptFilter filter) throws InterruptedException {
119         return spoolRep.accept(filter);
120     }
121 
122     /***
123      * @see org.apache.james.services.MailRepository#store(org.apache.james.core.MailImpl)
124      */
125     public void store(Mail mc) throws MessagingException {
126         spoolRep.store(mc);
127     }
128 
129     /***
130      * @see org.apache.james.services.MailRepository#list()
131      */
132     public Iterator list() throws MessagingException {
133         return spoolRep.list();
134     }
135 
136     /***
137      * @see org.apache.james.services.MailRepository#retrieve(java.lang.String)
138      */
139     public Mail retrieve(String key) throws MessagingException {
140         return spoolRep.retrieve(key);
141     }
142 
143     /***
144      * @see org.apache.james.services.MailRepository#remove(org.apache.james.core.MailImpl)
145      */
146     public void remove(Mail mail) throws MessagingException {
147         spoolRep.remove(mail);
148     }
149 
150     /***
151      * @see org.apache.james.services.MailRepository#remove(java.util.Collection)
152      */
153     public void remove(Collection mails) throws MessagingException {
154         spoolRep.remove(mails);
155     }
156 
157     /***
158      * @see org.apache.james.services.MailRepository#remove(java.lang.String)
159      */
160     public void remove(String key) throws MessagingException {
161         spoolRep.remove(key);
162     }
163 
164     /***
165      * @see org.apache.james.services.MailRepository#lock(java.lang.String)
166      */
167     public boolean lock(String key) throws MessagingException {
168         return spoolRep.lock(key);
169     }
170 
171     /***
172      * @see org.apache.james.services.MailRepository#unlock(java.lang.String)
173      */
174     public boolean unlock(String key) throws MessagingException {
175         return spoolRep.unlock(key);
176     }
177     
178 }