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