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