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  package org.apache.james.postage;
20  
21  import org.apache.commons.configuration.ConfigurationException;
22  import org.apache.commons.configuration.XMLConfiguration;
23  import org.apache.james.postage.configuration.ConfigurationLoader;
24  import org.apache.james.postage.configuration.PostageConfiguration;
25  
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /***
32   * bootstrapping the application
33   */
34  public class Main {
35      private static PostageRunner m_currentPostageRunner = null;
36  
37      public static void main(String[] args) {
38          if (args == null || args.length == 0) {
39              System.out.println("Please provide the configuration file");
40              return;
41          }
42  
43          String filename = args[0];
44  
45          List scenariosToRun = new ArrayList();
46          for (int i = 1; i < args.length; i++)
47          {
48              scenariosToRun.add(args[i]);
49          }
50  
51          // load all scenarios from configuration file
52          ConfigurationLoader configurationLoader = new ConfigurationLoader();
53          Map configurations;
54          try {
55              // TODO allow different (non-xml) configs - as Common-Configuration supports it
56              XMLConfiguration xmlConfiguration = new XMLConfiguration(filename);
57              //xmlConfiguration.setThrowExceptionOnMissing(false);
58              configurations = configurationLoader.create(xmlConfiguration);
59          } catch (ConfigurationException e) {
60              e.printStackTrace();
61              return;
62          }
63  
64          // register shutdown hook if this app is terminated from outside
65          Runtime.getRuntime().addShutdownHook(new Thread(){public void run() {shutdown();}});
66  
67          // run all scenarios
68          runScenarios(configurations, scenariosToRun);
69      }
70  
71      private static void runScenarios(Map configurations, List scenariosToRun) {
72          // run all scenarios which are requested to be run.
73          int playedScenarioCounter = 0;
74          Iterator iterator = configurations.keySet().iterator();
75          while (iterator.hasNext()) {
76              String id = (String)iterator.next();
77              // if no scenario is given on the command line, all get executed
78              // if one or more is given, all others are skipped
79              if (!scenariosToRun.isEmpty() && !scenariosToRun.contains(id)) continue;
80  
81              playedScenarioCounter++;
82              PostageConfiguration postageConfiguration = (PostageConfiguration) configurations.get(id);
83              m_currentPostageRunner = new PostageRunner(postageConfiguration);
84              m_currentPostageRunner.run(); // TODO maybe we want to run this Runnable in its own thread, but maybe not
85          }
86          m_currentPostageRunner = null;
87          if (playedScenarioCounter == 0) {
88              System.out.println("No scenario has been executed. ");
89              System.out.println("Either those on the command line where not matching those in the file.");
90              System.out.println("Or the configuration file is empty");
91          }
92      }
93  
94      private static void shutdown() {
95          if (m_currentPostageRunner != null) m_currentPostageRunner.terminate();
96      }
97  
98  }