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