001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.core;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.mailet.MailetConfig;
023: import org.apache.mailet.MailetContext;
024:
025: import java.util.Iterator;
026:
027: /**
028: * Implements the configuration object for a Mailet.
029: *
030: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
031: */
032: public class MailetConfigImpl implements MailetConfig {
033:
034: /**
035: * The mailet MailetContext
036: */
037: private MailetContext mailetContext;
038:
039: /**
040: * The mailet name
041: */
042: private String name;
043:
044: //This would probably be better.
045: //Properties params = new Properties();
046: //Instead, we're tied to the Configuration object
047: /**
048: * The mailet Avalon Configuration
049: */
050: private Configuration configuration;
051:
052: /**
053: * No argument constructor for this object.
054: */
055: public MailetConfigImpl() {
056: }
057:
058: /**
059: * Get the value of an parameter stored in this MailetConfig. Multi-valued
060: * parameters are returned as a comma-delineated string.
061: *
062: * @param name the name of the parameter whose value is to be retrieved.
063: *
064: * @return the parameter value
065: */
066: public String getInitParameter(String name) {
067: try {
068: String result = null;
069:
070: final Configuration[] values = configuration
071: .getChildren(name);
072: for (int i = 0; i < values.length; i++) {
073: if (result == null) {
074: result = "";
075: } else {
076: result += ",";
077: }
078: Configuration conf = values[i];
079: result += conf.getValue();
080: }
081: return result;
082: } catch (ConfigurationException ce) {
083: throw new RuntimeException(
084: "Embedded configuration exception was: "
085: + ce.getMessage());
086: }
087:
088: }
089:
090: /**
091: * Returns an iterator over the set of configuration parameter names.
092: *
093: * @return an iterator over the set of configuration parameter names.
094: */
095: public Iterator getInitParameterNames() {
096: return new Iterator() {
097: Configuration[] children;
098: int count = 0;
099: {
100: children = configuration.getChildren();
101: }
102:
103: public boolean hasNext() {
104: return count < children.length;
105: }
106:
107: public Object next() {
108: return children[count++].getName();
109: }
110:
111: public void remove() {
112: throw new UnsupportedOperationException(
113: "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: * Get the mailet's Avalon Configuration object.
140: *
141: * @return the Configuration 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: }
|