View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.service;
35  
36  import java.io.File;
37  import java.io.FileNotFoundException;
38  import java.io.IOException;
39  import java.io.PrintWriter;
40  import java.io.StringWriter;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  import javax.xml.bind.JAXBException;
45  
46  import org.apache.maven.shared.invoker.DefaultInvocationRequest;
47  import org.apache.maven.shared.invoker.DefaultInvoker;
48  import org.apache.maven.shared.invoker.InvocationOutputHandler;
49  import org.apache.maven.shared.invoker.InvocationRequest;
50  import org.apache.maven.shared.invoker.InvocationResult;
51  import org.apache.maven.shared.invoker.Invoker;
52  
53  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
54  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.MavenGoals;
55  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.vcs.AbstractVCSUser;
56  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
57  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
58  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ReleaseUtils;
59  import fr.paris.lutece.portal.service.datastore.DatastoreService;
60  import fr.paris.lutece.portal.service.spring.SpringContextService;
61  import fr.paris.lutece.portal.service.util.AppLogService;
62  import fr.paris.lutece.portal.service.util.AppPropertiesService;
63  
64  /**
65   *
66   * MavenService : provides maven command launcher
67   *
68   */
69  public class MavenService implements IMavenService
70  {
71      // private static IMavenService _singleton;
72      private Invoker _invoker;
73      private static IMavenService _singleton;
74  
75      private MavenService( )
76      {
77  
78      }
79  
80      public static IMavenService getService( )
81      {
82  
83          if ( _singleton == null )
84          {
85  
86              _singleton = SpringContextService.getBean( "appcenter-fastdeployapplication.MavenService" );
87  
88              _singleton.init( );
89          }
90          return _singleton;
91      }
92  
93      public void init( )
94      {
95          _invoker = new DefaultInvoker( );
96          _invoker.setMavenHome( new File( AppPropertiesService.getProperty( ConstanteUtils.CONSTANTE_MAVEN_HOME_PATH ) ) );
97          _invoker.setLocalRepositoryDirectory( new File( AppPropertiesService.getProperty( ConstanteUtils.CONSTANTE_MAVEN_LOCAL_REPOSITORY ) ) );
98      }
99  
100     /*
101      * (non-Javadoc)
102      * 
103      * @see fr.paris.lutece.plugins.deployment.service.IMavenService#mvnSiteAssembly(java.lang.String, fr.paris.lutece.plugins.deployment.business.Environment,
104      * fr.paris.lutece.plugins.deployment.business.MavenUser)
105      */
106     public void mvnSiteAssembly( String strSiteName, String strMavenProfile, AbstractVCSUser user, CommandResult commandResult )
107     {
108         String strSiteLocalBasePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
109 
110         List<String> listGoals = MavenGoals.LUTECE_SITE_ASSEMBLY.asList( );
111         List<String> listGoalsProfile = new ArrayList<String>( );
112         listGoalsProfile.addAll( listGoals );
113         listGoalsProfile.add( "-P " + strMavenProfile );
114         listGoalsProfile.add( "-U" );
115         mvn( strSiteLocalBasePath, listGoalsProfile, commandResult );
116     }
117 
118     public void saveMvnProfilName( String strProfilValue, String strIdApplication, String strCodeEnvironment, String strCodeServerApplicationInstance )
119     {
120 
121         DatastoreService.setDataValue( strIdApplication + "_" + strCodeEnvironment + "_" + strCodeServerApplicationInstance, strProfilValue );
122     }
123 
124     public String getMvnProfilSaved( String strIdApplication, String strCodeEnvironment, String strCodeServerApplicationInstance )
125     {
126 
127         return DatastoreService.getDataValue( strIdApplication + "_" + strCodeEnvironment + "_" + strCodeServerApplicationInstance, null );
128     }
129 
130     public String getSiteWarName( String strSiteName )
131     {
132         String strWarGeneratedName = null;
133         try
134         {
135             String strSiteArtifactId = ReleaseUtils.getSiteArtifactId( DeploymentUtils.getPathCheckoutSite( strSiteName ) );
136             String strSiteVersion = ReleaseUtils.getSiteVersion( DeploymentUtils.getPathCheckoutSite( strSiteName ) );
137             strWarGeneratedName = strSiteArtifactId + "-" + strSiteVersion;
138         }
139         catch( FileNotFoundException e )
140         {
141             AppLogService.error( e );
142         }
143         catch( JAXBException e )
144         {
145 
146             AppLogService.error( e );
147         }
148         return strWarGeneratedName;
149     }
150 
151     /**
152      * Transforme la liste en chaine, pour passer l'argument � la ligne de commande
153      * 
154      * @param goals
155      * @return
156      */
157     private String getGoalToString( List<String> goals )
158     {
159         StringBuilder sbGoal = new StringBuilder( );
160 
161         for ( String strGoal : goals )
162         {
163             sbGoal.append( strGoal ).append( ConstanteUtils.CONSTANTE_SPACE );
164         }
165 
166         return sbGoal.toString( );
167     }
168 
169     // public static IMavenService getInstance()
170     // {
171     //
172     // if(_singleton ==null)
173     // {
174     // _singleton=new MavenService();
175     // }
176     //
177     // return _singleton;
178     //
179     // }
180     //
181 
182     /**
183      * Launches mvn cmd
184      * 
185      * @param strPluginName
186      *            plugin name (ex: plugin-ods)
187      * @param goals
188      *            maven goals
189      * @param strSVNBinPath
190      *            svn bin path (ex: /home/svn/apps/subversion/bin)
191      */
192     @SuppressWarnings( "unchecked" )
193     private String mvn( String strSitePath, List<String> goals, CommandResult commandResult )
194     {
195         InvocationRequest request = new DefaultInvocationRequest( );
196         request.setPomFile( new File( DeploymentUtils.getPathPomFile( strSitePath ) ) );
197         request.setGoals( goals );
198 
199         try
200         {
201             final StringBuffer sbLog = commandResult.getLog( );
202 
203             // logger
204             _invoker.setOutputHandler( new InvocationOutputHandler( )
205             {
206                 public void consumeLine( String strLine )
207                 {
208                     sbLog.append( strLine + "\n" );
209                 }
210             } );
211 
212             InvocationResult invocationResult = _invoker.execute( request );
213 
214             int nStatus = invocationResult.getExitCode( );
215 
216             if ( nStatus != 0 )
217             {
218 
219                 DeploymentUtils.addTechnicalError( commandResult, commandResult.getLog( ).toString( ) );
220 
221             }
222             else
223             {
224                 commandResult.setStatus( CommandResult.STATUS_OK );
225             }
226 
227         }
228         catch( Exception e )
229         {
230             StringWriter sw = new StringWriter( );
231             PrintWriter pw = new PrintWriter( sw );
232             e.printStackTrace( pw );
233 
234             String errorLog = sw.toString( );
235             pw.flush( );
236             pw.close( );
237 
238             try
239             {
240                 sw.flush( );
241                 sw.close( );
242             }
243             catch( IOException e1 )
244             {
245                 // do nothing
246                 AppLogService.error( e1 );
247             }
248 
249             commandResult.getLog( ).append( errorLog );
250             // _result.setIdError( ReleaseLogger.logError( _result.getLog( ).toString( ), e ) );
251             DeploymentUtils.addTechnicalError( commandResult, errorLog );
252 
253         }
254 
255         // _endTime = new Date( );
256         return null;
257     }
258     
259     /**
260      * Run a custom maven cmd
261      * @param strSiteName the repo
262      * @param strMavenProfile
263      * @param strCustomCommand
264      * @param commandResult 
265      */
266     @Override
267     public void runCustomMavenCommand( String strSiteName, String strMavenProfile, String strCustomCommand, CommandResult commandResult )
268     {
269         String strSiteLocalBasePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
270         
271         //If the custom mvn command begin by "mvn "; we remove this. 
272         if ( strCustomCommand.startsWith( ConstanteUtils.CONSTANT_MAVEN_BASE_CMD ) )
273         {
274             strCustomCommand = strCustomCommand.replaceFirst( ConstanteUtils.CONSTANT_MAVEN_BASE_CMD, "");
275         }
276         
277         List<String> listGoals = new ArrayList<>();
278         listGoals.add( strCustomCommand );
279         
280         //Run the cmd 
281         mvn( strSiteLocalBasePath,listGoals, commandResult );
282     }
283     
284 }