1 package com.googlecode.t7mp;
2
3 import java.util.List;
4
5 import com.google.common.collect.Lists;
6
7 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
8
9
10
11
12
13
14
15
16 public final class InstanceConfigurationUtil {
17
18 private InstanceConfigurationUtil() {
19
20 }
21
22 public static List<InstanceConfiguration> createInstanceConfigurations(DefaultMavenPluginContext mavenPluginContext) {
23 validateInstanceCount(mavenPluginContext.getConfiguration().getInstanceCount());
24 final int configuredHttpPort = mavenPluginContext.getConfiguration().getTomcatHttpPort();
25 final int configuredShutdownPort = mavenPluginContext.getConfiguration().getTomcatShutdownPort();
26 final String configuredCatalinaBasePath = mavenPluginContext.getConfiguration().getCatalinaBase().getAbsolutePath();
27 List<InstanceConfiguration> configurations = Lists.newArrayList();
28 for (int i = 0; i < mavenPluginContext.getConfiguration().getInstanceCount(); i++) {
29 InstanceConfiguration coniguration = new InstanceConfiguration(i, new AtomicInteger(configuredHttpPort).addAndGet(i), new AtomicInteger(
30 configuredShutdownPort).addAndGet(i), configuredCatalinaBasePath + i);
31 configurations.add(coniguration);
32 }
33
34 validatePorts(configurations);
35 return configurations;
36 }
37
38 private static void validatePorts(List<InstanceConfiguration> configurations) {
39 List<Integer> ports = Lists.newArrayList();
40 for (InstanceConfiguration configuration : configurations) {
41 if (ports.contains(new Integer(configuration.getHttpPort()))) {
42 throw new TomcatSetupException("We have conflicting ports ...");
43 } else {
44 ports.add(new Integer(configuration.getHttpPort()));
45 }
46
47 if (ports.contains(new Integer(configuration.getShutdownPort()))) {
48 throw new TomcatSetupException("We have conflicting ports ...");
49 } else {
50 ports.add(new Integer(configuration.getShutdownPort()));
51 }
52 }
53 }
54
55 private static void validateInstanceCount(int instanceCount) {
56 if (instanceCount < 1) {
57 throw new TomcatSetupException("InstanceCount property should not be smaller than 1");
58 }
59 }
60
61 }