Add helpers for looking up ServiceConfig(s) by exe name
This commit is contained in:
		
							parent
							
								
									535ed8d79b
								
							
						
					
					
						commit
						116f0b5ffe
					
				@ -18,8 +18,11 @@
 | 
			
		||||
 | 
			
		||||
package org.hyperic.sigar.win32;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileFilter;
 | 
			
		||||
import java.io.PrintStream;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class Service extends Win32 {
 | 
			
		||||
@ -102,6 +105,51 @@ public class Service extends Win32 {
 | 
			
		||||
 | 
			
		||||
    public static native List getServiceNames() throws Win32Exception;
 | 
			
		||||
 | 
			
		||||
    private static class ExeFilter implements FileFilter {
 | 
			
		||||
        private String name;
 | 
			
		||||
        private ExeFilter(String name) {
 | 
			
		||||
            this.name = name.toLowerCase();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public boolean accept(File file) {
 | 
			
		||||
            return this.name.equals(file.getName().toLowerCase());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static List getServiceConfigs(String exe)
 | 
			
		||||
        throws Win32Exception {
 | 
			
		||||
 | 
			
		||||
        return getServiceConfigs(new ExeFilter(exe));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static List getServiceConfigs(FileFilter filter)
 | 
			
		||||
        throws Win32Exception {
 | 
			
		||||
 | 
			
		||||
        List services = new ArrayList();
 | 
			
		||||
        List names = Service.getServiceNames();
 | 
			
		||||
 | 
			
		||||
        for (int i=0; i<names.size(); i++) {
 | 
			
		||||
            Service service = null;
 | 
			
		||||
            try {
 | 
			
		||||
                service = new Service((String)names.get(i));
 | 
			
		||||
                ServiceConfig config = service.getConfig();
 | 
			
		||||
                String path = config.getExe().trim();
 | 
			
		||||
                if (!filter.accept(new File(path))) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                services.add(config);
 | 
			
		||||
            } catch (Win32Exception e){
 | 
			
		||||
                continue;
 | 
			
		||||
            } finally {
 | 
			
		||||
                if (service != null) {
 | 
			
		||||
                    service.close();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return services;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Service(String serviceName) throws Win32Exception
 | 
			
		||||
    {
 | 
			
		||||
        this();
 | 
			
		||||
@ -339,18 +387,8 @@ public class Service extends Win32 {
 | 
			
		||||
                                                     ServiceConfig config) throws Win32Exception;
 | 
			
		||||
 | 
			
		||||
    public void list(PrintStream out) throws Win32Exception {
 | 
			
		||||
        ServiceConfig config = getConfig();
 | 
			
		||||
        out.println("name..........[" + config.getName() + "]");
 | 
			
		||||
        out.println("display.......[" + config.getDisplayName() + "]");
 | 
			
		||||
        out.println("description...[" + config.getDescription() + "]");
 | 
			
		||||
        getConfig().list(out);
 | 
			
		||||
        out.println("status........[" + getStatusString() + "]");
 | 
			
		||||
        out.println("start type....[" + config.getStartTypeString() + "]");
 | 
			
		||||
        out.println("start name....[" + config.getStartName() + "]"); 
 | 
			
		||||
 | 
			
		||||
        out.println("type.........."  + config.getTypeList());
 | 
			
		||||
        out.println("path..........[" + config.getPath() + "]");
 | 
			
		||||
        out.println("deps.........."  + Arrays.asList(config.getDependencies()));
 | 
			
		||||
        out.println("error ctl.....[" + config.getErrorControlString() + "]");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public static void main(String[] args) throws Exception {
 | 
			
		||||
@ -372,6 +410,15 @@ public class Service extends Win32 {
 | 
			
		||||
            System.out.println(service.getStatusString());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        else if ((args.length == 1) && (args[0].endsWith(EXE_EXT))) {
 | 
			
		||||
            services = getServiceConfigs(args[0]);
 | 
			
		||||
            for (int i=0; i<services.size(); i++) {
 | 
			
		||||
                ServiceConfig config = (ServiceConfig)services.get(i);
 | 
			
		||||
                config.list(System.out);
 | 
			
		||||
                System.out.println("");
 | 
			
		||||
            }
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            services = Arrays.asList(args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,8 @@
 | 
			
		||||
 | 
			
		||||
package org.hyperic.sigar.win32;
 | 
			
		||||
 | 
			
		||||
import java.io.PrintStream;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@ -113,7 +115,7 @@ public class ServiceConfig {
 | 
			
		||||
    String description;
 | 
			
		||||
    String password;
 | 
			
		||||
    String name;
 | 
			
		||||
 | 
			
		||||
    String[] argv = null;
 | 
			
		||||
    ServiceConfig() {}
 | 
			
		||||
 | 
			
		||||
    public ServiceConfig(String name) {
 | 
			
		||||
@ -140,6 +142,24 @@ public class ServiceConfig {
 | 
			
		||||
    public void setPath(String path) {
 | 
			
		||||
        this.path = path;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String[] getArgv() {
 | 
			
		||||
        if (this.argv == null) {
 | 
			
		||||
            this.argv = Win32.parseCommandLine(getPath());
 | 
			
		||||
        }
 | 
			
		||||
        return this.argv;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getExe() {
 | 
			
		||||
        String[] args = getArgv();
 | 
			
		||||
        if (args.length == 0) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            return args[0];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Returns the dependencies.
 | 
			
		||||
     */
 | 
			
		||||
@ -314,4 +334,18 @@ public class ServiceConfig {
 | 
			
		||||
    public void setName(String name) {
 | 
			
		||||
        this.name = name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void list(PrintStream out) throws Win32Exception {
 | 
			
		||||
        out.println("name..........[" + getName() + "]");
 | 
			
		||||
        out.println("display.......[" + getDisplayName() + "]");
 | 
			
		||||
        out.println("description...[" + getDescription() + "]");
 | 
			
		||||
        out.println("start type....[" + getStartTypeString() + "]");
 | 
			
		||||
        out.println("start name....[" + getStartName() + "]"); 
 | 
			
		||||
 | 
			
		||||
        out.println("type.........."  + getTypeList());
 | 
			
		||||
        out.println("path..........[" + getPath() + "]");
 | 
			
		||||
        out.println("exe...........[" + getExe() + "]");
 | 
			
		||||
        out.println("deps.........."  + Arrays.asList(getDependencies()));
 | 
			
		||||
        out.println("error ctl.....[" + getErrorControlString() + "]");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -19,12 +19,16 @@
 | 
			
		||||
package org.hyperic.sigar.win32;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.StringTokenizer;
 | 
			
		||||
import org.hyperic.sigar.Sigar;
 | 
			
		||||
import org.hyperic.sigar.SigarException;
 | 
			
		||||
 | 
			
		||||
public abstract class Win32 {
 | 
			
		||||
 | 
			
		||||
    public static final String EXE_EXT = ".exe";
 | 
			
		||||
    private static final String EXE_EXT_U = EXE_EXT.toUpperCase();
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        try {
 | 
			
		||||
            Sigar.load();
 | 
			
		||||
@ -78,6 +82,54 @@ public abstract class Win32 {
 | 
			
		||||
        return exe;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String[] parseCommandLine(String args) {
 | 
			
		||||
        ArrayList res = new ArrayList();
 | 
			
		||||
        StringTokenizer quoteTok;
 | 
			
		||||
        boolean inQuote = false;
 | 
			
		||||
 | 
			
		||||
        if ((args == null) ||
 | 
			
		||||
            ((args = args.trim()).length() == 0))
 | 
			
		||||
        {
 | 
			
		||||
            return new String[0];
 | 
			
		||||
        }
 | 
			
		||||
            
 | 
			
		||||
        if (!args.startsWith("\"") &&
 | 
			
		||||
            (args.endsWith(EXE_EXT) ||
 | 
			
		||||
             args.endsWith(EXE_EXT_U)) &&
 | 
			
		||||
            new File(args).exists())
 | 
			
		||||
        {
 | 
			
		||||
            return new String[] { args };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        quoteTok = new StringTokenizer(args, "\"", true);
 | 
			
		||||
 | 
			
		||||
        while (quoteTok.hasMoreTokens()) {
 | 
			
		||||
            String elem = (String)quoteTok.nextElement();
 | 
			
		||||
 | 
			
		||||
            if (elem.equals("\"")) {
 | 
			
		||||
                inQuote = !inQuote;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (inQuote) {
 | 
			
		||||
                res.add(elem);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                StringTokenizer spaceTok = new StringTokenizer(elem.trim());
 | 
			
		||||
 | 
			
		||||
                while (spaceTok.hasMoreTokens()) {
 | 
			
		||||
                    res.add(spaceTok.nextToken());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if (inQuote) {
 | 
			
		||||
            throw new IllegalArgumentException("Unbalanced quotation marks");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return (String[])res.toArray(new String[0]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) throws Exception {
 | 
			
		||||
        for (int i=0; i<args.length; i++) {
 | 
			
		||||
            String file =
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user