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