001/** 002 * //////////////////////////////////////////////////////////////////////////////// 003 * // 004 * // D SOFTWARE INCORPORATED 005 * // Copyright 2007-2014 D Software Incorporated 006 * // All Rights Reserved. 007 * // 008 * // NOTICE: D Software permits you to use, modify, and distribute this file 009 * // in accordance with the terms of the license agreement accompanying it. 010 * // 011 * // Unless required by applicable law or agreed to in writing, software 012 * // distributed under the License is distributed on an "AS IS" BASIS, 013 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * // 015 * //////////////////////////////////////////////////////////////////////////////// 016 */ 017package com.thed.util; 018 019/** 020 * @serial exclude 021 * Utility class for NT/2K/XP/2003 that returns the status of a service. 022 */ 023import java.io.*; 024 025public class SrvcUtil { 026 /** 027 * NT/2K/XP/2003 service status is unknown. 028 */ 029 public static final int STATUS_UNKNOWN = -1; 030 031 /** 032 * NT/2K/XP/2003 service status is stopped. 033 */ 034 public static final int STATUS_STOPPED = 1; 035 036 /** 037 * NT/2K/XP/2003 service status is running. 038 */ 039 public static final int STATUS_RUNNING = 4; 040 041 /** 042 * Returns the status of an NT/2K/XP/2003 service with its given name. 043 * 044 * @param serviceName 045 * Name of the service. 046 * @return Either STATUS_UNKNOWN, STATUS_STOPPED or STATUS_RUNNING. 047 */ 048 public static int getStatusByServiceName(String serviceName) { 049 return new SrvcUtil().searchStatusFromQuery(SRVC_NAME + serviceName); 050 } 051 052 public static boolean isServiceInstalled(String serviceName) { 053 boolean flag = false; 054 int status = new SrvcUtil().searchStatusFromQuery(SRVC_NAME + serviceName); 055 if((STATUS_STOPPED==status)||(STATUS_RUNNING==status)){ 056 flag=true; 057 } 058 return flag; 059 } 060 061 public static boolean isServiceStarted(String serviceName) { 062 boolean flag = false; 063 int status = new SrvcUtil().searchStatusFromQuery(SRVC_NAME + serviceName); 064 if((STATUS_RUNNING==status)){ 065 flag=true; 066 } 067 return flag; 068 } 069 070 public static boolean isServiceStoped(String serviceName) { 071 boolean flag = false; 072 int status = new SrvcUtil().searchStatusFromQuery(SRVC_NAME + serviceName); 073 if((STATUS_STOPPED==status)){ 074 flag=true; 075 } 076 return flag; 077 } 078 079 /** 080 * Returns the status of an NT/2K/XP/2003 service with its given displayed 081 * name. 082 * 083 * @param displayName 084 * Displayed name of the service. 085 * @return Either STATUS_UNKNOWN, STATUS_STOPPED or STATUS_RUNNING. 086 */ 087 public static int getStatusByDisplayName(String displayName) { 088 return new SrvcUtil().searchStatusFromQuery(DISP_NAME + displayName); 089 } 090 091 private static final String QUERY_CMD = "sc query state= all"; 092 093 private static final String SRVC_NAME = "SERVICE_NAME: "; 094 095 private static final String DISP_NAME = "DISPLAY_NAME: "; 096 097 private static final String STATE_STR = " STATE :"; 098 099 private SrvcUtil() { 100 } 101 102 private int searchStatusFromQuery(String searchStr) { 103 try { 104 Process process = Runtime.getRuntime().exec(QUERY_CMD); 105 StreamPumper pumper = new StreamPumper(process.getInputStream(), 106 searchStr); 107 108 pumper.start(); 109 process.waitFor(); 110 pumper.join(); 111 112 return pumper.getStatus(); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 return STATUS_UNKNOWN; 116 } 117 } 118 119 private class StreamPumper extends Thread { 120 private InputStream is; 121 122 private String searchStr; 123 124 private int status = STATUS_UNKNOWN; 125 126 public StreamPumper(InputStream is, String searchStr) { 127 this.is = is; 128 this.searchStr = searchStr; 129 } 130 131 public void run() { 132 try { 133 BufferedReader br = new BufferedReader( 134 new InputStreamReader(is)); 135 String line; 136 boolean inBlock = false; 137 138 while ((line = br.readLine()) != null) { 139 if (!inBlock && line.startsWith(searchStr)) 140 inBlock = true; 141 else if (inBlock && line.startsWith(STATE_STR)) { 142 int len = STATE_STR.length() + 1; 143 int pos = line.indexOf(' ', len); 144 145 status = Integer.parseInt(line.substring(len, pos)); 146 inBlock = false; 147 } 148 } 149 } catch (Exception e) { 150 e.printStackTrace(); 151 } 152 } 153 154 public int getStatus() { 155 return status; 156 } 157 } 158}