Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* It specify what Tenant should be use when the hibernate session is created.
* @author jm
*/
public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {
Logger logger = Logger.getLogger(getClass());
@Override
public String resolveCurrentTenantIdentifier() {
String tenant = resolveTenantByHttpSession();
logger.trace("Tenant resolved: " + tenant);
return tenant;
}
/**
* Get tenantId in the session attribute KEY_TENANTID_SESSION
* @return TenantId on KEY_TENANTID_SESSION
*/
public String resolveTenantByHttpSession()
{
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//If session attribute exists returns tenantId saved on the session
if(attr != null){
HttpSession session = attr.getRequest().getSession(false); // true == allow create
if(session != null){
String tenant = (String) session.getAttribute(KEY_TENANTID_SESSION);
if(tenant != null){
return tenant;
}
}
}
//otherwise return default tenant
logger.trace("Tenant resolved in session is: " + DEFAULT_TENANTID);
return DEFAULT_TENANTID;
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.