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 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.guitools.controlpanel.util;
028
029import java.io.File;
030import java.net.InetAddress;
031import java.util.ArrayList;
032import java.util.Collections;
033import java.util.Comparator;
034import java.util.List;
035import java.util.Set;
036
037import org.forgerock.i18n.LocalizableMessage;
038import org.forgerock.i18n.slf4j.LocalizedLogger;
039import org.forgerock.opendj.config.server.ConfigException;
040import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
041import org.opends.guitools.controlpanel.datamodel.ConnectionHandlerDescriptor;
042import org.opends.guitools.controlpanel.datamodel.VLVSortOrder;
043import org.opends.guitools.controlpanel.task.OfflineUpdateException;
044import org.opends.server.admin.std.meta.AdministrationConnectorCfgDefn;
045import org.opends.server.core.DirectoryServer;
046import org.opends.server.tools.tasks.TaskEntry;
047import org.opends.server.types.DN;
048import org.opends.server.types.DirectoryEnvironmentConfig;
049import org.opends.server.types.DirectoryException;
050import org.opends.server.types.InitializationException;
051import org.opends.server.types.OpenDsException;
052import org.opends.server.types.Schema;
053
054import static org.opends.messages.AdminToolMessages.*;
055
056/**
057 * An abstract class providing some common interface for the class that read
058 * the configuration (and if the server is running, the monitoring information).
059 */
060public abstract class ConfigReader
061{
062  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
063  /**
064   * The class used to read the configuration from a file.
065   */
066  public static String configClassName;
067  /**
068   * The configuration file full path (-INSTANCE_ROOT-/config/config.ldif).
069   * of the installation of the control panel.
070   */
071  public static String configFile;
072
073  /**
074   * The error that occurred when setting the environment (null if no error
075   * occurred).
076   */
077  protected static OpenDsException environmentSettingException;
078  static
079  {
080    // This allows testing of configuration components when the OpenDS.jar
081    // in the classpath does not necessarily point to the server's
082    // This is done here since both implementations of ConfigReader require it.
083    String installRoot = System.getProperty("org.opends.quicksetup.Root");
084    if (installRoot == null) {
085      installRoot = Utilities.getServerRootDirectory().getAbsolutePath();
086    }
087    String instanceRoot =
088      Utilities.getInstanceRootDirectory(installRoot).getAbsolutePath();
089    configFile = instanceRoot + File.separator + "config" + File.separator +
090    "config.ldif";
091    configClassName = ReadOnlyConfigFileHandler.class.getName();
092    try
093    {
094      DirectoryEnvironmentConfig env = DirectoryServer.getEnvironmentConfig();
095      env.setServerRoot(new File(installRoot));
096      DirectoryServer instance = DirectoryServer.getInstance();
097      DirectoryServer.bootstrapClient();
098      DirectoryServer.initializeJMX();
099      instance.initializeConfiguration(configClassName, configFile);
100      instance.initializeSchema();
101    }
102    catch (Throwable t)
103    {
104      environmentSettingException = new OfflineUpdateException(
105          ERR_CTRL_PANEL_SETTING_ENVIRONMENT.get(t.getMessage()), t);
106    }
107    logger.info(LocalizableMessage.raw("Environment initialized."));
108  }
109
110  /**
111   * The exceptions that occurred reading the configuration.
112   */
113  protected List<OpenDsException> exceptions = Collections.emptyList();
114
115  /**
116   * Whether the configuration has already been read or not.
117   */
118  protected boolean configRead;
119
120  /**
121   * The set of connection listeners.
122   */
123  protected Set<ConnectionHandlerDescriptor> listeners = Collections.emptySet();
124
125  /**
126   * The administration connector.
127   */
128  protected ConnectionHandlerDescriptor adminConnector;
129
130  /**
131   * The set of backend descriptors.
132   */
133  protected Set<BackendDescriptor> backends = Collections.emptySet();
134
135  /**
136   * The set of administrative users.
137   */
138  protected Set<DN> administrativeUsers = Collections.emptySet();
139
140  /**
141   * The replication serve port (-1 if the replication server port is not
142   * defined).
143   */
144  protected int replicationPort = -1;
145
146  /**
147   * The java version used to run the server.
148   */
149  protected String javaVersion;
150
151  /**
152   * The number of connections opened on the server.
153   */
154  protected int numberConnections;
155
156  /**
157   * Whether the schema checking is enabled or not.
158   */
159  protected boolean isSchemaEnabled;
160
161  /**
162   * The schema used by the server.
163   */
164  protected Schema schema;
165
166  /**
167   * The task entries.
168   **/
169  protected Set<TaskEntry> taskEntries = Collections.emptySet();
170
171  /**
172   * Returns the Administrative User DNs found in the config.ldif.  The set
173   * must be unmodifiable (the inheriting classes must take care of this).
174   * @return the Administrative User DNs found in the config.ldif.
175   */
176  public Set<DN> getAdministrativeUsers()
177  {
178    return administrativeUsers;
179  }
180
181  /**
182   * Returns the backend descriptors found in the config.ldif.  The set
183   * must be unmodifiable (the inheriting classes must take care of this).
184   * @return the backend descriptors found in the config.ldif.
185   */
186  public Set<BackendDescriptor> getBackends()
187  {
188    return backends;
189  }
190
191  /**
192   * Returns the listener descriptors found in the config.ldif.  The set
193   * must be unmodifiable (the inheriting classes must take care of this).
194   * @return the listeners descriptors found in the config.ldif.
195   */
196  public Set<ConnectionHandlerDescriptor> getConnectionHandlers()
197  {
198    return listeners;
199  }
200
201  /**
202   * Returns the admin connector.
203   * @return the admin connector.
204   */
205  public ConnectionHandlerDescriptor getAdminConnector()
206  {
207    return adminConnector;
208  }
209
210  /**
211   * Returns the list of exceptions that were encountered reading the
212   * configuration.  The list must be unmodifiable (the inheriting classes must
213   * take care of this).
214   * @return the list of exceptions that were encountered reading the
215   * configuration.
216   */
217  public List<OpenDsException> getExceptions()
218  {
219    return exceptions;
220  }
221
222  /**
223   * Returns <CODE>true</CODE> if the configuration has been read at least once
224   * and <CODE>false</CODE> otherwise.
225   * @return <CODE>true</CODE> if the configuration has been read at least once
226   * and <CODE>false</CODE> otherwise.
227   */
228  public boolean isConfigRead()
229  {
230    return configRead;
231  }
232
233  /**
234   * Returns the replication server port. -1 if no replication server port is
235   * defined.
236   * @return the replication server port. -1 if no replication server port is
237   * defined.
238   */
239  public int getReplicationPort()
240  {
241    return replicationPort;
242  }
243
244  /**
245   * Returns <CODE>true</CODE> if the schema check is enabled and
246   * <CODE>false</CODE> otherwise.
247   * @return <CODE>true</CODE> if the schema check is enabled and
248   * <CODE>false</CODE> otherwise.
249   */
250  public boolean isSchemaEnabled()
251  {
252    return isSchemaEnabled;
253  }
254
255  /**
256   * Returns the java version used to run the server. <CODE>null</CODE> if no
257   * java version is used (because the server is down).
258   * @return the java version used to run the server. <CODE>null</CODE> if no
259   * java version is used (because the server is down).
260   */
261  public String getJavaVersion()
262  {
263    return javaVersion;
264  }
265
266  /**
267   * Returns the number of open connections on the server.   -1 if the server
268   * is down.
269   * @return the number of open connections on the server.
270   */
271  public int getOpenConnections()
272  {
273    return numberConnections;
274  }
275
276  /**
277   * Returns the schema of the server.
278   * @return the schema of the server.
279   */
280  public Schema getSchema()
281  {
282    return schema;
283  }
284
285  /**
286   * Returns the task entries.
287   * @return the task entries.
288   */
289  public Set<TaskEntry> getTaskEntries()
290  {
291    return taskEntries;
292  }
293
294  /**
295   * Reads the schema from the files.
296   * @throws ConfigException if an error occurs reading the schema.
297   * @throws InitializationException if an error occurs initializing
298   * configuration to read schema.
299   * @throws DirectoryException if there is an error registering the minimal
300   * objectclasses.
301   */
302  protected void readSchema() throws ConfigException, InitializationException,
303  DirectoryException
304  {
305    SchemaLoader loader = new SchemaLoader();
306    loader.readSchema();
307    schema = loader.getSchema();
308  }
309
310  /**
311   * Method that transforms the VLV sort order value as it is defined in the
312   * schema to a list of VLVSortOrder objects.
313   * @param s the string in the configuration.
314   * @return  a list of VLVSortOrder objects.
315   */
316  protected List<VLVSortOrder> getVLVSortOrder(String s)
317  {
318    ArrayList<VLVSortOrder> sortOrder = new ArrayList<>();
319    if (s != null)
320    {
321      String[] attrNames = s.split(" ");
322      for (String attrName : attrNames)
323      {
324        if (attrName.startsWith("+"))
325        {
326          sortOrder.add(new VLVSortOrder(attrName.substring(1), true));
327        }
328        else if (attrName.startsWith("-"))
329        {
330          sortOrder.add(new VLVSortOrder(attrName.substring(1), false));
331        }
332        else
333        {
334          sortOrder.add(new VLVSortOrder(attrName, true));
335        }
336      }
337    }
338    return sortOrder;
339  }
340
341  /**
342   * Returns the comparator to be used to sort InetAddresses.
343   * @return the comparator to be used to sort InetAddresses.
344   */
345  protected Comparator<InetAddress> getInetAddressComparator()
346  {
347    return AdministrationConnectorCfgDefn.getInstance().
348    getListenAddressPropertyDefinition();
349  }
350
351  /**
352   * Returns <CODE>true</CODE> if the schema must be read and
353   * <CODE>false</CODE> otherwise.
354   * @return <CODE>true</CODE> if the schema must be read and
355   * <CODE>false</CODE> otherwise.
356   */
357  protected boolean mustReadSchema()
358  {
359    return true;
360  }
361}