1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.maven;
17
18 import static de.smartics.ci.maven.HudsonUtils.determineJobName;
19
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.List;
24
25 import org.apache.commons.io.IOUtils;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.jdom.JDOMException;
29 import org.xml.sax.InputSource;
30
31 import de.smartics.ci.comm.CiController;
32 import de.smartics.ci.comm.LogLevel;
33 import de.smartics.ci.config.load.HudsonJobConfig;
34 import de.smartics.ci.config.load.HudsonJobConfigLoader;
35 import de.smartics.ci.config.load.HudsonJobConfigurationLoaderConfig;
36 import de.smartics.ci.config.load.LoaderPlan;
37 import de.smartics.ci.config.load.LoaderPlanLoader;
38 import de.smartics.ci.config.load.LocationManager;
39 import de.smartics.ci.config.maven.MavenConfig;
40
41
42
43
44 public abstract class AbstractConfigChoiceHudsonCiMojo extends
45 AbstractHudsonCiMojo
46 {
47
48
49
50
51
52
53
54
55
56
57
58
59 private String jobConfigFile;
60
61
62
63
64
65
66
67 private String jobName;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public final void execute() throws MojoExecutionException,
89 MojoFailureException
90 {
91 checkPreconditions();
92 if (HudsonUtils.checkForManualConfiguration(this.jobName,
93 this.jobConfigFile))
94 {
95 executeManualConfiguration();
96 }
97 else
98 {
99 executeConfigurationUsingCiConfigFile();
100 }
101 }
102
103 private void executeConfigurationUsingCiConfigFile()
104 throws MojoExecutionException
105 {
106 final HudsonJobConfigLoader configLoader = createJobConfigLoader();
107 final MavenConfig mavenConfig = createMavenConfig();
108 final LoaderPlanLoader loader =
109 HudsonUtils.createLoaderPlanLoader(mavenConfig);
110
111 final InputSource ciConfigurationInputSource = createInputSource();
112
113 final List<LoaderPlan> plans = loader.load(ciConfigurationInputSource);
114 for (final LoaderPlan plan : plans)
115 {
116 try
117 {
118 final String configString = readConfig(configLoader, plan);
119 final CiController controller = loginToCiServer(new LogLevel(verbose));
120 final String jobName = determineJobName(plan);
121 final String formattedConfigString =
122 HudsonUtils.prettyFormat(configString, 2);
123 executeCommand(jobName, formattedConfigString, controller);
124 }
125 catch (final Exception e)
126 {
127 logError("Terminated handling jobs. Rollback not possile. Please check status manually at: "
128 + getCiServer().getUrl());
129 throw new MojoExecutionException("Cannot load plan: " + plan, e);
130 }
131 logInfo("All Jobs successfully handled.");
132 }
133 }
134
135 private void executeManualConfiguration() throws MojoExecutionException
136 {
137 final LogLevel logLevel = new LogLevel(verbose);
138 try
139 {
140 if (HudsonUtils.checkIfHudsonJobConfigFileIsSpecified(this.jobConfigFile))
141 {
142 final String configString = readPlainUnparsedConfig();
143 final CiController controller = loginToCiServer(logLevel);
144 executeCommand(jobName, configString, controller);
145 }
146 else
147 {
148 final CiController controller = loginToCiServer(logLevel);
149 executeCommand(jobName, null, controller);
150 }
151 }
152 catch (final Exception e)
153 {
154 logError("Terminated handling jobs. Rollback not possile. Please check status manually at: "
155 + getCiServer().getUrl());
156 throw new MojoExecutionException("Cannot handle jobName " + jobName, e);
157 }
158 }
159
160 private HudsonJobConfigLoader createJobConfigLoader()
161 {
162 final HudsonJobConfigurationLoaderConfig loaderConfig =
163 createLoaderConfig();
164 final LocationManager locationManager = createLocationManager();
165 final HudsonJobConfigLoader configLoader =
166 new HudsonJobConfigLoader(locationManager, loaderConfig);
167 return configLoader;
168 }
169
170 private String readConfig(final HudsonJobConfigLoader configLoader,
171 final LoaderPlan plan) throws JDOMException, MojoExecutionException
172 {
173 final HudsonJobConfig hudsonConfig = configLoader.loadHudsonConfig(plan);
174 final String configString = hudsonConfig.getConfigXml();
175 writeHudsonConfigXml(hudsonConfig);
176 return configString;
177 }
178
179 private String readPlainUnparsedConfig() throws FileNotFoundException
180 {
181 final LocationManager locationManager = createLocationManager();
182 final InputSource source = locationManager.open(this.jobConfigFile);
183 final InputStream is = source.getByteStream();
184
185 String hudsonConfig = null;
186 try
187 {
188 hudsonConfig = IOUtils.toString(is);
189 }
190 catch (final IOException e)
191 {
192
193 }
194 return hudsonConfig;
195 }
196
197 private HudsonJobConfigurationLoaderConfig createLoaderConfig()
198 {
199 final HudsonJobConfigurationLoaderConfig.Builder builder =
200 new HudsonJobConfigurationLoaderConfig.Builder();
201 builder
202 .withAddScmElementWithFullContent(addScmElementWithFullContent)
203 .withRemoveCascadingChildrenNamesElement(
204 removeCascadingChildrenNamesElement)
205 .withRemoveCascadingJobPropertiesElement(
206 removeCascadingJobPropertiesElement);
207 final HudsonJobConfigurationLoaderConfig config = builder.build();
208 return config;
209 }
210
211
212
213 }