1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
63
64
65
66
67
68
69
70
71 private SvnService( )
72 {
73 init( );
74 }
75
76
77
78
79
80
81 public void init( )
82 {
83
84
85
86 DAVRepositoryFactory.setup( );
87
88
89
90 SVNRepositoryFactoryImpl.setup( );
91
92
93
94
95 FSRepositoryFactory.setup( );
96 }
97
98
99
100
101
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
156
157
158
159
160
161
162
163
164
165
166
167
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
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 }