001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.directory.ldap.management;
021
022import java.util.Calendar;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.Properties;
026
027import javax.naming.Context;
028
029import org.nuxeo.ecm.core.management.api.Probe;
030import org.nuxeo.ecm.core.management.api.ProbeStatus;
031import org.nuxeo.ecm.directory.Directory;
032import org.nuxeo.ecm.directory.DirectoryException;
033import org.nuxeo.ecm.directory.Session;
034import org.nuxeo.ecm.directory.ldap.LDAPDirectory;
035import org.nuxeo.ecm.directory.ldap.LDAPDirectoryFactory;
036import org.nuxeo.runtime.api.Framework;
037
038public class LDAPDirectoriesProbe implements Probe {
039
040    protected LDAPDirectoryFactory factory;
041
042    @Override
043    public ProbeStatus run() {
044
045        factory = (LDAPDirectoryFactory) Framework.getRuntime().getComponent(LDAPDirectoryFactory.NAME);
046        boolean success = true;
047        Map<String, String> infos = new HashMap<String, String>();
048        for (Directory dir : factory.getDirectories()) {
049            long startTime = Calendar.getInstance().getTimeInMillis();
050            String dirName = null;
051            try {
052                Session dirSession = dir.getSession();
053                dirSession.close();
054                dirName = dir.getName();
055            } catch (DirectoryException e) {
056                success = false;
057            }
058            long endTime = Calendar.getInstance().getTimeInMillis();
059            Properties props = ((LDAPDirectory) dir).getContextProperties();
060            String bindDN = (String) props.get(Context.SECURITY_PRINCIPAL);
061
062            infos.put(dirName + "-bind", bindDN);
063            infos.put(dirName + "-time", new Long(endTime - startTime).toString());
064        }
065        if (infos.size() == 0) {
066            infos.put("info", "No configured LDAP directory");
067        }
068        if (!success) {
069            return ProbeStatus.newFailure(infos);
070        }
071        return ProbeStatus.newSuccess(infos);
072    }
073
074}