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