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.wagon.authentication.AuthenticationInfo; 25 import org.apache.tomcat.maven.common.deployer.TomcatManager; 26 import org.apache.tomcat.maven.common.deployer.TomcatManagerException; 27 import org.codehaus.plexus.util.StringUtils; 28 29 import java.io.IOException; 30 import java.net.MalformedURLException; 31 import java.net.URL; 32 import java.util.StringTokenizer; 33 34 /** 35 * Abstract goal that provides common configuration for Catalina-based goals. 36 * 37 * @author Mark Hobson <markhobson@gmail.com> 38 * @version $Id: AbstractCatalinaMojo.html 1305321 2012-03-26 12:01:12Z olamy $ 39 */ 40 public abstract class AbstractCatalinaMojo 41 extends AbstractI18NTomcat6Mojo 42 { 43 // ---------------------------------------------------------------------- 44 // Constants 45 // ---------------------------------------------------------------------- 46 47 /** 48 * The name of this Maven plugin. Used to produce the user agent when communicating with Tomcat manager. 49 */ 50 private String name = "Apache Tomcat Maven Plugin"; 51 52 /** 53 * The default username to use when authenticating with Tomcat manager. 54 */ 55 private static final String DEFAULT_USERNAME = "admin"; 56 57 /** 58 * The default password to use when authenticating with Tomcat manager. 59 */ 60 private static final String DEFAULT_PASSWORD = ""; 61 62 // ---------------------------------------------------------------------- 63 // Mojo Parameters 64 // ---------------------------------------------------------------------- 65 66 /** 67 * The Maven Wagon manager to use when obtaining server authentication details. 68 * 69 * @component role="org.apache.maven.artifact.manager.WagonManager" 70 * @required 71 * @readonly 72 */ 73 private WagonManager wagonManager; 74 75 /** 76 * The full URL of the Tomcat manager instance to use. 77 * 78 * @parameter expression="${maven.tomcat.url}" default-value="http://localhost:8080/manager" 79 * @required 80 */ 81 private URL url; 82 83 /** 84 * The server id in settings.xml to use when authenticating with Tomcat manager, or <code>null</code> to use 85 * defaults of username <code>admin</code> and no password. 86 * 87 * @parameter expression="${maven.tomcat.server}" 88 */ 89 private String server; 90 91 /** 92 * The URL encoding charset to use when communicating with Tomcat manager. 93 * 94 * @parameter expression="${maven.tomcat.charset}" default-value="ISO-8859-1" 95 * @required 96 */ 97 private String charset; 98 99 /** 100 * The tomcat username to use for deployment 101 * 102 * @parameter expression="${tomcat.username}" 103 * @since 1.0-alpha-2 104 */ 105 private String username; 106 107 /** 108 * The password to use for deployment 109 * 110 * @parameter expression="${tomcat.password}" 111 * @since 1.0-alpha-2 112 */ 113 private String password; 114 115 /** 116 * @parameter expression="${plugin.version}" 117 * @required 118 * @readonly 119 */ 120 private String version; 121 122 // ---------------------------------------------------------------------- 123 // Fields 124 // ---------------------------------------------------------------------- 125 126 /** 127 * The Tomcat manager wrapper object. 128 */ 129 private TomcatManager manager; 130 131 // ---------------------------------------------------------------------- 132 // Mojo Implementation 133 // ---------------------------------------------------------------------- 134 135 /** 136 * {@inheritDoc} 137 */ 138 public void execute() 139 throws MojoExecutionException 140 { 141 try 142 { 143 invokeManager(); 144 } 145 catch ( TomcatManagerException exception ) 146 { 147 throw new MojoExecutionException( 148 messagesProvider.getMessage( "AbstractCatalinaMojo.managerError", exception.getMessage() ) ); 149 } 150 catch ( IOException exception ) 151 { 152 throw new MojoExecutionException( messagesProvider.getMessage( "AbstractCatalinaMojo.managerIOError" ), 153 exception ); 154 } 155 } 156 157 // ---------------------------------------------------------------------- 158 // Protected Methods 159 // ---------------------------------------------------------------------- 160 161 /** 162 * Invokes Tomcat manager when this Mojo is executed. 163 * 164 * @throws MojoExecutionException if there was a problem executing this goal 165 * @throws TomcatManagerException if the Tomcat manager request fails 166 * @throws IOException if an i/o error occurs 167 */ 168 protected abstract void invokeManager() 169 throws MojoExecutionException, TomcatManagerException, IOException; 170 171 /** 172 * Gets the Tomcat manager wrapper object configured for this goal. 173 * 174 * @return the Tomcat manager wrapper object 175 * @throws MojoExecutionException if there was a problem obtaining the authentication details 176 */ 177 protected TomcatManager getManager() 178 throws MojoExecutionException 179 { 180 // lazily instantiate when config values have been injected 181 if ( manager == null ) 182 { 183 String userName; 184 String password; 185 186 if ( server == null ) 187 { 188 // no server set, use defaults 189 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultAuth" ) ); 190 userName = DEFAULT_USERNAME; 191 password = DEFAULT_PASSWORD; 192 } 193 else 194 { 195 // obtain authenication details for specified server from wagon 196 AuthenticationInfo info = wagonManager.getAuthenticationInfo( server ); 197 if ( info == null ) 198 { 199 throw new MojoExecutionException( 200 messagesProvider.getMessage( "AbstractCatalinaMojo.unknownServer", server ) ); 201 } 202 203 // derive username 204 userName = info.getUserName(); 205 if ( userName == null ) 206 { 207 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultUserName" ) ); 208 userName = DEFAULT_USERNAME; 209 } 210 211 // derive password 212 password = info.getPassword(); 213 if ( password == null ) 214 { 215 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultPassword" ) ); 216 password = DEFAULT_PASSWORD; 217 } 218 } 219 220 // if userName/password are defined in the mojo or the cli they override 221 if ( !StringUtils.isEmpty( this.username ) ) 222 { 223 userName = this.username; 224 password = this.password == null ? "" : this.password; 225 //getLog( ).debug( "user user/password " + userName + "/" + password ); 226 } 227 228 manager = new TomcatManager( url, userName, password, charset ); 229 manager.setUserAgent( name + "/" + version ); 230 } 231 232 return manager; 233 } 234 235 /** 236 * Gets the full URL of the Tomcat manager instance. 237 * 238 * @return the full URL of the Tomcat manager instance to use 239 */ 240 protected URL getURL() 241 { 242 return url; 243 } 244 245 /** 246 * Gets the webapp context path to use when communicating with Tomcat manager. 247 * 248 * @return the webapp context path to use 249 */ 250 protected String getPath() 251 { 252 return path; 253 } 254 255 /** 256 * Gets the URL of the deployed webapp. 257 * 258 * @return the URL of the deployed webapp 259 * @throws MalformedURLException if the deployed webapp URL is invalid 260 */ 261 protected URL getDeployedURL() 262 throws MalformedURLException 263 { 264 return new URL( getURL(), getPath() ); 265 } 266 267 /** 268 * Splits the given string into lines and writes each one separately to the log at info level. 269 * 270 * @param string the string to write 271 */ 272 protected void log( String string ) 273 { 274 StringTokenizer tokenizer = new StringTokenizer( string, "\n\r" ); 275 276 while ( tokenizer.hasMoreTokens() ) 277 { 278 getLog().info( tokenizer.nextToken() ); 279 } 280 } 281 }