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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2007-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.tools;
028
029 import static org.opends.messages.ToolMessages.*;
030 import static org.opends.server.tools.ToolConstants.*;
031
032 import java.io.File;
033 import java.util.LinkedHashSet;
034
035 import org.opends.messages.Message;
036 import org.opends.quicksetup.Constants;
037 import org.opends.quicksetup.Installation;
038 import org.opends.quicksetup.util.Utils;
039 import org.opends.server.util.args.ArgumentException;
040 import org.opends.server.util.args.ArgumentParser;
041 import org.opends.server.util.args.BooleanArgument;
042 import org.opends.server.util.args.StringArgument;
043
044 /**
045 * Class used to parse the arguments of the java properties tool command-line.
046 */
047 public class JavaPropertiesToolArgumentParser extends ArgumentParser
048 {
049 // Usage argument
050 BooleanArgument showUsageArg;
051 // Quiet argument
052 BooleanArgument quietArg;
053 // The file containing the properties
054 StringArgument propertiesFileArg;
055 // The file that is generated
056 StringArgument destinationFileArg;
057
058 /**
059 * The default constructor for this class.
060 * @param mainClassName the class name of the main class for the command-line
061 * that is being used.
062 */
063 public JavaPropertiesToolArgumentParser(String mainClassName)
064 {
065 super(mainClassName,
066 INFO_JAVAPROPERTIES_TOOL_DESCRIPTION.get(getDefaultPropertiesValue()),
067 false);
068 }
069
070 /**
071 * Initializes the arguments without parsing them.
072 * @throws ArgumentException if there was an error creating or adding the
073 * arguments. If this occurs is likely to be a bug.
074 */
075 public void initializeArguments() throws ArgumentException
076 {
077 quietArg = new BooleanArgument(
078 "quiet", OPTION_SHORT_QUIET,
079 OPTION_LONG_QUIET,
080 INFO_JAVAPROPERTIES_DESCRIPTION_SILENT.get());
081 quietArg.setPropertyName(OPTION_LONG_QUIET);
082 addArgument(quietArg);
083
084 propertiesFileArg = new StringArgument("propertiesFile",
085 'p', "propertiesFile", false,
086 false, true, INFO_PATH_PLACEHOLDER.get(), getDefaultPropertiesValue(),
087 "propertiesFile",
088 INFO_JAVAPROPERTIES_DESCRIPTION_PROPERTIES_FILE.get(
089 getDefaultPropertiesValue()));
090 propertiesFileArg.setHidden(true);
091 addArgument(propertiesFileArg);
092
093 destinationFileArg = new StringArgument("destinationFile",
094 'd', "destinationFile", false,
095 false, true, INFO_PATH_PLACEHOLDER.get(), getDefaultDestinationValue(),
096 "destinationFile",
097 INFO_JAVAPROPERTIES_DESCRIPTION_DESTINATION_FILE.get(
098 getDefaultDestinationValue()));
099 destinationFileArg.setHidden(true);
100 addArgument(destinationFileArg);
101
102 showUsageArg = new BooleanArgument("help", OPTION_SHORT_HELP,
103 OPTION_LONG_HELP,
104 INFO_JAVAPROPERTIES_DESCRIPTION_HELP.get());
105 addArgument(showUsageArg);
106 setUsageArgument(showUsageArg);
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override()
113 public void parseArguments(String[] args) throws ArgumentException
114 {
115 LinkedHashSet<Message> errorMessages = new LinkedHashSet<Message>();
116 try
117 {
118 super.parseArguments(args);
119 }
120 catch (ArgumentException ae)
121 {
122 errorMessages.add(ae.getMessageObject());
123 }
124
125 if (!isUsageArgumentPresent() && !isVersionArgumentPresent())
126 {
127 String value = propertiesFileArg.getValue();
128 if (value != null)
129 {
130 File f = new File(value);
131 if (!f.exists() || !f.isFile() || !f.canRead())
132 {
133 errorMessages.add(ERR_JAVAPROPERTIES_WITH_PROPERTIES_FILE.get(value));
134 }
135 }
136 value = destinationFileArg.getValue();
137 if (value != null)
138 {
139 File f = new File(value);
140 if (f.isDirectory() || !Utils.canWrite(value))
141 {
142 errorMessages.add(
143 ERR_JAVAPROPERTIES_WITH_DESTINATION_FILE.get(value));
144 }
145 }
146 if (errorMessages.size() > 0)
147 {
148 Message message = ERR_CANNOT_INITIALIZE_ARGS.get(
149 Utils.getMessageFromCollection(errorMessages,
150 Constants.LINE_SEPARATOR));
151 throw new ArgumentException(message);
152 }
153 }
154 }
155
156 /**
157 * Returns the default destination file by inspecting the class loader.
158 * @return the default destination file retrieved by inspecting the class
159 * loader.
160 */
161 private String getDefaultDestinationValue()
162 {
163 String value;
164 // Use this instead of Installation.getLocal() because making that call
165 // starts a new JVM and the command-line becomes less responsive.
166 String root = Utils.getInstallPathFromClasspath();
167 if (root != null)
168 {
169 String libDir = Utils.getPath(root, Installation.LIBRARIES_PATH_RELATIVE);
170 if (Utils.isWindows())
171 {
172 value = Utils.getPath(libDir,
173 Installation.SET_JAVA_PROPERTIES_FILE_WINDOWS);
174 }
175 else
176 {
177 value = Utils.getPath(libDir,
178 Installation.SET_JAVA_PROPERTIES_FILE_UNIX);
179 }
180 }
181 else
182 {
183 // This can happen when we are not launched using the command-line (for
184 // instance from the WebInstaller).
185 if (Utils.isWindows())
186 {
187 value = Utils.getPath(Installation.LIBRARIES_PATH_RELATIVE,
188 Installation.SET_JAVA_PROPERTIES_FILE_WINDOWS);
189 }
190 else
191 {
192 value = Utils.getPath(Installation.LIBRARIES_PATH_RELATIVE,
193 Installation.SET_JAVA_PROPERTIES_FILE_UNIX);
194 }
195 }
196 return value;
197 }
198
199 /**
200 * Returns the default java properties file by inspecting the class loader.
201 * @return the default java properties file retrieved by inspecting the class
202 * loader.
203 */
204 private static String getDefaultPropertiesValue()
205 {
206 String defaultPropertiesValue;
207 // Use this instead of Installation.getLocal() because making that call
208 // starts a new JVM and the command-line becomes less responsive.
209 String root = Utils.getInstallPathFromClasspath();
210 if (root != null)
211 {
212 String configDir = Utils.getPath(root, Installation.CONFIG_PATH_RELATIVE);
213 defaultPropertiesValue = Utils.getPath(configDir,
214 Installation.DEFAULT_JAVA_PROPERTIES_FILE);
215 }
216 else
217 {
218 // This can happen when we are not launched using the command-line (for
219 // instance from the WebInstaller).
220 defaultPropertiesValue = Installation.DEFAULT_JAVA_PROPERTIES_FILE;
221 }
222 return defaultPropertiesValue;
223 }
224 }