/* * 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 2008-2009 Sun Microsystems, Inc. * Portions Copyright 2014-2016 ForgeRock AS. */ package org.opends.guitools.controlpanel.util; import static org.opends.messages.AdminToolMessages.*; import java.io.File; import java.net.InetAddress; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.logging.Logger; import org.forgerock.i18n.LocalizableMessage; import org.forgerock.i18n.slf4j.LocalizedLogger; import org.forgerock.opendj.config.server.ConfigException; import org.forgerock.opendj.ldap.DN; import org.forgerock.opendj.server.config.meta.AdministrationConnectorCfgDefn; import org.opends.guitools.controlpanel.datamodel.BackendDescriptor; import org.opends.guitools.controlpanel.datamodel.ConnectionHandlerDescriptor; import org.opends.guitools.controlpanel.datamodel.VLVSortOrder; import org.opends.guitools.controlpanel.task.OfflineUpdateException; import org.opends.server.core.DirectoryServer; import org.opends.server.tools.tasks.TaskEntry; import org.opends.server.types.DirectoryEnvironmentConfig; import org.opends.server.types.DirectoryException; import org.opends.server.types.InitializationException; import org.opends.server.types.OpenDsException; import org.opends.server.types.Schema; /** * An abstract class providing some common interface for the class that read * the configuration (and if the server is running, the monitoring information). */ public abstract class ConfigReader { private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); /** * The configuration file full path (-INSTANCE_ROOT-/config/config.ldif). * of the installation of the control panel. */ public static String configFile; /** The error that occurred when setting the environment (null if no error occurred). */ protected static OpenDsException environmentSettingException; static { // This allows testing of configuration components when the OpenDS.jar // in the classpath does not necessarily point to the server's // This is done here since both implementations of ConfigReader require it. String installRoot = System.getProperty("org.opends.quicksetup.Root"); if (installRoot == null) { installRoot = Utilities.getServerRootDirectory().getAbsolutePath(); } String instanceRoot = Utilities.getInstanceRootDirectory(installRoot).getAbsolutePath(); configFile = instanceRoot + File.separator + "config" + File.separator + "config.ldif"; try { DirectoryEnvironmentConfig env = DirectoryServer.getEnvironmentConfig(); env.setServerRoot(new File(installRoot)); DirectoryServer instance = DirectoryServer.getInstance(); DirectoryServer.bootstrapClient(); DirectoryServer.initializeJMX(); // Initialize configuration framework without logging anything. final Logger configFrameworkLogger = Logger.getLogger("com.forgerock.opendj.ldap.config.config"); configFrameworkLogger.setUseParentHandlers(false); instance.initializeConfiguration(configFile); configFrameworkLogger.setUseParentHandlers(true); instance.initializeSchema(); } catch (Throwable t) { environmentSettingException = new OfflineUpdateException( ERR_CTRL_PANEL_SETTING_ENVIRONMENT.get(t.getMessage()), t); } logger.info(LocalizableMessage.raw("Environment initialized.")); } /** The exceptions that occurred reading the configuration. */ protected List exceptions = Collections.emptyList(); /** Whether the configuration has already been read or not. */ private boolean configRead; /** The set of connection listeners. */ protected Set listeners = Collections.emptySet(); /** The administration connector. */ protected ConnectionHandlerDescriptor adminConnector; /** The set of backend descriptors. */ protected Set backends = Collections.emptySet(); /** The set of administrative users. */ protected Set administrativeUsers = Collections.emptySet(); /** The replication serve port (-1 if the replication server port is not defined). */ protected int replicationPort = -1; /** The java version used to run the server. */ protected String javaVersion; /** The number of connections opened on the server. */ protected int numberConnections; /** Whether the schema checking is enabled or not. */ protected boolean isSchemaEnabled; /** The schema used by the server. */ protected Schema schema; /** The task entries. */ protected Set taskEntries = Collections.emptySet(); /** * Returns the Administrative User DNs found in the config.ldif. The set * must be unmodifiable (the inheriting classes must take care of this). * @return the Administrative User DNs found in the config.ldif. */ public Set getAdministrativeUsers() { return administrativeUsers; } /** * Returns the backend descriptors found in the config.ldif. The set * must be unmodifiable (the inheriting classes must take care of this). * @return the backend descriptors found in the config.ldif. */ public Set getBackends() { return backends; } /** * Returns the listener descriptors found in the config.ldif. The set * must be unmodifiable (the inheriting classes must take care of this). * @return the listeners descriptors found in the config.ldif. */ public Set getConnectionHandlers() { return listeners; } /** * Returns the admin connector. * @return the admin connector. */ public ConnectionHandlerDescriptor getAdminConnector() { return adminConnector; } /** * Returns the list of exceptions that were encountered reading the * configuration. The list must be unmodifiable (the inheriting classes must * take care of this). * @return the list of exceptions that were encountered reading the * configuration. */ public List getExceptions() { return exceptions; } /** * Returns true if the configuration has been read at least once * and false otherwise. * @return true if the configuration has been read at least once * and false otherwise. */ public boolean isConfigRead() { return configRead; } /** * Returns the replication server port. -1 if no replication server port is * defined. * @return the replication server port. -1 if no replication server port is * defined. */ public int getReplicationPort() { return replicationPort; } /** * Returns true if the schema check is enabled and * false otherwise. * @return true if the schema check is enabled and * false otherwise. */ public boolean isSchemaEnabled() { return isSchemaEnabled; } /** * Returns the java version used to run the server. null if no * java version is used (because the server is down). * @return the java version used to run the server. null if no * java version is used (because the server is down). */ public String getJavaVersion() { return javaVersion; } /** * Returns the number of open connections on the server. -1 if the server * is down. * @return the number of open connections on the server. */ public int getOpenConnections() { return numberConnections; } /** * Returns the schema of the server. * @return the schema of the server. */ public Schema getSchema() { return schema; } /** * Returns the task entries. * @return the task entries. */ public Set getTaskEntries() { return taskEntries; } /** * Reads the schema from the files. * @throws ConfigException if an error occurs reading the schema. * @throws InitializationException if an error occurs initializing * configuration to read schema. * @throws DirectoryException if there is an error registering the minimal * objectclasses. */ protected void readSchema() throws ConfigException, InitializationException, DirectoryException { SchemaLoader loader = new SchemaLoader(); loader.readSchema(); schema = loader.getSchema(); } /** * Method that transforms the VLV sort order value as it is defined in the * schema to a list of VLVSortOrder objects. * @param s the string in the configuration. * @return a list of VLVSortOrder objects. */ protected List getVLVSortOrder(String s) { ArrayList sortOrder = new ArrayList<>(); if (s != null) { String[] attrNames = s.split(" "); for (String attrName : attrNames) { if (attrName.startsWith("+")) { sortOrder.add(new VLVSortOrder(attrName.substring(1), true)); } else if (attrName.startsWith("-")) { sortOrder.add(new VLVSortOrder(attrName.substring(1), false)); } else { sortOrder.add(new VLVSortOrder(attrName, true)); } } } return sortOrder; } /** * Returns the comparator to be used to sort InetAddresses. * @return the comparator to be used to sort InetAddresses. */ protected Comparator getInetAddressComparator() { return AdministrationConnectorCfgDefn.getInstance(). getListenAddressPropertyDefinition(); } /** * Returns true if the schema must be read and * false otherwise. * @return true if the schema must be read and * false otherwise. */ protected boolean mustReadSchema() { return true; } }