View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.appcenter.modules.fastdeployapplication.util;
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.io.OutputStream;
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  import javax.xml.bind.JAXBContext;
47  import javax.xml.bind.JAXBElement;
48  import javax.xml.bind.JAXBException;
49  import javax.xml.bind.Marshaller;
50  import javax.xml.bind.Unmarshaller;
51  import javax.xml.parsers.ParserConfigurationException;
52  import javax.xml.transform.TransformerException;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.tmatesoft.svn.core.SVNException;
56  import org.tmatesoft.svn.core.wc.SVNCommitClient;
57  import org.tmatesoft.svn.core.wc.SVNCommitPacket;
58  import org.xml.sax.SAXException;
59  
60  import fr.paris.lutece.plugins.deployment.business.jaxb.maven.Model;
61  import fr.paris.lutece.plugins.deployment.business.jaxb.maven.ObjectFactory;
62  
63  public final class ReleaseUtils
64  {
65      public static final String CONSTANTE_EMPTY_STRING = "";
66      public static final String CONSTANTE_SNAPSHOT = "-SNAPSHOT";
67      public static final String CONSTANT_CORE = "core";
68      public static final String CONSTANT_CORE_PREFIX = "lutece-";
69      public static final String CONSTANT_POM_PARENT = "lutece-parent-pom";
70      public static final String CONSTANT_POM_PLUGIN = "lutece-plugins-pom";
71  
72      /**
73       * Safe split (no empty value)
74       * 
75       * @param strToSplit
76       * @param strSeparator
77       * @return
78       */
79      public static List<String> split( String strToSplit, String strSeparator )
80      {
81          List<String> listSplit = new ArrayList<String>( );
82          String [ ] splitted = strToSplit.split( strSeparator );
83  
84          if ( splitted != null )
85          {
86              for ( String strValue : splitted )
87              {
88                  if ( !StringUtils.isBlank( strValue ) )
89                  {
90                      // add to list
91                      listSplit.add( strValue );
92                  }
93              }
94          }
95  
96          return listSplit;
97      }
98  
99      public static <T> T unmarshal( Class<T> docClass, InputStream inputStream ) throws JAXBException
100     {
101         String packageName = docClass.getPackage( ).getName( );
102         JAXBContext jc = JAXBContext.newInstance( packageName );
103         Unmarshaller u = jc.createUnmarshaller( );
104         JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputStream );
105 
106         return doc.getValue( );
107     }
108 
109     public static void save( Model model, OutputStream outputStream ) throws JAXBException
110     {
111         String packageName = model.getClass( ).getPackage( ).getName( );
112         ObjectFactory factory = new ObjectFactory( );
113         JAXBElement<Model> element = factory.createProject( model );
114 
115         JAXBContext jc = JAXBContext.newInstance( packageName );
116         Marshaller m = jc.createMarshaller( );
117         m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
118         m.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, "http://maven.apache.org/maven-v4_0_0.xsd" );
119         m.marshal( element, outputStream );
120     }
121 
122     public static String getNextVersion( String strVersion )
123     {
124         String strNextVersion = CONSTANTE_EMPTY_STRING;
125 
126         if ( strVersion.contains( CONSTANTE_SNAPSHOT ) )
127         {
128             strVersion = strVersion.substring( 0, strVersion.indexOf( CONSTANTE_SNAPSHOT ) );
129         }
130 
131         int nLastIndex = strVersion.lastIndexOf( "." );
132 
133         try
134         {
135             int nMinorVersion = Integer.parseInt( strVersion.substring( nLastIndex + 1, strVersion.length( ) ) );
136             nMinorVersion++;
137 
138             strNextVersion += strVersion.substring( 0, nLastIndex + 1 );
139             strNextVersion += nMinorVersion;
140             strNextVersion += CONSTANTE_SNAPSHOT;
141         }
142         catch( NumberFormatException nfe )
143         {
144             nfe.printStackTrace( );
145         }
146 
147         return strNextVersion;
148     }
149 
150     /**
151      * Retourne la version [stable] courante (sans -SNAPSHOT si present dans le pom)
152      * 
153      * @return
154      * @throws JAXBException
155      * @throws FileNotFoundException
156      */
157     public static String getReleaseVersion( String strPathLocalSiteName ) throws JAXBException, FileNotFoundException
158     {
159         InputStream is = new FileInputStream( DeploymentUtils.getPathPomFile( strPathLocalSiteName ) );
160 
161         Model model = unmarshal( Model.class, is );
162 
163         String strVersion = model.getVersion( );
164 
165         if ( strVersion.contains( "-SNAPSHOT" ) )
166         {
167             strVersion = strVersion.substring( 0, strVersion.indexOf( "-SNAPSHOT" ) );
168         }
169 
170         return strVersion;
171     }
172 
173     /**
174      * Retourne la version dans le pom)
175      * 
176      * @return
177      * @throws JAXBException
178      * @throws FileNotFoundException
179      */
180     public static String getSiteVersion( String strPathLocalSiteName ) throws JAXBException, FileNotFoundException
181     {
182         InputStream is = new FileInputStream( DeploymentUtils.getPathPomFile( strPathLocalSiteName ) );
183 
184         Model model = unmarshal( Model.class, is );
185 
186         String strVersion = model.getVersion( );
187         return strVersion;
188     }
189 
190     /**
191      * Retourne la version dans le pom)
192      * 
193      * @return
194      * @throws JAXBException
195      * @throws FileNotFoundException
196      */
197     public static String getSiteArtifactId( String strPathLocalSiteName ) throws JAXBException, FileNotFoundException
198     {
199         InputStream is = new FileInputStream( DeploymentUtils.getPathPomFile( strPathLocalSiteName ) );
200 
201         Model model = unmarshal( Model.class, is );
202 
203         String strVersion = model.getArtifactId( );
204         return strVersion;
205     }
206 
207     public static String updateReleaseVersion( String strPathLocalSiteName, String strNewVersion, String commitMessage, SVNCommitClient svnCommitClient )
208             throws ParserConfigurationException, SAXException, IOException, TransformerException, SVNException, JAXBException
209     {
210         String strPomFile = DeploymentUtils.getPathPomFile( strPathLocalSiteName );
211         InputStream inputStream = null;
212         OutputStream outputStream = null;
213 
214         try
215         {
216             inputStream = new FileInputStream( strPomFile );
217 
218             Model model = unmarshal( Model.class, inputStream );
219 
220             if ( !model.getVersion( ).equals( strNewVersion ) )
221             {
222                 model.setVersion( strNewVersion );
223 
224                 outputStream = new FileOutputStream( strPomFile );
225 
226                 save( model, outputStream );
227 
228                 // COMMIT
229                 /*
230                  * paths - an array of local items which should be traversed to collect information on every changed item (one SVNCommitItem per each modified
231                  * local item) keepLocks - if true and there are local items that were locked then these items will be left locked after traversing all of them,
232                  * otherwise the items will be unlocked force - forces collecting commit items for a non-recursive commit recursive - relevant only for
233                  * directory items: if true then the entire directory tree will be traversed including all child directories, otherwise only items located in
234                  * the directory itself will be processed
235                  */
236                 SVNCommitPacket commitPacket = svnCommitClient.doCollectCommitItems( new File [ ] {
237                         new File( strPomFile )
238                 }, false, false, false );
239 
240                 if ( !SVNCommitPacket.EMPTY.equals( commitPacket ) )
241                 {
242                     svnCommitClient.doCommit( commitPacket, false, commitMessage );
243                 }
244             }
245             else
246             {
247                 return "Pom already up to date\n";
248             }
249         }
250         finally
251         {
252             if ( outputStream != null )
253             {
254                 try
255                 {
256                     outputStream.close( );
257                 }
258                 catch( IOException ex )
259                 {
260                     // nothing...
261                     ex.printStackTrace( );
262                 }
263             }
264         }
265 
266         return "";
267     }
268 }