View Javadoc

1   package org.apache.tomcat.maven.plugin.tomcat7;
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 AbstractTomcat7Mojo
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/html"
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 org.apache.maven.plugin.MojoExecutionException
165      *                             if there was a problem executing this goal
166      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
167      *                             if the Tomcat manager request fails
168      * @throws java.io.IOException if an i/o error occurs
169      */
170     protected abstract void invokeManager()
171         throws MojoExecutionException, TomcatManagerException, IOException;
172 
173     /**
174      * Gets the Tomcat manager wrapper object configured for this goal.
175      *
176      * @return the Tomcat manager wrapper object
177      * @throws org.apache.maven.plugin.MojoExecutionException
178      *          if there was a problem obtaining the authentication details
179      */
180     protected TomcatManager getManager()
181         throws MojoExecutionException
182     {
183         // lazily instantiate when config values have been injected
184         if ( manager == null )
185         {
186             String userName;
187             String password;
188 
189             if ( server == null )
190             {
191                 // no server set, use defaults
192                 getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultAuth" ) );
193                 userName = DEFAULT_USERNAME;
194                 password = DEFAULT_PASSWORD;
195             }
196             else
197             {
198                 // obtain authenication details for specified server from wagon
199                 AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
200                 if ( info == null )
201                 {
202                     throw new MojoExecutionException(
203                         messagesProvider.getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
204                 }
205 
206                 // derive username
207                 userName = info.getUserName();
208                 if ( userName == null )
209                 {
210                     getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
211                     userName = DEFAULT_USERNAME;
212                 }
213 
214                 // derive password
215                 password = info.getPassword();
216                 if ( password == null )
217                 {
218                     getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultPassword" ) );
219                     password = DEFAULT_PASSWORD;
220                 }
221             }
222 
223             // if userName/password are defined in the mojo or the cli they override
224             if ( !StringUtils.isEmpty( this.username ) )
225             {
226                 userName = this.username;
227                 password = this.password == null ? "" : this.password;
228             }
229 
230             manager = new TomcatManager( url, userName, password, charset );
231             manager.setUserAgent( name + "/" + version );
232         }
233 
234         return manager;
235     }
236 
237     /**
238      * Gets the full URL of the Tomcat manager instance.
239      *
240      * @return the full URL of the Tomcat manager instance to use
241      */
242     protected URL getURL()
243     {
244         return url;
245     }
246 
247     /**
248      * Gets the webapp context path to use when communicating with Tomcat manager.
249      *
250      * @return the webapp context path to use
251      */
252     protected String getPath()
253     {
254         return path;
255     }
256 
257     /**
258      * Gets the URL of the deployed webapp.
259      *
260      * @return the URL of the deployed webapp
261      * @throws java.net.MalformedURLException if the deployed webapp URL is invalid
262      */
263     protected URL getDeployedURL()
264         throws MalformedURLException
265     {
266         return new URL( getURL(), getPath() );
267     }
268 
269     /**
270      * Splits the given string into lines and writes each one separately to the log at info level.
271      *
272      * @param string the string to write
273      */
274     protected void log( String string )
275     {
276         StringTokenizer tokenizer = new StringTokenizer( string, "\n\r" );
277 
278         while ( tokenizer.hasMoreTokens() )
279         {
280             getLog().info( tokenizer.nextToken() );
281         }
282     }
283 }