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.config.load;
17  
18  import org.jdom.Document;
19  import org.jdom.Element;
20  import org.jdom.JDOMException;
21  
22  import de.smartics.ci.config.hudson.HudsonConfigEntry;
23  import de.smartics.ci.config.hudson.HudsonConfigScmElement;
24  import de.smartics.ci.config.hudson.ScmType;
25  import de.smartics.ci.config.maven.MavenConfig;
26  import de.smartics.ci.config.maven.MavenPom;
27  
28  /**
29   * Applies Maven settings and POM information to a Hudson config XML document.
30   */
31  class MavenInfoApplier
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * The loader configuration to control the loading and creation of Hudson job
41     * configuration files.
42     */
43    private final HudsonJobConfigurationLoaderConfig loaderConfig;
44  
45    /**
46     * The plan to access plan information.
47     */
48    private final LoaderPlan plan;
49  
50    /**
51     * The Maven configuration to provide information for the Hudson
52     * configuration.
53     */
54    private final MavenConfig mavenConfig;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    MavenInfoApplier(final HudsonJobConfigurationLoaderConfig loaderConfig,
61        final LoaderPlan plan)
62    {
63      this.plan = plan;
64      this.loaderConfig = loaderConfig;
65      this.mavenConfig = plan.getMavenConfig();
66    }
67  
68    // ****************************** Inner Classes *****************************
69  
70    // ********************************* Methods ********************************
71  
72    // --- init -----------------------------------------------------------------
73  
74    // --- get&set --------------------------------------------------------------
75  
76    // --- business -------------------------------------------------------------
77  
78    void addAllInfo(final Document hudsonConfigXml) throws JDOMException
79    {
80      addScmInfo(hudsonConfigXml);
81      addProjectInfo(hudsonConfigXml);
82      addProjectDescription(hudsonConfigXml);
83    }
84  
85    void addScmInfo(final Document hudsonConfigXml) throws JDOMException
86    {
87      final HudsonConfigScmElement element =
88          new HudsonConfigScmElement(hudsonConfigXml);
89  
90      final MavenPom pom = mavenConfig.getPom();
91  
92      if (pom.isScmInfoProvided())
93      {
94        final ScmType scmType = pom.getScmType();
95        final String scmUrl = pom.getScmUrl();
96  
97        final HudsonConfigEntry entry = new HudsonConfigEntry(hudsonConfigXml);
98        entry.addScmConfig(scmType, scmUrl);
99  
100       if (loaderConfig.isAddScmElementWithFullContent())
101       {
102         element.addScmConfig(scmType, scmUrl);
103       }
104     }
105   }
106 
107   void addProjectInfo(final Document hudsonConfigXml) throws JDOMException
108   {
109     final Element rootModule = new Element("rootModule");
110     final Element groupId = new Element("groupId");
111     final Element artifactId = new Element("artifactId");
112 
113     final MavenPom pom = mavenConfig.getPom();
114     groupId.addContent(pom.getGroupId());
115     artifactId.addContent(pom.getArtifactId());
116 
117     rootModule.addContent(groupId);
118     rootModule.addContent(artifactId);
119 
120     final Element authToken = new Element("authToken");
121     final String authTokenString = createAuthToken();
122     authToken.addContent(authTokenString);
123 
124     final Element root = hudsonConfigXml.getRootElement();
125     root.addContent(authToken);
126     root.addContent(rootModule);
127   }
128 
129   private String createAuthToken()
130   {
131     final MavenPom pom = mavenConfig.getPom();
132     final String artifactId = pom.getArtifactId();
133     final String planId = plan.getId();
134     final String authToken = artifactId + '-' + planId;
135     return authToken;
136   }
137 
138   void addProjectDescription(final Document hudsonConfigXml)
139     throws JDOMException
140   {
141     final Element description = new Element("description");
142 
143     final MavenPom pom = mavenConfig.getPom();
144     description.addContent(pom.getDescription());
145 
146     hudsonConfigXml.getRootElement().addContent(description);
147   }
148 
149   // --- object basics --------------------------------------------------------
150 
151 }