1 package org.apache.tomcat.maven.plugin.tomcat6;
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 AbstractI18NTomcat6Mojo
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( role = WagonManager.class )
71 private WagonManager wagonManager;
72
73
74
75
76 @Parameter( property = "maven.tomcat.url", defaultValue = "http://localhost:8080/manager", 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( property = "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 protected abstract void invokeManager()
158 throws MojoExecutionException, TomcatManagerException, IOException;
159
160
161
162
163
164
165
166 protected TomcatManager getManager()
167 throws MojoExecutionException
168 {
169
170 if ( manager == null )
171 {
172 String userName;
173 String password;
174
175 if ( server == null )
176 {
177
178 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultAuth" ) );
179 userName = DEFAULT_USERNAME;
180 password = DEFAULT_PASSWORD;
181 }
182 else
183 {
184
185 AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
186 if ( info == null )
187 {
188 throw new MojoExecutionException(
189 messagesProvider.getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
190 }
191
192
193 userName = info.getUserName();
194 if ( userName == null )
195 {
196 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
197 userName = DEFAULT_USERNAME;
198 }
199
200
201 password = info.getPassword();
202 if ( password == null )
203 {
204 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultPassword" ) );
205 password = DEFAULT_PASSWORD;
206 }
207 }
208
209
210 if ( !StringUtils.isEmpty( this.username ) )
211 {
212 userName = this.username;
213 password = this.password == null ? "" : this.password;
214
215 }
216
217 manager = new TomcatManager( url, userName, password, charset );
218 manager.setUserAgent( name + "/" + version );
219 }
220
221 return manager;
222 }
223
224
225
226
227
228
229 protected URL getURL()
230 {
231 return url;
232 }
233
234
235
236
237
238
239 protected String getPath()
240 {
241 return path;
242 }
243
244
245
246
247
248
249
250 protected URL getDeployedURL()
251 throws MalformedURLException
252 {
253 return new URL( getURL(), getPath() );
254 }
255
256
257
258
259
260
261 protected void log( String string )
262 {
263 StringTokenizer tokenizer = new StringTokenizer( string, "\n\r" );
264
265 while ( tokenizer.hasMoreTokens() )
266 {
267 getLog().info( tokenizer.nextToken() );
268 }
269 }
270 }