mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

vharseko
18.22.2023 d9ef90b49705daa2223432cd073e9dff39ae3f1b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2008 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.server.tools;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.adapter.server3x.Converters;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.server.config.server.BackendCfg;
import org.forgerock.opendj.server.config.server.RootCfg;
import org.opends.server.api.LocalBackend;
import org.opends.server.config.ConfigurationHandler;
import org.opends.server.core.DirectoryServer;
import org.opends.server.core.ServerContext;
import org.opends.server.types.Attribute;
import org.opends.server.types.Entry;
 
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.config.ConfigConstants.*;
import static org.opends.server.util.StaticUtils.*;
 
/** This class provides utility functions for all backend client tools. */
public class BackendToolUtils
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  private static final int ERROR = 1;
  private static final int SUCCESS = 0;
 
  /**
   * Retrieves information about the backends defined in the Directory Server
   * configuration.
   *
   * @param backendList
   *          A list into which instantiated (but not initialized) backend
   *          instances will be placed.
   * @param entryList
   *          A list into which the config entries associated with the backends
   *          will be placed.
   * @param dnList
   *          A list into which the set of base DNs for each backend will be
   *          placed.
   * @return 0 if everything went fine. 1 if an error occurred.
   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public static int getBackends(final List<LocalBackend<?>> backendList, final List<BackendCfg> entryList,
      final List<List<DN>> dnList)
  {
    try
    {
      // Iterate through the immediate children, attempting to parse them as backends.
      ServerContext serverContext = DirectoryServer.getInstance().getServerContext();
      final RootCfg root = serverContext.getRootConfig();
      ConfigurationHandler configHandler = serverContext.getConfigurationHandler();
      final DN backendBaseDN = getBackendBaseDN();
      for (final DN childrenDn : configHandler.getChildren(backendBaseDN))
      {
        Entry configEntry = Converters.to(configHandler.getEntry(childrenDn));
        final String backendID = getBackendID(configEntry);
        final String backendClassName = getBackendClassName(configEntry);
        if (backendID == null || backendClassName == null)
        {
          continue;
        }
 
        final Class<?> backendClass = getBackendClass(backendClassName, configEntry);
        final LocalBackend backend;
        final BackendCfg cfg;
        try
        {
          backend = (LocalBackend) backendClass.newInstance();
          backend.setBackendID(backendID);
          cfg = root.getBackend(backendID);
          backend.configureBackend(cfg, serverContext);
        }
        catch (final Exception e)
        {
          logger.error(
              ERR_CANNOT_INSTANTIATE_BACKEND_CLASS, backendClassName, configEntry.getName(), getExceptionMessage(e));
          return ERROR;
        }
 
        backendList.add(backend);
        entryList.add(cfg);
        dnList.add(getBaseDNsForEntry(configEntry));
      }
 
      return SUCCESS;
    }
    catch (final Exception e)
    {
      // Error message has already been logged.
      return ERROR;
    }
  }
 
  private static List<DN> getBaseDNsForEntry(final Entry configEntry) throws Exception
  {
    try
    {
      Iterator<Attribute> attributes = configEntry.getAllAttributes(ATTR_BACKEND_BASE_DN).iterator();
      if (attributes.hasNext())
      {
        Attribute attribute = attributes.next();
        List<DN> dns = new ArrayList<>();
        for (ByteString byteString : attribute)
        {
          dns.add(DN.valueOf(byteString.toString()));
        }
        return dns;
      }
      logger.error(ERR_NO_BASES_FOR_BACKEND, configEntry.getName());
      return null;
    }
    catch (final Exception e)
    {
      logger.error(ERR_CANNOT_DETERMINE_BASES_FOR_BACKEND, configEntry.getName(), getExceptionMessage(e));
      throw e;
    }
  }
 
  private static Class<?> getBackendClass(String backendClassName, Entry configEntry) throws Exception
  {
    try
    {
      return Class.forName(backendClassName);
    }
    catch (final Exception e)
    {
      logger.error(ERR_CANNOT_LOAD_BACKEND_CLASS, backendClassName, configEntry.getName(), getExceptionMessage(e));
      throw e;
    }
  }
 
  private static String getBackendClassName(final Entry configEntry) throws Exception
  {
    try
    {
      return configEntry.parseAttribute(ATTR_BACKEND_CLASS).asString();
    }
    catch (final Exception e)
    {
      logger.error(ERR_CANNOT_DETERMINE_BACKEND_CLASS, configEntry.getName(), getExceptionMessage(e));
      throw e;
    }
  }
 
  private static String getBackendID(final Entry configEntry) throws Exception
  {
    try
    {
      return configEntry.parseAttribute(ATTR_BACKEND_ID).asString();
    }
    catch (final Exception e)
    {
      logger.error(ERR_CANNOT_DETERMINE_BACKEND_ID, configEntry.getName(), getExceptionMessage(e));
      throw e;
    }
  }
 
  private static DN getBackendBaseDN() throws Exception
  {
    try
    {
      return DN.valueOf(DN_BACKEND_BASE);
    }
    catch (final Exception e)
    {
      logger.error(ERR_CANNOT_DECODE_BACKEND_BASE_DN, DN_BACKEND_BASE, getExceptionMessage(e));
      throw e;
    }
  }
}