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