View Javadoc

1   package org.apache.tomcat.maven.plugin.tomcat7;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.maven.plugin.AbstractMojo;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Component;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.settings.Settings;
26  import org.apache.tomcat.maven.common.deployer.TomcatManagerResponse;
27  import org.apache.tomcat.maven.common.messages.MessagesProvider;
28  
29  /**
30   * @author Olivier Lamy
31   * @since 2.0
32   */
33  public abstract class AbstractTomcat7Mojo
34      extends AbstractMojo
35  {
36      @Component
37      protected Settings settings;
38  
39      @Component
40      protected MessagesProvider messagesProvider;
41  
42      // ----------------------------------------------------------------------
43      // Mojo Parameters
44      // ----------------------------------------------------------------------
45  
46      /**
47       * The webapp context path to use for the web application being run. This must always start with a forward-slash
48       * ('/').
49       */
50      @Parameter(defaultValue = "/${project.artifactId}", property = "maven.tomcat.path", required = true)
51      protected String path;
52  
53  
54      protected String getPath()
55      {
56          return path;
57      }
58  
59      /**
60       * Check response of Tomcat to know if ok or not.
61       *
62       * @param tomcatResponse response of tomcat return by TomcatManager class
63       * @throws org.apache.maven.plugin.MojoExecutionException
64       *          if HTTP status code greater than 400 (included)
65       */
66      protected void checkTomcatResponse( TomcatManagerResponse tomcatResponse )
67          throws MojoExecutionException
68      {
69          int statusCode = tomcatResponse.getStatusCode();
70  
71          if ( statusCode >= 400 )
72          {
73              getLog().error( messagesProvider.getMessage( "tomcatHttpStatusError", statusCode,
74                                                           tomcatResponse.getReasonPhrase() ) );
75  
76              throw new MojoExecutionException(
77                  messagesProvider.getMessage( "tomcatHttpStatusError", statusCode,
78                                               tomcatResponse.getReasonPhrase() ) + ": "
79                      + tomcatResponse.getHttpResponseBody() );
80          }
81      }
82  }