package com.dbxml.db.server.services;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: HTTPService.java,v 1.3 2004/03/09 00:12:08 bradford Exp $
 */

import com.dbxml.db.server.SimpleService;
import com.dbxml.db.server.dbXML;
import com.dbxml.labrador.http.HTTPServerBase;
import com.dbxml.labrador.http.NIOHTTPServer;
import com.dbxml.labrador.http.SecureHTTPServer;
import com.dbxml.labrador.http.StandardHTTPServer;
import com.dbxml.util.Configuration;
import com.dbxml.util.dbXMLException;

/**
 * HTTPService is a dbXML Service wrapper around the Labrador HTTP Server.
 * This means that the only type of content that can be retrieved is based
 * on the web services hub.
 */

public final class HTTPService extends SimpleService {
   private static final String HOST = "host";
   private static final String PORT = "port";
   private static final String SECURE = "secure";
   private static final String KEYFILE = "keyfile";
   private static final String KEYPASS = "keypass";
   private static final String TRUSTFILE = "trustfile";
   private static final String TRUSTPASS = "trustpass";
   private static final String CLIENTAUTH = "clientauth";
   private static final String BLOCKING = "blocking";

   private HTTPServerBase server;
   private boolean secure;
   private boolean blocking;

   public void setConfig(Configuration config) throws dbXMLException {
      super.setConfig(config);

      blocking = config.getBooleanAttribute(BLOCKING, false);

      secure = config.getBooleanAttribute(SECURE, false);
      if ( secure ) {
         SecureHTTPServer s = new SecureHTTPServer();
         s.setKeyFile(config.getAttribute(KEYFILE));
         s.setKeyPass(config.getAttribute(KEYPASS));
         s.setTrustFile(config.getAttribute(TRUSTFILE));
         s.setTrustPass(config.getAttribute(TRUSTPASS));
         s.setClientAuth(config.getBooleanAttribute(CLIENTAUTH));
         server = s;
      }
      else if ( blocking )
         server = new StandardHTTPServer();
      else
         server = new NIOHTTPServer();

      int defaultPort;
      if ( secure )
         defaultPort = dbXML.DEFAULT_SSL_PORT;
      else
         defaultPort = dbXML.DEFAULT_PORT;

      String host = config.getAttribute(HOST, dbXML.DEFAULT_HOST);
      int port = config.getIntAttribute(PORT, defaultPort);

      server.setHostName(host);
      server.setPort(port);
   }

   public int start() {
      server.start();
      String hostName = server.getHostName();
      StringBuffer sb = new StringBuffer();
      sb.append("started ");
      if ( hostName != null && hostName.length() > 0 )
         sb.append("on "+hostName+" ");
      if ( blocking )
         sb.append("(Blocking) ");
      sb.append("at ");
      if ( secure )
         sb.append("secure ");
      sb.append("port "+server.getPort());
      statusMsg = sb.toString();
      status = STATE_STARTED;
      return RESULT_OK;
   }

   public int stop() {
      server.shutdown();
      return super.stop();
   }
}
