package org.dbxml.core.server.services;

/*
 * 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: dbXMLService.java,v 1.10 2001/06/13 19:54:42 tom Exp $
 */

import org.dbxml.server.*;
import org.dbxml.core.Database;

/**
 * dbXMLService controls the dbXML server instance.
 *
 * TODO: We need to properly handle the control states for services.
 */
public class dbXMLService extends SimpleConfigurable
      implements Service, KernelAccess {
   private static final String ROOT_COLLECTION = "root-collection";
   private static final String NAME = "name";

   protected Kernel kernel;
   protected Configuration config;
   protected String serviceName;

   protected Database db;

   // Service status variables - these aren't used currently.
   protected int status;
   protected String statusMessage;

   public void setConfig(Configuration config) throws dbXMLException {
      try {
         this.config = config.getChild(ROOT_COLLECTION);
         serviceName = this.config.getAttribute(NAME);
      } catch (Exception e) {
         org.dbxml.Debug.println(this, e);
      }
   }

   public synchronized int initialize() {
      try {
         System.out.println("Database: '" + serviceName + "' initializing");

         /*
          * Setup the Database instance. This is the key to the dbXML system
          */
         db = new Database();
         db.setKernel(kernel);
         db.setConfig(config);
      }
      catch (Exception e) {
         org.dbxml.Debug.printStackTrace(e);
      }

      return RESULT_OK;
   }

   /**
    * Returns the Database instance for the currently running server.
    *
    * @return the Database instance.
    */
   public Database getDatabase() {
      return db;
   }

   public void setKernel(Kernel kernel) {
      this.kernel = kernel;
   }

   /**
    * status returns the current operational status of this Service.  This
    * value can either be STATE_STOPPED, STATE_STARTED, or STATE_SUSPENDED.
    *
    * @return The Service's status
    */
    public int status() {
        return status;
    }

   /**
    * statusMessage returns a textual message about the Service's
    * operational state.
    *
    * @return The Service's status as a text message
    */
    public String statusMessage() {
        return statusMessage;
    }

   /**
    * start will attempt to start the Service.  Depending on whether the
    * Service was happy about being started or not, a value of RESULT_OK
    * should be returned.
    *
    * @return The result of the start attempt
    */
    public int start() {
       // Nothing really to do here yet.
       return RESULT_OK;
    }

   /**
    * stop will attempt to stop the Service.  Depending on whether the
    * Service was happy about being stopped or not, a value of RESULT_OK
    * should be returned.
    *
    * @return The result of the stop attempt
    */
    public int stop() {
        return 0;
    }

   /**
    * suspend suspends execution of the Service.  It basically puts it into
    * a paused state until it is resumed or started again.  Depending on
    * whether the Service was happy about being suspended or not, a value of
    * RESULT_OK should be returned.
    *
    * @return The result of the suspend attempt
    */
    public int suspend() {
        return 0;
    }

   /**
    * resume resumes exection of a suspended Service.  It will also start a
    * stopped Service.  Depending on whether the Service was happy about
    * being resumed or not, a value of RESULT_OK should be returned.
    *
    * @return The result of the resume attempt
    */
    public int resume() {
        return 0;
    }

   /**
    * uninitialize will uninitialize the Service based on its current
    * configuration and will clean up any existing resources for that
    * configuration.  RESULT_INVALID will be returned if the Service's
    * configuration is invalid.
    *
    * @return The uninitialization status
    */
    public int uninitialize() {
        return 0;
    }

     /**
     * getName retrieves the contextually important name of the object
     *
     * @return The object's name
     */
     public String getName() {
         return serviceName;
     }
}
