use of org.jboss.as.cli.handlers.WindowsFilenameTabCompleter in project wildfly-core by wildfly.
the class ASModuleHandler method addModule.
protected void addModule(CommandContext ctx, final ParsedCommandLine parsedCmd) throws CommandLineException {
final String moduleName = name.getValue(parsedCmd, true);
// resources required only if we are generating module.xml
if (!moduleArg.isPresent(parsedCmd) && !(resources.isPresent(parsedCmd) || absoluteResources.isPresent(parsedCmd))) {
throw new CommandFormatException("You must specify at least one resource: use --resources or --absolute-resources parameter");
}
final String resourcePaths = resources.getValue(parsedCmd);
final String absoluteResourcePaths = absoluteResources.getValue(parsedCmd);
String pathDelimiter = PATH_SEPARATOR;
if (resourceDelimiter.isPresent(parsedCmd)) {
pathDelimiter = resourceDelimiter.getValue(parsedCmd);
}
final FilenameTabCompleter pathCompleter = Util.isWindows() ? new WindowsFilenameTabCompleter(ctx) : new DefaultFilenameTabCompleter(ctx);
final String[] resourceArr = (resourcePaths == null) ? new String[0] : resourcePaths.split(pathDelimiter);
File[] resourceFiles = new File[resourceArr.length];
boolean allowNonExistent = allowNonExistentResources.isPresent(parsedCmd);
for (int i = 0; i < resourceArr.length; ++i) {
final File f = new File(pathCompleter.translatePath(resourceArr[i]));
if (!f.exists() && !allowNonExistent) {
throw new CommandLineException("Failed to locate " + f.getAbsolutePath() + ", if you defined a nonexistent resource on purpose you should " + "use the " + allowNonExistentResources.getFullName() + " option");
}
resourceFiles[i] = f;
}
final String[] absoluteResourceArr = (absoluteResourcePaths == null) ? new String[0] : absoluteResourcePaths.split(pathDelimiter);
File[] absoluteResourceFiles = new File[absoluteResourceArr.length];
for (int i = 0; i < absoluteResourceArr.length; ++i) {
final File f = new File(pathCompleter.translatePath(absoluteResourceArr[i]));
if (!f.exists()) {
throw new CommandLineException("Failed to locate " + f.getAbsolutePath());
}
absoluteResourceFiles[i] = f;
}
final File moduleDir = getModulePath(getModulesDir(ctx), moduleName, slot.getValue(parsedCmd));
if (moduleDir.exists()) {
throw new CommandLineException("Module " + moduleName + " already exists at " + moduleDir.getAbsolutePath());
}
if (!moduleDir.mkdirs()) {
throw new CommandLineException("Failed to create directory " + moduleDir.getAbsolutePath());
}
final ModuleConfigImpl config;
final String moduleXml = moduleArg.getValue(parsedCmd);
if (moduleXml != null) {
config = null;
final File source = new File(moduleXml);
if (!source.exists()) {
throw new CommandLineException("Failed to locate the file on the filesystem: " + source.getAbsolutePath());
}
copy(source, new File(moduleDir, "module.xml"));
} else {
config = new ModuleConfigImpl(moduleName);
}
for (File f : resourceFiles) {
copyResource(f, new File(moduleDir, f.getName()), ctx, this);
if (config != null) {
config.addResource(new ResourceRoot(f.getName()));
}
}
for (File f : absoluteResourceFiles) {
if (config != null) {
try {
config.addResource(new ResourceRoot(f.getCanonicalPath()));
} catch (IOException ioe) {
throw new CommandLineException("Failed to read path: " + f.getAbsolutePath(), ioe);
}
}
}
if (config != null) {
Set<String> modules = new HashSet<>();
final String dependenciesStr = dependencies.getValue(parsedCmd);
if (dependenciesStr != null) {
final String[] depsArr = dependenciesStr.split(",+");
for (String dep : depsArr) {
// TODO validate dependencies
String depName = dep.trim();
config.addDependency(new ModuleDependency(depName));
modules.add(depName);
}
}
final String exportDependenciesStr = exportDependencies.getValue(parsedCmd);
if (exportDependenciesStr != null) {
final String[] depsArr = exportDependenciesStr.split(",+");
for (String dep : depsArr) {
// TODO validate dependencies
String depName = dep.trim();
if (modules.contains(depName)) {
deleteRecursively(moduleDir);
throw new CommandLineException("Error, duplicated dependency " + depName);
}
modules.add(depName);
config.addDependency(new ModuleDependency(depName, true));
}
}
final String propsStr = props.getValue(parsedCmd);
if (propsStr != null) {
final String[] pairs = propsStr.split(",");
for (String pair : pairs) {
int equals = pair.indexOf('=');
if (equals == -1) {
throw new CommandFormatException("Property '" + pair + "' in '" + propsStr + "' is missing the equals sign.");
}
final String propName = pair.substring(0, equals);
if (propName.isEmpty()) {
throw new CommandFormatException("Property name is missing for '" + pair + "' in '" + propsStr + "'");
}
config.setProperty(propName, pair.substring(equals + 1));
}
}
final String slotVal = slot.getValue(parsedCmd);
if (slotVal != null) {
config.setSlot(slotVal);
}
final String mainCls = mainClass.getValue(parsedCmd);
if (mainCls != null) {
config.setMainClass(mainCls);
}
FileOutputStream fos = null;
final File moduleFile = new File(moduleDir, "module.xml");
try {
fos = new FileOutputStream(moduleFile);
XMLExtendedStreamWriter xmlWriter = create(XMLOutputFactory.newInstance().createXMLStreamWriter(fos, StandardCharsets.UTF_8.name()));
config.writeContent(xmlWriter, null);
xmlWriter.flush();
} catch (IOException e) {
throw new CommandLineException("Failed to create file " + moduleFile.getAbsolutePath(), e);
} catch (XMLStreamException e) {
throw new CommandLineException("Failed to write to " + moduleFile.getAbsolutePath(), e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}
}
Aggregations