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