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;
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   * Hudson CI mojo.
43   */
44  public abstract class AbstractConfigChoiceHudsonCiMojo extends
45      AbstractHudsonCiMojo
46  {
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    // --- members --------------------------------------------------------------
52  
53    /**
54     * Use this file as is and do not use the ci-config mechanism.
55     *
56     * @parameter expression="${hudson-maven-plugin.jobConfigFile}"
57     * @since 1.0
58     */
59    private String jobConfigFile;
60  
61    /**
62     * Use this name as the job name and do not use the ci-config mechanism.
63     *
64     * @parameter expression="${hudson-maven-plugin.jobName}"
65     * @since 1.0
66     */
67    private String jobName;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    // --- business -------------------------------------------------------------
82  
83    /**
84     * {@inheritDoc}
85     *
86     * @see org.apache.maven.plugin.Mojo#execute()
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       // do nothin
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   // --- object basics --------------------------------------------------------
212 
213 }