1 package org.apache.continuum.scm;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.scm.ScmException;
25 import org.apache.maven.scm.ScmFileSet;
26 import org.apache.maven.scm.ScmTag;
27 import org.apache.maven.scm.ScmVersion;
28 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
29 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
30 import org.apache.maven.scm.command.update.UpdateScmResult;
31 import org.apache.maven.scm.manager.NoSuchScmProviderException;
32 import org.apache.maven.scm.manager.ScmManager;
33 import org.apache.maven.scm.repository.ScmRepository;
34 import org.apache.maven.scm.repository.ScmRepositoryException;
35 import org.springframework.stereotype.Service;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.Calendar;
40 import java.util.Date;
41 import javax.annotation.Resource;
42
43
44
45
46
47
48 @Service( "continuumScm" )
49 public class DefaultContinuumScm
50 implements ContinuumScm
51 {
52
53
54
55 @Resource
56 private ScmManager scmManager;
57
58 public CheckOutScmResult checkout( ContinuumScmConfiguration configuration )
59 throws IOException, ScmException
60 {
61 ScmVersion scmVersion = getScmVersion( configuration );
62
63
64 File workingDirectory = configuration.getWorkingDirectory();
65
66 ScmRepository repository = getScmRepository( configuration );
67
68 CheckOutScmResult result;
69
70
71 synchronized ( this )
72 {
73 if ( !workingDirectory.exists() )
74 {
75 if ( !workingDirectory.mkdirs() )
76 {
77 throw new IOException( "Could not make directory: " + workingDirectory.getAbsolutePath() );
78 }
79 }
80 else
81 {
82 FileUtils.cleanDirectory( workingDirectory );
83 }
84
85 ScmFileSet fileSet = new ScmFileSet( workingDirectory );
86
87 result = scmManager.checkOut( repository, fileSet, scmVersion );
88 }
89 return result;
90 }
91
92 private ScmVersion getScmVersion( ContinuumScmConfiguration configuration )
93 {
94 String tag = configuration.getTag();
95
96 ScmVersion scmVersion = null;
97 if ( tag != null )
98 {
99
100 scmVersion = new ScmTag( tag );
101 }
102 return scmVersion;
103 }
104
105 public UpdateScmResult update( ContinuumScmConfiguration configuration )
106 throws ScmException
107 {
108 ScmVersion scmVersion = getScmVersion( configuration );
109
110 File workingDirectory = configuration.getWorkingDirectory();
111 if ( !workingDirectory.exists() )
112 {
113
114 throw new IllegalStateException(
115 "The working directory for the project doesn't exist " + "(" + workingDirectory.getAbsolutePath() +
116 ")." );
117 }
118
119 ScmRepository repository = getScmRepository( configuration );
120
121
122 if ( "starteam".equals( repository.getProvider() ) )
123 {
124
125
126
127
128 System.setProperty( "maven.scm.starteam.deleteLocal", "true" );
129 }
130
131 UpdateScmResult result;
132
133 ScmFileSet fileSet = new ScmFileSet( workingDirectory );
134
135
136 synchronized ( this )
137 {
138 result = scmManager.update( repository, fileSet, scmVersion, configuration.getLatestUpdateDate() );
139 }
140
141 return result;
142 }
143
144 public ChangeLogScmResult changeLog( ContinuumScmConfiguration configuration )
145 throws ScmException
146 {
147 ScmVersion scmVersion = getScmVersion( configuration );
148 Date startDate = null;
149
150
151 File workingDirectory = configuration.getWorkingDirectory();
152
153 ScmRepository repository = getScmRepository( configuration );
154
155 ChangeLogScmResult result;
156
157 ScmFileSet fileSet = new ScmFileSet( workingDirectory );
158
159 if ( scmVersion == null || StringUtils.isBlank( scmVersion.getName() ) )
160 {
161
162 startDate = getScmStartDate( configuration );
163
164 result = scmManager.changeLog( repository, fileSet, startDate, null, 0, null, null );
165 }
166 else
167 {
168 result = scmManager.changeLog( repository, fileSet, scmVersion, scmVersion );
169 }
170
171 return result;
172 }
173
174
175
176
177
178
179
180
181
182 private ScmRepository getScmRepository( ContinuumScmConfiguration configuration )
183 throws ScmRepositoryException, NoSuchScmProviderException
184 {
185 ScmRepository repository = scmManager.makeScmRepository( configuration.getUrl() );
186
187
188
189 repository.getProviderRepository().setPersistCheckout( true );
190
191
192 if ( !configuration.isUseCredentialsCache() || !"svn".equals( repository.getProvider() ) )
193 {
194 if ( !StringUtils.isEmpty( configuration.getUsername() ) )
195 {
196 repository.getProviderRepository().setUser( configuration.getUsername() );
197
198 if ( !StringUtils.isEmpty( configuration.getPassword() ) )
199 {
200 repository.getProviderRepository().setPassword( configuration.getPassword() );
201 }
202 else
203 {
204 repository.getProviderRepository().setPassword( "" );
205 }
206 }
207 }
208
209 if ( "perforce".equals( repository.getProvider() ) )
210 {
211
212
213
214
215 System.setProperty( "maven.scm.perforce.clientspec.name", "" );
216 }
217
218 return repository;
219 }
220
221 private Date getScmStartDate( ContinuumScmConfiguration configuration )
222 {
223 Date startDate = configuration.getLatestUpdateDate();
224
225 if ( startDate == null )
226 {
227
228 Calendar cal = Calendar.getInstance();
229 cal.set( 1970, Calendar.JANUARY, 1 );
230 startDate = cal.getTime();
231 }
232
233 return startDate;
234 }
235
236 public ScmManager getScmManager()
237 {
238 return scmManager;
239 }
240
241 public void setScmManager( ScmManager scmManager )
242 {
243 this.scmManager = scmManager;
244 }
245
246
247 }