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.vcs;
35  
36  import java.util.List;
37  import org.apache.commons.lang.StringUtils;
38  import org.tmatesoft.svn.core.SVNException;
39  import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
40  import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
41  import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
42  import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
43  import org.tmatesoft.svn.core.wc.ISVNOptions;
44  import org.tmatesoft.svn.core.wc.SVNClientManager;
45  import org.tmatesoft.svn.core.wc.SVNWCUtil;
46  
47  import fr.paris.lutece.plugins.deployment.business.CommandResult;
48  import fr.paris.lutece.plugins.deployment.business.vcs.SvnUser;
49  import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
50  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
51  import fr.paris.lutece.plugins.deployment.util.FileUtil;
52  import fr.paris.lutece.plugins.deployment.util.ReleaseSVNCheckoutClient;
53  import fr.paris.lutece.plugins.deployment.util.vcs.SVNUtils;
54  import fr.paris.lutece.portal.service.util.AppException;
55  import fr.paris.lutece.portal.service.util.AppLogService;
56  import fr.paris.lutece.util.ReferenceList;
57  
58  public class SvnService implements IVCSService<SvnUser>
59  {
60      // private static IVCSService _singleton;
61  
62      // public static IVCSService getInstance( )
63      // {
64      // if ( _singleton == null )
65      // {
66      // _singleton = new SvnService( );
67      // }
68      //
69      // return _singleton;
70      // }
71      private SvnService( )
72      {
73          init( );
74      }
75  
76      /*
77       * (non-Javadoc)
78       * 
79       * @see fr.paris.lutece.plugins.deployment.service.IVCSService#init()
80       */
81      public void init( )
82      {
83          /*
84           * For using over http:// and https:/
85           */
86          DAVRepositoryFactory.setup( );
87          /*
88           * For using over svn:// and svn+xxx:/
89           */
90          SVNRepositoryFactoryImpl.setup( );
91  
92          /*
93           * For using over file://
94           */
95          FSRepositoryFactory.setup( );
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see fr.paris.lutece.plugins.deployment.service.IVCSService#getTagsSite(java.lang.String, fr.paris.lutece.plugins.deployment.business.User)
102      */
103     public ReferenceList getTagsSite( String strUrlSite, SvnUser user, String strSiteName )
104     {
105         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( user.getLogin( ), user.getPassword( ) );
106         ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
107         SVNClientManager clientManager = SVNClientManager.newInstance( options, authManager );
108         ReferenceList listReferenceItems = null;
109 
110         try
111         {
112             listReferenceItems = SVNUtils.getSvnDirChildren( strUrlSite + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONSTANTE_TAGS,
113                     clientManager );
114         }
115         catch( Exception e )
116         {
117             AppLogService.error( e );
118         }
119 
120         return listReferenceItems;
121     }
122 
123     public String doCheckoutSite( String strSiteName, String strRepoUrl, SvnUser user, CommandResult commandResult, String strBranch, String strTagToDeploy )
124     {
125         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( user.getLogin( ), user.getPassword( ) );
126         String strSiteLocalBasePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
127 
128         if ( StringUtils.isNotBlank( strSiteName ) && ( user != null ) )
129         {
130             ReleaseSVNCheckoutClient updateClient = new ReleaseSVNCheckoutClient( authManager, SVNWCUtil.createDefaultOptions( false ) );
131 
132             String strError = null;
133 
134             String strSvnCheckoutSiteUrl = null;
135             if ( strTagToDeploy != null )
136             {
137                 strSvnCheckoutSiteUrl = getUrlTagSite( strRepoUrl, strTagToDeploy );
138             }
139             else
140             {
141                 strSvnCheckoutSiteUrl = getUrlDevSite( strRepoUrl );
142             }
143 
144             try
145             {
146                 strError = SVNUtils.doSvnCheckoutSite( strSiteName, strSvnCheckoutSiteUrl, strSiteLocalBasePath, updateClient, commandResult );
147             }
148             catch( Exception e )
149             {
150                 DeploymentUtils.addTechnicalError( commandResult, "errreur lors du checkout du site " + e.getMessage( ), e );
151             }
152         }
153         else
154         {
155             // ErrorCommandThread thread;
156             // if ( StringUtils.isBlank( strSiteName) )
157             // {
158             // thread = new ErrorCommandThread( AppPropertiesService
159             // .getProperty( ConstanteUtils.PROPERTY_MESSAGE_CHECKOUT_ERROR_SITE_EMPTY ), ConstanteUtils.CONSTANTE_CHECKOUT_ERROR );
160             // }
161             // else
162             // {
163             // thread = new ErrorCommandThread( AppPropertiesService
164             // .getProperty(ConstanteUtils.PROPERTY_MESSAGE_CHECKOUT_ERROR_LOGIN_MDP_EMPTY ), ConstanteUtils.CONSTANTE_CHECKOUT_ERROR );
165             // }
166             //
167             // ThreadUtils.launchThread( thread, user );
168             //
169         }
170 
171         return ConstanteUtils.CONSTANTE_EMPTY_STRING;
172     }
173 
174     @Override
175     public ReferenceList getUpgradesFiles( String strSiteName, String strUrlSite, SvnUser user )
176     {
177 
178         CommandResult commandResult = new CommandResult( );
179         DeploymentUtils.startCommandResult( commandResult );
180         ReferenceList upgradeResults = new ReferenceList( );
181         // TODO Auto-generated method stub
182         String strUrlTrunkSite = getUrlDevSite( strUrlSite );
183 
184         doCheckoutSite( strSiteName, strUrlTrunkSite, user, commandResult, null, null );
185 
186         List<String> listUpgradeFiles = FileUtil.list( DeploymentUtils.getPathUpgradeFiles( DeploymentUtils.getPathCheckoutSite( strSiteName ) ), "sql" );
187 
188         for ( String upgradeFile : listUpgradeFiles )
189         {
190             upgradeResults.addItem( upgradeFile, upgradeFile );
191 
192         }
193 
194         DeploymentUtils.stopCommandResult( commandResult );
195 
196         return upgradeResults;
197 
198     }
199 
200     public ReferenceList getSVNDirChildren( String strRepoUrl, SvnUser user )
201     {
202         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( user.getLogin( ), user.getPassword( ) );
203         ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
204         SVNClientManager clientManager = SVNClientManager.newInstance( options, authManager );
205         try
206         {
207             return SVNUtils.getSvnDirChildren( strRepoUrl, clientManager );
208         }
209         catch( SVNException e )
210         {
211             return null;
212         }
213 
214     }
215 
216     @Override
217     public String getArtifactId( String strPathRepo )
218     {
219         String [ ] tabPathSplit = strPathRepo.split( ConstanteUtils.CONSTANTE_SEPARATOR_SLASH );
220         return tabPathSplit [tabPathSplit.length - 1];
221     }
222 
223     public String getUrlTagSite( String strBaseUrl, String strTagName )
224     {
225         return strBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONSTANTE_TAGS + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strTagName;
226     }
227 
228     public String getUrlDevSite( String strBaseUrl )
229     {
230         return strBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONSTANTE_TRUNK;
231     }
232 
233     @Override
234     public boolean isPrivate( )
235     {
236         return true;
237     }
238 
239     @Override
240     public void checkAuthentication( String strRepoUrl, SvnUser user )
241     {
242         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( user.getLogin( ), user.getPassword( ) );
243         if ( !SVNUtils.checkAuthentication( authManager, strRepoUrl ) )
244         {
245             throw new AppException( "Bad credentials to SVN : Unauthorized" );
246         }
247     }
248 }