1 package org.apache.tomcat.maven.plugin.tomcat6;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Abstract goal that provides common configuration for Catalina-based goals.
38 *
39 * @author Mark Hobson <markhobson@gmail.com>
40 */
41 public abstract class AbstractCatalinaMojo
42 extends AbstractI18NTomcat6Mojo
43 {
44 // ----------------------------------------------------------------------
45 // Constants
46 // ----------------------------------------------------------------------
47
48 /**
49 * The name of this Maven plugin. Used to produce the user agent when communicating with Tomcat manager.
50 */
51 private String name = "Apache Tomcat Maven Plugin";
52
53 /**
54 * The default username to use when authenticating with Tomcat manager.
55 */
56 private static final String DEFAULT_USERNAME = "admin";
57
58 /**
59 * The default password to use when authenticating with Tomcat manager.
60 */
61 private static final String DEFAULT_PASSWORD = "";
62
63 // ----------------------------------------------------------------------
64 // Mojo Parameters
65 // ----------------------------------------------------------------------
66
67 /**
68 * The Maven Wagon manager to use when obtaining server authentication details.
69 */
70 @Component( role = WagonManager.class )
71 private WagonManager wagonManager;
72
73 /**
74 * The full URL of the Tomcat manager instance to use.
75 */
76 @Parameter( property = "maven.tomcat.url", defaultValue = "http://localhost:8080/manager", required = true )
77 private URL url;
78
79 /**
80 * The server id in settings.xml to use when authenticating with Tomcat manager, or <code>null</code> to use
81 * defaults of username <code>admin</code> and no password.
82 */
83 @Parameter( property = "maven.tomcat.server" )
84 private String server;
85
86 /**
87 * The URL encoding charset to use when communicating with Tomcat manager.
88 */
89 @Parameter( property = "maven.tomcat.charset", defaultValue = "ISO-8859-1", required = true )
90 private String charset;
91
92 /**
93 * The tomcat username to use for deployment
94 *
95 * @since 1.0-alpha-2
96 */
97 @Parameter( property = "tomcat.username" )
98 private String username;
99
100 /**
101 * The password to use for deployment
102 *
103 * @since 1.0-alpha-2
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 // Fields
113 // ----------------------------------------------------------------------
114
115 /**
116 * The Tomcat manager wrapper object.
117 */
118 private TomcatManager manager;
119
120 // ----------------------------------------------------------------------
121 // Mojo Implementation
122 // ----------------------------------------------------------------------
123
124 /**
125 * {@inheritDoc}
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 // Protected Methods
148 // ----------------------------------------------------------------------
149
150 /**
151 * Invokes Tomcat manager when this Mojo is executed.
152 *
153 * @throws MojoExecutionException if there was a problem executing this goal
154 * @throws TomcatManagerException if the Tomcat manager request fails
155 * @throws IOException if an i/o error occurs
156 */
157 protected abstract void invokeManager()
158 throws MojoExecutionException, TomcatManagerException, IOException;
159
160 /**
161 * Gets the Tomcat manager wrapper object configured for this goal.
162 *
163 * @return the Tomcat manager wrapper object
164 * @throws MojoExecutionException if there was a problem obtaining the authentication details
165 */
166 protected TomcatManager getManager()
167 throws MojoExecutionException
168 {
169 // lazily instantiate when config values have been injected
170 if ( manager == null )
171 {
172 String userName;
173 String password;
174
175 if ( server == null )
176 {
177 // no server set, use defaults
178 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultAuth" ) );
179 userName = DEFAULT_USERNAME;
180 password = DEFAULT_PASSWORD;
181 }
182 else
183 {
184 // obtain authenication details for specified server from wagon
185 AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
186 if ( info == null )
187 {
188 throw new MojoExecutionException(
189 messagesProvider.getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
190 }
191
192 // derive username
193 userName = info.getUserName();
194 if ( userName == null )
195 {
196 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
197 userName = DEFAULT_USERNAME;
198 }
199
200 // derive password
201 password = info.getPassword();
202 if ( password == null )
203 {
204 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultPassword" ) );
205 password = DEFAULT_PASSWORD;
206 }
207 }
208
209 // if userName/password are defined in the mojo or the cli they override
210 if ( !StringUtils.isEmpty( this.username ) )
211 {
212 userName = this.username;
213 password = this.password == null ? "" : this.password;
214 //getLog( ).debug( "user user/password " + userName + "/" + password );
215 }
216
217 manager = new TomcatManager( url, userName, password, charset );
218 manager.setUserAgent( name + "/" + version );
219 }
220
221 return manager;
222 }
223
224 /**
225 * Gets the full URL of the Tomcat manager instance.
226 *
227 * @return the full URL of the Tomcat manager instance to use
228 */
229 protected URL getURL()
230 {
231 return url;
232 }
233
234 /**
235 * Gets the webapp context path to use when communicating with Tomcat manager.
236 *
237 * @return the webapp context path to use
238 */
239 protected String getPath()
240 {
241 return path;
242 }
243
244 /**
245 * Gets the URL of the deployed webapp.
246 *
247 * @return the URL of the deployed webapp
248 * @throws MalformedURLException if the deployed webapp URL is invalid
249 */
250 protected URL getDeployedURL()
251 throws MalformedURLException
252 {
253 return new URL( getURL(), getPath() );
254 }
255
256 /**
257 * Splits the given string into lines and writes each one separately to the log at info level.
258 *
259 * @param string the string to write
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 }