/* ===================================================================
 * FeatureSecurityInterceptor.java
 * 
 * Created Mar 10, 2005 7:49:07 PM
 * 
 * Copyright (c) 2005 Matt Magoffin (spamsqr@msqr.us)
 * 
 * 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: FeatureSecurityInterceptor.java 34 2009-05-04 03:35:56Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.aop;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;

import magoffin.matt.ieat.IllegalActionException;
import magoffin.matt.util.StringUtil;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.core.io.Resource;

/**
 * Base implementation for feature security interceptors that need 
 * access to the environment properties resource.
 * 
 * <p>The configurable properties of this class are:</p>
 * 
 * <dl>
 *   <dt>feature</dt>
 *   <dd>The feature to test for. Note that this property <em>must be 
 *   set prior to the <code>environment</code> property</em>.</dd>
 * 
 *   <dt>environment</dt>
 *   <dd>The environment Properties file resource.</dd>
 * 
 * </dl>
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 34 $ $Date: 2009-05-04 15:35:56 +1200 (Mon, 04 May 2009) $
 */
public class FeatureSecurityInterceptor implements MethodBeforeAdvice {
	
	/* Injected properties. */
	
	private String feature = null;

	/* Internal properties. */
	
	private boolean featureEnabled = false;

	/* (non-Javadoc)
	 * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
	 */
	public final void before(Method method, Object[] args, Object target)
			throws Throwable {
		if ( !featureEnabled ) {
			throw new IllegalActionException();
		}
	}
	
	/* Method injectors. */
	
	/**
	 * Set the environment property file resource.
	 * 
	 * <p>The {@link #setFeature(String)} method must have been 
	 * called prior to calling this method. Calling this 
	 * method initializes this instance for testing the 
	 * specified feature property for boolean truth.</p>
	 * 
	 * @param environment the property file resource
	 * @deprecated simply call {@link #setFeatureEnabled(boolean)} using property configurer
	 * as this method does not honor database-overridden property values
	 */
	@Deprecated
	public void setEnvironment(Resource environment) {
		Properties props = new Properties();
		try{
			props.load(environment.getInputStream());
		} catch ( IOException e ) {
			throw new RuntimeException("Unable to load environment properties",e);
		}
		featureEnabled = StringUtil.parseBoolean(
				props.getProperty(this.feature));
	}
	
	/**
	 * @return the feature
	 */
	public String getFeature() {
		return feature;
	}
	
	/**
	 * @param feature the feature to set
	 */
	public void setFeature(String feature) {
		this.feature = feature;
	}

	/**
	 * @return the featureEnabled
	 */
	public boolean isFeatureEnabled() {
		return featureEnabled;
	}

	/**
	 * @param featureEnabled the featureEnabled to set
	 */
	public void setFeatureEnabled(boolean featureEnabled) {
		this.featureEnabled = featureEnabled;
	}
	
}
