1 package org.apache.maven.shared.release.exec;
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.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.settings.Proxy;
27 import org.apache.maven.settings.Server;
28 import org.apache.maven.settings.Settings;
29 import org.apache.maven.settings.SettingsUtils;
30 import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
31 import org.apache.maven.shared.release.ReleaseResult;
32 import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
33 import org.apache.maven.shared.release.env.ReleaseEnvironment;
34 import org.codehaus.plexus.logging.LogEnabled;
35 import org.codehaus.plexus.logging.Logger;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
38 import org.sonatype.plexus.components.cipher.PlexusCipher;
39 import org.sonatype.plexus.components.cipher.PlexusCipherException;
40 import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
41 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
42 import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
43 import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
44
45
46
47
48 public abstract class AbstractMavenExecutor
49 implements MavenExecutor, LogEnabled
50 {
51
52 private Logger logger;
53
54
55
56
57
58
59
60 private DefaultSecDispatcher secDispatcher;
61
62
63
64
65 private PlexusCipher cipher;
66
67 protected AbstractMavenExecutor()
68 {
69 }
70
71
72 public void executeGoals( File workingDirectory, String goals, boolean interactive, String additionalArguments,
73 String pomFileName, ReleaseResult result )
74 throws MavenExecutorException
75 {
76 executeGoals( workingDirectory, goals, new DefaultReleaseEnvironment(), interactive, additionalArguments,
77 pomFileName, result );
78 }
79
80
81 public void executeGoals( File workingDirectory, String goals, boolean interactive, String additionalArguments,
82 ReleaseResult result )
83 throws MavenExecutorException
84 {
85 executeGoals( workingDirectory, goals, new DefaultReleaseEnvironment(), interactive, additionalArguments,
86 result );
87 }
88
89
90 public void executeGoals( File workingDirectory, String goals, ReleaseEnvironment releaseEnvironment,
91 boolean interactive, String arguments, ReleaseResult result )
92 throws MavenExecutorException
93 {
94 executeGoals( workingDirectory, goals, releaseEnvironment, interactive, arguments, null, result );
95 }
96
97
98 public void executeGoals( File workingDirectory, String goals, ReleaseEnvironment releaseEnvironment,
99 boolean interactive, String additionalArguments, String pomFileName,
100 ReleaseResult result )
101 throws MavenExecutorException
102 {
103 List<String> goalsList = new ArrayList<String>();
104 if ( goals != null )
105 {
106
107
108
109 for ( String token : StringUtils.split( goals, ", \n\r\t" ) )
110 {
111 goalsList.add( token );
112 }
113 }
114 executeGoals( workingDirectory, goalsList, releaseEnvironment, interactive, additionalArguments, pomFileName,
115 result );
116 }
117
118 protected abstract void executeGoals( File workingDirectory, List<String> goals,
119 ReleaseEnvironment releaseEnvironment, boolean interactive,
120 String additionalArguments, String pomFileName, ReleaseResult result )
121 throws MavenExecutorException;
122
123 protected final Logger getLogger()
124 {
125 return logger;
126 }
127
128
129 public void enableLogging( Logger logger )
130 {
131 this.logger = logger;
132 }
133
134
135 protected Settings encryptSettings( Settings settings )
136 {
137 Settings encryptedSettings = SettingsUtils.copySettings( settings );
138
139 for ( Server server : encryptedSettings.getServers() )
140 {
141 String password = server.getPassword();
142 if ( password != null && !isEncryptedString( password ) )
143 {
144 try
145 {
146 server.setPassword( encryptAndDecorate( password ) );
147 }
148 catch ( IllegalStateException e )
149 {
150
151 }
152 catch ( SecDispatcherException e )
153 {
154
155 }
156 catch ( PlexusCipherException e )
157 {
158
159 }
160 }
161
162 String passphrase = server.getPassphrase();
163 if ( passphrase != null && !isEncryptedString( passphrase ) )
164 {
165 try
166 {
167 server.setPassphrase( encryptAndDecorate( passphrase ) );
168 }
169 catch ( IllegalStateException e )
170 {
171
172 }
173 catch ( SecDispatcherException e )
174 {
175
176 }
177 catch ( PlexusCipherException e )
178 {
179
180 }
181 }
182 }
183
184 for ( Proxy proxy : encryptedSettings.getProxies() )
185 {
186 String password = proxy.getPassword();
187 if ( password != null && !isEncryptedString( password ) )
188 {
189 try
190 {
191 proxy.setPassword( encryptAndDecorate( password ) );
192 }
193 catch ( IllegalStateException e )
194 {
195
196 }
197 catch ( SecDispatcherException e )
198 {
199
200 }
201 catch ( PlexusCipherException e )
202 {
203
204 }
205 }
206 }
207
208 return encryptedSettings;
209 }
210
211
212 private String encryptAndDecorate( String passwd )
213 throws IllegalStateException, SecDispatcherException, PlexusCipherException
214 {
215 String configurationFile = secDispatcher.getConfigurationFile();
216
217 if ( configurationFile.startsWith( "~" ) )
218 {
219 configurationFile = System.getProperty( "user.home" ) + configurationFile.substring( 1 );
220 }
221
222 String file = System.getProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile );
223
224 String master = null;
225
226 SettingsSecurity sec = SecUtil.read( file, true );
227 if ( sec != null )
228 {
229 master = sec.getMaster();
230 }
231
232 if ( master == null )
233 {
234 throw new IllegalStateException( "Master password is not set in the setting security file: " + file );
235 }
236
237 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
238 String masterPasswd = cipher.decryptDecorated( master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION );
239 return cipher.encryptAndDecorate( passwd, masterPasswd );
240 }
241
242 private boolean isEncryptedString( String str )
243 {
244 return cipher.isEncryptedString( str );
245 }
246
247 protected SettingsXpp3Writer getSettingsWriter()
248 {
249 return new SettingsXpp3Writer();
250 }
251 }