* @see javax.jcr.Repository#login(javax.jcr.Credentials, java.lang.String)
*/
public synchronized Session login( Credentials credentials,
String workspaceName ) throws RepositoryException {
// Ensure credentials are either null or provide a JAAS method
ExecutionContext execContext;
if (credentials == null) {
execContext = executionContextFactory.create();
} else {
try {
// Check if credentials provide a login context
try {
Method method = credentials.getClass().getMethod("getLoginContext");
if (method.getReturnType() != LoginContext.class) {
throw new IllegalArgumentException(JcrI18n.credentialsMustReturnLoginContext.text(credentials.getClass()));
}
LoginContext loginContext = (LoginContext)method.invoke(credentials);
if (loginContext == null) {
throw new IllegalArgumentException(JcrI18n.credentialsMustReturnLoginContext.text(credentials.getClass()));
}
execContext = executionContextFactory.create(loginContext);
} catch (NoSuchMethodException error) {
// Check if credentials provide an access control context
try {
Method method = credentials.getClass().getMethod("getAccessControlContext");
if (method.getReturnType() != AccessControlContext.class) {
throw new IllegalArgumentException(
JcrI18n.credentialsMustReturnAccessControlContext.text(credentials.getClass()));
}
AccessControlContext accessControlContext = (AccessControlContext)method.invoke(credentials);
if (accessControlContext == null) {
throw new IllegalArgumentException(
JcrI18n.credentialsMustReturnAccessControlContext.text(credentials.getClass()));
}
execContext = executionContextFactory.create(accessControlContext);
} catch (NoSuchMethodException error2) {
throw new IllegalArgumentException(JcrI18n.credentialsMustProvideJaasMethod.text(credentials.getClass()),
error2);
}
}
} catch (RuntimeException error) {
throw error;
} catch (Exception error) {
throw new RepositoryException(error);
}
}
// Authenticate if possible
assert execContext != null;
LoginContext loginContext = execContext.getLoginContext();
if (loginContext != null) {
try {
loginContext.login();
} catch (javax.security.auth.login.LoginException error) {
throw new LoginException(error);