Return-Path: X-Original-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AEA0410F4B for ; Mon, 29 Jul 2013 19:03:21 +0000 (UTC) Received: (qmail 8888 invoked by uid 500); 29 Jul 2013 19:03:21 -0000 Delivered-To: apmail-jackrabbit-oak-commits-archive@jackrabbit.apache.org Received: (qmail 8873 invoked by uid 500); 29 Jul 2013 19:03:21 -0000 Mailing-List: contact oak-commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-commits@jackrabbit.apache.org Received: (qmail 8865 invoked by uid 99); 29 Jul 2013 19:03:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Jul 2013 19:03:21 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Jul 2013 19:03:18 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 5DC8A23888E4; Mon, 29 Jul 2013 19:02:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1508182 - /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java Date: Mon, 29 Jul 2013 19:02:57 -0000 To: oak-commits@jackrabbit.apache.org From: mduerig@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130729190257.5DC8A23888E4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mduerig Date: Mon Jul 29 19:02:56 2013 New Revision: 1508182 URL: http://svn.apache.org/r1508182 Log: OAK-803: Backwards compatibility of long-lived sessions Evaluate refresh-interval attribute on SimpleCredentials and TokenCredentials Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java?rev=1508182&r1=1508181&r2=1508182&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java (original) +++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/RepositoryImpl.java Mon Jul 29 19:02:56 2013 @@ -18,7 +18,6 @@ package org.apache.jackrabbit.oak.jcr; import static com.google.common.base.Preconditions.checkNotNull; -import java.security.Principal; import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; @@ -27,15 +26,16 @@ import javax.jcr.Credentials; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; +import javax.jcr.SimpleCredentials; import javax.jcr.Value; import javax.security.auth.login.LoginException; +import org.apache.jackrabbit.api.security.authentication.token.TokenCredentials; import org.apache.jackrabbit.commons.SimpleValueFactory; import org.apache.jackrabbit.oak.api.ContentRepository; import org.apache.jackrabbit.oak.api.ContentSession; import org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate; import org.apache.jackrabbit.oak.spi.security.SecurityProvider; -import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal; import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,8 +50,18 @@ public class RepositoryImpl implements R */ private static final Logger log = LoggerFactory.getLogger(RepositoryImpl.class); - // TODO implement auto refresh configuration. See OAK-803, OAK-88 - private static final long AUTO_REFRESH_INTERVAL = TimeUnit.SECONDS.toMillis(1); + /** + * Name of the session attribute value determining the session refresh + * behaviour. + * + * @see SessionDelegate#SessionDelegate(ContentSession, long) + */ + public static final String REFRESH_INTERVAL = "refresh-interval"; + + /** + * Default value for {@link #REFRESH_INTERVAL}. + */ + private static final long DEFAULT_REFRESH_INTERVAL = TimeUnit.SECONDS.toMillis(1); private final Descriptors descriptors = new Descriptors(new SimpleValueFactory()); private final ContentRepository contentRepository; @@ -123,33 +133,58 @@ public class RepositoryImpl implements R return descriptors.isSingleValueDescriptor(key); } - /** - * @see javax.jcr.Repository#login(javax.jcr.Credentials, String) - */ - @Override - public Session login(@Nullable Credentials credentials, @Nullable String workspaceName) throws RepositoryException { + private Session login( + @Nullable Credentials credentials, @Nullable String workspaceName, + long refreshInterval) throws RepositoryException { try { - ContentSession contentSession = - contentRepository.login(credentials, workspaceName); - - // For better backwards compatibility admin sessions should always - // be on the latest revision: set refresh interval to 0. See OAK-803. - SessionContext context = new SessionContext( - this, whiteboard, new SessionDelegate( - contentSession, /* isAdmin(contentSession) ? 0 : */ AUTO_REFRESH_INTERVAL)); + ContentSession contentSession = contentRepository.login(credentials, workspaceName); + SessionContext context = new SessionContext(this, whiteboard, + new SessionDelegate(contentSession, refreshInterval)); return context.getSession(); } catch (LoginException e) { throw new javax.jcr.LoginException(e.getMessage(), e); } } - private static boolean isAdmin(ContentSession contentSession) { - for (Principal p : contentSession.getAuthInfo().getPrincipals()) { - if (p instanceof AdminPrincipal) { - return true; + private static long getRefreshInterval(Credentials credentials) { + if (credentials instanceof SimpleCredentials) { + Object refreshAttribute = ((SimpleCredentials) credentials) + .getAttribute(REFRESH_INTERVAL); + if (refreshAttribute instanceof Long) { + return (Long) refreshAttribute; + } else if (refreshAttribute instanceof Integer) { + return (Integer) refreshAttribute; + } else if (refreshAttribute instanceof String) { + return toLong((String) refreshAttribute); } + } else if (credentials instanceof TokenCredentials) { + String refreshAttribute = ((TokenCredentials) credentials) + .getAttribute(REFRESH_INTERVAL); + if (refreshAttribute != null) { + return toLong(refreshAttribute); + } + } + return DEFAULT_REFRESH_INTERVAL; + } + + private static long toLong(String longValue) { + try { + return Long.parseLong(longValue); + } catch (NumberFormatException e) { + log.warn("Invalid value '" + longValue + "' for " + REFRESH_INTERVAL + + ". Expected long. Defaulting to '" + DEFAULT_REFRESH_INTERVAL + + "' seconds .", e); + return DEFAULT_REFRESH_INTERVAL; } - return false; + } + + /** + * @see javax.jcr.Repository#login(javax.jcr.Credentials, String) + */ + @Override + public Session login(@Nullable Credentials credentials, @Nullable String workspaceName) + throws RepositoryException { + return login(credentials, workspaceName, getRefreshInterval(credentials)); } /**