/**
 * Copyright (c) 2000/2001 Thomas Kopp
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS 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 AUTHOR OR 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: SSLProperties.java,v 1.1 2003/02/04 17:54:04 ylafon Exp $

package org.w3c.jigsaw.https.socket;

import org.w3c.jigsaw.http.httpd;

import org.w3c.tools.resources.Attribute;
import org.w3c.tools.resources.StringAttribute;
import org.w3c.tools.resources.FileAttribute;
import org.w3c.tools.resources.AttributeRegistry;

import org.w3c.jigsaw.config.PropertySet;

/**
 * @author Thomas Kopp, Dialogika GmbH
 * @version 1.0.2, 27 December 2000
 * 
 * This class supplies a management resource for SSL properties
 */
public class SSLProperties extends PropertySet {
    
    /**
     * property key for specifying the TLS security provider
     */
    public static final String SECURITY_PROVIDER_P = 
                                        "org.w3c.jigsaw.ssl.security.provider";

    /**
     * property key for specifying the keystore path
     */
    public static final String KEYSTORE_PATH_P = 
                                            "org.w3c.jigsaw.ssl.keystore.path";

    /**
     * property key for specifying the keystore password
     */
    public static final String KEYSTORE_PASSWORD_P = 
                                        "org.w3c.jigsaw.ssl.keystore.password";

    /**
     * property key for specifying the truststore path 
     * (future usage for client mode)
     */
    public static final String TRUSTSTORE_PATH_P = 
                                          "org.w3c.jigsaw.ssl.truststore.path";

    /**
     * property key for specifying the truststore password 
     * (future usage for client mode)
     */
    public static final String TRUSTSTORE_PASSWORD_P = 
                                      "org.w3c.jigsaw.ssl.truststore.password";

    /**
     * default security provider for TLS support
     */
    public static final String DEFAULT_SECURITY_PROVIDER = 
                                       "com.sun.net.ssl.internal.ssl.Provider";
    
    /**
     * default properties name
     */
    public static final String DEFAULT_NAME = "SSL";
  

    /**
     * security provider attribute index
     */
    protected static int ATTR_SSL_SECURITY_PROVIDER = -1 ;

    /**
     * keystore path attribute index
     */
    protected static int ATTR_SSL_KEYSTORE_PATH = -1 ;

    /**
     * keystore password attribute index
     */
    protected static int ATTR_SSL_KEYSTORE_PASSWORD = -1 ;

    /**
     * keystore path attribute index (future usage for client mode)
     */
    protected static int ATTR_SSL_TRUSTSTORE_PATH = -1 ;

    /**
     * keystore password attribute index (future usage for client mode)
     */
    protected static int ATTR_SSL_TRUSTSTORE_PASSWORD = -1 ;

    /**
     * static initializer for TLS properties
     */
    static {
	Class cls = null;
	Attribute a = null;

	try {
	    cls = Class.forName("org.w3c.jigsaw.https.socket.SSLProperties");
	} 
	catch (Exception ex) {
	    ex.printStackTrace();
	    System.exit(1);
	}
        
	a = new StringAttribute(SECURITY_PROVIDER_P, 
				DEFAULT_SECURITY_PROVIDER, Attribute.EDITABLE);
	ATTR_SSL_SECURITY_PROVIDER = 
	    AttributeRegistry.registerAttribute(cls, a);
        
	a = new FileAttribute(KEYSTORE_PATH_P, null, Attribute.EDITABLE);
	ATTR_SSL_KEYSTORE_PATH = AttributeRegistry.registerAttribute(cls, a);
        
	a = new StringAttribute(KEYSTORE_PASSWORD_P, null, Attribute.EDITABLE);
	ATTR_SSL_KEYSTORE_PASSWORD = 
	    AttributeRegistry.registerAttribute(cls, a);

    /*        
        a = new FileAttribute(TRUSTSTORE_PATH_P, null, Attribute.EDITABLE);
	ATTR_SSL_TRUSTSTORE_PATH = AttributeRegistry.registerAttribute(cls, a);
        
	a = new StringAttribute(TRUSTSTORE_PASSWORD_P, null, 
	                        Attribute.EDITABLE);
        ATTR_SSL_TRUSTSTORE_PASSWORD = 
            AttributeRegistry.registerAttribute(cls, a);
    */
    }

    /**
     * method for supplying the current security provider
     * @return SSL security provider
     */
    public String getSecurityProvider() {
        return ((String)(getValue(ATTR_SSL_SECURITY_PROVIDER, 
				  DEFAULT_SECURITY_PROVIDER)));
    }
    
    /**
     * nameless constructor for SSL properties
     * @param server reference to the current daemon
     */
    public SSLProperties(httpd server) {
	super(DEFAULT_NAME, server);
    }

    /**
     * named constructor for SSL properties
     * @param name the name for the daemon in question
     * @param server reference to the current daemon
     */
    public SSLProperties(String name, httpd server) {
        super(name, server);
    }
}
