*/
static Group[] getRoleSets(String username, String dsJndiName,
String rolesQuery, AbstractServerLoginModule aslm, boolean suspendResume)
throws LoginException
{
Logger log = aslm.log;
boolean trace = log.isTraceEnabled();
Connection conn = null;
HashMap<String,Group> setsMap = new HashMap<String,Group>();
PreparedStatement ps = null;
ResultSet rs = null;
TransactionManager tm = null;
if(suspendResume)
{
TransactionManagerLocator tml = new TransactionManagerLocator();
try
{
tm = tml.getTM("java:/TransactionManager");
}
catch (NamingException e1)
{
throw new RuntimeException(e1);
}
if(tm == null)
throw new IllegalStateException("Transaction Manager is null");
}
Transaction tx = null;
if (suspendResume)
{
// tx = TransactionDemarcationSupport.suspendAnyTransaction();
try
{
tx = tm.suspend();
}
catch (SystemException e)
{
throw new RuntimeException(e);
}
if( trace )
log.trace("suspendAnyTransaction");
}
try
{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(dsJndiName);
conn = ds.getConnection();
// Get the user role names
if (trace)
log.trace("Excuting query: "+rolesQuery+", with username: "+username);
ps = conn.prepareStatement(rolesQuery);
try
{
ps.setString(1, username);
}
catch(ArrayIndexOutOfBoundsException ignore)
{
// The query may not have any parameters so just try it
}
rs = ps.executeQuery();
if( rs.next() == false )
{
if( trace )
log.trace("No roles found");
if( aslm.getUnauthenticatedIdentity() == null )
throw new FailedLoginException("No matching username found in Roles");
/* We are running with an unauthenticatedIdentity so create an
empty Roles set and return.
*/
Group[] roleSets = { new SimpleGroup("Roles") };
return roleSets;
}
do
{
String name = rs.getString(1);
String groupName = rs.getString(2);
if( groupName == null || groupName.length() == 0 )
groupName = "Roles";
Group group = (Group) setsMap.get(groupName);
if( group == null )
{
group = new SimpleGroup(groupName);
setsMap.put(groupName, group);
}
try
{
Principal p = aslm.createIdentity(name);
if( trace )
log.trace("Assign user to role " + name);
group.addMember(p);
}
catch(Exception e)
{
log.debug("Failed to create principal: "+name, e);
}
} while( rs.next() );
}
catch(NamingException ex)
{
LoginException le = new LoginException("Error looking up DataSource from: "+dsJndiName);
le.initCause(ex);
throw le;
}
catch(SQLException ex)
{
LoginException le = new LoginException("Query failed");
le.initCause(ex);
throw le;
}
finally
{
if( rs != null )
{
try
{
rs.close();
}
catch(SQLException e)
{}
}
if( ps != null )
{
try
{
ps.close();
}
catch(SQLException e)
{}
}
if( conn != null )
{
try
{
conn.close();
}
catch (Exception ex)
{}
}
if (suspendResume)
{
//TransactionDemarcationSupport.resumeAnyTransaction(tx);
try
{
tm.resume(tx);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
if( trace )
log.trace("resumeAnyTransaction");
}
}
Group[] roleSets = new Group[setsMap.size()];
setsMap.values().toArray(roleSets);