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