View Javadoc

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