001/*
002 * (C) Copyright 2010 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 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.directory.ldap.management;
019
020import java.util.Calendar;
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Properties;
024
025import javax.naming.Context;
026
027import org.nuxeo.ecm.core.management.api.Probe;
028import org.nuxeo.ecm.core.management.api.ProbeStatus;
029import org.nuxeo.ecm.directory.Directory;
030import org.nuxeo.ecm.directory.DirectoryException;
031import org.nuxeo.ecm.directory.Session;
032import org.nuxeo.ecm.directory.ldap.LDAPDirectory;
033import org.nuxeo.ecm.directory.ldap.LDAPDirectoryFactory;
034import org.nuxeo.runtime.api.Framework;
035
036public class LDAPDirectoriesProbe implements Probe {
037
038    protected LDAPDirectoryFactory factory;
039
040    @Override
041    public ProbeStatus run() {
042
043        factory = (LDAPDirectoryFactory) Framework.getRuntime().getComponent(LDAPDirectoryFactory.NAME);
044        boolean success = true;
045        Map<String, String> infos = new HashMap<String, String>();
046        for (Directory dir : factory.getDirectories()) {
047            long startTime = Calendar.getInstance().getTimeInMillis();
048            String dirName = null;
049            try {
050                Session dirSession = dir.getSession();
051                dirSession.close();
052                dirName = dir.getName();
053            } catch (DirectoryException e) {
054                success = false;
055            }
056            long endTime = Calendar.getInstance().getTimeInMillis();
057            Properties props = ((LDAPDirectory) dir).getContextProperties();
058            String bindDN = (String) props.get(Context.SECURITY_PRINCIPAL);
059
060            infos.put(dirName + "-bind", bindDN);
061            infos.put(dirName + "-time", new Long(endTime - startTime).toString());
062        }
063        if (infos.size() == 0) {
064            infos.put("info", "No configured LDAP directory");
065        }
066        if (!success) {
067            return ProbeStatus.newFailure(infos);
068        }
069        return ProbeStatus.newSuccess(infos);
070    }
071
072}