



/*
 * TeamSpaceConfig.java
 *
 * Created on September 5, 2002, 11:20 AM
 */

package tspace.jmx.config;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.mail.internet.InternetAddress;

import org.apache.log4j.Category;



/**
 * This MBean load the TeamSpace configuration.
 * <br>
 * <b>IMPORTANT:</b> This MBean should be the 1st TeamSpace MBean to load since it initializes the system
 *
 * @author  Herve Tchepannou
 * @version $Revision$
 *
 * @jmx:mbean service=":service=TeamSpaceConfig"
 */
public class TeamSpaceConfig 
    implements TeamSpaceConfigMBean
{    
    private static final Category __logger = Category.getInstance( TeamSpaceConfig.class );
    
    public static final String MBEAN_NAME       = ":service=TeamSpaceConfig";   
    public static final String CONFIG_MAIL_FROM = "tspace.mail.from";
    public static final String DIR_FILES        = "/files";
    public static final String DIR_CONFIG       = "/etc";
    public static final String DIR_TEMPLATES    = DIR_CONFIG + "/templates";
    
    private String _homeDir;
    private String _filesDir;
    private String _configDir;
    private String _templatesDir;
    private String _mailFrom;
    
    
    /** 
     * Constructor
     *
     * @param home   TeamSpace home directory
     * 
     * @jmx:managed-constructor 
     */
    public TeamSpaceConfig( String home )
    {
        _homeDir = home;
    }
    
    
    
    /**
     * Initialize the MBean.
     * This function loads the TeamSpace configuration file.
     *
     * @throws Exception    if any error occurs
     *
     * @jmx:managed-operation
     */
    public void init() 
        throws Exception
    {
        __logger.info( "Initializing" );
        
        String          dir;
        FileInputStream in = null;
        try
        {
            /* Home directory */
            __logger.info( " directories " );
            __logger.info( "  home=" +  _homeDir );
        
            /* Files directory */
            _filesDir = fixPath( _homeDir + DIR_FILES );
            mkdir( _filesDir );
            __logger.info( "  files=" +  _filesDir );
        
            /* Config dir */
            _configDir = fixPath( _homeDir + DIR_CONFIG );
            mkdir( _homeDir );
            __logger.info( "  config=" +  _configDir );
            
            /* Template directory */
            _templatesDir = fixPath( _homeDir + DIR_TEMPLATES );
            mkdir( _templatesDir );
            __logger.info( "  templates=" +  _templatesDir );
            
            
            /* Load the TeamSpace configuration */
            File file = new File( _configDir, "tspace.properties" );
            __logger.info( " loading configuration from: " + file.getAbsolutePath() );
            in = new FileInputStream( file );
            Properties props = new Properties();
            props.load( in );
            
            _mailFrom = props.getProperty( CONFIG_MAIL_FROM );
            __logger.info( "  " + CONFIG_MAIL_FROM + "=" + _mailFrom );
            new InternetAddress( _mailFrom );   // Validate the address
            
            __logger.info( "Initialized" );
        }
        finally
        {
            if ( in != null )
            {
                in.close();
            }
        }
    }
    private String fixPath( String path )
    {
        path = path.replace( '/', File.separatorChar );
        return path;
    }
    private void mkdir( String path )
    {
        File file = new File( path );
        if ( !file.exists() )
        {
            __logger.info( "  creating directory: " + path );
            file.mkdirs();
        }
    }
    
    /** @jmx:managed-attribute */
    public String getHomeDir() throws Exception { return _homeDir; }
    
    /** @jmx:managed-attribute */
    public String getFilesDir() throws Exception { return _filesDir; }

    /** @jmx:managed-attribute */
    public String getConfigDir() throws Exception { return _configDir; }
    
    /** @jmx:managed-attribute */
    public String getTemplatesDir() throws Exception { return _templatesDir; }
    
    /** @jmx:managed-attribute */
    public String getMailFrom() throws Exception { return _mailFrom; }
}