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.BaseDirectoryDescriptor; 033import org.nuxeo.ecm.directory.DirectoryException; 034import org.nuxeo.ecm.directory.Session; 035import org.nuxeo.ecm.directory.api.DirectoryService; 036import org.nuxeo.ecm.directory.ldap.LDAPDirectory; 037import org.nuxeo.ecm.directory.ldap.LDAPDirectoryDescriptor; 038import org.nuxeo.runtime.api.Framework; 039 040public class LDAPDirectoriesProbe implements Probe { 041 042 @Override 043 public ProbeStatus run() { 044 DirectoryService directoryService = Framework.getService(DirectoryService.class); 045 boolean success = true; 046 Map<String, String> infos = new HashMap<>(); 047 for (String id : directoryService.getDirectoryNames()) { 048 BaseDirectoryDescriptor descriptor = directoryService.getDirectoryDescriptor(id); 049 if (!(descriptor instanceof LDAPDirectoryDescriptor)) { 050 continue; 051 } 052 Directory dir = directoryService.getDirectory(id); 053 long startTime = Calendar.getInstance().getTimeInMillis(); 054 String dirName = null; 055 try { 056 Session dirSession = dir.getSession(); 057 dirSession.close(); 058 dirName = dir.getName(); 059 } catch (DirectoryException e) { 060 success = false; 061 } 062 long endTime = Calendar.getInstance().getTimeInMillis(); 063 Properties props = ((LDAPDirectory) dir).getContextProperties(); 064 String bindDN = (String) props.get(Context.SECURITY_PRINCIPAL); 065 066 infos.put(dirName + "-bind", bindDN); 067 infos.put(dirName + "-time", new Long(endTime - startTime).toString()); 068 } 069 if (infos.size() == 0) { 070 infos.put("info", "No configured LDAP directory"); 071 } 072 if (!success) { 073 return ProbeStatus.newFailure(infos); 074 } 075 return ProbeStatus.newSuccess(infos); 076 } 077 078}