1 package org.apache.maven.scm.provider.vss.commands;
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.ScmFileSet;
24 import org.apache.maven.scm.log.ScmLogger;
25 import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
26 import org.apache.maven.scm.providers.vss.settings.Settings;
27 import org.apache.maven.scm.providers.vss.settings.io.xpp3.VssXpp3Reader;
28 import org.codehaus.plexus.util.ReaderFactory;
29 import org.codehaus.plexus.util.StringUtils;
30 import org.codehaus.plexus.util.cli.CommandLineException;
31 import org.codehaus.plexus.util.cli.CommandLineUtils;
32 import org.codehaus.plexus.util.cli.Commandline;
33 import org.codehaus.plexus.util.cli.StreamConsumer;
34 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.util.Iterator;
40
41
42
43
44
45 public final class VssCommandLineUtils
46
47 {
48
49 private VssCommandLineUtils()
50 {
51 }
52
53 private static File scmConfDir = new File( System.getProperty( "user.home" ), ".scm" );
54
55 private static Settings settings;
56
57 public static void addFiles( Commandline cl, ScmFileSet fileSet )
58 {
59 Iterator<File> it = fileSet.getFileList().iterator();
60
61 while ( it.hasNext() )
62 {
63 File file = it.next();
64
65 cl.createArg().setValue( file.getPath().replace( '\\', '/' ) );
66 }
67
68 }
69
70 public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd,
71 VssScmProviderRepository repository )
72 {
73 Commandline cl = new Commandline();
74
75 cl.setExecutable( VssConstants.SS_EXE );
76
77 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
78
79 if ( !StringUtils.isEmpty( repository.getUser() ) )
80 {
81 cl.createArg().setValue( "-Y" );
82
83 StringBuilder sb = new StringBuilder( repository.getUser() );
84 if ( !StringUtils.isEmpty( repository.getPassword() ) )
85 {
86 sb.append( "," ).append( repository.getPassword() );
87 }
88 cl.createArg().setValue( sb.toString() );
89 }
90
91 return cl;
92 }
93
94 public static int executeCommandline( Commandline cl, StreamConsumer consumer,
95 CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
96 throws ScmException
97 {
98 try
99 {
100 if ( logger.isInfoEnabled() )
101 {
102 logger.info( "Executing: " + cl );
103 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
104 }
105
106 int exitcode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
107
108 if ( logger.isDebugEnabled() )
109 {
110 logger.debug( "VSS Command Exit_Code: " + exitcode );
111 }
112
113 return exitcode;
114 }
115 catch ( CommandLineException ex )
116 {
117 throw new ScmException( "Error while executing command.", ex );
118 }
119 }
120
121
122 public static Settings getSettings()
123 {
124 if ( settings == null )
125 {
126 settings = readSettings();
127 }
128 return settings;
129 }
130
131 public static Settings readSettings()
132 {
133 Settings settings = null;
134 File settingsFile = getScmConfFile();
135 if ( settingsFile.exists() )
136 {
137 VssXpp3Reader reader = new VssXpp3Reader();
138 try
139 {
140 settings = reader.read( ReaderFactory.newXmlReader( settingsFile ) );
141 }
142 catch ( FileNotFoundException e )
143 {
144
145 }
146 catch ( IOException e )
147 {
148
149 }
150 catch ( XmlPullParserException e )
151 {
152 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
153
154 System.err.println( message );
155 }
156 }
157
158
159 String vssDirectory = System.getProperty( "vssDirectory" );
160 if ( StringUtils.isNotEmpty( vssDirectory ) )
161 {
162 if ( settings == null )
163 {
164 settings = new Settings();
165 }
166 settings.setVssDirectory( vssDirectory );
167 }
168 return settings;
169 }
170
171 protected static File getScmConfDir()
172 {
173 return scmConfDir;
174 }
175
176 protected static void setScmConfDir( File directory )
177 {
178 scmConfDir = directory;
179 settings = readSettings();
180 }
181
182 public static String getSsDir()
183 {
184 String ssDir = "";
185 if ( VssCommandLineUtils.getSettings() != null )
186 {
187 String ssDir2 = VssCommandLineUtils.getSettings().getVssDirectory();
188
189 if ( ssDir2 != null )
190 {
191 ssDir = StringUtils.replace( ssDir2, "\\", "/" );
192
193 if ( !ssDir.endsWith( "/" ) )
194 {
195 ssDir += "/";
196 }
197 }
198 }
199 return ssDir;
200 }
201
202 public static File getScmConfFile()
203 {
204 return new File( scmConfDir, "vss-settings.xml" );
205 }
206 }