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