mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Fabio Pistolesi
08.58.2015 71ee88697d285e8932a8dcab3ebc27c79387dd68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2007-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2014-2015 ForgeRock AS
 */
package org.opends.server.tools;
 
import static com.forgerock.opendj.cli.Utils.*;
import static com.forgerock.opendj.util.OperatingSystem.*;
 
import static org.opends.messages.ToolMessages.*;
 
import java.io.File;
import java.util.LinkedHashSet;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.quicksetup.Constants;
import org.opends.quicksetup.Installation;
import org.opends.quicksetup.util.Utils;
import org.opends.server.core.DirectoryServer.DirectoryServerVersionHandler;
 
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.CommonArguments;
import com.forgerock.opendj.cli.StringArgument;
 
/**
 * Class used to parse the arguments of the java properties tool command-line.
 */
public class JavaPropertiesToolArgumentParser extends ArgumentParser
{
  /** Usage argument. */
  BooleanArgument   showUsageArg;
  /** Quiet argument. */
  BooleanArgument   quietArg;
  /** The file containing the properties. */
  StringArgument propertiesFileArg;
  /** The file that is generated. */
  StringArgument destinationFileArg;
 
  /**
   * The default constructor for this class.
   * @param mainClassName the class name of the main class for the command-line
   * that is being used.
   */
  public JavaPropertiesToolArgumentParser(String mainClassName)
  {
    super(mainClassName,
        INFO_JAVAPROPERTIES_TOOL_DESCRIPTION.get(getDefaultPropertiesValue()),
        false);
    setShortToolDescription(REF_SHORT_DESC_DSJAVAPROPERTIES.get());
    setVersionHandler(new DirectoryServerVersionHandler());
  }
 
  /**
   * Initializes the arguments without parsing them.
   * @throws ArgumentException if there was an error creating or adding the
   * arguments.  If this occurs is likely to be a bug.
   */
  public void initializeArguments() throws ArgumentException
  {
    quietArg = CommonArguments.getQuiet();
    addArgument(quietArg);
 
    propertiesFileArg = new StringArgument("propertiesFile",
        'p', "propertiesFile", false,
        false, true, INFO_PATH_PLACEHOLDER.get(), getDefaultPropertiesValue(),
        "propertiesFile",
        INFO_JAVAPROPERTIES_DESCRIPTION_PROPERTIES_FILE.get(
            getDefaultPropertiesValue()));
    propertiesFileArg.setHidden(true);
    addArgument(propertiesFileArg);
 
    destinationFileArg = new StringArgument("destinationFile",
        'd', "destinationFile", false,
        false, true, INFO_PATH_PLACEHOLDER.get(), getDefaultDestinationValue(),
        "destinationFile",
        INFO_JAVAPROPERTIES_DESCRIPTION_DESTINATION_FILE.get(
            getDefaultDestinationValue()));
    destinationFileArg.setHidden(true);
    addArgument(destinationFileArg);
 
    showUsageArg = CommonArguments.getShowUsage();
    addArgument(showUsageArg);
    setUsageArgument(showUsageArg);
  }
 
  /** {@inheritDoc} */
  @Override
  public void parseArguments(String[] args) throws ArgumentException
  {
    LinkedHashSet<LocalizableMessage> errorMessages = new LinkedHashSet<LocalizableMessage>();
    try
    {
      super.parseArguments(args);
    }
    catch (ArgumentException ae)
    {
      errorMessages.add(ae.getMessageObject());
    }
 
    if (!isUsageArgumentPresent() && !isVersionArgumentPresent())
    {
      String value = propertiesFileArg.getValue();
      if (value != null)
      {
        File f = new File(value);
        if (!f.exists() || !f.isFile() || !f.canRead())
        {
          errorMessages.add(ERR_JAVAPROPERTIES_WITH_PROPERTIES_FILE.get(value));
        }
      }
      value = destinationFileArg.getValue();
      if (value != null)
      {
        File f = new File(value);
        if (f.isDirectory() || !canWrite(value))
        {
          errorMessages.add(
              ERR_JAVAPROPERTIES_WITH_DESTINATION_FILE.get(value));
        }
      }
      if (errorMessages.size() > 0)
      {
        LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(
            Utils.getMessageFromCollection(errorMessages,
                Constants.LINE_SEPARATOR));
        throw new ArgumentException(message);
      }
    }
  }
 
  /**
   * Returns the default destination file by inspecting the class loader.
   * @return the default destination file retrieved by inspecting the class
   * loader.
   */
  private String getDefaultDestinationValue()
  {
    // Use this instead of Installation.getLocal() because making that call
    // starts a new JVM and the command-line becomes less responsive.
    String installPath = Utils.getInstallPathFromClasspath();
    String root = Utils.getInstancePathFromInstallPath(installPath);
    if (root != null)
    {
      return getPath(Utils.getPath(root, Installation.LIBRARIES_PATH_RELATIVE));
    }
    else
    {
      // This can happen when we are not launched using the command-line (for
      // instance from the WebInstaller).
      return getPath(Installation.LIBRARIES_PATH_RELATIVE);
    }
  }
 
  private String getPath(String libDir)
  {
    final String relativePath = isWindows()
        ? Installation.SET_JAVA_PROPERTIES_FILE_WINDOWS
        : Installation.SET_JAVA_PROPERTIES_FILE_UNIX;
    return Utils.getPath(libDir, relativePath);
  }
 
  /**
   * Returns the default java properties file by inspecting the class loader.
   * @return the default java properties file retrieved by inspecting the class
   * loader.
   */
  private static String getDefaultPropertiesValue()
  {
    // Use this instead of Installation.getLocal() because making that call
    // starts a new JVM and the command-line becomes less responsive.
    String installPath = Utils.getInstallPathFromClasspath();
    String root = Utils.getInstancePathFromInstallPath(installPath);
    if (root != null)
    {
      String configDir = Utils.getPath(root, Installation.CONFIG_PATH_RELATIVE);
      return Utils.getPath(configDir, Installation.DEFAULT_JAVA_PROPERTIES_FILE);
    }
    else
    {
      // This can happen when we are not launched using the command-line (for
      // instance from the WebInstaller).
      return Installation.DEFAULT_JAVA_PROPERTIES_FILE;
    }
  }
}