Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * RHQ Management Platform
   * Copyright (C) 2005-2008 Red Hat, Inc.
   * All rights reserved.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation version 2 of the License.
   *
  * This program 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 General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 package org.rhq.plugins.iis.util;
 
 import java.util.List;
 
 import  org.apache.commons.logging.Log;
 import  org.apache.commons.logging.LogFactory;
 import  org.hyperic.sigar.win32.Service;
 import  org.hyperic.sigar.win32.Win32Exception;
 
Support for starting and stopping windows services along with their dependencies and dependents.

Author(s):
Greg Hinkle
 
 public class Win32ServiceControlDelegate {
 
     private Log log = LogFactory.getLog(Win32ServiceControlDelegate.class);
 
     private String serviceName;
 
     private List<Stringdependencies;
     private List<StringreverseDependencies;
     private List<Stringdependents;
     private List<StringreverseDependents;
 
     private Service service;
     private long timeout;
 
     public Win32ServiceControlDelegate(String serviceNamelong timeoutthrows Win32Exception {
         this(serviceNamenullnulltimeout);
     }
 
     public Win32ServiceControlDelegate(String serviceNameList<StringdependenciesList<Stringdependents,
         long timeoutthrows Win32Exception {
         .debug("serviceName = " + serviceName);
         .debug("serviceDependencies = " + (dependencies == null ? "none" : dependencies));
         .debug("serviceDependents = " + (dependents == null ? "none" : dependents));
         .debug("serviceOperationTimeout = " + timeout + " ms");
 
         this. = serviceName;
         this. = dependencies;
         if (dependencies != null) {
             this. = new ArrayList<String>(dependencies);
             Collections.reverse(this.);
         }
         this. = dependents;
         if (dependents != null) {
             this. = new ArrayList<String>(dependents);
             Collections.reverse(this.);
         }
         this. = timeout;
 
          = new Service(serviceName);
     }
 
     public AvailabilityType getAvailabilityType() {
         return getAvailability(this.);
     }
 
     public void start() throws Win32Exception {
         startServices();
         .start();
         startServices();
     }
 
     public void stop() throws Win32Exception {
         stopServices();
         .stop();
     }
 
     public void restart() throws Win32Exception {
         stop();
         start();
     }
 
     protected void startServices(List<Stringservices) {
        if (services == null)
            return;
        .debug("Starting list of services for Service [" +  + "], services [" + services + "]");
        for (String relatedServiceName : services) {
            try {
                Service relatedService = new Service(relatedServiceName);
                int relatedServiceStatus = relatedService.getStatus();
                if (relatedServiceStatus == Service.SERVICE_STOPPED
                    || relatedServiceStatus == Service.SERVICE_STOP_PENDING) {
                    .debug("Starting Service [" + relatedServiceName + "]");
                    relatedService.start(this.);
                    .debug("Service started [" + relatedServiceName + "]");
                    relatedService.close();
                }
            } catch (Exception e) {
                .warn("Unable to start [" + relatedServiceName + "] from the list [" + services
                    + "]. Will continue with the rest. Cause:" + e);
            }
        }
    }
    protected void stopServices(List<Stringservices) {
        if (services == null)
            return;
        .debug("Stopping list of services for Service [" +  + "], services [" + services + "]");
        for (String relatedServiceName : services) {
            try {
                Service relatedService = new Service(relatedServiceName);
                int relatedServiceStatus = relatedService.getStatus();
                if (!(relatedServiceStatus == Service.SERVICE_STOPPED || relatedServiceStatus == Service.SERVICE_STOP_PENDING)) {
                    .debug("Stopping Service [" + relatedServiceName + "]");
                    relatedService.stop(this.);
                    .debug("Service stopped [" + relatedServiceName + "]");
                    relatedService.close();
                }
            } catch (Exception e) {
                .warn("Unable to stop [" + relatedServiceName + "] from the list [" + services
                    + "]. Will continue with the rest. Cause:" + e);
            }
        }
    }
    protected AvailabilityType getAvailability(Service service) {
        switch (service.getStatus()) {
        case Service.SERVICE_START_PENDING:
        case Service.SERVICE_STOP_PENDING:
        case Service.SERVICE_RUNNING:
            return .;
        case Service.SERVICE_STOPPED:
        case Service.SERVICE_CONTINUE_PENDING:
        case Service.SERVICE_PAUSE_PENDING:
        case Service.SERVICE_PAUSED:
        default:
            return .;
        }
    }
New to GrepCode? Check out our FAQ X