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

org.jutils.jprocesses.info.AbstractProcessesService Maven / Gradle / Ivy

There is a newer version: 1.6.5
Show newest version
/*
 * Copyright 2016 Javier Garcia Alonso.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jutils.jprocesses.info;

import org.jutils.jprocesses.model.JProcessesResponse;
import org.jutils.jprocesses.model.ProcessInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Info related with processes
 *
 * @author Javier Garcia Alonso
 */
abstract class AbstractProcessesService implements ProcessesService {

    protected String nameFilter = null;

    @Override
    public List getList() {
        return getList(null);
    }

    @Override
    public List getList(String name) {
        String rawData = getProcessesData(name);

        List> mapList = parseList(rawData);

        return buildInfoFromMap(mapList);
    }

    @Override
    public JProcessesResponse killProcess(int pid) {
        return kill(pid);
    }

    @Override
    public JProcessesResponse killProcessGracefully(int pid) {
        return killGracefully(pid);
    }

    protected abstract List> parseList(String rawData);

    protected abstract String getProcessesData(String name);

    protected abstract JProcessesResponse kill(int pid);

    protected abstract JProcessesResponse killGracefully(int pid);

    private List buildInfoFromMap(List> mapList) {
        List infoList = new ArrayList();

        for (final Map map : mapList) {
            ProcessInfo info = new ProcessInfo();
            info.setPid(map.get("pid"));
            info.setName(map.get("proc_name"));
            info.setTime(map.get("proc_time"));
            info.setCommand((map.get("command") != null) ? map.get("command") : "");
            info.setCpuUsage(map.get("cpu_usage"));
            info.setPhysicalMemory(map.get("physical_memory"));
            info.setStartTime(map.get("start_time"));
            info.setUser(map.get("user"));
            info.setVirtualMemory(map.get("virtual_memory"));
            info.setPriority(map.get("priority"));

            infoList.add(info);
        }

        return infoList;
    }

    protected void filterByName(List> processesDataList) {
        List> processesToRemove = new ArrayList>();
        for (final Map process : processesDataList) {
            if (!nameFilter.equals(process.get("proc_name"))) {
                processesToRemove.add(process);
            }
        }
        processesDataList.removeAll(processesToRemove);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy