Search in sources :

Example 1 with ArtifactData

use of aQute.jpm.lib.ArtifactData in project bnd by bndtools.

the class Windows method createCommand.

/**
	 * Create a new command. Firgure out if we need the console or the window
	 * version and the 64 or 32 bit version of the exe. Copy it, and create the
	 * ini file.
	 */
@Override
public String createCommand(CommandData data, Map<String, String> map, boolean force, String... extra) throws Exception {
    //
    // The path to the executable
    //
    data.bin = getExecutable(data);
    File f = new File(data.bin);
    if (!force && f.exists())
        return "Command already exists " + data.bin + ", try to use --force";
    //
    if (data.windows)
        IO.copy(new File(getMisc(), "winrun4j.exe"), f);
    else
        IO.copy(new File(getMisc(), "winrun4jc.exe"), f);
    //
    // Make the ini file
    //
    File ini = new File(f.getAbsolutePath().replaceAll("\\.exe$", ".ini"));
    try (PrintWriter pw = new PrintWriter(ini)) {
        pw.printf("main.class=%s%n", data.main);
        pw.printf("log.level=error%n");
        String del = "classpath.1=";
        //
        for (byte[] dependency : data.dependencies) {
            ArtifactData d = jpm.get(dependency);
            pw.printf("%s%s", del, d.file);
            del = ",";
        }
        pw.printf("%n");
        //
        if (data.jvmArgs != null && data.jvmArgs.length() != 0) {
            String[] parts = data.jvmArgs.split("\\s+");
            for (int i = 0; i < parts.length; i++) pw.printf("vmarg.%d=%s%n", i + 1, parts[i]);
        }
    }
    logger.debug("Ini content {}", IO.collect(ini));
    return null;
}
Also used : File(java.io.File) ArtifactData(aQute.jpm.lib.ArtifactData) PrintWriter(java.io.PrintWriter)

Example 2 with ArtifactData

use of aQute.jpm.lib.ArtifactData in project bnd by bndtools.

the class Main method _service.

@Description("Manage the jpm4j services")
public void _service(ServiceOptions opts) throws Exception {
    if (opts._arguments().isEmpty()) {
        for (ServiceData sd : jpm.getServices()) print(sd);
        return;
    }
    List<String> cmdline = opts._arguments();
    String name = cmdline.remove(0);
    Service s = jpm.getService(name);
    if (opts.remove()) {
        if (!jpm.hasAccess()) {
            error("No write access to remove service %s", name);
            return;
        }
        if (s == null) {
            error("No such service %s to remove", name);
            return;
        }
        s.stop();
        s.remove();
        return;
    }
    if (opts.create() != null) {
        logger.debug("create service");
        if (s != null) {
            error("Service already exists, cannot be created: %s. Update or remove it first", name);
            return;
        }
        ArtifactData target = jpm.getCandidate(opts.create());
        if (target == null) {
            error("Cannot find candidate for coordinates", opts.create());
            return;
        }
        ServiceData data = new ServiceData();
        CommandData cmd = jpm.parseCommandData(target);
        for (Field f : cmd.getClass().getFields()) {
            f.set(data, f.get(cmd));
        }
        logger.debug("service data {}", cmd);
        data.name = name;
        updateServiceData(data, opts);
        logger.debug("update service data");
        String result = jpm.createService(data, false);
        if (result != null)
            error("Create service failed: %s", result);
        return;
    }
    if (s == null) {
        error("No such service: %s", name);
        return;
    }
    ServiceData data = s.getServiceData();
    if (updateServiceData(data, opts) || opts.coordinates() != null || opts.update()) {
        if (!jpm.hasAccess()) {
            error("No write access to update service %s", name);
            return;
        }
        if (opts.coordinates() != null || opts.update()) {
            String coordinates = opts.coordinates();
            if (coordinates == null) {
                error("No coordinate found in old service record");
                return;
            }
            int n = coordinates.indexOf('@');
            if (n > 0)
                coordinates = coordinates.substring(0, n);
            logger.debug("Updating from coordinate: {}", coordinates);
            ArtifactData target = jpm.getCandidate(coordinates);
            if (target == null) {
                error("No candidates found for %s (%s)", coordinates, opts.staged() ? "staged" : "only masters");
                return;
            }
            CommandData cmd = jpm.parseCommandData(target);
            for (Field f : cmd.getClass().getFields()) {
                f.set(data, f.get(cmd));
            }
            data.name = name;
        }
        String result = jpm.createService(data, true);
        if (result != null)
            error("Update service failed: %s", result);
        else if (s.isRunning())
            warning("Changes will not affect the currently running process");
    }
    Data.details(data, out);
}
Also used : Field(java.lang.reflect.Field) Service(aQute.jpm.lib.Service) CommandData(aQute.jpm.lib.CommandData) ServiceData(aQute.jpm.lib.ServiceData) ArtifactData(aQute.jpm.lib.ArtifactData) Description(aQute.lib.getopt.Description)

