All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anrisoftware.sscontrol.scripts.findusedport.FindUsedPort Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-2015 Erwin Müller 
 *
 * This file is part of sscontrol-scripts-unix.
 *
 * sscontrol-scripts-unix is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * sscontrol-scripts-unix is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with sscontrol-scripts-unix. If not, see .
 */
package com.anrisoftware.sscontrol.scripts.findusedport;

import static java.lang.String.format;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.anrisoftware.globalpom.exec.api.ProcessTask;
import com.anrisoftware.globalpom.exec.runcommands.RunCommands;
import com.anrisoftware.globalpom.threads.api.Threads;
import com.google.inject.assistedinject.Assisted;

/**
 * Find the services for the specified ports.
 *
 * @author Erwin Mueller, [email protected]
 * @since 1.0
 */
public class FindUsedPort implements Callable {

    private static Pattern SERVICES_PATTERN = Pattern.compile("\\d+\\/.*");

    private final FindUsedPortLogger log;

    private final Map args;

    private final Object parent;

    private final Threads threads;

    private final Collection ports;

    private final RunCommands runCommands;

    @Inject
    private FindUsedPortProcessFactory findUsedPortProcessFactory;

    private Map services;

    /**
     * @see FindUsedPortFactory#create(Map, Object, Threads)
     */
    @Inject
    FindUsedPort(FindUsedPortLogger log, @Assisted Map args,
            @Assisted Object parent, @Assisted Threads threads) {
        this.log = log;
        this.args = args;
        this.parent = parent;
        this.threads = threads;
        this.ports = log.ports(args, parent);
        this.runCommands = log.runCommands(args, parent);
        log.command(args, parent);
    }

    public Map getServices() {
        return Collections.unmodifiableMap(services);
    }

    @Override
    public FindUsedPort call() throws Exception {
        ProcessTask task;
        task = findUsedPortProcessFactory.create(threads, args).call();
        this.services = findServices(task);
        log.portsFound(parent, runCommands, task, args, services);
        return this;
    }

    private Map findServices(ProcessTask task) {
        String out = task.getOut();
        String[] lines = StringUtils.split(out, "\n");
        Map services = new HashMap();
        for (int i = 0; i < lines.length; i++) {
            for (Integer port : ports) {
                if (StringUtils.contains(lines[i], format(":%d ", port))) {
                    parsePortService(services, port, lines[i]);
                }
            }
        }
        return services;
    }

    private void parsePortService(Map services, int port,
            String line) {
        String[] split = StringUtils.split(line, " ");
        String service = null;
        for (int i = 0; i < split.length; i++) {
            if (SERVICES_PATTERN.matcher(split[i]).matches()) {
                service = split[i];
            }
        }
        String[] servicesplit = StringUtils.split(service, "/");
        service = servicesplit[servicesplit.length - 1];
        services.put(port, service);
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy