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.transport;
23  
24  import org.apache.avalon.framework.configuration.Configuration;
25  import org.apache.avalon.framework.configuration.ConfigurationException;
26  import org.apache.mailet.MailetConfig;
27  import org.apache.mailet.MailetContext;
28  
29  import java.util.Iterator;
30  
31  /**
32   * Implements the configuration object for a Mailet.
33   *
34   * @version CVS $Revision: 684700 $ $Date: 2008-08-11 11:27:40 +0100 (Mon, 11 Aug 2008) $
35   */
36  public class MailetConfigImpl implements MailetConfig {
37  
38      /**
39       * The mailet MailetContext
40       */
41      private MailetContext mailetContext;
42  
43      /**
44       * The mailet name
45       */
46      private String name;
47  
48      //This would probably be better.
49      //Properties params = new Properties();
50      //Instead, we're tied to the Configuration object
51      /**
52       * The mailet Avalon Configuration
53       */
54      private Configuration configuration;
55  
56      /**
57       * No argument constructor for this object.
58       */
59      public MailetConfigImpl() {
60      }
61  
62      /**
63       * Get the value of an parameter stored in this MailetConfig.  Multi-valued
64       * parameters are returned as a comma-delineated string.
65       *
66       * @param name the name of the parameter whose value is to be retrieved.
67       *
68       * @return the parameter value
69       */
70      public String getInitParameter(String name) {
71          try {
72              String result = null;
73  
74              final Configuration[] values = configuration.getChildren( name );
75              for ( int i = 0; i < values.length; i++ ) {
76                  if (result == null) {
77                      result = "";
78                  } else {
79                      result += ",";
80                  }
81                  Configuration conf = values[i];
82                  result += conf.getValue();
83              }
84              return result;
85          } catch (ConfigurationException ce) {
86              throw new RuntimeException("Embedded configuration exception was: " + ce.getMessage());
87          }
88  
89      }
90  
91      /**
92       * Returns an iterator over the set of configuration parameter names.
93       *
94       * @return an iterator over the set of configuration parameter names.
95       */
96      public Iterator getInitParameterNames() {
97          return new Iterator () {
98              Configuration[] children;
99              int count = 0;
100             {
101                 children = configuration.getChildren();
102             }
103 
104             public boolean hasNext() {
105                 return count < children.length;
106             }
107 
108             public Object next() {
109                 return children[count++].getName();
110             }
111 
112             public void remove() {
113                 throw new UnsupportedOperationException ("remove not supported");
114             }
115         };
116     }
117 
118     /**
119      * Get the value of an (XML) attribute stored in this MailetConfig.
120      *
121      * @param name the name of the attribute whose value is to be retrieved.
122      *
123      * @return the attribute value or null if missing
124      */
125     public String getInitAttribute(String name) {
126         return configuration.getAttribute(name, null);
127     }
128 
129     /**
130      * Get the mailet's MailetContext object.
131      *
132      * @return the MailetContext for the mailet
133      */
134     public MailetContext getMailetContext() {
135         return mailetContext;
136     }
137 
138     /**
139      * Set the mailet's Avalon Configuration object.
140      *
141      * @param newContext the MailetContext for the mailet
142      */
143     public void setMailetContext(MailetContext newContext) {
144         mailetContext = newContext;
145     }
146 
147     /**
148      * Set the Avalon Configuration object for the mailet.
149      *
150      * @param newConfiguration the new Configuration for the mailet
151      */
152     public void setConfiguration(Configuration newConfiguration) {
153         configuration = newConfiguration;
154     }
155 
156     /**
157      * Get the name of the mailet.
158      *
159      * @return the name of the mailet
160      */
161     public String getMailetName() {
162         return name;
163     }
164 
165     /**
166      * Set the name for the mailet.
167      *
168      * @param newName the new name for the mailet
169      */
170     public void setMailetName(String newName) {
171         name = newName;
172     }
173 }