| | |
| | | |
| | | package org.opends.quicksetup; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.FileReader; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | |
| | | import org.opends.quicksetup.i18n.ResourceProvider; |
| | | import org.opends.quicksetup.util.Utils; |
| | |
| | | |
| | | public class CurrentInstallStatus |
| | | { |
| | | static private final Logger LOG = |
| | | Logger.getLogger(CurrentInstallStatus.class.getName()); |
| | | |
| | | private boolean isInstalled; |
| | | |
| | | private boolean canOverwriteCurrentInstall; |
| | | |
| | | private String installationMsg; |
| | | |
| | | private String configFileContents; |
| | | |
| | | private Set<String> directoryManagerDns; |
| | | |
| | | private Set<String> dbPaths; |
| | | |
| | | private Set<String> logPaths; |
| | | |
| | | private String ldapUrl; |
| | | |
| | | private String ldapsUrl; |
| | | |
| | | private static boolean lockPathInitialized; |
| | | |
| | | /** |
| | | * The constructor of a CurrentInstallStatus object. |
| | | * |
| | |
| | | isInstalled = false; |
| | | } else |
| | | { |
| | | Installation installation = Installation.getLocal(); |
| | | boolean dbFileExists = false; |
| | | ArrayList<String> msgs = new ArrayList<String>(); |
| | | |
| | | if (isServerRunning()) |
| | | if (installation.getStatus().isServerRunning()) |
| | | { |
| | | msgs.add(getMsg("installstatus-serverrunning", new String[] |
| | | { String.valueOf(getPort()) })); |
| | |
| | | return installationMsg; |
| | | } |
| | | |
| | | /** |
| | | * Returns the list of directory manager dns as they appear in the |
| | | * configuration file. |
| | | * |
| | | * @return the list of directory manager dns as they appear in the |
| | | * configuration file. |
| | | */ |
| | | public Set<String> getDirectoryManagerDns() |
| | | { |
| | | if (directoryManagerDns == null) |
| | | { |
| | | directoryManagerDns = new HashSet<String>(); |
| | | String directoryManagerDnAttr = "ds-cfg-alternate-bind-dn"; |
| | | updateSetWithValues(directoryManagerDns, directoryManagerDnAttr); |
| | | } |
| | | return directoryManagerDns; |
| | | } |
| | | |
| | | /** |
| | | * Returns the list of paths where the databases are installed as they appear |
| | | * in the configuration file. |
| | | * |
| | | * @return the list of paths where the databases are installed as they appear |
| | | * in the configuration file. |
| | | */ |
| | | public Set<String> getDatabasePaths() |
| | | { |
| | | if (dbPaths == null) |
| | | { |
| | | dbPaths = new HashSet<String>(); |
| | | String dbFileAttr = "ds-cfg-backend-directory"; |
| | | updateSetWithValues(dbPaths, dbFileAttr); |
| | | } |
| | | return dbPaths; |
| | | } |
| | | |
| | | /** |
| | | * Returns the list of paths where the logs files are located as they appear |
| | | * in the configuration file. |
| | | * |
| | | * @return the list of paths where the logs files are located as they appear |
| | | * in the configuration file. |
| | | */ |
| | | public Set<String> getLogPaths() |
| | | { |
| | | if (logPaths == null) |
| | | { |
| | | logPaths = new HashSet<String>(); |
| | | String logFileAttr = "ds-cfg-log-file"; |
| | | updateSetWithValues(logPaths, logFileAttr); |
| | | } |
| | | return logPaths; |
| | | } |
| | | |
| | | /** |
| | | * Returns if the server is running on the given path. The location |
| | | * of the 'locks' directory which is required for this method is |
| | | * determined by getting the installation path from the classpath. |
| | | * NOTE: this method is to be called only when the OpenDS.jar class has |
| | | * already been loaded as it uses classes in that jar. |
| | | * |
| | | * LIMITATIONS: |
| | | * If the locks directory does not exist the mechanism fails if the server is |
| | | * stopped. However if the server.lock does not exist AND the server is not |
| | | * running the mechanism should work most of the times (see failing case 3). |
| | | * |
| | | * The cases where this mechanism does not work are: |
| | | * |
| | | * 1. The user deletes/renames the locks directory. |
| | | * 2. The user deletes/renames the server.lock file AND the server is running. |
| | | * 3. The server is not running but the user that is running the code does not |
| | | * have file system access rights. |
| | | * 4. The server is not running and another process has a lock on the file. |
| | | * @return <CODE>true</CODE> if the server is running and <CODE>false</CODE> |
| | | * otherwise. |
| | | */ |
| | | public static boolean isServerRunning() { |
| | | File locksDir = new File(Utils.getInstallPathFromClasspath(), |
| | | org.opends.server.util.ServerConstants.LOCKS_DIRECTORY); |
| | | return isServerRunning(locksDir); |
| | | } |
| | | |
| | | /** |
| | | * Returns if the server is running on the given path. |
| | | * NOTE: this method is to be called only when the OpenDS.jar class has |
| | | * already been loaded as it uses classes in that jar. |
| | | * @param locksDir File representing the location of the server's 'locks' |
| | | * directory |
| | | * @return <CODE>true</CODE> if the server is running and <CODE>false</CODE> |
| | | * otherwise. |
| | | */ |
| | | public static boolean isServerRunning(File locksDir) |
| | | { |
| | | boolean isServerRunning; |
| | | if (!lockPathInitialized) |
| | | { |
| | | System.setProperty( |
| | | org.opends.server.util.ServerConstants.PROPERTY_LOCK_DIRECTORY, |
| | | Utils.getPath(locksDir)); |
| | | lockPathInitialized = true; |
| | | } |
| | | String lockFile = |
| | | org.opends.server.core.LockFileManager.getServerLockFileName(); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | try |
| | | { |
| | | if (org.opends.server.core.LockFileManager.acquireExclusiveLock(lockFile, |
| | | failureReason)) |
| | | { |
| | | org.opends.server.core.LockFileManager.releaseLock(lockFile, |
| | | failureReason); |
| | | isServerRunning = false; |
| | | } |
| | | else |
| | | { |
| | | isServerRunning = true; |
| | | } |
| | | } |
| | | catch (Throwable t) |
| | | { |
| | | // Assume that if we cannot acquire the lock file the server is |
| | | // running. |
| | | isServerRunning = true; |
| | | } |
| | | return isServerRunning; |
| | | } |
| | | |
| | | /** |
| | | * Provides the ldap url to the server (assumes we are calling this locally). |
| | | * |
| | | * @return the ldap url to the server. |
| | | */ |
| | | public String getLdapUrl() |
| | | { |
| | | if (ldapUrl == null) |
| | | { |
| | | if (getPort() != -1) |
| | | { |
| | | ldapUrl = "ldap://localhost:"+getPort(); |
| | | } |
| | | } |
| | | return ldapUrl; |
| | | } |
| | | |
| | | /** |
| | | * Provides the ldap secure url to the server (assumes we are calling this |
| | | * locally). |
| | | * |
| | | * @return the ldap secure url to the server. |
| | | */ |
| | | public String getLdapsUrl() |
| | | { |
| | | if (ldapsUrl == null) |
| | | { |
| | | if (getSecurePort() != -1) |
| | | { |
| | | ldapsUrl = "ldaps://localhost:"+getSecurePort(); |
| | | } |
| | | } |
| | | return ldapsUrl; |
| | | } |
| | | |
| | | /** |
| | | * Provides the LDAP port as is specified in the config.ldif file. |
| | | * |
| | | * @return the LDAP port specified in the config.ldif file. |
| | | */ |
| | | private int getPort() |
| | | { |
| | | return getPort("ds-cfg-listen-port"); |
| | | } |
| | | |
| | | /** |
| | | * Provides the LDAP secure port as is specified in the config.ldif file. |
| | | * |
| | | * @return the LDAP secure port specified in the config.ldif file. |
| | | */ |
| | | private int getSecurePort() |
| | | { |
| | | // TODO find out which is the attribute for this port. |
| | | return getPort("ds-cfg-listen-secure-port"); |
| | | } |
| | | |
| | | private int getPort(String portAttr) |
| | | { |
| | | int port = -1; |
| | | |
| | | int index = getConfigFileContents().indexOf("cn=ldap connection handler"); |
| | | |
| | | if (index != -1) |
| | | { |
| | | String attrWithPoints = portAttr+":"; |
| | | int index1 = getConfigFileContents().indexOf(attrWithPoints, index); |
| | | if (index1 != -1) |
| | | { |
| | | int index2 = |
| | | getConfigFileContents().indexOf(Constants.LINE_SEPARATOR, index1); |
| | | if (index2 != -1) |
| | | { |
| | | String sPort = |
| | | getConfigFileContents().substring(attrWithPoints.length() + |
| | | index1, |
| | | index2).trim(); |
| | | try |
| | | { |
| | | port = Integer.parseInt(sPort); |
| | | } catch (NumberFormatException nfe) |
| | | { |
| | | } |
| | | } |
| | | } |
| | | try { |
| | | port = Installation.getLocal().getCurrentConfiguration(). |
| | | getPort(); |
| | | } catch (IOException ioe) { |
| | | LOG.log(Level.INFO, "Failed to get port", ioe); |
| | | } |
| | | return port; |
| | | } |
| | |
| | | private boolean dbFilesExist() |
| | | { |
| | | boolean dbFilesExist = false; |
| | | File dbDir = new File(Utils.getInstallPathFromClasspath(), "db"); |
| | | |
| | | File dbDir = Installation.getLocal().getDatabasesDirectory(); |
| | | File[] children = dbDir.listFiles(); |
| | | if ((children != null) && (children.length > 0)) |
| | | { |
| | |
| | | */ |
| | | private boolean isConfigFileModified() |
| | | { |
| | | boolean isConfigFileModified = getPort() != 389; |
| | | |
| | | if (!isConfigFileModified) |
| | | { |
| | | // TODO: this is not really stable |
| | | isConfigFileModified = |
| | | getConfigFileContents().indexOf("# cddl header start") == -1; |
| | | boolean mod = false; |
| | | try { |
| | | mod = Installation.getLocal().getCurrentConfiguration() |
| | | .hasBeenModified(); |
| | | } catch (IOException ioe) { |
| | | LOG.log(Level.INFO, "failed to determine if config modified", ioe); |
| | | } |
| | | |
| | | return isConfigFileModified; |
| | | return mod; |
| | | } |
| | | |
| | | /** |
| | | * Provides the contents of the config.ldif file in a String. |
| | | * |
| | | * @return a String representing the contents of the config.ldif file. |
| | | */ |
| | | private String getConfigFileContents() |
| | | { |
| | | if (configFileContents == null) |
| | | { |
| | | StringBuilder buf = new StringBuilder(); |
| | | try |
| | | { |
| | | Installation installation = getInstallationFromClassPath(); |
| | | FileReader reader = |
| | | new FileReader(installation.getCurrentConfigurationFile()); |
| | | BufferedReader in = new BufferedReader(reader); |
| | | String line; |
| | | // We do not care about encoding: we are just interested in the ports |
| | | while ((line = in.readLine()) != null) |
| | | { |
| | | buf.append(line).append(Constants.LINE_SEPARATOR); |
| | | } |
| | | reader.close(); |
| | | } catch (IOException ioe) |
| | | { |
| | | } |
| | | configFileContents = buf.toString().toLowerCase(); |
| | | } |
| | | return configFileContents; |
| | | } |
| | | |
| | | /** |
| | | * The following three methods are just commodity methods to get localized |
| | | * messages. |
| | | */ |
| | | private String getMsg(String key) |
| | | { |
| | | return getI18n().getMsg(key); |
| | |
| | | return ResourceProvider.getInstance(); |
| | | } |
| | | |
| | | private void updateSetWithValues(Set<String> set, String attrName) |
| | | { |
| | | attrName += ":"; |
| | | int index1 = getConfigFileContents().indexOf(attrName); |
| | | while (index1 != -1) |
| | | { |
| | | int index2 = getConfigFileContents().indexOf( |
| | | Constants.LINE_SEPARATOR, index1); |
| | | String value; |
| | | if (index2 > (index1 + attrName.length())) |
| | | { |
| | | value = getConfigFileContents().substring(attrName.length() + index1, |
| | | index2).trim(); |
| | | } |
| | | else if (getConfigFileContents().length() > (index1 + attrName.length())) |
| | | { |
| | | // Assume end of file |
| | | value = getConfigFileContents().substring( |
| | | attrName.length() + index1).trim(); |
| | | } |
| | | else |
| | | { |
| | | value = null; |
| | | } |
| | | |
| | | if ((value != null) && (value.length() > 0)) |
| | | { |
| | | set.add(value); |
| | | } |
| | | |
| | | index1 = getConfigFileContents().indexOf(attrName, |
| | | index1 + attrName.length()); |
| | | } |
| | | } |
| | | |
| | | private Installation getInstallationFromClassPath() { |
| | | return new Installation(Utils.getInstallPathFromClasspath()); |
| | | } |
| | | } |