Example 3 with ArtifactData

use of aQute.jpm.lib.ArtifactData in project bnd by bndtools.

the class Platform method process.

protected void process(String resource, CommandData data, String path, Map<String, String> map, String... extra) throws Exception {
    File file = new File(path);
    copy(getClass().getResourceAsStream(resource), file);
    Sed sed = new Sed(file);
    sed.setBackup(false);
    if (data.title == null || data.title.trim().length() == 0)
        data.title = data.name;
    //
    // Allow commands to be done in java or javaw
    //
    sed.replace("%java%", data.windows ? "javaw" : "java");
    for (Field key : data.getClass().getFields()) {
        Object value = key.get(data);
        if (value == null) {
            value = "";
        }
        // executed as one command and thus logged as one command
        if ("epilog".equals(key.getName()) || "prolog".equals(key.getName())) {
            String s = (String) value;
            if (s != null && s.trim().length() > 0) {
                value = "(" + s + ")";
            }
        }
        String v = "" + value;
        v = v.replace("\\", "\\\\");
        sed.replace("%" + key.getName() + "%", v);
    }
    ExtList<String> deps = new ExtList<String>();
    for (byte[] dependency : data.dependencies) {
        ArtifactData d = jpm.get(dependency);
        deps.add(d.file);
    }
    for (String x : extra) {
        deps.add(x);
    }
    String classpath = deps.join(File.pathSeparator);
    sed.replace("%classpath%", classpath.replace("\\", "\\\\"));
    if (map != null) {
        StringBuilder sb = new StringBuilder();
        String del = "-D";
        for (Map.Entry<String, String> e : map.entrySet()) {
            sed.replace("%" + e.getKey() + "%", e.getValue());
            sb.append(del).append(e.getKey()).append("=\"").append(e.getValue()).append("\"");
            del = " -D";
        }
        sed.replace("%defines%", sb.toString());
    } else {
        sed.replace("%defines%", "");
    }
    sed.doIt();
}
Also used : Field(java.lang.reflect.Field) Sed(aQute.libg.sed.Sed) ExtList(aQute.lib.collections.ExtList) File(java.io.File) Map(java.util.Map) ArtifactData(aQute.jpm.lib.ArtifactData)

Example 4 with ArtifactData

use of aQute.jpm.lib.ArtifactData in project bnd by bndtools.

the class Main method _install.

/**
	 * A better way to install
	 */
@Description("Install an artifact from a url, file, or http://www.jpm4j.org")
public void _install(installOptions opts) throws Exception {
    if (!this.options.user() && !jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    for (String coordinate : opts._arguments()) {
        logger.debug("install {}", coordinate);
        File file = IO.getFile(base, coordinate);
        if (file.isFile()) {
            coordinate = file.toURI().toString();
            logger.debug("is existing file: {}", coordinate);
        }
        ArtifactData artifact = jpm.getCandidate(coordinate);
        logger.debug("candidate {}", artifact);
        if (artifact == null) {
            if (jpm.isWildcard(coordinate))
                error("no candidate found for %s", coordinate);
            else
                error("no candidate found for %s, you could try %s@* to also see staged and withdrawn revisions", coordinate, coordinate);
        } else {
            if (!opts.ignore()) {
                CommandData cmd = jpm.parseCommandData(artifact);
                updateCommandData(cmd, opts);
                logger.debug("main={}, name={}", cmd.main, cmd.name);
                if (cmd.main != null) {
                    if (cmd.name == null && !artifact.local) {
                        cmd.name = artifact.coordinate.getArtifactId();
                    }
                    List<Error> errors = cmd.validate();
                    if (!errors.isEmpty()) {
                        error("Command not valid");
                        for (Error error : errors) {
                            error("[%s] %s %s %s %s", error.code, error.description, error.path, error.failure, error.value);
                        }
                    } else {
                        String result = jpm.createCommand(cmd, opts.force());
                        if (result != null) {
                            error("[%s] %s", coordinate, result);
                        }
                    }
                } else
                    error("No main class found. Please specify");
            }
        }
    }
}
Also used : Error(aQute.struct.struct.Error) CommandData(aQute.jpm.lib.CommandData) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ArtifactData(aQute.jpm.lib.ArtifactData) Description(aQute.lib.getopt.Description)

Aggregations

ArtifactData (aQute.jpm.lib.ArtifactData)4 File (java.io.File)3 CommandData (aQute.jpm.lib.CommandData)2 Description (aQute.lib.getopt.Description)2 Field (java.lang.reflect.Field)2 Service (aQute.jpm.lib.Service)1 ServiceData (aQute.jpm.lib.ServiceData)1 ExtList (aQute.lib.collections.ExtList)1 Sed (aQute.libg.sed.Sed)1 Error (aQute.struct.struct.Error)1 PrintWriter (java.io.PrintWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 Map (java.util.Map)1