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 package org.apache.james.postage.configuration;
22
23 import org.apache.commons.configuration.Configuration;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.james.postage.user.UserList;
27
28 import java.util.Iterator;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 /***
34 * generates named PostageConfigurations
35 */
36 public class ConfigurationLoader {
37
38 private static Log log = LogFactory.getLog(ConfigurationLoader.class);
39
40 public Map create(Configuration configuration) {
41 log.debug("reading configuration.");
42
43 Map postageConfigurations = new LinkedHashMap();
44
45 List scenariosIds = configuration.getList("scenario[@id]");
46 log.debug("scenarios contained in configuration: " + scenariosIds.size());
47
48 Iterator scenarioIter = scenariosIds.iterator();
49 int scenarioCount = 0;
50 while (scenarioIter.hasNext()) {
51 String scenarioId = (String) scenarioIter.next();
52
53 if (postageConfigurations.containsKey(scenarioId)) {
54 log.error("found in configuration more than one scenario which is named: " + scenarioId);
55 continue;
56 }
57
58 PostageConfiguration postageConfiguration = new PostageConfiguration(scenarioId);
59
60 String scenario = getIndexedPropertyName("scenario", scenarioCount);
61
62 postageConfiguration.setDurationMinutes(configuration.getInt(getAttributedPropertyName(scenario, "runtimeMinutes")));
63
64 addDescription(postageConfiguration, configuration.subset(scenario + ".description"));
65
66 String scenarioInternalUsers = scenario + ".users.internal";
67 UserList internals = new UserList(configuration.getInt(getAttributedPropertyName(scenarioInternalUsers, "count")),
68 configuration.getString(getAttributedPropertyName(scenarioInternalUsers, "username-prefix")),
69 configuration.getString(getAttributedPropertyName(scenarioInternalUsers, "domain")),
70 configuration.getString(getAttributedPropertyName(scenarioInternalUsers, "password")));
71 postageConfiguration.setInternalUsers(internals);
72 postageConfiguration.setInternalReuseExisting(configuration.getBoolean(getAttributedPropertyName(scenarioInternalUsers, "reuseExisting")));
73
74 String scenarioExternalUsers = scenario + ".users.external";
75 UserList externals = new UserList(configuration.getInt(getAttributedPropertyName(scenarioExternalUsers, "count")),
76 configuration.getString(getAttributedPropertyName(scenarioExternalUsers, "username-prefix")),
77 configuration.getString(getAttributedPropertyName(scenarioExternalUsers, "domain")));
78 postageConfiguration.setExternalUsers(externals);
79
80 String scenarioTestserver = scenario + ".testserver";
81 postageConfiguration.setTestserverHost(configuration.getString(getAttributedPropertyName(scenarioTestserver, "host")));
82 postageConfiguration.setTestserverPortPOP3(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".pop3", "port")));
83 postageConfiguration.setTestserverPOP3FetchesPerMinute(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".pop3", "count-per-min")));
84 postageConfiguration.setTestserverPortSMTPInbound(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".smtp-inbound", "port")));
85 postageConfiguration.setTestserverPortSMTPForwarding(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".smtp-forwarding", "port")));
86 postageConfiguration.setTestserverSMTPForwardingWaitSeconds(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".smtp-forwarding", "latecomer-wait-seconds")));
87 postageConfiguration.setTestserverRemoteManagerPort(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".remotemanager", "port")));
88 postageConfiguration.setTestserverRemoteManagerUsername(configuration.getString(getAttributedPropertyName(scenarioTestserver + ".remotemanager", "name")));
89 postageConfiguration.setTestserverRemoteManagerPassword(configuration.getString(getAttributedPropertyName(scenarioTestserver + ".remotemanager", "password")));
90 postageConfiguration.setTestserverSpamAccountUsername(configuration.getString(getAttributedPropertyName(scenarioTestserver + ".spam-account", "name")));
91 postageConfiguration.setTestserverSpamAccountPassword(configuration.getString(getAttributedPropertyName(scenarioTestserver + ".spam-account", "password")));
92 postageConfiguration.setTestserverPortJMXRemoting(configuration.getInt(getAttributedPropertyName(scenarioTestserver + ".jvm-resources", "jmx-remoting-port")));
93
94 addSendProfiles(postageConfiguration, configuration, scenario);
95
96 postageConfigurations.put(postageConfiguration.getId(), postageConfiguration);
97
98 scenarioCount++;
99 }
100
101 return postageConfigurations;
102 }
103
104 private void addDescription(PostageConfiguration postageConfiguration, Configuration configuration) {
105 Iterator keys = configuration.getKeys();
106
107
108 while (keys.hasNext()) {
109 String itemName = (String) keys.next();
110 String itemContent = configuration.getString(itemName);
111
112 postageConfiguration.addDescriptionItem(itemName, itemContent);
113 }
114 }
115
116 private void addSendProfiles(PostageConfiguration postageConfiguration, Configuration configuration, String scenario) {
117 List profileNames = configuration.getList(scenario + ".profiles.profile[@name]");
118 log.debug("profiles contained in scenario " + postageConfiguration.getId() + ": " + profileNames.size());
119
120 Iterator profileIter = profileNames.iterator();
121 int profileCount = 0;
122 while (profileIter.hasNext()) {
123 String profileName = (String) profileIter.next();
124
125 SendProfile profile = new SendProfile(profileName);
126
127 String profilePath = getIndexedPropertyName(scenario + ".profiles.profile", profileCount);
128
129 profile.setSourceInternal(convertToInternalExternalFlag(configuration.getString(getAttributedPropertyName(profilePath, "source"))));
130 profile.setTargetInternal(convertToInternalExternalFlag(configuration.getString(getAttributedPropertyName(profilePath, "target"))));
131
132 addMailSender(profile, configuration, profilePath);
133
134 postageConfiguration.addProfile(profile);
135
136 profileCount++;
137 }
138 }
139
140 private void addMailSender(SendProfile profile, Configuration configuration, String profilePath) {
141 List mailSenders = configuration.getList(profilePath + ".send[@count-per-min]");
142
143 Iterator mailSenderIter = mailSenders.iterator();
144 int mailSenderCount = 0;
145 while (mailSenderIter.hasNext()) {
146 mailSenderIter.next();
147
148 String mailSenderPath = getIndexedPropertyName(profilePath + ".send", mailSenderCount);
149
150 MailSender mailSender = new MailSender(profile);
151 mailSender.setSubject(configuration.getString(getAttributedPropertyName(mailSenderPath, "subject"), "Apache JAMES Postage test mail"));
152 mailSender.setSendPerMinute(configuration.getInt(getAttributedPropertyName(mailSenderPath, "count-per-min")));
153 mailSender.setSizeMinText(configuration.getInt(getAttributedPropertyName(mailSenderPath, "text-size-min"), 0));
154 mailSender.setSizeMaxText(configuration.getInt(getAttributedPropertyName(mailSenderPath, "text-size-max"), 0));
155 mailSender.setSizeMinBinary(configuration.getInt(getAttributedPropertyName(mailSenderPath, "binary-size-min"), 0));
156 mailSender.setSizeMaxBinary(configuration.getInt(getAttributedPropertyName(mailSenderPath, "binary-size-max"), 0));
157 mailSender.setMailFactoryClassname(configuration.getString(getAttributedPropertyName(mailSenderPath, "mail-factory-class"), null));
158
159 profile.addMailSender(mailSender);
160
161 mailSenderCount++;
162 }
163 }
164
165 private boolean convertToInternalExternalFlag(String flagCleartext) {
166 return flagCleartext == null || !"extern".equals(flagCleartext.toLowerCase().trim());
167 }
168
169 private String getIndexedPropertyName(String name, int scenarioCount) {
170 return name + "(" + scenarioCount + ")";
171 }
172
173 private String getAttributedPropertyName(String name, String attr) {
174 return name + "[@" + attr + "]";
175 }
176
177 }