1 package org.apache.maven.scm.provider.synergy.command.update;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmFileStatus;
26 import org.apache.maven.scm.ScmVersion;
27 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
28 import org.apache.maven.scm.command.update.AbstractUpdateCommand;
29 import org.apache.maven.scm.command.update.UpdateScmResult;
30 import org.apache.maven.scm.provider.ScmProviderRepository;
31 import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
32 import org.apache.maven.scm.provider.synergy.command.changelog.SynergyChangeLogCommand;
33 import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
34 import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
35 import org.codehaus.plexus.util.FileUtils;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.List;
41
42
43
44
45
46 public class SynergyUpdateCommand
47 extends AbstractUpdateCommand
48 implements SynergyCommand
49 {
50
51 protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
52 ScmVersion version )
53 throws ScmException
54 {
55 if ( getLogger().isDebugEnabled() )
56 {
57 getLogger().debug( "executing update command..." );
58 }
59 SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
60
61 if ( getLogger().isDebugEnabled() )
62 {
63 getLogger().debug( "basedir: " + fileSet.getBasedir() );
64 }
65
66 String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
67
68 File waPath;
69 try
70 {
71 String projectSpec =
72 SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
73 SynergyUtil.reconfigureProperties( getLogger(), projectSpec, ccmAddr );
74 SynergyUtil.reconfigure( getLogger(), projectSpec, ccmAddr );
75
76 waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
77 }
78 finally
79 {
80 SynergyUtil.stop( getLogger(), ccmAddr );
81 }
82
83 File source = new File( waPath, repo.getProjectName() );
84
85
86 List<ScmFile> modifications = new ArrayList<ScmFile>();
87 if ( !source.equals( fileSet.getBasedir() ) )
88 {
89 if ( getLogger().isInfoEnabled() )
90 {
91 getLogger().info( "We will copy modified files from Synergy Work Area [" + source
92 + "] to expected folder [" + fileSet.getBasedir() + "]" );
93 }
94 try
95 {
96 copyDirectoryStructure( source, fileSet.getBasedir(), modifications );
97 }
98 catch ( IOException e1 )
99 {
100 throw new ScmException( "Unable to copy directory structure", e1 );
101 }
102 }
103
104 return new UpdateScmResult( "ccm reconcile -uwa ...", modifications );
105 }
106
107 protected ChangeLogCommand getChangeLogCommand()
108 {
109 SynergyChangeLogCommand changeLogCmd = new SynergyChangeLogCommand();
110
111 changeLogCmd.setLogger( getLogger() );
112
113 return changeLogCmd;
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public static void copyDirectoryStructure( File sourceDirectory, File destinationDirectory,
130 List<ScmFile> modifications )
131 throws IOException
132 {
133 if ( !sourceDirectory.exists() )
134 {
135 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
136 }
137
138 File[] files = sourceDirectory.listFiles();
139
140 String sourcePath = sourceDirectory.getAbsolutePath();
141
142 for ( File file : files )
143 {
144 String dest = file.getAbsolutePath();
145
146 dest = dest.substring( sourcePath.length() + 1 );
147
148 File destination = new File( destinationDirectory, dest );
149
150 if ( file.isFile() )
151 {
152
153 if ( file.lastModified() != destination.lastModified() )
154 {
155
156 destination = destination.getParentFile();
157
158 FileUtils.copyFileToDirectory( file, destination );
159
160 modifications.add( new ScmFile( file.getAbsolutePath(), ScmFileStatus.UPDATED ) );
161
162 }
163 }
164 else if ( file.isDirectory() )
165 {
166 if ( !destination.exists() && !destination.mkdirs() )
167 {
168 throw new IOException(
169 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
170 }
171
172 copyDirectoryStructure( file, destination, modifications );
173 }
174 else
175 {
176 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
177 }
178 }
179 }
180
181 }