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.util.vcs;
35
36 import fr.paris.lutece.plugins.deployment.business.CommandResult;
37 import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
38 import fr.paris.lutece.plugins.deployment.util.FileUtil;
39 import fr.paris.lutece.plugins.deployment.util.ReleaseSVNCheckoutClient;
40 import fr.paris.lutece.util.ReferenceItem;
41 import fr.paris.lutece.util.ReferenceList;
42
43 import org.tmatesoft.svn.core.ISVNDirEntryHandler;
44 import org.tmatesoft.svn.core.SVNAuthenticationException;
45 import org.tmatesoft.svn.core.SVNCancelException;
46 import org.tmatesoft.svn.core.SVNDirEntry;
47 import org.tmatesoft.svn.core.SVNException;
48 import org.tmatesoft.svn.core.SVNNodeKind;
49 import org.tmatesoft.svn.core.SVNURL;
50 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
51 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
52 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
53 import org.tmatesoft.svn.core.io.SVNRepository;
54 import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
55 import org.tmatesoft.svn.core.wc.ISVNEventHandler;
56 import org.tmatesoft.svn.core.wc.SVNClientManager;
57 import org.tmatesoft.svn.core.wc.SVNEvent;
58 import org.tmatesoft.svn.core.wc.SVNEventAction;
59 import org.tmatesoft.svn.core.wc.SVNRevision;
60
61 import java.io.File;
62 import java.io.IOException;
63 import java.io.PrintWriter;
64 import java.io.StringWriter;
65
66 import java.util.ArrayList;
67 import java.util.Collections;
68 import java.util.Comparator;
69 import java.util.List;
70 import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
71
72 public final class SVNUtils
73 {
74 private static final Comparator<SVNDirEntry> _compareSvnEntries = new Comparator<SVNDirEntry>( )
75 {
76 @Override
77 public int compare( SVNDirEntry o1, SVNDirEntry o2 )
78 {
79 return -1 * o1.getDate( ).compareTo( o2.getDate( ) );
80 }
81 };
82
83
84
85
86 private SVNUtils( )
87 {
88
89 }
90
91
92
93
94 public static void init( )
95 {
96
97
98
99 DAVRepositoryFactory.setup( );
100
101
102
103 SVNRepositoryFactoryImpl.setup( );
104
105
106
107
108 FSRepositoryFactory.setup( );
109
110 }
111
112 public static String doSvnCheckoutSite( String strSiteName, String strUrl, String strCheckoutBaseSitePath, ReleaseSVNCheckoutClient updateClient,
113 CommandResult result ) throws SVNException
114 {
115 SVNURL url = SVNURL.parseURIEncoded( strUrl );
116 File file = new File( strCheckoutBaseSitePath );
117
118 if ( file.exists( ) )
119 {
120 if ( !FileUtil.delete( file, result.getLog( ) ) )
121 {
122 result.setError( result.getLog( ).toString( ) );
123
124 return result.getLog( ).toString( );
125 }
126 }
127
128 SVNRepository repository = SVNRepositoryFactory.create( url, null );
129 final StringBuffer logBuffer = result.getLog( );
130
131 try
132 {
133 updateClient.setEventHandler( new ISVNEventHandler( )
134 {
135 public void checkCancelled( ) throws SVNCancelException
136 {
137
138 }
139
140 public void handleEvent( SVNEvent event, double progress ) throws SVNException
141 {
142 logBuffer.append( ( ( event.getAction( ) == SVNEventAction.UPDATE_ADD ) ? "ADDED " : event.getAction( ) ) + " " + event.getFile( ) + "\n" );
143 }
144 } );
145
146
147 updateClient.doCheckout( repository.getLocation( ), file, SVNRevision.HEAD, SVNRevision.HEAD, true );
148 }
149 catch( SVNAuthenticationException e )
150 {
151
152
153
154
155 DeploymentUtils.addTechnicalError( result, "Une erreur est survenue lors de la tentative d'authentification avec le svn" + e, e );
156
157 StringWriter sw = new StringWriter( );
158 PrintWriter pw = new PrintWriter( sw );
159 e.printStackTrace( pw );
160
161 String errorLog = sw.toString( );
162 pw.flush( );
163 pw.close( );
164
165 try
166 {
167 sw.flush( );
168 sw.close( );
169 }
170 catch( IOException e1 )
171 {
172
173
174 }
175
176
177
178
179
180 }
181 catch( Exception e )
182 {
183
184
185 StringWriter sw = new StringWriter( );
186 PrintWriter pw = new PrintWriter( sw );
187 e.printStackTrace( pw );
188
189 String errorLog = sw.toString( );
190 pw.flush( );
191 pw.close( );
192
193 try
194 {
195 sw.flush( );
196 sw.close( );
197 }
198 catch( IOException e1 )
199 {
200
201
202 }
203
204 DeploymentUtils.addTechnicalError( result, "Une erreur svn est survenue:" + e, e );
205
206 }
207
208 return null;
209 }
210
211 public static ReferenceList getSvnDirChildren( String strUrlSite, SVNClientManager clientManager ) throws SVNException
212 {
213 final ReferenceList listSites = new ReferenceList( );
214 final SVNURL url;
215
216 final List<SVNDirEntry> listSvnEntries = new ArrayList<SVNDirEntry>( );
217 url = SVNURL.parseURIEncoded( strUrlSite );
218
219 SVNRepository repository = SVNRepositoryFactory.create( url, null );
220
221 clientManager.getLogClient( ).doList( repository.getLocation( ), SVNRevision.HEAD, SVNRevision.HEAD, false, false, new ISVNDirEntryHandler( )
222 {
223 public void handleDirEntry( SVNDirEntry entry ) throws SVNException
224 {
225 ReferenceItem referenceItem;
226
227 if ( !url.equals( entry.getURL( ) ) )
228 {
229 if ( entry.getKind( ) == SVNNodeKind.DIR )
230 {
231 listSvnEntries.add( entry );
232 }
233 }
234 }
235 } );
236
237 Collections.sort( listSvnEntries, _compareSvnEntries );
238
239 for ( SVNDirEntry svnEntry : listSvnEntries )
240 {
241 listSites.addItem( svnEntry.getName( ), svnEntry.getName( ) );
242 }
243
244 return listSites;
245 }
246
247 public static boolean checkAuthentication( ISVNAuthenticationManager authManager, String strUrl )
248 {
249 try
250 {
251 SVNURL url = SVNURL.parseURIEncoded( strUrl );
252 SVNRepository repository = SVNRepositoryFactory.create( url, null );
253 repository.setAuthenticationManager( authManager );
254 repository.testConnection( );
255 }
256 catch( SVNException e )
257 {
258 return false;
259 }
260 return true;
261
262 }
263 }