All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.anosite.guard.SystemEnvironmentAbstractGuard Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
package net.anotheria.anosite.guard;

import javax.servlet.http.HttpServletRequest;

import net.anotheria.anoplass.api.APIFinder;
import net.anotheria.anosite.api.configuration.SystemConfigurationAPI;
import net.anotheria.asg.data.DataObject;
import net.anotheria.asg.exception.ASGRuntimeException;

/**
 * Abstract class that provides common methods and String constants for 
 * all derived guards.
 * 
 * @author Alex Osadchy
 *
 */
public abstract class SystemEnvironmentAbstractGuard implements ConditionalGuard {

    /**
     * System configuration API.
     */
    private SystemConfigurationAPI systemConfigurationAPI;

    /**
     * Constructor.
     */
    public SystemEnvironmentAbstractGuard() {
        systemConfigurationAPI = APIFinder.findAPI(SystemConfigurationAPI.class);
    }
    
    /**
     * Override this method to provide name of target environment.
     * 
     * @return name of the environment
     */
    protected abstract String getTargetEnvironmentName();
    
    /**
     * Override this method in a way that it would return true if 
     * environments should match, false otherwise.
     * 
     * @return boolean value.
     */
    protected abstract boolean shouldMatch();

    /**
     * Returns true if the condition is fullfilled.
     */
    @Override
    public final boolean isConditionFullfilled(DataObject object, HttpServletRequest req) throws ASGRuntimeException {
       return getEnvironmentName().startsWith(getTargetEnvironmentName().toUpperCase()) == shouldMatch();
    }
    
    /**
     * Gets environment name from API.
     * 
     * @return {@link String} with environment name
     */
    private String getEnvironmentName() {
        return systemConfigurationAPI.getCurrentSystem();
    }  
   
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy