1 package org.codehaus.mojo.tomcat;
2
3 /*
4 * Copyright 2005 Mark Hobson.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.util.StringTokenizer;
23
24 import org.apache.maven.artifact.manager.WagonManager;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.wagon.authentication.AuthenticationInfo;
27
28 /**
29 * Abstract goal that provides common configuration for Catalina-based goals.
30 *
31 * @author Mark Hobson <markhobson@gmail.com>
32 * @version $Id: AbstractCatalinaMojo.java 6588 2008-03-28 12:22:57Z bentmann $
33 */
34 public abstract class AbstractCatalinaMojo
35 extends AbstractI18NMojo
36 {
37 // ----------------------------------------------------------------------
38 // Constants
39 // ----------------------------------------------------------------------
40
41 /**
42 * The name of this Maven plugin. Used to produce the user agent when
43 * communicating with Tomcat manager.
44 */
45 private static final String NAME = "Tomcat Maven Plugin";
46
47 /**
48 * The version of this Maven plugin. Used to produce the user agent when
49 * communicating with Tomcat manager.
50 */
51 private static final String VERSION = "1.0-SNAPSHOT";
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
69 * details.
70 *
71 * @parameter expression = "${component.org.apache.maven.artifact.manager.WagonManager}"
72 * @required
73 * @readonly
74 */
75 private WagonManager wagonManager;
76
77 /**
78 * The full URL of the Tomcat manager instance to use.
79 *
80 * @parameter expression = "${maven.tomcat.url}" default-value = "http://localhost:8080/manager"
81 * @required
82 */
83 private URL url;
84
85 /**
86 * The server id in settings.xml to use when authenticating with Tomcat
87 * manager, or <code>null</code> to use defaults of username
88 * <code>admin</code> and no password.
89 *
90 * @parameter expression = "${maven.tomcat.server}"
91 */
92 private String server;
93
94 /**
95 * The URL encoding charset to use when communicating with Tomcat manager.
96 *
97 * @parameter expression = "${maven.tomcat.charset}" default-value = "ISO-8859-1"
98 * @required
99 */
100 private String charset;
101
102 /**
103 * The webapp context path to use when communicating with Tomcat manager.
104 * This must always start with a forward-slash ('/').
105 *
106 * @parameter expression = "/${project.build.finalName}"
107 * @required
108 */
109 private String path;
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 * @see org.apache.maven.plugin.Mojo#execute()
126 */
127 public void execute()
128 throws MojoExecutionException
129 {
130 try
131 {
132 invokeManager();
133 }
134 catch ( TomcatManagerException exception )
135 {
136 throw new MojoExecutionException( getMessage( "AbstractCatalinaMojo.managerError", exception.getMessage() ) );
137 }
138 catch ( IOException exception )
139 {
140 throw new MojoExecutionException( getMessage( "AbstractCatalinaMojo.managerIOError" ), exception );
141 }
142 }
143
144 // ----------------------------------------------------------------------
145 // Protected Methods
146 // ----------------------------------------------------------------------
147
148 /**
149 * Invokes Tomcat manager when this Mojo is executed.
150 *
151 * @throws MojoExecutionException if there was a problem executing this goal
152 * @throws TomcatManagerException if the Tomcat manager request fails
153 * @throws IOException if an i/o error occurs
154 */
155 protected abstract void invokeManager()
156 throws MojoExecutionException, TomcatManagerException, IOException;
157
158 /**
159 * Gets the Tomcat manager wrapper object configured for this goal.
160 *
161 * @return the Tomcat manager wrapper object
162 * @throws MojoExecutionException if there was a problem obtaining the authentication details
163 */
164 protected TomcatManager getManager()
165 throws MojoExecutionException
166 {
167 // lazily instantiate when config values have been injected
168 if ( manager == null )
169 {
170 String userName;
171 String password;
172
173 if ( server == null )
174 {
175 // no server set, use defaults
176 getLog().debug( getMessage( "AbstractCatalinaMojo.defaultAuth" ) );
177 userName = DEFAULT_USERNAME;
178 password = DEFAULT_PASSWORD;
179 }
180 else
181 {
182 // obtain authenication details for specified server from wagon
183 AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
184 if ( info == null )
185 {
186 throw new MojoExecutionException( getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
187 }
188
189 // derive username
190 userName = info.getUserName();
191 if ( userName == null )
192 {
193 getLog().debug( getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
194 userName = DEFAULT_USERNAME;
195 }
196
197 // derive password
198 password = info.getPassword();
199 if ( password == null )
200 {
201 getLog().debug( getMessage( "AbstractCatalinaMojo.defaultPassword" ) );
202 password = DEFAULT_PASSWORD;
203 }
204 }
205
206 manager = new TomcatManager( url, userName, password, charset );
207 manager.setUserAgent( NAME + "/" + VERSION );
208 }
209
210 return manager;
211 }
212
213 /**
214 * Gets the full URL of the Tomcat manager instance.
215 *
216 * @return the full URL of the Tomcat manager instance to use
217 */
218 protected URL getURL()
219 {
220 return url;
221 }
222
223 /**
224 * Gets the webapp context path to use when communicating with Tomcat
225 * manager.
226 *
227 * @return the webapp context path to use
228 */
229 protected String getPath()
230 {
231 return path;
232 }
233
234 /**
235 * Gets the URL of the deployed webapp.
236 *
237 * @return the URL of the deployed webapp
238 * @throws MalformedURLException if the deployed webapp URL is invalid
239 */
240 protected URL getDeployedURL()
241 throws MalformedURLException
242 {
243 return new URL( getURL(), getPath() );
244 }
245
246 /**
247 * Splits the given string into lines and writes each one separately to the
248 * log at info level.
249 *
250 * @param string the string to write
251 */
252 protected void log( String string )
253 {
254 StringTokenizer tokenizer = new StringTokenizer( string, "\n\r" );
255
256 while ( tokenizer.hasMoreTokens() )
257 {
258 getLog().info( tokenizer.nextToken() );
259 }
260 }
261 }