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  package org.apache.james.transport;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.Vector;
27  
28  import org.apache.avalon.framework.activity.Initializable;
29  import org.apache.avalon.framework.configuration.Configurable;
30  import org.apache.avalon.framework.configuration.Configuration;
31  import org.apache.avalon.framework.configuration.ConfigurationException;
32  import org.apache.avalon.framework.logger.AbstractLogEnabled;
33  import org.apache.avalon.framework.logger.Logger;
34  import org.apache.avalon.framework.service.DefaultServiceManager;
35  import org.apache.avalon.framework.service.ServiceException;
36  import org.apache.avalon.framework.service.ServiceManager;
37  import org.apache.avalon.framework.service.Serviceable;
38  import org.apache.james.services.FileSystem;
39  import org.apache.mailet.Mail;
40  import org.apache.mailet.MailAddress;
41  import org.apache.mailet.MailetContext;
42  
43  import javax.mail.MessagingException;
44  import javax.mail.internet.MimeMessage;
45  
46  /**
47   *
48   * $Id: Loader.java 684792 2008-08-11 16:18:23Z bago $
49   */
50  public abstract class Loader extends AbstractLogEnabled implements Serviceable, Configurable, Initializable {
51  
52      protected String baseDirectory = null;
53      protected final String MAILET_PACKAGE = "mailetpackage";
54      protected final String MATCHER_PACKAGE = "matcherpackage";
55      /**
56       * The list of packages that may contain Mailets or matchers
57       */
58      protected Vector packages;
59  
60      /**
61       * System service manager
62       */
63      private ServiceManager serviceManager;
64  
65      /**
66       * Mailet context
67       */
68      protected MailetContext mailetContext;
69  
70      /**
71       * Set the MailetContext
72       * 
73       * @param mailetContext the MailetContext
74       */
75      public void setMailetContext(MailetContext mailetContext) {
76          this.mailetContext = mailetContext;
77      }
78  
79      protected void getPackages(Configuration conf, String packageType)
80          throws ConfigurationException {
81          packages = new Vector();
82          packages.addElement("");
83          final Configuration[] pkgConfs = conf.getChildren(packageType);
84          for (int i = 0; i < pkgConfs.length; i++) {
85              Configuration c = pkgConfs[i];
86              String packageName = c.getValue();
87              if (!packageName.endsWith(".")) {
88                  packageName += ".";
89              }
90              packages.addElement(packageName);
91          }
92      }
93  
94      /**
95       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
96       */
97      public void service(ServiceManager sm) throws ServiceException {
98          serviceManager = new DefaultServiceManager(sm);
99          try {
100             baseDirectory = ((FileSystem) serviceManager.lookup(FileSystem.ROLE)).getBasedir().getCanonicalPath();
101         } catch (FileNotFoundException e) {
102             throw new ServiceException(FileSystem.ROLE, "Cannot find the base directory of the application", e);
103         } catch (IOException e) {
104             throw new ServiceException(FileSystem.ROLE, "Cannot find the base directory of the application", e);
105         }
106     }
107 
108     /**
109      * @see org.apache.avalon.framework.activity.Initializable#initialize()
110      */
111     public void initialize() throws Exception {
112         setMailetContext((MailetContext) serviceManager.lookup(MailetContext.class.getName()));
113     }
114         
115     /**
116      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
117      */
118     public abstract void configure(Configuration arg0) throws ConfigurationException;
119 
120     /**
121      * Wrapper fot a MailetContext that simply override the used logger.
122      */
123     protected final static class MailetContextWrapper implements MailetContext {
124         
125         /** the mailetContext */
126         private MailetContext mailetContext;
127         /** the logger */
128         private Logger logger;
129 
130         /**
131          * Create a mailetContext wrapper that use a different logger for the log
132          * operations
133          * 
134          * @param mailetContext the mailet context to be wrapped
135          * @param logger the logger to be used instead of the parent one. 
136          */
137         public MailetContextWrapper(MailetContext mailetContext, Logger logger) {
138             this.mailetContext = mailetContext;
139             this.logger = logger;
140         }
141 
142         /**
143          * @see org.apache.mailet.MailetContext#bounce(org.apache.mailet.Mail, java.lang.String)
144          */
145         public void bounce(Mail mail, String message) throws MessagingException {
146             mailetContext.bounce(mail, message);
147         }
148 
149         /**
150          * @see org.apache.mailet.MailetContext#bounce(org.apache.mailet.Mail, java.lang.String, org.apache.mailet.MailAddress)
151          */
152         public void bounce(Mail mail, String message, MailAddress bouncer) throws MessagingException {
153             mailetContext.bounce(mail, message, bouncer);
154         }
155 
156         /**
157          * @see org.apache.mailet.MailetContext#getAttribute(java.lang.String)
158          */
159         public Object getAttribute(String name) {
160             return mailetContext.getAttribute(name);
161         }
162 
163         /**
164          * @see org.apache.mailet.MailetContext#getAttributeNames()
165          */
166         public Iterator getAttributeNames() {
167             return mailetContext.getAttributeNames();
168         }
169 
170         /**
171          * @see org.apache.mailet.MailetContext#getMailServers(java.lang.String)
172          */
173         public Collection getMailServers(String host) {
174             return mailetContext.getMailServers(host);
175         }
176 
177         /**
178          * @see org.apache.mailet.MailetContext#getMajorVersion()
179          */
180         public int getMajorVersion() {
181             return mailetContext.getMajorVersion();
182         }
183 
184         /**
185          * @see org.apache.mailet.MailetContext#getMinorVersion()
186          */
187         public int getMinorVersion() {
188             return mailetContext.getMinorVersion();
189         }
190 
191         /**
192          * @see org.apache.mailet.MailetContext#getPostmaster()
193          */
194         public MailAddress getPostmaster() {
195             return mailetContext.getPostmaster();
196         }
197 
198         /**
199          * @see org.apache.mailet.MailetContext#getSMTPHostAddresses(java.lang.String)
200          */
201         public Iterator getSMTPHostAddresses(String domainName) {
202             return mailetContext.getSMTPHostAddresses(domainName);
203         }
204 
205         /**
206          * @see org.apache.mailet.MailetContext#getServerInfo()
207          */
208         public String getServerInfo() {
209             return mailetContext.getServerInfo();
210         }
211 
212         /**
213          * @see org.apache.mailet.MailetContext#isLocalEmail(org.apache.mailet.MailAddress)
214          */
215         public boolean isLocalEmail(MailAddress mailAddress) {
216             return mailetContext.isLocalEmail(mailAddress);
217         }
218 
219         /**
220          * @see org.apache.mailet.MailetContext#isLocalServer(java.lang.String)
221          */
222         public boolean isLocalServer(String serverName) {
223             return mailetContext.isLocalServer(serverName);
224         }
225 
226         /**
227          * @see org.apache.mailet.MailetContext#isLocalUser(java.lang.String)
228          */
229         public boolean isLocalUser(String userAccount) {
230             return mailetContext.isLocalUser(userAccount);
231         }
232 
233         /**
234          * @see org.apache.mailet.MailetContext#log(java.lang.String)
235          */
236         public void log(String message) {
237             logger.info(message);
238         }
239 
240         /**
241          * @see org.apache.mailet.MailetContext#log(java.lang.String, java.lang.Throwable)
242          */
243         public void log(String message, Throwable t) {
244             logger.info(message, t);
245         }
246 
247         /**
248          * @see org.apache.mailet.MailetContext#removeAttribute(java.lang.String)
249          */
250         public void removeAttribute(String name) {
251             mailetContext.removeAttribute(name);
252         }
253 
254         /**
255          * @see org.apache.mailet.MailetContext#sendMail(javax.mail.internet.MimeMessage)
256          */
257         public void sendMail(MimeMessage msg) throws MessagingException {
258             mailetContext.sendMail(msg);
259         }
260 
261         /**
262          * @see org.apache.mailet.MailetContext#sendMail(org.apache.mailet.MailAddress, java.util.Collection, javax.mail.internet.MimeMessage)
263          */
264         public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg) throws MessagingException {
265             mailetContext.sendMail(sender, recipients, msg);
266         }
267 
268         /**
269          * @see org.apache.mailet.MailetContext#sendMail(org.apache.mailet.MailAddress, java.util.Collection, javax.mail.internet.MimeMessage, java.lang.String)
270          */
271         public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg, String state) throws MessagingException {
272             mailetContext.sendMail(sender, recipients, msg, state);
273         }
274 
275         /**
276          * @see org.apache.mailet.MailetContext#sendMail(org.apache.mailet.Mail)
277          */
278         public void sendMail(Mail mail) throws MessagingException {
279             mailetContext.sendMail(mail);
280         }
281 
282         /**
283          * @see org.apache.mailet.MailetContext#setAttribute(java.lang.String, java.lang.Object)
284          */
285         public void setAttribute(String name, Object object) {
286             mailetContext.setAttribute(name, object);
287         }
288 
289         /**
290          * @see org.apache.mailet.MailetContext#storeMail(MailAddress, MailAddress, MimeMessage)
291          */
292         public void storeMail(MailAddress sender, MailAddress recipient, MimeMessage msg) throws MessagingException {
293             mailetContext.storeMail(sender, recipient, msg);
294         }
295     }
296 
297 }