001/*
002 * (C) Copyright 2006-2012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     tmartins
016 *
017 */
018package org.nuxeo.ecm.directory.ldap;
019
020import java.lang.reflect.InvocationHandler;
021import java.lang.reflect.InvocationTargetException;
022import java.lang.reflect.Method;
023import java.lang.reflect.Proxy;
024
025import javax.naming.ServiceUnavailableException;
026import javax.naming.directory.DirContext;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030
031/**
032 * Wrapper to encapsulate the calls to LDAP and retry the requests in case of ServiceUnavailableException errors
033 *
034 * @since 5.7
035 * @author Thierry Martins <tm@nuxeo.com>
036 */
037public class LdapRetryHandler implements InvocationHandler {
038
039    private static final Log log = LogFactory.getLog(LdapRetryHandler.class);
040
041    protected DirContext dirContext;
042
043    protected int attemptsNumber;
044
045    protected LdapRetryHandler(DirContext object, int attempts) {
046        dirContext = object;
047        attemptsNumber = attempts;
048    }
049
050    @Override
051    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
052        int attempts = attemptsNumber;
053        Throwable e = null;
054        while (attempts-- > 0) {
055            try {
056                return method.invoke(dirContext, args);
057            } catch (InvocationTargetException sue) {
058                e = sue.getTargetException();
059                if (!(e instanceof ServiceUnavailableException)) {
060                    throw sue.getTargetException();
061                } else {
062                    log.debug("Retrying ...", e);
063                }
064            }
065        }
066        throw e;
067    }
068
069    public static DirContext wrap(DirContext dirContext, int retries) {
070        LdapRetryHandler handler = new LdapRetryHandler(dirContext, retries);
071        return (DirContext) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
072                new Class<?>[] { DirContext.class }, handler);
073    }
074}