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