1 package org.apache.tomcat.maven.plugin.tomcat7;
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.artifact.manager.WagonManager;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugins.annotations.Component;
25 import org.apache.maven.plugins.annotations.Parameter;
26 import org.apache.maven.wagon.authentication.AuthenticationInfo;
27 import org.apache.tomcat.maven.common.deployer.TomcatManager;
28 import org.apache.tomcat.maven.common.deployer.TomcatManagerException;
29 import org.codehaus.plexus.util.StringUtils;
30
31 import java.io.IOException;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.util.StringTokenizer;
35
36
37
38
39
40
41 public abstract class AbstractCatalinaMojo
42 extends AbstractTomcat7Mojo
43 {
44
45
46
47
48
49
50
51 private String name = "Apache Tomcat Maven Plugin";
52
53
54
55
56 private static final String DEFAULT_USERNAME = "admin";
57
58
59
60
61 private static final String DEFAULT_PASSWORD = "";
62
63
64
65
66
67
68
69
70 @Component
71 private WagonManager wagonManager;
72
73
74
75
76 @Parameter( property = "maven.tomcat.url", defaultValue = "http://localhost:8080/manager/html", required = true )
77 private URL url;
78
79
80
81
82
83 @Parameter( property = "maven.tomcat.server" )
84 private String server;
85
86
87
88
89 @Parameter( property = "maven.tomcat.charset", defaultValue = "ISO-8859-1", required = true )
90 private String charset;
91
92
93
94
95
96
97 @Parameter( property = "tomcat.username" )
98 private String username;
99
100
101
102
103
104
105 @Parameter( property = "tomcat.password" )
106 private String password;
107
108 @Parameter( defaultValue = "${plugin.version}", required = true, readonly = true )
109 private String version;
110
111
112
113
114
115
116
117
118 private TomcatManager manager;
119
120
121
122
123
124
125
126
127 public void execute()
128 throws MojoExecutionException
129 {
130 try
131 {
132 invokeManager();
133 }
134 catch ( TomcatManagerException exception )
135 {
136 throw new MojoExecutionException(
137 messagesProvider.getMessage( "AbstractCatalinaMojo.managerError", exception.getMessage() ) );
138 }
139 catch ( IOException exception )
140 {
141 throw new MojoExecutionException( messagesProvider.getMessage( "AbstractCatalinaMojo.managerIOError" ),
142 exception );
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 protected abstract void invokeManager()
160 throws MojoExecutionException, TomcatManagerException, IOException;
161
162
163
164
165
166
167
168
169 protected TomcatManager getManager()
170 throws MojoExecutionException
171 {
172
173 if ( manager == null )
174 {
175 String userName;
176 String password;
177
178 if ( server == null )
179 {
180
181 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultAuth" ) );
182 userName = DEFAULT_USERNAME;
183 password = DEFAULT_PASSWORD;
184 }
185 else
186 {
187
188 AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
189 if ( info == null )
190 {
191 throw new MojoExecutionException(
192 messagesProvider.getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
193 }
194
195
196 userName = info.getUserName();
197 if ( userName == null )
198 {
199 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
200 userName = DEFAULT_USERNAME;
201 }
202
203
204 password = info.getPassword();
205 if ( password == null )
206 {
207 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultPassword" ) );
208 password = DEFAULT_PASSWORD;
209 }
210 }
211
212
213 if ( !StringUtils.isEmpty( this.username ) )
214 {
215 userName = this.username;
216 password = this.password == null ? "" : this.password;
217 }
218
219 manager = new TomcatManager( url, userName, password, charset );
220 manager.setUserAgent( name + "/" + version );
221 }
222
223 return manager;
224 }
225
226
227
228
229
230
231 protected URL getURL()
232 {
233 return url;
234 }
235
236
237
238
239
240
241 protected String getPath()
242 {
243 return path;
244 }
245
246
247
248
249
250
251
252 protected URL getDeployedURL()
253 throws MalformedURLException
254 {
255 return new URL( getURL(), getPath() );
256 }
257
258
259
260
261
262
263 protected void log( String string )
264 {
265 StringTokenizer tokenizer = new StringTokenizer( string, "\n\r" );
266
267 while ( tokenizer.hasMoreTokens() )
268 {
269 getLog().info( tokenizer.nextToken() );
270 }
271 }
272 }