package org.dbxml.tools;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * 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 dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * 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 DBXML GROUP 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.
 *
 * $Id: DeploymentTask.java,v 1.16 2001/08/11 07:32:25 kstaken Exp $
 */

import org.dbxml.core.server.dbXML;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 *  Handles basic deployment of dbXML services using ant.
 *
 * @author     kstaken
 * @created    October 20, 2000
 */

public class DeploymentTask extends Task {
   private String collection = "test/ocs";
   private String db = "Database";
   private String home = "";
   private XMLAdminTools admin = null;

   /**
    * Constructor for creating a new DeploymentTask.
    */
   public DeploymentTask() {
      admin = new XMLAdminTools();
   }

   /**
    *  Sets the Name to use for the document or XMLObject. This method always
    *  sets both documentName and Name on the admin object
    *
    * @param  name  The new Name value
    */
   public void setName(String name) {
      admin.setDocumentName(name);
      admin.setName(name);
   }


   /**
    *  Sets the Home attribute of the DeploymentTask object
    *
    * @param  name  The new Home value
    */
   public void setHome(String name) {
      this.home = name;
   }


   /**
    *  Sets the Path attribute of the DeploymentTask object
    *
    * @param  name  The new Path value
    */
   public void setPath(String name) {
      admin.setFilePath(name);
   }


   /**
    *  Sets the Collection attribute of the DeploymentTask object
    *
    * @param  name  The new Collection value
    */
   public void setCollection(String name) {
      this.collection = name;
   }


   /**
    *  Sets the Database attribute of the DeploymentTask object
    *
    * @param  name  The new Database value
    */
   public void setDatabase(String name) {
      this.db = name;
   }


   /**
    *  Sets the Activity attribute of the DeploymentTask object
    *
    * @param  name  The new Activity value
    */
   public void setActivity(String name) {
      admin.setAction(name);
   }

   /**
    *  Sets the Implementation class attribute of the DeploymentTask object
    *
    * @param  name  The new Activity value
    */
   public void setClass(String name) {
      admin.setImplementClass(name);
   }

   /**
    *  Executes the task.
    *
    * @exception  BuildException  Description of Exception
    */
   public void execute() throws BuildException {
      if (this.home.equals("")) {
         throw new BuildException(
            "You must specify the home attribute for deployment");
      }

      // Set the home directory so the XMLAdmin object can find its configuration.
      System.getProperties().put( dbXML.PROP_DBXML_HOME, home );

      // Add the parameters that had defaults
      admin.setDatabaseServer(db);
      admin.setCollectionName(collection);

      // Run the task.
      try {
         admin.execute();
      }
      catch ( Exception e ) {
         e.printStackTrace();
         throw new BuildException("Error executing deployment task: "+e);
      }
   }

}

