View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Hudson CI mojo.
42   */
43  public abstract class AbstractConfigChoiceHudsonCiMojo extends
44      AbstractHudsonCiMojo
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    // --- members --------------------------------------------------------------
51  
52    /**
53     * Use this file as is and do not use the ci-config mechanism.
54     *
55     * @parameter expression="${hudson-maven-plugin.jobConfigFile}"
56     * @since 1.0
57     */
58    private String jobConfigFile;
59  
60    /**
61     * Use this name as the job name and do not use the ci-config mechanism.
62     *
63     * @parameter expression="${hudson-maven-plugin.jobName}"
64     * @since 1.0
65     */
66    private String jobName;
67  
68    // ****************************** Initializer *******************************
69  
70    // ****************************** Constructors ******************************
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    // --- get&set --------------------------------------------------------------
79  
80    // --- business -------------------------------------------------------------
81  
82    /**
83     * {@inheritDoc}
84     *
85     * @see org.apache.maven.plugin.Mojo#execute()
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       // do nothin
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   // --- object basics --------------------------------------------------------
211 
212 }