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