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