View Javadoc

    1   //========================================================================
    2   //$Id: Jetty6RunMojo.java 2299 2008-01-03 23:40:50Z janb $
    3   //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package org.mortbay.jetty.plugin;
    17
    18  import java.io.File;
    19
    20  import org.apache.maven.plugin.MojoExecutionException;
    21  import org.apache.maven.plugin.MojoFailureException;
    22  import org.mortbay.jetty.Connector;
    23  import org.mortbay.jetty.Handler;
    24  import org.mortbay.jetty.RequestLog;
    25  import org.mortbay.jetty.Server;
    26  import org.mortbay.jetty.handler.ContextHandler;
    27  import org.mortbay.jetty.handler.ContextHandlerCollection;
    28  import org.mortbay.jetty.handler.HandlerCollection;
    29  import org.mortbay.jetty.plugin.util.JettyPluginServer;
    30  import org.mortbay.jetty.security.UserRealm;
    31  import org.mortbay.xml.XmlConfiguration;
    32
    33  /**
    34   *  <p>
    35   *  This goal is used in-situ on a Maven project without first requiring that the project
    36   *  is assembled into a war, saving time during the development cycle.
    37   *  The plugin forks a parallel lifecycle to ensure that the "compile" phase has been completed before invoking Jetty. This means
    38   *  that you do not need to explicity execute a "mvn compile" first. It also means that a "mvn clean jetty:run" will ensure that
    39   *  a full fresh compile is done before invoking Jetty.
    40   *  </p>
    41   *  <p>
    42   *  Once invoked, the plugin can be configured to run continuously, scanning for changes in the project and automatically performing a
    43   *  hot redeploy when necessary. This allows the developer to concentrate on coding changes to the project using their IDE of choice and have those changes
    44   *  immediately and transparently reflected in the running web container, eliminating development time that is wasted on rebuilding, reassembling and redeploying.
    45   *  </p>
    46   *  <p>
    47   *  You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
    48   *  This can be used, for example, to deploy a static webapp that is not part of your maven build.
    49   *  </p>
    50   *  <p>
    51   *  There is a <a href="run-mojo.html">reference guide</a> to the configuration parameters for this plugin, and more detailed information
    52   *  with examples in the <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin">Configuration Guide</a>.
    53   *  </p>
    54   * @author janb
    55   *
    56   * @goal run
    57   * @requiresDependencyResolution runtime
    58   * @execute phase="test-compile"
    59   * @description Runs jetty6 directly from a maven project
    60   */
    61  public class Jetty6RunMojo extends AbstractJettyRunMojo
    62  {
    63
    64
    65      /**
    66       * List of connectors to use. If none are configured
    67       * then the default is a single SelectChannelConnector at port 8080. You can
    68       * override this default port number by using the system property jetty.port
    69       * on the command line, eg:  mvn -Djetty.port=9999 jetty:run
    70       *
    71       * @parameter
    72       */
    73      private Connector[] connectors;
    74
    75
    76      /**
    77       * List of other contexts to set up. Optional.
    78       * @parameter
    79       */
    80      private ContextHandler[] contextHandlers;
    81
    82
    83      /**
    84       * List of security realms to set up. Optional.
    85       * @parameter
    86       */
    87      private UserRealm[] userRealms;
    88
    89
    90
    91      /**
    92       * A RequestLog implementation to use for the webapp at runtime.
    93       * Optional.
    94       * @parameter
    95       */
    96      private RequestLog requestLog;
    97
    98
    99      public Object getConfiguredRequestLog()
    100     {
    101         return this.requestLog;
    102     }
    103
    104
    105
    106     /**
    107      *
    108      *
    109      * @see org.mortbay.jetty.plugin.AbstractJettyRunMojo#getConfiguredConnectors()
    110      */
    111     public Object[] getConfiguredConnectors()
    112     {
    113         return this.connectors;
    114     }
    115
    116
    117
    118     /**
    119      *
    120      *
    121      * @see org.mortbay.jetty.plugin.AbstractJettyRunMojo#getConfiguredUserRealms()
    122      */
    123     public Object[] getConfiguredUserRealms()
    124     {
    125         return this.userRealms;
    126     }
    127
    128
    129
    130     /**
    131      * @return Returns the contextHandlers.
    132      */
    133     public ContextHandler[] getConfiguredContextHandlers()
    134     {
    135         return this.contextHandlers;
    136     }
    137
    138
    139     /**
    140      *
    141      *
    142      * @see org.mortbay.jetty.plugin.AbstractJettyRunMojo#createServer()
    143      */
    144     public JettyPluginServer createServer()
    145     {
    146         return new Jetty6PluginServer();
    147     }
    148
    149
    150     public void finishConfigurationBeforeStart() throws Exception
    151     {
    152         Handler[] handlers = getConfiguredContextHandlers();
    153         JettyPluginServer plugin=getServer();
    154         Server server=(Server)plugin.getProxiedObject();
    155
    156         HandlerCollection contexts = (HandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class);
    157         if (contexts==null)
    158             contexts = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class);
    159
    160         for (int i=0; (handlers != null) && (i < handlers.length); i++)
    161         {
    162             contexts.addHandler(handlers[i]);
    163         }
    164     }
    165
    166
    167
    168
    169     public void applyJettyXml() throws Exception
    170     {
    171         if (getJettyXmlFile() == null)
    172             return;
    173
    174         getLog().info( "Configuring Jetty from xml configuration file = " + getJettyXmlFile() );
    175         XmlConfiguration xmlConfiguration = new XmlConfiguration(getJettyXmlFile().toURL());
    176         xmlConfiguration.configure(getServer().getProxiedObject());
    177     }
    178
    179
    180
    181
    182     public void execute() throws MojoExecutionException, MojoFailureException
    183     {
    184         super.execute();
    185     }
    186 }