/*
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
*/


package net.brutex.xservices.security;

import org.apache.logging.log4j.Logger;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.servlet.ServletContext;

import org.apache.catalina.core.ApplicationContext;
import org.apache.logging.log4j.LogManager;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.config.Ini;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.realm.text.TextConfigurationRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.Nameable;
import org.apache.shiro.web.env.IniWebEnvironment;
import org.apache.shiro.web.util.WebUtils;

// TODO: Auto-generated Javadoc
/* 
 * For later use. A Realm connects to a DS where Users/ Passes are defined
 * and allows Shiro to transparently work against different user/pass stores
 * (i.e. LDAP, Custom, etc.)
 *
 * @author Brian Rosenberger, bru(at)brutex.de
 *
 */
/**
 * The Class XServicesRealm.
 */
public class XServicesRealm extends IniRealm implements Nameable  {

	/** The logger. */
	private final Logger logger = LogManager.getLogger();
	
	/** The name. */
	private String name;
	
	/* (non-Javadoc)
	 * @see org.apache.shiro.realm.AuthorizingRealm#setName(java.lang.String)
	 */
	@Override
	public void setName(String name) {
		this.name = name;
	}

	
	/**
	 * Instantiates a new x services realm with default
	 * 'shiro.ini' in classpath and {@link net.brutex.xservices.security.PermissionResolver PermissionResolver}.
	 * 
	 */
	
	public XServicesRealm() {
		super();
		/* needs review */
		URI classesRootDir;
		try {
			classesRootDir = getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
			String dir = classesRootDir.toString().replaceAll("%20", " ");
			dir = dir.substring(0,  dir.lastIndexOf("WEB-INF"));
		this.setIni(Ini.fromResourcePath(dir+"/WEB-INF/shiro.ini"));
		} catch (URISyntaxException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		
		
		this.setPermissionResolver(new net.brutex.xservices.security.PermissionResolver());
	}


	
}
