plynch      2002/07/07 16:50:36

  Added:       src/java/org/apache/maven/jelly/tags
                        ServerStartedCheckTag.java
               src/plugins/webserver apache-2.x.jelly plugin.jelly
                        plugin.properties
  Removed:     src/java/org/apache/maven/ant ServerStartedCheckTask.java
  Log:
  Adding the core guts of a new webserver plugin. Using this plugin users can install 
and control a webserver instance, without having to install the whole thing everytime. 
Currently gets past bootstrap...
  
  ServerStartedCheckTag - A jelly bean that checks a server's status and sets a 
property indicating the started status.
  
  apache-2.x.jelly - a xml external entity jelly script that will contain a control 
implementation for apache 2.x
  
  plugin.properties - the default properties for this plugin
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/ServerStartedCheckTag.java
  
  Index: ServerStartedCheckTag.java
  ===================================================================
  package org.apache.maven.jelly.tags;
  
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.net.MalformedURLException;
  import java.net.HttpURLConnection;
  import java.net.URLConnection;
  import java.net.URL;
  
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  import org.apache.maven.MavenUtils;
  import org.apache.maven.project.Project;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  
  /**
   * A simple tag that takes a URL and if there is a response from that URL
   * with a http status code less than 400 and greater than 0, sets a property
   * to true, else does not set the property. If the property is already set
   * before calling this tag and the URL can be reached, the property value
   * is overriden with the new value.
   *
   * Functionality borrowed from Ant 1.5's http condition.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Peter Lynch</a>
   * @version $id$
   */
  public class ServerStartedCheckTag
    extends TagSupport
  {
  
      /**
       * the name of the property to set to true if the url can be reached */
      private String propertyName = null;
  
      /**
       * the URL to check if the
       */
      private String URL  = null;
  
      /**
       * the status value that http errors begin at
       */
      private int errorsBeginAt=400;
  
      /**
       * Required property name to set to true if the url can be reached
       *
       * @param the property to set to true if the URL can be reached
       */
      public void setPropertyName(String propertyName)
      {
          this.propertyName = propertyName;
      }
  
      /**
       * Required URL to check to see if the server is running.
       *
       * @param The URL to check to see if a server is running
       */
      public void setURL(String URL)
      {
          this.URL = URL;
      }
  
      /**
       * Perform functionality provided by the tag.
       *
       * @param output the place to write output
       * @throws Exception when the URL property is malformed or a property is
       * missing
       *
       */
      public void doTag(XMLOutput output)
        throws Exception
      {
          // till I find a better way
          Log log = LogFactory.getLog(context.getClass());
  
          if (this.URL == null)
          {
            throw new MissingAttributeException("URL");
          }
          if (this.propertyName == null)
          {
            throw new MissingAttributeException("propertyName");
          }
  
          log.info("Checking for " + this.URL);
  
          try
          {
              // this code pretty much copied from Ant 1.5's Http condition
              URL url = new URL(this.URL);
              try
              {
                  URLConnection conn = url.openConnection();
                  if (conn instanceof HttpURLConnection)
                  {
                      HttpURLConnection http = (HttpURLConnection) conn;
                      int code = http.getResponseCode();
  
                      log.info("Result code for " + this.URL + " was " + code);
  
                      if (code > 0 && code < this.errorsBeginAt)
                      {
                          context.setVariable(this.propertyName, "true");
                      }
                  }
              }
              catch (java.io.IOException ioe)
              {
                  //log("The URL connection could not be established to " + this.URL, 
Project.MSG_VERBOSE);
              }
          }
          catch (MalformedURLException mue)
          {
              log.error("The URL you provided as a property to this "
              + "tag was malformed: " + this.URL);
              throw new MalformedURLException();
          }
          finally
          {
            getBody().run(context, output);
          }
      }
  }
  
  
  
  1.1                  jakarta-turbine-maven/src/plugins/webserver/apache-2.x.jelly
  
  Index: apache-2.x.jelly
  ===================================================================
  <?xml version="1.0"?>
  
  <!-- needs work :-) -->
  <goal name="install-apache-2.x" />
  
  <goal name="start-apache-2.x" />
  
  <goal name="stop-apache-2.x" />
  
  
  
  1.1                  jakarta-turbine-maven/src/plugins/webserver/plugin.jelly
  
  Index: plugin.jelly
  ===================================================================
  <?xml version="1.0"?>
  
  
  
  <!-- ================================================================== -->
  <!-- W E B S E R V E R  P L U G I N                                     -->
  <!-- ================================================================== -->
  <project xmlns:j="jelly:core" xmlns:log="jelly:log"
   xmlns:define="jelly:define" xmlns:server="serverLib">
  
    <!--==================================================================-->
    <!-- S U P P O R T E D  W E B S E R V E R S                           -->
    <!--==================================================================-->
    <!-- include the implmentation entities here -->
  
  
    <!--==================================================================-->
    <!-- B E G I N  C O R E  P R O C E S S I N G                          -->
    <!--==================================================================-->
  
    <!-- BOB - this causes the null pointer -->
    <define:taglib uri="serverLib">
      <define:jellybean name="server-started-check"
        className="org.apache.maven.jelly.tags.ServerStartedCheckTag"
        method="execute" />
    </define:taglib>
    <server:server-started-check propertyName="maven.webserver.started"
        URL="http://www.appiant.com"/>
  
  
    <!-- BOB -->
    <!-- This works as long as I define the task in maven-task-defs.properties -->
    <!--
    <taskdef name="server-started-check"
      classname="org.apache.maven.ant.ServerStartedCheckTask" />
    <server-started-check propertyName="maven.webserver.started"
        URL="${maven.webserver.url}"/>
        -->
  
    <available property="maven.webserver.installed"
      file="${maven.webserver.dir}" type="dir" />
  
    <j:set var="maven.webserver.fullname"
      value="${maven.webserver.name}-${maven-webserver.version}" />
  
    <!-- tell the user what Maven thinks about their webserver instance -->
    <log:info>
      <j:set var="maven.webserver.installed.msg">
        <j:choose>
          <j:when test="${maven.webserver.installed}">installed</j:when>
          <j:otherwise>not installed</j:otherwise>
        </j:choose>
      </j:set>
      <j:set var="maven.webserver.started.msg">
        <j:choose>
          <j:when test="${maven.webserver.installed}">started</j:when>
          <j:otherwise>not started</j:otherwise>
        </j:choose>
      </j:set>
      Maven has determined your <j:expr value="${maven.webserver.fullname}"/>
      webserver instance is <j:expr value="${maven.webserver.installed.msg}"/>
      in directory <j:expr value="${maven.webserver.dir}"/>
      and <j:expr value="${maven.webserver.started.msg}"/>
      on <j:expr value="${maven.webserver.url}"/>
    </log:info>
  
    <!--==================================================================-->
    <!-- I S T A L L                                                      -->
    <!--==================================================================-->
    <goal name="maven:webserver-install"
      description="Install a webserver instance.">
  
      <j:choose>
        <!-- not installed - simply install -->
        <j:when test="${!maven.webserver.installed}">
          <attainGoal name="install-${maven.webserver.fullname}" />
        </j:when>
        <!-- installed and stopped -->
        <j:when
          test="${maven.webserver.installed} and ${!maven.webserver.started}">
          <attainGoal name="maven:webserver-reinstall" />
        </j:when>
        <!-- installed and started -->
        <j:when test="${maven.webserver.installed} and ${!maven.webserver.started}">
          <attain>
            <attainGoal name="maven:webserver-stop" />
            <attainGoal name="maven:webserver-reinstall" />
            <attainGoal name="maven:webserver-start" />
          </attain>
        </j:when>
        <!-- one of those 'things' that should never happen -->
        <j:otherwise>
          <log:info>
            Could not determine the current status for your
            <j:expr value="${maven.webserver.name}"/>-<j:expr 
value="${maven-webserver.version}"/>
            instance.
          </log:info>
        </j:otherwise>
      </j:choose>
  
    </goal>
  
    <!--==================================================================-->
    <!-- R E I N S T A L L                                                -->
    <!--==================================================================-->
    <!-- the purpose of this target is to allow the user to be very       -->
    <!-- when using pre and post goals                                    -->
    <goal name="maven:webserver-reinstall"
      prereqs="install-${maven.webserver.fullname}" />
  
    <!--==================================================================-->
    <!-- S T O P                                                          -->
    <!--==================================================================-->
    <goal name="webserver-stop" description="Stop a started webserver">
  
      <j:choose>
        <j:when test="${maven.webserver.installed} and ${maven.webserver.started}">
          <attainGoal
            name="stop-${maven.webserver.fullname}" />
        </j:when>
        <j:otherwise>
          <j:if test="${!webserver-clean.called}">
            <log:info>
              The <j:expr value="${maven.webserver.fullname}"/>
              is already stopped!
            </log:info>
          </j:if>
        </j:otherwise>
      </j:choose>
  
    </goal>
  
  
    <!--==================================================================-->
    <!-- S T A R T                                                        -->
    <!--==================================================================-->
    <goal name="webserver-start"
      description="Start a webserver instance">
  
      <!-- property to remember what goal the user first called -->
      <property name="webserver-start.called" value="true" />
  
      <!-- must install before starting -->
      <j:if test="${!maven.webserver.installed}">
        <log:info>
          Installing the <j:expr value="${maven.webserver.fullname}"/>
          webserver instance before starting it..."/>
        </log:info>
        <attainGoal name="webserver-install" />
      </j:if>
  
      <j:choose>
        <j:when test="${maven.webserver.started}">
          <attain>
            <attainGoal name="maven:webserver-stop" />
            <log:info>
            Restarting the <j:expr value="${maven.webserver.fullname}"/>
            webserver instance ..."/>
            </log:info>
            <attainGoal name="maven:webserver-restart" />
          </attain>
        </j:when>
        <j:when test="${!maven.webserver.installed}">
          <log:info>
            Installing the <j:expr value="${maven.webserver.fullname}"/>
            webserver instance before starting it ..."/>
          </log:info>
          <attainGoal name="maven:webserver-install" />
          <attainGoal name="start-${maven.webserver.fullname}"/>
        </j:when>
      </j:choose>
  
    </goal>
  
    <!--==================================================================-->
    <!-- R E S T A R T                                                    -->
    <!--==================================================================-->
    <!-- the purpose of this target is to allow the user to be very       -->
    <!-- when using pre and post goals                                    -->
    <goal name="maven:webserver-restart"
      prereqs="start-${maven.webserver.name}-${maven-webserver.version}" />
  
  
    <!--==================================================================-->
    <!-- C L E A N                                                        -->
    <!--==================================================================-->
    <goal name="webserver-clean"
      description="Safely delete a webserver instance install">
  
      <j:if test="${maven.webserver.started}">
        <log:info>
          Stopping the <j:expr value="${maven.webserver.fullname}"/>
          webserver instance before deleting it..."/>
        </log:info>
        <attainGoal name="webserver-stop" />
      </j:if>
  
      <j:if test="${maven.webserver.installed}">
        <log:info>
          Deleting the <j:expr value="${maven.webserver.fullname}"/>
          webserver instance ..."/>
        </log:info>
        <delete dir="${maven.webserver.dir}"/>
      </j:if>
  
      <j:if test="${!maven.webserver.installed}">
        <log:info>
          The <j:expr value="${maven.webserver.fullname}"/>
          webserver instance is already cleaned!"/>
        </log:info>
      </j:if>
  
    </goal>
  
  </project>
  
  
  1.1                  jakarta-turbine-maven/src/plugins/webserver/plugin.properties
  
  Index: plugin.properties
  ===================================================================
  #############################################################################
  # A P P S E R V E R
  #----------------------------------------------------------------------------
  
  #
  # the place where the root install of the web server resides
  #
  maven.webserver.home=/apps/apache2
  
  #
  # a supported name of an application server instance to install
  #
  maven.webserver.name=apache
  
  #
  # version number of the application server
  #
  maven.webserver.version=2.x
  
  #
  # ports to access this server instance
  #
  maven.webserver.port.http=80
  maven.webserver.port.https=443
  maven.webserver.port.one=8090
  maven.webserver.port.two=8091
  maven.webserver.port.three=8092
  
  #
  # where to install the local instance of the server
  #
  maven.webserver.dir=${basedir}/server
  
  #
  # files in an 'includes' pattern to include when controlling the server
  # For example, if you have classes in your classpath you would like to
  # include when starting the server, you could use
  # maven.webserver.classpath=${java.class.path}
  # paths can be absolute or relative to ${maven.webserver.dir} as that is
  # where a jvm will be started. This is common for libraries that need sharing
  # across all webapps installed in the server
  #
  maven.webserver.classpath=
  
  #
  # The host name, used when configuring, defaults to localhost
  #
  maven.webserver.host=localhost
  
  #
  # The URL used to test the current running status of the webserver
  #
  
maven.webserver.url=http://${maven.webserver.host}:${maven.webserver.port.http}/index.html
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to