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 2012-2016 ForgeRock AS.
016 */
017package org.opends.quicksetup;
018
019import static org.opends.messages.QuickSetupMessages.*;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.forgerock.i18n.LocalizableMessage;
027import org.forgerock.i18n.LocalizableMessageBuilder;
028import org.forgerock.i18n.slf4j.LocalizedLogger;
029import org.opends.quicksetup.util.Utils;
030
031/**
032 * This class is used to know which is the status of the install.
033 * It is required to do an installation after the user has unzipped the zip file.
034 * The main goal of the class is to help identifying whether there is already
035 * something installed or not.
036 *
037 * This class assumes that we are running in the case of an offline install.
038 */
039public class CurrentInstallStatus
040{
041  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
042
043  private boolean isInstalled;
044  private boolean canOverwriteCurrentInstall;
045  private LocalizableMessage installationMsg;
046
047  /** The constructor of a CurrentInstallStatus object. */
048  public CurrentInstallStatus()
049  {
050
051    Installation installation = Installation.getLocal();
052    List<LocalizableMessage> msgs = new ArrayList<>();
053
054    if (installation.getStatus().isServerRunning())
055    {
056      msgs.add(INFO_INSTALLSTATUS_SERVERRUNNING.get(getPort()));
057    }
058
059    if (dbFilesExist())
060    {
061      canOverwriteCurrentInstall = true;
062      msgs.add(INFO_INSTALLSTATUS_DBFILEEXIST.get());
063    }
064
065    if (configExists())
066    {
067      canOverwriteCurrentInstall = false;
068      isInstalled = true;
069      msgs.add(INFO_INSTALLSTATUS_CONFIGFILEMODIFIED.get());
070    }
071
072    if (canOverwriteCurrentInstall)
073    {
074      installationMsg = !Utils.isCli()?
075        INFO_INSTALLSTATUS_CANOVERWRITECURRENTINSTALL_MSG.get() :
076        INFO_INSTALLSTATUS_CANOVERWRITECURRENTINSTALL_MSG_CLI.get();
077    }
078    else if (isInstalled)
079    {
080      LocalizableMessageBuilder buf = new LocalizableMessageBuilder();
081      if (Utils.isCli())
082      {
083        buf = new LocalizableMessageBuilder();
084        for (LocalizableMessage msg : msgs)
085        {
086          buf.append(Constants.LINE_SEPARATOR);
087          buf.append("- ").append(msg);
088        }
089        String cmd = Installation.getSetupFileName();
090        installationMsg = INFO_INSTALLSTATUS_INSTALLED_CLI.get(cmd, buf);
091      }
092      else
093      {
094        buf.append("<ul>");
095        for (LocalizableMessage msg : msgs)
096        {
097          buf.append("\n<li>");
098          buf.append(msg);
099          buf.append("</li>");
100        }
101        buf.append("</ul>");
102        installationMsg = INFO_INSTALLSTATUS_INSTALLED.get(buf);
103      }
104    }
105    if (!isInstalled)
106    {
107      installationMsg = INFO_INSTALLSTATUS_NOT_INSTALLED.get();
108    }
109  }
110
111  /**
112   * Indicates whether there is something installed or not.
113   *
114   * @return <CODE>true</CODE> if there is something installed under the
115   *         binaries that we are running, or <CODE>false</CODE> if not.
116   */
117  public boolean isInstalled()
118  {
119    return isInstalled;
120  }
121
122  /**
123   * Indicates can overwrite current install.
124   *
125   * @return <CODE>true</CODE> if there is something installed under the
126   *         binaries that we are running and we can overwrite it and
127   *         <CODE>false</CODE> if not.
128   */
129  public boolean canOverwriteCurrentInstall()
130  {
131    return canOverwriteCurrentInstall;
132  }
133
134  /**
135   * Provides a localized message to be displayed to the user in HTML format
136   * informing of the installation status.
137   *
138   * @return an String in HTML format describing the status of the installation.
139   */
140  public LocalizableMessage getInstallationMsg()
141  {
142    return installationMsg;
143  }
144
145  private int getPort()
146  {
147    try {
148      return Installation.getLocal().getCurrentConfiguration().getPort();
149    } catch (IOException ioe) {
150      logger.info(LocalizableMessage.raw("Failed to get port", ioe));
151      return -1;
152    }
153  }
154
155  /**
156   * Indicates whether there are database files under this installation.
157   *
158   * @return <CODE>true</CODE> if there are database files, or
159   *         <CODE>false</CODE> if not.
160   */
161  private boolean dbFilesExist()
162  {
163    File dbDir = Installation.getLocal().getDatabasesDirectory();
164    File[] children = dbDir.listFiles();
165    return children != null && children.length > 0;
166  }
167
168  /**
169   * Indicates whether there are config files under this installation.
170   *
171   * @return <CODE>true</CODE> if there are configuration files, or
172   *         <CODE>false</CODE> if not.
173   */
174  private boolean configExists()
175  {
176    File configDir = Installation.getLocal().getConfigurationDirectory();
177    File[] children = configDir.listFiles();
178    return children != null && children.length > 0;
179  }
180}