View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-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  package org.apache.james.transport;
18  import java.io.File;
19  import java.util.Vector;
20  
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.context.Context;
26  import org.apache.avalon.framework.context.ContextException;
27  import org.apache.avalon.framework.context.Contextualizable;
28  import org.apache.avalon.framework.logger.AbstractLogEnabled;
29  import org.apache.avalon.framework.service.DefaultServiceManager;
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.mailet.MailetContext;
34  
35  /***
36   *
37   * $Id: Loader.java 365943 2006-01-04 16:56:41 +0000 (mer, 04 gen 2006) bago $
38   */
39  public abstract class Loader extends AbstractLogEnabled implements Contextualizable, Serviceable, Configurable, Initializable {
40  
41      protected String baseDirectory = null;
42      protected final String MAILET_PACKAGE = "mailetpackage";
43      protected final String MATCHER_PACKAGE = "matcherpackage";
44      /***
45       * The list of packages that may contain Mailets or matchers
46       */
47      protected Vector packages;
48  
49      /***
50       * System service manager
51       */
52      private ServiceManager serviceManager;
53  
54      /***
55       * Mailet context
56       */
57      protected MailetContext mailetContext;
58  
59      /***
60       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
61       */
62      public void contextualize(final Context context) throws ContextException 
63      {
64          try 
65          {
66              baseDirectory = ((File)context.get( "app.home") ).getCanonicalPath();
67          } 
68          catch (Throwable e) 
69          {
70              getLogger().error( "can't get base directory for mailet loader" );
71              throw new ContextException("can't contextualise mailet loader " + e.getMessage(), e);
72          }
73      }
74  
75      protected void getPackages(Configuration conf, String packageType)
76          throws ConfigurationException {
77          packages = new Vector();
78          packages.addElement("");
79          final Configuration[] pkgConfs = conf.getChildren(packageType);
80          for (int i = 0; i < pkgConfs.length; i++) {
81              Configuration c = pkgConfs[i];
82              String packageName = c.getValue();
83              if (!packageName.endsWith(".")) {
84                  packageName += ".";
85              }
86              packages.addElement(packageName);
87          }
88      }
89  
90      /***
91       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
92       */
93      public void service(ServiceManager sm) throws ServiceException {
94          serviceManager = new DefaultServiceManager(sm);
95      }
96  
97      /***
98       * @see org.apache.avalon.framework.activity.Initializable#initialize()
99       */
100     public void initialize() throws Exception {
101         mailetContext = (MailetContext) serviceManager.lookup("org.apache.mailet.MailetContext");
102     }
103         
104     /***
105      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
106      */
107     public abstract void configure(Configuration arg0) throws ConfigurationException;
108 
109 }