001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2008 Sun Microsystems, Inc. 025 * Portions Copyright 2013-2015 ForgeRock AS 026 */ 027package org.opends.server.tools; 028 029import java.util.List; 030 031import org.forgerock.i18n.slf4j.LocalizedLogger; 032import org.forgerock.opendj.config.server.ConfigException; 033import org.opends.server.admin.server.ServerManagementContext; 034import org.opends.server.admin.std.server.BackendCfg; 035import org.opends.server.admin.std.server.RootCfg; 036import org.opends.server.api.Backend; 037import org.opends.server.config.ConfigEntry; 038import org.opends.server.config.DNConfigAttribute; 039import org.opends.server.config.StringConfigAttribute; 040import org.opends.server.core.DirectoryServer; 041import org.opends.server.types.DN; 042import org.opends.server.types.DirectoryException; 043 044import static org.opends.messages.ConfigMessages.*; 045import static org.opends.messages.ToolMessages.*; 046import static org.opends.server.config.ConfigConstants.*; 047import static org.opends.server.util.StaticUtils.*; 048 049/** This class provides utility functions for all JE related client tools. */ 050public class BackendToolUtils 051{ 052 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 053 054 private static final int ERROR = 1; 055 private static final int SUCCESS = 0; 056 057 /** 058 * Retrieves information about the backends defined in the Directory Server 059 * configuration. 060 * 061 * @param backendList 062 * A list into which instantiated (but not initialized) backend 063 * instances will be placed. 064 * @param entryList 065 * A list into which the config entries associated with the backends 066 * will be placed. 067 * @param dnList 068 * A list into which the set of base DNs for each backend will be 069 * placed. 070 * @return 0 if everything went fine. 1 if an error occurred. 071 */ 072 @SuppressWarnings({ "unchecked", "rawtypes" }) 073 public static int getBackends(final List<Backend> backendList, final List<BackendCfg> entryList, 074 final List<List<DN>> dnList) 075 { 076 try 077 { 078 final DN backendBaseDN = getBackendBaseDN(); 079 final ConfigEntry baseEntry = getBaseEntry(backendBaseDN); 080 081 // Iterate through the immediate children, attempting to parse them as backends. 082 final RootCfg root = ServerManagementContext.getInstance().getRootConfiguration(); 083 for (final ConfigEntry configEntry : baseEntry.getChildren().values()) 084 { 085 final String backendID = getBackendID(configEntry); 086 final String backendClassName = getBackendClassName(configEntry); 087 if (backendID == null || backendClassName == null) 088 { 089 continue; 090 } 091 092 final Class<?> backendClass = getBackendClass(backendClassName, configEntry); 093 final Backend backend; 094 final BackendCfg cfg; 095 try 096 { 097 backend = (Backend) backendClass.newInstance(); 098 backend.setBackendID(backendID); 099 cfg = root.getBackend(backendID); 100 backend.configureBackend(cfg, DirectoryServer.getInstance().getServerContext()); 101 } 102 catch (final Exception e) 103 { 104 logger.error( 105 ERR_CANNOT_INSTANTIATE_BACKEND_CLASS, backendClassName, configEntry.getDN(), getExceptionMessage(e)); 106 return ERROR; 107 } 108 109 backendList.add(backend); 110 entryList.add(cfg); 111 dnList.add(getBaseDNsForEntry(configEntry)); 112 } 113 114 return SUCCESS; 115 } 116 catch (final Exception e) 117 { 118 // Error message has already been logged. 119 return ERROR; 120 } 121 } 122 123 private static List<DN> getBaseDNsForEntry(final ConfigEntry configEntry) throws Exception 124 { 125 try 126 { 127 final DNConfigAttribute baseDNStub = new DNConfigAttribute( 128 ATTR_BACKEND_BASE_DN, INFO_CONFIG_BACKEND_ATTR_DESCRIPTION_BASE_DNS.get(), true, true, true); 129 final DNConfigAttribute baseDNAttr = (DNConfigAttribute) configEntry.getConfigAttribute(baseDNStub); 130 if (baseDNAttr != null) 131 { 132 return baseDNAttr.activeValues(); 133 } 134 logger.error(ERR_NO_BASES_FOR_BACKEND, configEntry.getDN()); 135 return null; 136 } 137 catch (final Exception e) 138 { 139 logger.error(ERR_CANNOT_DETERMINE_BASES_FOR_BACKEND, configEntry.getDN(), getExceptionMessage(e)); 140 throw e; 141 } 142 } 143 144 private static Class<?> getBackendClass(String backendClassName, ConfigEntry configEntry) throws Exception 145 { 146 try 147 { 148 return Class.forName(backendClassName); 149 } 150 catch (final Exception e) 151 { 152 logger.error(ERR_CANNOT_LOAD_BACKEND_CLASS, backendClassName, configEntry.getDN(), getExceptionMessage(e)); 153 throw e; 154 } 155 } 156 157 private static String getBackendClassName(final ConfigEntry configEntry) throws Exception 158 { 159 try 160 { 161 final StringConfigAttribute classStub = new StringConfigAttribute( 162 ATTR_BACKEND_CLASS, INFO_CONFIG_BACKEND_ATTR_DESCRIPTION_CLASS.get(), true, false, false); 163 final StringConfigAttribute classAttr = (StringConfigAttribute) configEntry.getConfigAttribute(classStub); 164 return classAttr != null ? classAttr.activeValue() : null; 165 } 166 catch (final org.opends.server.config.ConfigException ce) 167 { 168 logger.error(ERR_CANNOT_DETERMINE_BACKEND_CLASS, configEntry.getDN(), ce.getMessage()); 169 throw ce; 170 } 171 catch (final Exception e) 172 { 173 logger.error(ERR_CANNOT_DETERMINE_BACKEND_CLASS, configEntry.getDN(), getExceptionMessage(e)); 174 throw e; 175 } 176 } 177 178 private static String getBackendID(final ConfigEntry configEntry) throws Exception 179 { 180 try 181 { 182 final StringConfigAttribute idStub = new StringConfigAttribute( 183 ATTR_BACKEND_ID, INFO_CONFIG_BACKEND_ATTR_DESCRIPTION_BACKEND_ID.get(), true, false, true); 184 final StringConfigAttribute idAttr = (StringConfigAttribute) configEntry.getConfigAttribute(idStub); 185 return idAttr != null ? idAttr.activeValue() : null; 186 } 187 catch (final org.opends.server.config.ConfigException ce) 188 { 189 logger.error(ERR_CANNOT_DETERMINE_BACKEND_ID, configEntry.getDN(), ce.getMessage()); 190 throw ce; 191 } 192 catch (final Exception e) 193 { 194 logger.error(ERR_CANNOT_DETERMINE_BACKEND_ID, configEntry.getDN(), getExceptionMessage(e)); 195 throw e; 196 } 197 } 198 199 private static ConfigEntry getBaseEntry(final DN backendBaseDN) throws Exception 200 { 201 try 202 { 203 return DirectoryServer.getConfigEntry(backendBaseDN); 204 } 205 catch (final ConfigException ce) 206 { 207 logger.error(ERR_CANNOT_RETRIEVE_BACKEND_BASE_ENTRY, DN_BACKEND_BASE, ce.getMessage()); 208 throw ce; 209 } 210 catch (final Exception e) 211 { 212 logger.error(ERR_CANNOT_RETRIEVE_BACKEND_BASE_ENTRY, DN_BACKEND_BASE, getExceptionMessage(e)); 213 throw e; 214 } 215 } 216 217 private static DN getBackendBaseDN() throws Exception 218 { 219 try 220 { 221 return DN.valueOf(DN_BACKEND_BASE); 222 } 223 catch (final DirectoryException de) 224 { 225 logger.error(ERR_CANNOT_DECODE_BACKEND_BASE_DN, DN_BACKEND_BASE, de.getMessageObject()); 226 throw de; 227 } 228 catch (final Exception e) 229 { 230 logger.error(ERR_CANNOT_DECODE_BACKEND_BASE_DN, DN_BACKEND_BASE, getExceptionMessage(e)); 231 throw e; 232 } 233 } 234 235}