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