<commit_before>/**
 * Copyright (C) 2003, Intalio Inc.
 *
 * The program(s) herein may be used and/or copied only with the
 * written permission of Intalio Inc. or in accordance with the terms
 * and conditions stipulated in the agreement/contract under which the
 * program(s) have been supplied.
 */

package org.intalio.tempo.security.simple;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.intalio.tempo.security.authentication.AuthenticationAdmin;
import org.intalio.tempo.security.authentication.AuthenticationException;
import org.intalio.tempo.security.authentication.AuthenticationQuery;
import org.intalio.tempo.security.authentication.AuthenticationRuntime;
import org.intalio.tempo.security.authentication.provider.AuthenticationProvider;
import org.intalio.tempo.security.provider.SecurityProvider;
import org.intalio.tempo.security.rbac.RBACAdmin;
import org.intalio.tempo.security.rbac.RBACException;
import org.intalio.tempo.security.rbac.RBACQuery;
import org.intalio.tempo.security.rbac.RBACRuntime;
import org.intalio.tempo.security.rbac.provider.RBACProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.SystemPropertyUtils;

/**
 * Simple Security provider
 *
 * @author <a href="<EMAIL>"><NAME></a>
 */
public final class SimpleSecurityProvider
    implements SecurityProvider
{

    /**
     * RBAC providers: Map of { String, RBACProvider }.
     */
    private HashMap<String,RBACProvider> _rbacMap;

    /**
     * Authentication providders: Map of { String, AuthenticationProvider }.
     */
    private HashMap<String,AuthenticationProvider> _authMap;
    

    
    /** 
     * Package-accessible database of realms, users, roles and permissions.
     */
    private SimpleDatabase _database;
    
    
    /**
     * Configuration
     */
    private Map<String, Object> _config;


	/**
	 * Name
	 */    
	private String _name = "simple";


	/**
	 * Filename of user, role, permissions database
	 */	
	private String _filename;
	
    
	/**
	 * Time when we last checked for modified database.
	 */	
	private long _databaseLastCheck;


	/**
	 * Time when database was last modified.
	 */	
	private long _databaseLastModified;
	
	
	/**
	 * Amount of time between checks of modified database (in milliseconds).
	 */
	private long _databaseCheckPeriod = 5000L;


    /**
     * Log4J logger
     */
	static final Logger LOG = LoggerFactory.getLogger( "tempo.security.simple" );


    /**
     * Public no-arg constructor.
     */
    public SimpleSecurityProvider()
    {
        // empty
    }
    
        
    // implement SecurityProvider interface
    public void initialize( Object config )
        throws AuthenticationException, RBACException
    {
    	String period;

		try {		
        	_config = (Map<String, Object>) config;
		} catch ( Exception except ) {
			throw new RBACException( "Configuration object must be a 'java.util.Map'" );			
		}

        _filename = (String) _config.get( "configFile" );
        checkFilename();
		
		period = (String) _config.get( "refreshInterval" );
		if ( period != null ) {
            setRefreshInterval( Integer.parseInt( period ) );
		}

		reloadDatabase();
    }

    // Spring init method
    public void init()
        throws AuthenticationException, RBACException
    {
        checkFilename();
        
        reloadDatabase();
    }
    
    private void checkFilename()
    {
        if ( _filename == null ) {
            throw new IllegalStateException( "Missing configuration property 'configFile'");
        }
    }
    
    public void setConfigFile( String filename )
    {
        _filename = filename;
    }
    
    public void setRefreshInterval( int period )
    {
        _databaseCheckPeriod = period * 1000L;
        if ( _databaseCheckPeriod < 1000L ) {
            _databaseCheckPeriod = 1000L;
        }
    }
    

    private InputStream getConfigStream()
        throws IOException
    {
        File file = new File( SystemPropertyUtils.resolvePlaceholders(_filename) );
        if ( !file.exists() ) {
            ClassPathResource resource = new ClassPathResource( _filename );
            return resource.getInputStream();
        }
        return new FileInputStream( _filename );
    }
    
    // implement SecurityProvider interface
    public String getName()
    {
        return _name;
    }
    
    
	// implement SecurityProvider interface
	public void setName( String name ) {
		_name = name;
	}


    // implement SecurityProvider interface
    public String[] getRealms()
        throws AuthenticationException, RBACException
    {
        return _authMap.keySet().toArray( new String[ _authMap.size() ] );
    }

    
    // implement SecurityProvider interface
    public RBACProvider getRBACProvider( String realm )
        throws RBACException
    {<commit_msg>Fix bug where property placeholders are not resolved correctly<commit_after>/**
 * Copyright (C) 2003, Intalio Inc.
 *
 * The program(s) herein may be used and/or copied only with the
 * written permission of Intalio Inc. or in accordance with the terms
 * and conditions stipulated in the agreement/contract under which the
 * program(s) have been supplied.
 */

package org.intalio.tempo.security.simple;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.intalio.tempo.security.authentication.AuthenticationAdmin;
import org.intalio.tempo.security.authentication.AuthenticationException;
import org.intalio.tempo.security.authentication.AuthenticationQuery;
import org.intalio.tempo.security.authentication.AuthenticationRuntime;
import org.intalio.tempo.security.authentication.provider.AuthenticationProvider;
import org.intalio.tempo.security.provider.SecurityProvider;
import org.intalio.tempo.security.rbac.RBACAdmin;
import org.intalio.tempo.security.rbac.RBACException;
import org.intalio.tempo.security.rbac.RBACQuery;
import org.intalio.tempo.security.rbac.RBACRuntime;
import org.intalio.tempo.security.rbac.provider.RBACProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.SystemPropertyUtils;

/**
 * Simple Security provider
 *
 * @author <a href="<EMAIL>"><NAME></a>
 */
public final class SimpleSecurityProvider
    implements SecurityProvider
{

    /**
     * RBAC providers: Map of { String, RBACProvider }.
     */
    private HashMap<String,RBACProvider> _rbacMap;

    /**
     * Authentication providders: Map of { String, AuthenticationProvider }.
     */
    private HashMap<String,AuthenticationProvider> _authMap;
    

    
    /** 
     * Package-accessible database of realms, users, roles and permissions.
     */
    private SimpleDatabase _database;
    
    
    /**
     * Configuration
     */
    private Map<String, Object> _config;


	/**
	 * Name
	 */    
	private String _name = "simple";


	/**
	 * Filename of user, role, permissions database
	 */	
	private String _filename;
	
    
	/**
	 * Time when we last checked for modified database.
	 */	
	private long _databaseLastCheck;


	/**
	 * Time when database was last modified.
	 */	
	private long _databaseLastModified;
	
	
	/**
	 * Amount of time between checks of modified database (in milliseconds).
	 */
	private long _databaseCheckPeriod = 5000L;


    /**
     * Log4J logger
     */
	static final Logger LOG = LoggerFactory.getLogger( "tempo.security.simple" );


    /**
     * Public no-arg constructor.
     */
    public SimpleSecurityProvider()
    {
        // empty
    }
    
        
    // implement SecurityProvider interface
    public void initialize( Object config )
        throws AuthenticationException, RBACException
    {
    	String period;

		try {		
        	_config = (Map<String, Object>) config;
		} catch ( Exception except ) {
			throw new RBACException( "Configuration object must be a 'java.util.Map'" );			
		}

        _filename = (String) _config.get( "configFile" );
        checkFilename();
		
		period = (String) _config.get( "refreshInterval" );
		if ( period != null ) {
            setRefreshInterval( Integer.parseInt( period ) );
		}

		reloadDatabase();
    }

    // Spring init method
    public void init()
        throws AuthenticationException, RBACException
    {
        checkFilename();
        
        reloadDatabase();
    }
    
    private void checkFilename()
    {
        if ( _filename == null ) {
            throw new IllegalStateException( "Missing configuration property 'configFile'");
        }
    }
    
    public void setConfigFile( String filename )
    {
        _filename = filename;
    }
    
    public void setRefreshInterval( int period )
    {
        _databaseCheckPeriod = period * 1000L;
        if ( _databaseCheckPeriod < 1000L ) {
            _databaseCheckPeriod = 1000L;
        }
    }
    

    private InputStream getConfigStream()
        throws IOException
    {
        String filename = SystemPropertyUtils.resolvePlaceholders(_filename);
        File file = new File(filename);
        if ( !file.exists() ) {
            ClassPathResource resource = new ClassPathResource( filename );
            return resource.getInputStream();
        }
        return new FileInputStream( filename );
    }
    
    // implement SecurityProvider interface
    public String getName()
    {
        return _name;
    }
    
    
	// implement SecurityProvider interface
	public void setName( String name ) {
		_name = name;
	}


    // implement SecurityProvider interface
    public String[] getRealms()
        throws AuthenticationException, RBACException
    {
        return _authMap.keySet().toArray( new String[ _authMap.size() ] );
    }

    
    // implement SecurityProvider interface
    public RBACProvider getRBACProvider( String realm )
        throws RBACException
    {
{'fraction_non_alphanumeric': 0.09264574878748887, 'fraction_numerical': 0.004553103038701376, 'mean_word_length': 2.7942170484416073, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1797266', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2747, 'n_words': 706}