Search in sources :

Example 1 with WindowsFilenameTabCompleter

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) {
                }
            }
        }
    }
}
Also used : DefaultFilenameTabCompleter(org.jboss.as.cli.handlers.DefaultFilenameTabCompleter) IOException(java.io.IOException) WindowsFilenameTabCompleter(org.jboss.as.cli.handlers.WindowsFilenameTabCompleter) DefaultFilenameTabCompleter(org.jboss.as.cli.handlers.DefaultFilenameTabCompleter) FilenameTabCompleter(org.jboss.as.cli.handlers.FilenameTabCompleter) CommandLineException(org.jboss.as.cli.CommandLineException) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) CommandFormatException(org.jboss.as.cli.CommandFormatException) FileOutputStream(java.io.FileOutputStream) WindowsFilenameTabCompleter(org.jboss.as.cli.handlers.WindowsFilenameTabCompleter) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 DefaultFilenameTabCompleter (org.jboss.as.cli.handlers.DefaultFilenameTabCompleter)1 FilenameTabCompleter (org.jboss.as.cli.handlers.FilenameTabCompleter)1 WindowsFilenameTabCompleter (org.jboss.as.cli.handlers.WindowsFilenameTabCompleter)1 XMLExtendedStreamWriter (org.jboss.staxmapper.XMLExtendedStreamWriter)1