View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.releaser.util.pom;
35  
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.OutputStream;
42  import java.util.Iterator;
43  
44  import javax.xml.bind.JAXBContext;
45  import javax.xml.bind.JAXBElement;
46  import javax.xml.bind.JAXBException;
47  import javax.xml.bind.Marshaller;
48  import javax.xml.bind.Unmarshaller;
49  
50  import fr.paris.lutece.plugins.releaser.business.Component;
51  import fr.paris.lutece.plugins.releaser.business.Site;
52  import fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model;
53  import fr.paris.lutece.plugins.releaser.business.jaxb.maven.ObjectFactory;
54  import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
55  import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
56  import fr.paris.lutece.portal.service.i18n.I18nService;
57  import fr.paris.lutece.portal.service.util.AppLogService;
58  import fr.paris.lutece.portal.service.util.AppPropertiesService;
59  
60  // TODO: Auto-generated Javadoc
61  /**
62   * The Class PomUpdater.
63   */
64  public class PomUpdater
65  {
66  
67      /**
68       * Update site before tag.
69       *
70       * @param site
71       *            the site
72       * @param strSiteLocalPomPath
73       *            the str site local pom path
74       * @return the string
75       * @throws JAXBException
76       *             the JAXB exception
77       */
78      public static String updateSiteBeforeTag( Site site, String strSiteLocalPomPath ) throws JAXBException
79      {
80  
81          InputStream inputStream = null;
82          OutputStream outputStream = null;
83  
84          try
85          {
86  
87              inputStream = new FileInputStream( strSiteLocalPomPath );
88              Model model = unmarshal( Model.class, inputStream );
89  
90              model.setDescription( site.getTagInformation( ) );
91  
92              fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( );
93              // update dependencies
94              if ( dependencies != null )
95              {
96                  for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) )
97                  {
98                      for ( Component component : site.getComponents( ) )
99                      {
100 
101                         if ( jaxDependency.getArtifactId( ).equals( component.getArtifactId( ) ) )
102                         {
103 
104                             jaxDependency.setVersion( "[" + component.getTargetVersion( ) + "]" );
105                         }
106                     }
107 
108                 }
109             }
110 
111             String strParentSiteVersion = model.getParent( ).getVersion( );
112             String strPomParentReferenceVersion = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_SITE_VERSION );
113 
114             if ( ReleaserUtils.compareVersion( strParentSiteVersion, strPomParentReferenceVersion ) < 0 )
115             {
116                 // update pom parent version
117                 String strPomParentArtifactId = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_ARTIFCAT_ID );
118                 String strPomParentGroupId = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_GROUP_ID );
119                 model.getParent( ).setArtifactId( strPomParentArtifactId );
120                 model.getParent( ).setGroupId( strPomParentGroupId );
121                 model.getParent( ).setVersion( strPomParentReferenceVersion );
122             }
123 
124             outputStream = new FileOutputStream( strSiteLocalPomPath );
125 
126             save( model, outputStream );
127 
128         }
129         catch( FileNotFoundException e )
130         {
131             AppLogService.error( e );
132         }
133         finally
134         {
135             if ( outputStream != null )
136             {
137                 try
138                 {
139                     outputStream.close( );
140                 }
141                 catch( IOException ex )
142                 {
143                     // nothing...
144                     AppLogService.error( ex );
145                 }
146             }
147         }
148 
149         return "";
150     }
151 
152     /**
153      * Update site after tag.
154      *
155      * @param site
156      *            the site
157      * @param strSiteLocalPomPath
158      *            the str site local pom path
159      * @return the string
160      * @throws JAXBException
161      *             the JAXB exception
162      */
163     public static String updateSiteAfterTag( Site site, String strSiteLocalPomPath ) throws JAXBException
164     {
165 
166         InputStream inputStream = null;
167         OutputStream outputStream = null;
168 
169         try
170         {
171 
172             inputStream = new FileInputStream( strSiteLocalPomPath );
173             Model model = unmarshal( Model.class, inputStream );
174 
175             model.setVersion( site.getNextSnapshotVersion( ) );
176             model.setDescription( "" );
177 
178             fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( );
179             // update dependencies
180             if ( dependencies != null )
181             {
182                 for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) )
183                 {
184                     for ( Component component : site.getComponents( ) )
185                     {
186 
187                         if ( jaxDependency.getArtifactId( ).equals( component.getArtifactId( ) ) )
188                         {
189 
190                             if ( component.isProject( ) && component.isSnapshotVersion( ) )
191                             {
192                                 jaxDependency.setVersion( component.getNextSnapshotVersion( ) );
193                             }
194 
195                         }
196                     }
197 
198                 }
199             }
200 
201             outputStream = new FileOutputStream( strSiteLocalPomPath );
202 
203             save( model, outputStream );
204 
205         }
206         catch( FileNotFoundException e )
207         {
208             AppLogService.error( e );
209         }
210         finally
211         {
212             if ( outputStream != null )
213             {
214                 try
215                 {
216                     outputStream.close( );
217                 }
218                 catch( IOException ex )
219                 {
220                     // nothing...
221                     AppLogService.error( ex );
222                 }
223             }
224         }
225 
226         return "";
227     }
228 
229     /**
230      * Save.
231      *
232      * @param model
233      *            the model
234      * @param outputStream
235      *            the output stream
236      * @throws JAXBException
237      *             the JAXB exception
238      */
239     public static void save( Model model, OutputStream outputStream ) throws JAXBException
240     {
241         String packageName = model.getClass( ).getPackage( ).getName( );
242         ObjectFactory factory = new ObjectFactory( );
243         JAXBElement<Model> element = factory.createProject( model );
244 
245         JAXBContext jc = JAXBContext.newInstance( packageName );
246         Marshaller m = jc.createMarshaller( );
247         m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
248         m.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, "http://maven.apache.org/maven-v4_0_0.xsd" );
249         m.marshal( element, outputStream );
250     }
251 
252     /**
253      * Unmarshal.
254      *
255      * @param <T>
256      *            the generic type
257      * @param docClass
258      *            the doc class
259      * @param inputStream
260      *            the input stream
261      * @return the t
262      * @throws JAXBException
263      *             the JAXB exception
264      */
265     public static <T> T unmarshal( Class<T> docClass, InputStream inputStream ) throws JAXBException
266     {
267         String packageName = docClass.getPackage( ).getName( );
268         JAXBContext jc = JAXBContext.newInstance( packageName );
269         Unmarshaller u = jc.createUnmarshaller( );
270         JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputStream );
271         return doc.getValue( );
272     }
273 
274 }